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

subdivPlanarQuadM.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 // SubdivPlanarQuadM.cpp: implementation of the SubdivPlanarQuadM class.
00021 //
00022 /////////////////////////////////////////////////////////////////////
00023 
00024 #include <drawable/subdivPlanarQuadM.h>
00025 #include <drawable/primsGL.h>
00026 
00027 #include <iostream>
00028 #include <math.h>
00029 
00030 using namespace std;
00031 using namespace gutz;
00032 
00033 using namespace glift;
00034 
00035 SubdivPlanarQuadM::SubdivPlanarQuadM( GLenum primType, const vec2i& subDiv, const vec2f& lowerLeft, 
00036                                      const vec2f& upperRight, bool genTexCoords, bool genNorms,
00037                                      TexCoordGen* texGen )
00038 {
00039    m_lowerLeft = lowerLeft;
00040    m_upperRight = upperRight;
00041    m_subDiv = subDiv;
00042    m_z = 0.0f;
00043 
00044    const VecSinglePrimP& prims = createStrips( primType, subDiv, lowerLeft, upperRight, 
00045       0.0f, genTexCoords, genNorms, texGen ); 
00046    MultiPrim::init( prims );
00047 }
00048 
00049 SubdivPlanarQuadM::SubdivPlanarQuadM( GLenum primType, const vec2i& subDiv, const vec2f& lowerLeft, 
00050                                      const vec2f& upperRight, float z, bool genTexCoords, bool genNorms,
00051                                      TexCoordGen* texGen )
00052 {
00053    m_lowerLeft = lowerLeft;
00054    m_upperRight = upperRight;
00055    m_subDiv = subDiv;
00056    m_z = z;
00057 
00058    const VecSinglePrimP& prims = createStrips( primType, subDiv, lowerLeft, upperRight, 
00059       z, genTexCoords, genNorms, texGen ); 
00060    MultiPrim::init( prims );
00061 }
00062 
00063 
00064 VecSinglePrimP SubdivPlanarQuadM::createStrips( GLenum primType, const vec2i& subDiv,
00065                                                const vec2f& lowerLeft, const vec2f& upperRight, 
00066                                                float z, bool genTexCoords, bool genNorms,
00067                                                TexCoordGen* texGen )
00068 {
00069    if( primType != GL_QUAD_STRIP &&
00070       primType != GL_TRIANGLE_STRIP ) {
00071          err() << "initMembers Error:\n"
00072             << "\tprimType must be GL_QUAD_STRIP or GL_TRIANGLE_STRIP.\n";
00073          exit(1);
00074       }
00075 
00076       /// Setup the texture coordinate generation functor
00077       bool createdTexGen = false;
00078       if( genTexCoords && texGen==NULL )  {
00079          texGen = new ScaleTexGen2D();
00080          createdTexGen = true;
00081       }
00082       else if( genTexCoords==false && texGen ) {
00083          err() << "createStrips(...) Error:\n"
00084             << "\tIllegal to disable texCoordGeneration and pass\n"
00085             << "\tin alternate texCoordGenerator.\n";
00086          exit(1);
00087       }
00088 
00089       GLfloat normVec[3] = {0.0f, 0.0f, 1.0f};  //Constant for this primitive since always orthog to z-axis.
00090 
00091       vec3f     quadScale( (float)fabs(upperRight.x - lowerLeft.x),  /// Scale factor for entire quad
00092          (float)fabs(upperRight.y - lowerLeft.y),
00093          1.0f );
00094       vec2i     numVertsPerStrip( subDiv.x + 1, 2 );     /// Number of vertices in each dimension for each strip
00095       const int NUM_STRIPS = subDiv.y;                             /// How many triangle/quad strips should the quad be split into?
00096       const int NUM_VERTS_PER_STRIP = numVertsPerStrip.x * numVertsPerStrip.y;
00097       vec2f     patchDimen( quadScale.x/subDiv.x,                  /// Dimension of each subdivision patch
00098          quadScale.y/subDiv.y );
00099 
00100       /// Allocate memory for the prims.
00101       VecSinglePrimP            strips( NUM_STRIPS );
00102       gutz::arrayo2f    vert( NUM_VERTS_PER_STRIP, 3, (float)0 );
00103       gutz::arrayo2f    norm( NUM_VERTS_PER_STRIP, 3, (float)0 );
00104 
00105       /// Create all vertices and texture coords
00106       for(int s=0; s < NUM_STRIPS; s++ ) {
00107          vec3f curPos( 0.0, quadScale.y - patchDimen.y*s, z );/// Start at top-left corner
00108 
00109          /// Compute all vertices & texCoords in the strip
00110          int v = 0;                                      /// Vertex index
00111          for(int i=0; i < numVertsPerStrip.x; i++) { 
00112             for(int j=0; j < numVertsPerStrip.y; j++) {
00113                vert[v][0] = curPos.x;
00114                vert[v][1] = curPos.y;
00115                vert[v][2] = curPos.z;
00116 
00117                norm[v][0] = normVec[0];
00118                norm[v][1] = normVec[1];
00119                norm[v][2] = normVec[2];
00120 
00121                curPos.y -= patchDimen.y;
00122                v++;
00123             }
00124             curPos.x += patchDimen.x;
00125             curPos.y += patchDimen.y*2;
00126          }
00127 
00128          /// Generate texture coordinates based on vertices and dimen of quad
00129          gutz::arrayo2f texCoord = genTexCoords ? texGen->genTexCoords( vert, quadScale ) : gutz::arrayo2f();
00130 
00131          /// Generate the primitive
00132          switch(primType) {
00133       case GL_QUAD_STRIP:       
00134          strips[s] = genNorms ? new QuadStripGL( vert, texCoord, quadScale, norm, arrayo1ui(), arrayo2f(), arrayo1ub(), GLIFT_DRAW_ARR )
00135             : new QuadStripGL( vert, texCoord, quadScale, arrayo2f(), arrayo1ui(), arrayo2f(), arrayo1ub(), GLIFT_DRAW_ARR );
00136          break;
00137       case GL_TRIANGLE_STRIP: 
00138          strips[s] = genNorms ? new TriangleStripGL( vert, texCoord, quadScale, norm, arrayo1ui(), arrayo2f(), arrayo1ub(), GLIFT_DRAW_ARR )
00139             : new TriangleStripGL( vert, texCoord, quadScale, arrayo2f(), arrayo1ui(), arrayo2f(), arrayo1ub(), GLIFT_DRAW_ARR );
00140          break;
00141          }
00142       }
00143 
00144       if( createdTexGen ) {
00145          delete texGen;
00146       }
00147 
00148       return strips;
00149 }
00150 
00151 

Send questions, comments, and bug reports to:
jmk