00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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};
00090
00091 vec3f quadScale( (float)fabs(upperRight.x - lowerLeft.x),
00092 (float)fabs(upperRight.y - lowerLeft.y),
00093 1.0f );
00094 vec2i numVertsPerStrip( subDiv.x + 1, 2 );
00095 const int NUM_STRIPS = subDiv.y;
00096 const int NUM_VERTS_PER_STRIP = numVertsPerStrip.x * numVertsPerStrip.y;
00097 vec2f patchDimen( quadScale.x/subDiv.x,
00098 quadScale.y/subDiv.y );
00099
00100
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
00106 for(int s=0; s < NUM_STRIPS; s++ ) {
00107 vec3f curPos( 0.0, quadScale.y - patchDimen.y*s, z );
00108
00109
00110 int v = 0;
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
00129 gutz::arrayo2f texCoord = genTexCoords ? texGen->genTexCoords( vert, quadScale ) : gutz::arrayo2f();
00130
00131
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