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

primGL.cpp

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////
00002 // 6/6/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 //  PrimGL - GLIFT wrapper for an OpenGL primitive
00021 //
00022 // 
00023 /////////////////////////////////////////////////////////////////////
00024 
00025 #include <drawable/primGL.h>
00026 #include <iostream>
00027 #include <assert.h>
00028 
00029 using namespace std;
00030 using namespace gutz;
00031 
00032 using namespace glift;
00033 
00034 PrimGL::PrimGL( GLenum primType, GLint minVerts, GLint vertDivisor, const vec3f& scale,
00035                const arrayw2f& vert, const arrayw2f& texCoord, const arrayw2f& normal,  
00036                const arrayw1ui& indices, DrawAlg drawAlgType)
00037 {
00038    bool hasTexCoords = (texCoord.size() > 0);
00039    MultiTexCoord  texCoords = hasTexCoords ? MultiTexCoord(1,texCoord) : MultiTexCoord(1);
00040    MultiTexCoord* curTexCoords = hasTexCoords ? &m_texCoords : NULL;
00041 
00042    arrayw1ui* curIndices = indices.empty() ? NULL : &m_indices;
00043 
00044    initMembers( primType, minVerts, vertDivisor, scale, vert, 
00045       texCoords, curTexCoords, normal, indices, arrayo2f(), 
00046       arrayo1ub(), curIndices, makeDrawAlg( drawAlgType ) );
00047 }
00048 
00049 PrimGL::PrimGL( GLenum primType, GLint minVerts, GLint vertDivisor, const vec3f& scale,
00050                const arrayw2f& vert, const arrayw2f& texCoord, const arrayw2f& normal, 
00051                const arrayw1ui& indices, const arrayw2f& color, const arrayw1ub& edgeFlag, 
00052                DrawAlg drawAlgType )
00053 {
00054    bool hasTexCoords = (texCoord.size() > 0);
00055    MultiTexCoord  texCoords = hasTexCoords ? MultiTexCoord(1,texCoord) : MultiTexCoord(1);
00056    MultiTexCoord* curTexCoords = hasTexCoords ? &m_texCoords : NULL;
00057 
00058    arrayw1ui* curIndices = indices.empty() ? NULL : &m_indices;
00059 
00060    initMembers( primType, minVerts, vertDivisor, scale, vert, 
00061       texCoords, curTexCoords, normal, indices, color, edgeFlag, 
00062       curIndices, makeDrawAlg( drawAlgType ) );
00063 }
00064 
00065 PrimGL::PrimGL( const PrimGL& rhs )
00066 {
00067    initMembers( rhs );
00068 }
00069 
00070 PrimGL& PrimGL::operator=( const PrimGL& rhs )
00071 {
00072    if( &rhs != this ) {
00073       initMembers(rhs);
00074    }
00075    return *this;
00076 }
00077 
00078 PrimGL::~PrimGL()
00079 {
00080    delete m_drawAlg;
00081 }
00082 
00083 void PrimGL::initMembers( GLenum primType, GLint minVerts, GLint vertDivisor, const vec3f& scale,
00084                          const arrayw2f& vert, const MultiTexCoord& texCoords, const MultiTexCoord* curTexCoords,
00085                          const arrayw2f& normal, const arrayw1ui& indices, const arrayw2f& color, 
00086                          const arrayw1ub& edgeFlag, const arrayw1ui* curIndices, DrawAlgorithm* drawAlg )
00087 {
00088    if( vert.size() == 0 ) {
00089       err() << "initMembers(...) Error:\n\tvert.size() == 0.\n";
00090       exit(1);
00091    }
00092 
00093    m_primType = primType;
00094    m_minVerts = minVerts;
00095    m_vertDiv  = vertDivisor; 
00096    m_scale    = scale;
00097    m_vert          = vert;
00098    m_texCoords= texCoords;
00099    m_curTexCoords = curTexCoords;
00100    m_norm     = normal;
00101    m_indices  = indices;
00102    m_color         = color;
00103    m_edgeFlag = edgeFlag;
00104    m_curIndices  = curIndices;
00105    m_drawAlg  = drawAlg;
00106 
00107    assert( m_drawAlg != NULL );
00108    verifyScale();
00109    verifySizes();
00110    verifyMinVerts();
00111    verifyPrimType();
00112    verifyNumPrims();
00113 }
00114 
00115 void PrimGL::initMembers(const PrimGL& rhs)
00116 {
00117    initMembers( rhs.m_primType, rhs.m_minVerts, rhs.m_vertDiv, rhs.m_scale,
00118       rhs.m_vert, rhs.m_texCoords, rhs.m_curTexCoords, 
00119       rhs.m_norm, rhs.m_indices, rhs.m_color, rhs.m_edgeFlag, rhs.m_curIndices,
00120       rhs.m_drawAlg );
00121 }
00122 
00123 void PrimGL::verifyScale()
00124 {               
00125    if( m_scale.x < 1.0 ||
00126       m_scale.y < 1.0 ||
00127       m_scale.z < 1.0 )
00128    {
00129       err() << "verifyScale() Warning:\n"
00130          << "\tm_scale has a component smaller than 1.0.\n";
00131    }
00132 
00133    const float SMALL = 1e-3f;
00134    if( m_scale.x < SMALL ||
00135       m_scale.y < SMALL ||
00136       m_scale.z < SMALL )
00137    {
00138       err() << "verifyScale() Error:\n"
00139          << "\tscale must not have any components that are less than " << SMALL << endl;
00140       exit(1);
00141    }
00142 }
00143 
00144 void PrimGL::verifySizes()
00145 {
00146    if( m_vert.dim(0) != m_norm.dim(0) &&
00147       m_norm.size() > 0 ) {
00148          err() << "verifySizes() Error:\n"
00149             << "\tNot the same number of verts(" << m_vert.dim(0) << ") and normals ("
00150             << m_norm.dim(0) << ").\n";
00151          exit(1);
00152       }
00153       if( m_vert.dim(0) != m_color.dim(0) &&
00154          m_color.size() > 0 ) {
00155             err() << "verifySizes() Error:\n"
00156                << "\tNot the same number of verts(" << m_vert.dim(0) << ") and colors ("
00157                << m_color.dim(0) << ").\n";
00158             exit(1);
00159          }
00160 
00161          if( hasTexCoords() &&             
00162             m_vert.dim(0) != m_texCoords[0].dim(0) ) { /// Size must match verts
00163                err() << "verifySizes() Error:\n"
00164                   << "\tNot the same number of verts(" << m_vert.dim(0) << ") and texCoords ("
00165                   << m_texCoords[0].dim(0) << ").\n";
00166                exit(1);
00167             }
00168 
00169             if( m_texCoords.size() > 1 ) {
00170                err() << "verifySizes() Error:\n"
00171                   << "\tInput more than a single set of texture coordinates.\n"
00172                   << "\tMust apply a MultiTexture via ShadedPrim to generate multiple\n"
00173                   << "\tsets of texture coordinates.\n";
00174                exit(1);
00175             }
00176 
00177             if( m_edgeFlag.size() > 0 &&                /// Must specify none or all edges
00178                m_vert.dim(0) != m_edgeFlag.size() ) { 
00179                   err() << "verifySizes() Error:\n"
00180                      << "\tedgeFlag array (" << m_edgeFlag.size() << ") must be either empty or the same size as verts(" 
00181                      << m_vert.dim(0) << ").\n";                
00182                }
00183 }
00184 
00185 void PrimGL::verifyMinVerts()
00186 {
00187    if( m_vert.dim(0) < m_minVerts ) {
00188       err() << "verifyMinVerts() Error:\n\tNot enough vertices (" << m_vert.dim(0) << ")\n"
00189          << "\tThis primitive requires at least " << m_minVerts << " number of vertices.\n";
00190       exit(1);
00191    }
00192 }
00193 
00194 void PrimGL::verifyPrimType()
00195 {
00196    switch( m_primType )
00197    {
00198    case GL_POINTS:
00199    case GL_LINES:
00200    case GL_LINE_STRIP:
00201    case GL_LINE_LOOP:
00202    case GL_TRIANGLES:
00203    case GL_TRIANGLE_STRIP:
00204    case GL_TRIANGLE_FAN:
00205    case GL_QUADS:
00206    case GL_QUAD_STRIP:
00207    case GL_POLYGON:
00208       break;            //Supported primitive so alles klar.
00209    default:
00210       err() << "verifyPrimType() Error:\n\tUnsupported primitive (" << m_primType << ")\n";
00211       exit(1);
00212       break;
00213    }
00214 }
00215 
00216 void PrimGL::verifyNumPrims()
00217 {
00218    bool bad = false;
00219    if( m_indices.empty() ) { //Check the number of verts
00220       if( (numVerts() % m_vertDiv ) != 0 ) {
00221          bad = true;
00222       }
00223    }                    /// Check number of indices if using index-drawing
00224    else if( (m_indices.size() % m_vertDiv ) != 0 ) {
00225       bad = true;
00226    }
00227 
00228    if( bad ) {
00229       err() << "verifyNumPrims() Error:\n"
00230          << "\tNumber of primitives must be divisible by\n"
00231          << m_vertDiv << " for this primitive (" << m_primType << ")\n";
00232       exit(1);
00233    }
00234 }
00235 
00236 void PrimGL::bindTexCoords( const MultiPrimTexCoord* texCoordsPtr )
00237 {
00238    if( isCompiled() ) {
00239       err() << "bindTexCoords(...) Error:\n"
00240          << "\tCannot bind new tex coords to compiled\n"
00241          << "\tprimitive.\n";
00242       exit(1);
00243    }
00244 
00245    m_curTexCoords = (*texCoordsPtr)[0];
00246 }
00247 
00248 void PrimGL::bindTexCoords( const MultiTexCoord* texCoordsPtr )
00249 {
00250    if( isCompiled() ) {
00251       err() << "bindTexCoords(...) Error:\n"
00252          << "\tCannot bind new tex coords to compiled\n"
00253          << "\tprimitive.\n";
00254       exit(1);
00255    }
00256 
00257    m_curTexCoords = texCoordsPtr;
00258 }
00259 
00260 void PrimGL::releaseTexCoords()
00261 {
00262    m_curTexCoords = &m_texCoords;
00263 }
00264 
00265 void PrimGL::bindIndices(       const MultiPrimIndices* indices )
00266 {
00267    if( isCompiled() ) {
00268       err() << "bindIndices(...) Error:\n"
00269          << "\tCannot bind new indices to compiled\n"
00270          << "\tprimitive.\n";
00271       exit(1);
00272    }
00273 
00274    m_curIndices = (*indices)[0];
00275 
00276    //   if( DrawAlgArr* drawAlg = dynamic_cast<DrawAlgArr*>(m_drawAlg) ) {
00277    //           drawAlg->enable( m_vert, m_curTexCoords, m_norm, m_color, m_edgeFlag  );
00278    //   }
00279 }
00280 
00281 void PrimGL::bindIndices( const arrayw1ui* indices )
00282 {
00283    if( isCompiled() ) {
00284       err() << "bindIndices(...) Error:\n"
00285          << "\tCannot bind new indices to compiled\n"
00286          << "\tprimitive.\n";
00287       exit(1);
00288    }
00289 
00290    m_curIndices = indices;//Assign reference
00291 
00292    //   if( DrawAlgArr* drawAlg = dynamic_cast<DrawAlgArr*>(m_drawAlg) ) {
00293    //           drawAlg->enable( m_vert, m_curTexCoords, m_norm, m_color, m_edgeFlag  );
00294    //   }
00295 }
00296 
00297 void PrimGL::releaseIndices()
00298 {
00299    /// Draw all vertices specified by the constructor
00300    m_curIndices = m_indices.empty() ? NULL : &m_indices;
00301 }
00302 
00303 void PrimGL::drawDef()
00304 {
00305    m_drawAlg->draw( m_primType, m_vert, m_curTexCoords, m_norm, 
00306       m_color, m_edgeFlag, m_curIndices );
00307 }
00308 
00309 

Send questions, comments, and bug reports to:
jmk