From d85c1ff566830830dae09c5cb9b3b6f15a781369 Mon Sep 17 00:00:00 2001 From: Diederick Niehorster Date: Fri, 16 Mar 2012 03:14:23 +0000 Subject: [PATCH] Cube also moved to glDrawElements git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@1145 7f0cb862-5218-0410-a997-914c9d46530a --- src/fg_geometry.c | 190 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 117 insertions(+), 73 deletions(-) diff --git a/src/fg_geometry.c b/src/fg_geometry.c index 2ecf63c..583b544 100644 --- a/src/fg_geometry.c +++ b/src/fg_geometry.c @@ -61,8 +61,7 @@ */ -/* - * General function for drawing geometry. As for all geometry we have no +/* General function for drawing geometry. As for all geometry we have no * redundancy (or hardly any in the case of cones and cylinders) in terms * of the vertex/normal combinations, we just use glDrawArrays. * useWireMode controls the drawing of solids (false) or wire frame @@ -99,6 +98,73 @@ static unsigned int ipow (int x, unsigned int y) return y==0? 1: y==1? x: (y%2? x: 1) * ipow(x*x, y/2); } +/* -- stuff that can be cached -- */ +/* + * In general, we build arrays with all vertices or normals. + * We cant compress this and use glDrawElements as all combinations of + * vertex and normals are unique. + */ + +/* -- Cube -- */ +#define CUBE_NUM_VERT 8 +#define CUBE_NUM_FACES 6 +#define CUBE_NUM_VERT_PER_FACE 4 +#define CUBE_VERT_PER_TETR CUBE_NUM_FACES*CUBE_NUM_VERT_PER_FACE +#define CUBE_VERT_ELEM_PER_TETR CUBE_VERT_PER_TETR*3 +/* Vertex Coordinates */ +static GLdouble cube_v[CUBE_NUM_VERT][3] = +{ + { .5, .5, .5}, + {-.5, .5, .5}, + {-.5,-.5, .5}, + { .5,-.5, .5}, + { .5,-.5,-.5}, + { .5, .5,-.5}, + {-.5, .5,-.5}, + {-.5,-.5,-.5} +}; +/* Normal Vectors */ +static GLdouble cube_n[CUBE_NUM_FACES][3] = +{ + { 1.0, 0.0, 0.0}, + { 0.0, 1.0, 0.0}, + { 0.0, 0.0, 1.0}, + {-1.0, 0.0, 0.0}, + { 0.0,-1.0, 0.0}, + { 0.0, 0.0,-1.0} +}; + +/* Vertex indices */ +static GLubyte cube_vi[CUBE_NUM_FACES][CUBE_NUM_VERT_PER_FACE] = +{ + {0,1,2,3}, {0,3,4,5}, {0,5,6,1}, {1,6,7,2}, {7,4,3,2}, {4,7,6,5} +}; + +/* Cache of input to glDrawArrays */ +static GLboolean cubeCached = FALSE; +static double cube_verts[CUBE_VERT_ELEM_PER_TETR]; +static double cube_norms[CUBE_VERT_ELEM_PER_TETR]; + +static void fghCubeGenerate() +{ + int i,j; + for (i=0; i - */ -void FGAPIENTRY glutWireCube( GLdouble dSize ) -{ - double size = dSize * 0.5; - - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCube" ); - -# define V(a,b,c) glVertex3d( a size, b size, c size ); -# define N(a,b,c) glNormal3d( a, b, c ); - - /* PWO: I dared to convert the code to use macros... */ - glBegin( GL_LINE_LOOP ); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); glEnd(); - glBegin( GL_LINE_LOOP ); N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); glEnd(); - glBegin( GL_LINE_LOOP ); N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+); glEnd(); - glBegin( GL_LINE_LOOP ); N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-); glEnd(); - glBegin( GL_LINE_LOOP ); N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+); glEnd(); - glBegin( GL_LINE_LOOP ); N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-); glEnd(); - -# undef V -# undef N -} - -/* - * Draws a solid cube. Code contributed by Andreas Umbach - */ -void FGAPIENTRY glutSolidCube( GLdouble dSize ) -{ - double size = dSize * 0.5; - - FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCube" ); - -# define V(a,b,c) glVertex3d( a size, b size, c size ); -# define N(a,b,c) glNormal3d( a, b, c ); - - /* PWO: Again, I dared to convert the code to use macros... */ - glBegin( GL_QUADS ); - N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); - N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); - N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+); - N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-); - N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+); - N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-); - glEnd(); - -# undef V -# undef N -} - /* * Draws a solid sphere @@ -1263,7 +1295,19 @@ void FGAPIENTRY glutSolidRhombicDodecahedron( void ) /* -- INTERFACE FUNCTIONS -------------------------------------------------- */ - +/* + * Draws a wireframed cube. + */ +void FGAPIENTRY glutWireCube( GLdouble dSize ) +{ + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCube" ); + fghCube( dSize, TRUE ); +} +void FGAPIENTRY glutSolidCube( GLdouble dSize ) +{ + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCube" ); + fghCube( dSize, FALSE ); +} void FGAPIENTRY glutWireTetrahedron( void ) { -- 1.7.10.4