From: Diederick Niehorster Date: Sun, 31 Mar 2013 09:21:14 +0000 (+0000) Subject: teapot now using vertex arrays or vertex attribute arrays, using handwritten Bezier... X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=commitdiff_plain;h=7e0a8e93d578a3609fc84a68a2c57201e58f8ecc;p=freeglut teapot now using vertex arrays or vertex attribute arrays, using handwritten Bezier surface evaluation code in fg_geometry, fixed up normal generation for visualization and added support for drawing texture coordinates (teapot has texture coordinates, other geometric shapes don't) git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@1567 7f0cb862-5218-0410-a997-914c9d46530a --- diff --git a/src/fg_geometry.c b/src/fg_geometry.c index 12d1035..11bde1c 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -49,7 +49,7 @@ static void fghDrawGeometryWire11(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode, GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2 ); -static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, +static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices, GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart); #endif static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, @@ -57,12 +57,11 @@ static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei n GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2, GLint attribute_v_coord, GLint attribute_v_normal ); -static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, +static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices, GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart, - GLint attribute_v_coord, GLint attribute_v_normal); + GLint attribute_v_coord, GLint attribute_v_normal, GLint attribute_v_texture); /* declare function for generating visualization of normals */ -static void fghGenerateNormalVisualization(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, - GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart); +static void fghGenerateNormalVisualization(GLfloat *vertices, GLfloat *normals, GLsizei numVertices); #ifndef GL_ES_VERSION_2_0 static void fghDrawNormalVisualization11(); #endif @@ -134,7 +133,7 @@ static void fghDrawNormalVisualization20(GLint attribute_v_coord); * * Feel free to contribute better naming ;) */ -static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, +void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode, GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2 ) @@ -159,9 +158,9 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num /* Draw the geometric shape with filled triangles * * Arguments: - * GLfloat *vertices, GLfloat *normals, GLsizei numVertices - * The vertex coordinate and normal buffers, and the number of entries in - * those + * GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices + * The vertex coordinate, normal and texture coordinate buffers, and the + * number of entries in those * GLushort *vertIdxs * a vertex indices buffer, optional (not passed for the polyhedra with * triangular faces) @@ -182,23 +181,23 @@ static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei num * numParts * numVertPerPart gives the number of entries in the vertex * array vertIdxs */ -static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, - GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart) +void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices, + GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart) { - GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord; - GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal; + GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord; + GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal; + GLint attribute_v_texture = -1; // TODO!!! if (fgStructure.CurrentWindow->State.VisualizeNormals) /* generate normals for each vertex to be drawn as well */ - fghGenerateNormalVisualization(vertices, normals, numVertices, - vertIdxs, numParts, numVertIdxsPerPart); + fghGenerateNormalVisualization(vertices, normals, numVertices); if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1)) { /* User requested a 2.0 draw */ - fghDrawGeometrySolid20(vertices, normals, numVertices, + fghDrawGeometrySolid20(vertices, normals, textcs, numVertices, vertIdxs, numParts, numVertIdxsPerPart, - attribute_v_coord, attribute_v_normal); + attribute_v_coord, attribute_v_normal, attribute_v_texture); if (fgStructure.CurrentWindow->State.VisualizeNormals) /* draw normals for each vertex as well */ @@ -207,7 +206,7 @@ static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLsizei nu #ifndef GL_ES_VERSION_2_0 else { - fghDrawGeometrySolid11(vertices, normals, numVertices, + fghDrawGeometrySolid11(vertices, normals, textcs, numVertices, vertIdxs, numParts, numVertIdxsPerPart); if (fgStructure.CurrentWindow->State.VisualizeNormals) @@ -252,7 +251,7 @@ static void fghDrawGeometryWire11(GLfloat *vertices, GLfloat *normals, } -static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, +static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices, GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart) { int i; @@ -263,6 +262,12 @@ static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLsizei glVertexPointer(3, GL_FLOAT, 0, vertices); glNormalPointer(GL_FLOAT, 0, normals); + if (textcs) + { + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, textcs); + } + if (!vertIdxs) glDrawArrays(GL_TRIANGLES, 0, numVertices); else @@ -274,6 +279,8 @@ static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLsizei glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); + if (textcs) + glDisableClientState(GL_TEXTURE_COORD_ARRAY); } #endif @@ -390,11 +397,11 @@ static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei n /* Version for OpenGL (ES) >= 2.0 */ -static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, +static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices, GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart, - GLint attribute_v_coord, GLint attribute_v_normal) + GLint attribute_v_coord, GLint attribute_v_normal, GLint attribute_v_texture) { - GLuint vbo_coords = 0, vbo_normals = 0, ibo_elements = 0; + GLuint vbo_coords = 0, vbo_normals = 0, vbo_textcs = 0, ibo_elements = 0; GLsizei numVertIdxs = numParts * numVertIdxsPerPart; int i; @@ -413,6 +420,14 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLsizei normals, FGH_STATIC_DRAW); fghBindBuffer(FGH_ARRAY_BUFFER, 0); } + + if (numVertices > 0 && attribute_v_texture != -1 && textcs) { + fghGenBuffers(1, &vbo_textcs); + fghBindBuffer(FGH_ARRAY_BUFFER, vbo_textcs); + fghBufferData(FGH_ARRAY_BUFFER, numVertices * 2 * sizeof(textcs[0]), + textcs, FGH_STATIC_DRAW); + fghBindBuffer(FGH_ARRAY_BUFFER, 0); + } if (vertIdxs != NULL) { fghGenBuffers(1, &ibo_elements); @@ -449,6 +464,20 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLsizei ); fghBindBuffer(FGH_ARRAY_BUFFER, 0); }; + + if (vbo_textcs) { + fghEnableVertexAttribArray(attribute_v_texture); + fghBindBuffer(FGH_ARRAY_BUFFER, vbo_textcs); + fghVertexAttribPointer( + attribute_v_texture,/* attribute */ + 2, /* number of elements per vertex, here (s,t) */ + GL_FLOAT, /* the type of each element */ + GL_FALSE, /* take our values as-is */ + 0, /* no extra data between each position */ + 0 /* offset of first element */ + ); + fghBindBuffer(FGH_ARRAY_BUFFER, 0); + }; if (vertIdxs == NULL) { glDrawArrays(GL_TRIANGLES, 0, numVertices); @@ -470,11 +499,15 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLsizei fghDisableVertexAttribArray(attribute_v_coord); if (vbo_normals != 0) fghDisableVertexAttribArray(attribute_v_normal); + if (vbo_textcs != 0) + fghDisableVertexAttribArray(attribute_v_texture); if (vbo_coords != 0) fghDeleteBuffers(1, &vbo_coords); if (vbo_normals != 0) fghDeleteBuffers(1, &vbo_normals); + if (vbo_textcs != 0) + fghDeleteBuffers(1, &vbo_textcs); if (ibo_elements != 0) fghDeleteBuffers(1, &ibo_elements); } @@ -483,47 +516,26 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLsizei /** * Generate vertex indices for visualizing the normals. + * vertices are written into verticesForNormalVisualization. + * This must be freed by caller, we do the free at the + * end of fghDrawNormalVisualization11/fghDrawNormalVisualization20 */ static GLfloat *verticesForNormalVisualization; -static GLushort numNormalVertices = 0; -static void fghGenerateNormalVisualization(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, - GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart) +static GLsizei numNormalVertices = 0; +static void fghGenerateNormalVisualization(GLfloat *vertices, GLfloat *normals, GLsizei numVertices) { - GLushort i,j; - /* calc number of vertices to generate, allocate. Must be freed by caller - * We do the free at the end of fghDrawNormalVisualization11/fghDrawNormalVisualization20 - */ - if (!vertIdxs) - numNormalVertices = numVertices * 2; - else - numNormalVertices = numParts * numVertIdxsPerPart * 2; + int i,j; + numNormalVertices = numVertices * 2; verticesForNormalVisualization = malloc(numNormalVertices*3 * sizeof(GLfloat)); - /* Now generate vertices for lines to draw the normals */ - if (!vertIdxs) - { - for (i=0,j=0; i #include "fg_internal.h" #include "fg_teapot_data.h" +/* -- STATIC VARS: CACHES ---------------------------------------------------- */ + +/* Teapot defs */ +#define GLUT_TEAPOT_N_PATCHES (6*4 + 4*2) /* 6 patches are reproduced (rotated) 4 times, 4 patches (flipped) 2 times */ +#define GLUT_SOLID_TEAPOT_N_SUBDIV 10 +#define GLUT_SOLID_TEAPOT_N_VERT GLUT_SOLID_TEAPOT_N_SUBDIV*GLUT_SOLID_TEAPOT_N_SUBDIV * GLUT_TEAPOT_N_PATCHES /* N_SUBDIV^2 vertices per patch */ +#define GLUT_SOLID_TEAPOT_N_TRI (GLUT_SOLID_TEAPOT_N_SUBDIV-1)*(GLUT_SOLID_TEAPOT_N_SUBDIV-1) * GLUT_TEAPOT_N_PATCHES * 2 /* if e.g. 7x7 vertices for each patch, there are 6*6 squares for each patch. Each square is decomposed into 2 triangles */ + +#define GLUT_WIRE_TEAPOT_N_SUBDIV 7 +#define GLUT_WIRE_TEAPOT_N_VERT GLUT_WIRE_TEAPOT_N_SUBDIV*GLUT_WIRE_TEAPOT_N_SUBDIV * GLUT_TEAPOT_N_PATCHES /* N_SUBDIV^2 vertices per patch */ + +/* Bernstein coefficients only have to be precomputed once (number of patch subdivisions is fixed) + * Can thus define arrays for them here, they will be filled upon first use. + * 3rd order Bezier surfaces have 4 Bernstein coeffs. + * Have separate caches for solid and wire as they use a different number of subdivisions + * _0 is for Bernstein polynomials, _1 for their first derivative (which we need for normals) + */ +static GLfloat bernWire_0 [GLUT_WIRE_TEAPOT_N_SUBDIV] [4]; +static GLfloat bernWire_1 [GLUT_WIRE_TEAPOT_N_SUBDIV] [4]; +static GLfloat bernSolid_0[GLUT_SOLID_TEAPOT_N_SUBDIV][4]; +static GLfloat bernSolid_1[GLUT_SOLID_TEAPOT_N_SUBDIV][4]; + +/* Bit of caching: + * vertex indices and normals only need to be generated once for + * a given number of subdivisions as they don't change with scale. + * Vertices can be cached and reused if scale didn't change. + */ +static GLushort vertIdxsSolid[GLUT_SOLID_TEAPOT_N_TRI*3]; +static GLfloat normsSolid [GLUT_SOLID_TEAPOT_N_VERT*3]; +static GLfloat vertsSolid [GLUT_SOLID_TEAPOT_N_VERT*3]; +static GLfloat texcsSolid [GLUT_SOLID_TEAPOT_N_VERT*2]; +static GLfloat lastScaleSolid = 0.f; +static GLboolean initedSolid = GL_FALSE; + +static GLushort vertIdxsWire [GLUT_WIRE_TEAPOT_N_VERT*2]; +static GLfloat normsWire [GLUT_WIRE_TEAPOT_N_VERT*3]; +static GLfloat vertsWire [GLUT_WIRE_TEAPOT_N_VERT*3]; +static GLfloat lastScaleWire = 0.f; +static GLboolean initedWire = GL_FALSE; + /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */ +extern void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices, + GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart); +extern void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numVertices, + GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode, + GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2); +/* evaluate 3rd order Bernstein polynomial and its 1st deriv */ +static void bernstein3(int i, GLfloat x, GLfloat *r0, GLfloat *r1) +{ + float invx = 1.f - x; + + /* r0: zero order coeff, r1: first deriv coeff */ + switch (i) + { + GLfloat temp; + case 0: + temp = invx*invx; + *r0 = invx * temp; /* invx * invx * invx */ + *r1 = -3 * temp; /* -3 * invx * invx */ + break; + case 1: + temp = invx*invx; + *r0 = 3 * x * temp; /* 3 * x * invx * invx */ + *r1 = 3 * temp - 6 * x * invx; /* 3 * invx * invx - 6 * x * invx */ + break; + case 2: + temp = x*x; + *r0 = 3 * temp * invx; /* 3 * x * x * invx */ + *r1 = 6 * x * invx - 3 * temp; /* 6 * x * invx - 3 * x * x */ + break; + case 3: + temp = x*x; + *r0 = x * temp; /* x * x * x */ + *r1 = 3 * temp; /* 3 * x * x */ + break; + default: + *r0 = *r1 = 0; + } +} -static void fghTeapot( GLint grid, GLdouble scale, GLenum type ) +static void pregenBernstein(int nSubDivs, GLfloat (*bern_0)[4], GLfloat (*bern_1)[4]) { -#if defined(_WIN32_WCE) - int i, numV=sizeof(strip_vertices)/4, numI=sizeof(strip_normals)/4; -#else - double p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3]; - long i, j, k, l; -#endif - - glPushAttrib( GL_ENABLE_BIT | GL_EVAL_BIT ); - glEnable( GL_AUTO_NORMAL ); - glEnable( GL_NORMALIZE ); - glEnable( GL_MAP2_VERTEX_3 ); - glEnable( GL_MAP2_TEXTURE_COORD_2 ); - - glPushMatrix(); - glRotated( 270.0, 1.0, 0.0, 0.0 ); - glScaled( 0.5 * scale, 0.5 * scale, 0.5 * scale ); - glTranslated( 0.0, 0.0, -1.5 ); - -#if defined(_WIN32_WCE) - glRotated( 90.0, 1.0, 0.0, 0.0 ); - glBegin( GL_TRIANGLE_STRIP ); - - for( i = 0; i < numV-1; i++ ) + int s,i; + for (s=0; s