arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

shadedPrim.cpp

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////
00002 // 6/12/02      Aaron Lefohn    Scientific Computing and Imaging Institute
00003 // School of Computing          University of Utah
00004 //
00005 //  This library is free software; you can redistribute it and/or
00006 //  modify it under the terms of the GNU Lesser General Public
00007 //  License as published by the Free Software Foundation; either
00008 //  version 2.1 of the License, or (at your option) any later version.
00009 //
00010 //  This library is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 //  Lesser General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU Lesser General Public
00016 //  License along with this library; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 ////////////////////////////////////////////////////////////////////////
00019 
00020 #include <drawable/shadedPrim.h>
00021 #include <iostream>
00022 #include <GL/glUtil.h>
00023 
00024 using namespace std;
00025 
00026 using namespace glift;
00027 
00028 //#define USE_DISP_LIST
00029 
00030 ShadedPrim::ShadedPrim( DrawableGLI* prim, Shader* shader )
00031 : m_prim(                 prim  ), 
00032 m_rawPrim(        NULL  ), 
00033 m_shader(         shader ),
00034 m_texCoordsPtr( NULL    ),
00035 m_dispList(       0 )
00036 {
00037    if( m_prim == NULL) {
00038       err() << "ShadedPrim(...) Error:\n\tPrimitive == NULL.\n";
00039       exit(1);
00040    }
00041    if( m_shader == NULL) {
00042       err() << "ShadedPrim(...) Error:\n\tShader == NULL.\n";
00043       exit(1);
00044    }
00045 
00046    /// Initialize the multi- and/or perturbed texture coords
00047    /// if 'prim' is a 'RawPrim'
00048    if( m_rawPrim = dynamic_cast<RawPrim*>( m_prim ) ) {
00049       m_texCoordsPtr = m_rawPrim->genTexCoords( shader );//m_rawPrim->genTexCoords( shader ); ///
00050    }
00051 
00052    // DEBUGGING CODE
00053 #ifdef USE_DISP_LIST
00054    m_dispList = glGenLists(1);
00055    glNewList( m_dispList, GL_COMPILE );
00056    {
00057       if( m_rawPrim && m_texCoordsPtr ) {
00058          m_rawPrim->bindTexCoords( m_texCoordsPtr );
00059       }
00060 
00061       m_prim->draw();
00062 
00063       if( m_rawPrim && m_texCoordsPtr ) {
00064          m_rawPrim->releaseTexCoords();
00065       }
00066    }
00067    glEndList();
00068 #endif
00069 }
00070 
00071 ShadedPrim::ShadedPrim( const ShadedPrim& rhs)
00072 {
00073    initMembers(rhs);    
00074 }
00075 
00076 ShadedPrim& ShadedPrim::operator=(const ShadedPrim& rhs)
00077 {
00078    if( &rhs != this ) {
00079       initMembers(rhs);
00080    }
00081    return *this;
00082 }
00083 
00084 ShadedPrim::~ShadedPrim()
00085 {
00086    for(uint i=0; i < m_texCoordsPtr->size(); i++) {
00087       delete (*m_texCoordsPtr)[i];
00088    }
00089    delete m_texCoordsPtr;
00090 }
00091 
00092 void ShadedPrim::initMembers( DrawableGLI* prim, RawPrim* rawPrim, Shader* shader, MultiPrimTexCoord* texCoordsPtr )
00093 {
00094    m_prim = prim;
00095    m_rawPrim = rawPrim;
00096    m_shader = shader;
00097    m_texCoordsPtr = texCoordsPtr;
00098 
00099    if( m_texCoordsPtr ) {
00100       m_texCoordsPtr = new MultiPrimTexCoord( texCoordsPtr->size() );
00101 
00102       /// Duplicate all elts of 'texCoordsPtr'
00103       for(uint i=0; i < m_texCoordsPtr->size(); i++) {
00104          MultiTexCoord* primTexCoords = (*texCoordsPtr)[i];
00105          (*m_texCoordsPtr)[i] = new MultiTexCoord( *primTexCoords );
00106       }
00107    }
00108 
00109    if( m_prim == NULL) {
00110       err() << "ShadedPrim(...) Error:\n\tPrimitive == NULL.\n";
00111       exit(1);
00112    }
00113    if( m_shader == NULL) {
00114       err() << "ShadedPrim(...) Error:\n\tShader == NULL.\n";
00115       exit(1);
00116    }
00117 }
00118 
00119 void ShadedPrim::initMembers( const ShadedPrim& rhs )
00120 {
00121    initMembers( rhs.m_prim, rhs.m_rawPrim, rhs.m_shader, rhs.m_texCoordsPtr );
00122 }
00123 
00124 /// Set a new 'm_prim' and 'm_rawPrim' if 'prim' is non-NULL
00125 /// - Note: The texture coords are NOT altered...just the primitive.
00126 /// - VERY DANGEROUS. Designed to be fast.
00127 void ShadedPrim::setPrim( DrawableGLI*  prim )
00128 {
00129    if( prim ) {
00130       m_prim = prim;
00131 
00132       if( RawPrim* rawPrim = dynamic_cast<RawPrim*>(prim) ) {
00133          m_rawPrim = rawPrim;
00134       }
00135    }
00136 }
00137 
00138 /// Set the texture coords used if 'texCoordsPtr' is non-NULL
00139 /// - Note: No checks are done to ensure compatibility between vertices, texCoords, shaders, etc.
00140 /// - DANGEROUS. Designed to be fast, not safe
00141 void ShadedPrim::setTexCoordPtr( MultiPrimTexCoord* texCoordsPtr )
00142 {
00143    if( texCoordsPtr ) {
00144       m_texCoordsPtr = texCoordsPtr;
00145    }
00146 }
00147 
00148 void ShadedPrim::drawDef()
00149 {
00150    m_shader->bind(); /// Enables all necessary texture units and shaders
00151 
00152 #ifdef USE_DISP_LIST
00153    glCallList( m_dispList  );
00154 #else
00155    if( m_rawPrim && m_texCoordsPtr ) {
00156       m_rawPrim->bindTexCoords( m_texCoordsPtr );
00157    }
00158 
00159    m_prim->draw();
00160 
00161    if( m_rawPrim && m_texCoordsPtr ) {
00162       m_rawPrim->releaseTexCoords();
00163    }
00164 #endif
00165 
00166    m_shader->release(); /// Disables all texture units and shaders that were used
00167 
00168    glerr("draw()");
00169 }
00170 

Send questions, comments, and bug reports to:
jmk