4 * Freeglut geometry rendering methods.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Fri Dec 3 1999
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <GL/freeglut.h>
29 #include "fg_internal.h"
32 * Need more types of polyhedra? See CPolyhedron in MRPT
36 /* General functions for drawing geometry
37 * Solids are drawn by glDrawArrays if composed of triangles, or by
38 * glDrawElements if consisting of squares or pentagons that were
39 * decomposed into triangles (some vertices are repeated in that case).
40 * WireFrame drawing will have to be done per face, using GL_LINE_LOOP and
41 * issuing one draw call per face. Always use glDrawArrays as no triangle
42 * decomposition needed. We use the "first" parameter in glDrawArrays to go
45 static void fghDrawGeometryWire(GLdouble *vertices, GLdouble *normals, GLsizei numFaces, GLsizei numEdgePerFace)
49 glEnableClientState(GL_VERTEX_ARRAY);
50 glEnableClientState(GL_NORMAL_ARRAY);
52 glVertexPointer(3, GL_DOUBLE, 0, vertices);
53 glNormalPointer(GL_DOUBLE, 0, normals);
55 /* Draw per face (TODO: could use glMultiDrawArrays if available) */
56 for (i=0; i<numFaces; i++)
57 glDrawArrays(GL_LINE_LOOP, i*numEdgePerFace, numEdgePerFace);
59 glDisableClientState(GL_VERTEX_ARRAY);
60 glDisableClientState(GL_NORMAL_ARRAY);
62 static void fghDrawGeometrySolid(GLdouble *vertices, GLdouble *normals, GLubyte *vertIdxs, GLsizei numVertices, GLsizei numEdgePerFace)
64 glEnableClientState(GL_VERTEX_ARRAY);
65 glEnableClientState(GL_NORMAL_ARRAY);
67 glVertexPointer(3, GL_DOUBLE, 0, vertices);
68 glNormalPointer(GL_DOUBLE, 0, normals);
69 if (numEdgePerFace==3)
70 glDrawArrays(GL_TRIANGLES, 0, numVertices);
72 glDrawElements(GL_TRIANGLES, numVertices, GL_UNSIGNED_BYTE, vertIdxs);
74 glDisableClientState(GL_VERTEX_ARRAY);
75 glDisableClientState(GL_NORMAL_ARRAY);
78 /* Shape decomposition to triangles
79 * We'll use glDrawElements to draw all shapes that are not triangles, so
80 * generate an index vector here, using the below sampling scheme.
81 * Be careful to keep winding of all triangles counter-clockwise,
82 * assuming that input has correct winding...
84 static GLubyte vert4Decomp[6] = {0,1,2, 0,2,3}; /* quad : 4 input vertices, 6 output (2 triangles) */
85 static GLubyte vert5Decomp[9] = {0,1,2, 0,2,4, 4,2,3}; /* pentagon: 5 input vertices, 9 output (3 triangles) */
87 static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace, GLdouble *vertices, GLubyte *vertIndices, GLdouble *normals, GLdouble *vertOut, GLdouble *normOut, GLubyte *vertIdxOut)
89 int i,j,numEdgeIdxPerFace;
90 GLubyte *vertSamps = NULL;
91 switch (numEdgePerFace)
94 /* nothing to do here, we'll drawn with glDrawArrays */
97 vertSamps = vert4Decomp;
98 numEdgeIdxPerFace = 6; /* 6 output vertices for each face */
101 vertSamps = vert5Decomp;
102 numEdgeIdxPerFace = 9; /* 9 output vertices for each face */
106 * Build array with vertices using vertex coordinates and vertex indices
107 * Do same for normals.
108 * Need to do this because of different normals at shared vertices.
110 for (i=0; i<numFaces; i++)
113 int faceIdxVertIdx = i*numEdgePerFace; // index to first element of "row" in vertex indices
114 for (j=0; j<numEdgePerFace; j++)
116 int outIdx = i*numEdgePerFace*3+j*3;
117 int vertIdx = vertIndices[faceIdxVertIdx+j]*3;
119 vertOut[outIdx ] = vertices[vertIdx ];
120 vertOut[outIdx+1] = vertices[vertIdx+1];
121 vertOut[outIdx+2] = vertices[vertIdx+2];
123 normOut[outIdx ] = normals [normIdx ];
124 normOut[outIdx+1] = normals [normIdx+1];
125 normOut[outIdx+2] = normals [normIdx+2];
128 /* generate vertex indices for each face */
130 for (j=0; j<numEdgeIdxPerFace; j++)
131 vertIdxOut[i*numEdgeIdxPerFace+j] = faceIdxVertIdx + vertSamps[j];
135 static void fghGenerateGeometry(int numFaces, int numEdgePerFace, GLdouble *vertices, GLubyte *vertIndices, GLdouble *normals, GLdouble *vertOut, GLdouble *normOut)
137 /* This function does the same as fghGenerateGeometryWithIndexArray, just skipping the index array generation... */
138 fghGenerateGeometryWithIndexArray(numFaces, numEdgePerFace, vertices, vertIndices, normals, vertOut, normOut, NULL);
142 /* -- INTERNAL SETUP OF GEOMETRY --------------------------------------- */
143 /* -- stuff that can be cached -- */
144 /* Cache of input to glDrawArrays or glDrawElements
145 * In general, we build arrays with all vertices or normals.
146 * We cant compress this and use glDrawElements as all combinations of
147 * vertex and normals are unique.
149 #define DECLARE_SHAPE_CACHE(name,nameICaps,nameCaps)\
150 static GLboolean name##Cached = FALSE;\
151 static GLdouble name##_verts[nameCaps##_VERT_ELEM_PER_OBJ];\
152 static GLdouble name##_norms[nameCaps##_VERT_ELEM_PER_OBJ];\
153 static void fgh##nameICaps##Generate()\
155 fghGenerateGeometry(nameCaps##_NUM_FACES, nameCaps##_NUM_EDGE_PER_FACE,\
156 name##_v, name##_vi, name##_n,\
157 name##_verts, name##_norms);\
159 #define DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(name,nameICaps,nameCaps)\
160 static GLboolean name##Cached = FALSE;\
161 static GLdouble name##_verts[nameCaps##_VERT_ELEM_PER_OBJ];\
162 static GLdouble name##_norms[nameCaps##_VERT_ELEM_PER_OBJ];\
163 static GLubyte name##_vertIdxs[nameCaps##_VERT_PER_OBJ_TRI];\
164 static void fgh##nameICaps##Generate()\
166 fghGenerateGeometryWithIndexArray(nameCaps##_NUM_FACES, nameCaps##_NUM_EDGE_PER_FACE,\
167 name##_v, name##_vi, name##_n,\
168 name##_verts, name##_norms, name##_vertIdxs);\
172 #define CUBE_NUM_VERT 8
173 #define CUBE_NUM_FACES 6
174 #define CUBE_NUM_EDGE_PER_FACE 4
175 #define CUBE_VERT_PER_OBJ (CUBE_NUM_FACES*CUBE_NUM_EDGE_PER_FACE)
176 #define CUBE_VERT_ELEM_PER_OBJ (CUBE_VERT_PER_OBJ*3)
177 #define CUBE_VERT_PER_OBJ_TRI (CUBE_VERT_PER_OBJ+CUBE_NUM_FACES*2) /* 2 extra edges per face when drawing quads as triangles */
178 /* Vertex Coordinates */
179 static GLdouble cube_v[CUBE_NUM_VERT*3] =
191 static GLdouble cube_n[CUBE_NUM_FACES*3] =
202 static GLubyte cube_vi[CUBE_VERT_PER_OBJ] =
211 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(cube,Cube,CUBE);
213 /* -- Dodecahedron -- */
214 /* Magic Numbers: It is possible to create a dodecahedron by attaching two
215 * pentagons to each face of of a cube. The coordinates of the points are:
216 * (+-x,0, z); (+-1, 1, 1); (0, z, x )
217 * where x = (-1 + sqrt(5))/2, z = (1 + sqrt(5))/2 or
218 * x = 0.61803398875 and z = 1.61803398875.
220 #define DODECAHEDRON_NUM_VERT 20
221 #define DODECAHEDRON_NUM_FACES 12
222 #define DODECAHEDRON_NUM_EDGE_PER_FACE 5
223 #define DODECAHEDRON_VERT_PER_OBJ (DODECAHEDRON_NUM_FACES*DODECAHEDRON_NUM_EDGE_PER_FACE)
224 #define DODECAHEDRON_VERT_ELEM_PER_OBJ (DODECAHEDRON_VERT_PER_OBJ*3)
225 #define DODECAHEDRON_VERT_PER_OBJ_TRI (DODECAHEDRON_VERT_PER_OBJ+DODECAHEDRON_NUM_FACES*4) /* 4 extra edges per face when drawing pentagons as triangles */
226 /* Vertex Coordinates */
227 static GLdouble dodecahedron_v[DODECAHEDRON_NUM_VERT*3] =
229 0.0 , 1.61803398875, 0.61803398875,
231 -0.61803398875, 0.0 , 1.61803398875,
232 0.61803398875, 0.0 , 1.61803398875,
234 0.0 , 1.61803398875, -0.61803398875,
236 0.61803398875, 0.0 , -1.61803398875,
237 -0.61803398875, 0.0 , -1.61803398875,
239 0.0 , -1.61803398875, 0.61803398875,
242 0.0 , -1.61803398875, -0.61803398875,
245 1.61803398875, -0.61803398875, 0.0 ,
246 1.61803398875, 0.61803398875, 0.0 ,
247 -1.61803398875, 0.61803398875, 0.0 ,
248 -1.61803398875, -0.61803398875, 0.0
251 static GLdouble dodecahedron_n[DODECAHEDRON_NUM_FACES*3] =
253 0.0 , 0.525731112119, 0.850650808354,
254 0.0 , 0.525731112119, -0.850650808354,
255 0.0 , -0.525731112119, 0.850650808354,
256 0.0 , -0.525731112119, -0.850650808354,
258 0.850650808354, 0.0 , 0.525731112119,
259 -0.850650808354, 0.0 , 0.525731112119,
260 0.850650808354, 0.0 , -0.525731112119,
261 -0.850650808354, 0.0 , -0.525731112119,
263 0.525731112119, 0.850650808354, 0.0 ,
264 0.525731112119, -0.850650808354, 0.0 ,
265 -0.525731112119, 0.850650808354, 0.0 ,
266 -0.525731112119, -0.850650808354, 0.0 ,
270 static GLubyte dodecahedron_vi[DODECAHEDRON_VERT_PER_OBJ] =
287 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON);
290 /* -- Icosahedron -- */
291 #define ICOSAHEDRON_NUM_VERT 12
292 #define ICOSAHEDRON_NUM_FACES 20
293 #define ICOSAHEDRON_NUM_EDGE_PER_FACE 3
294 #define ICOSAHEDRON_VERT_PER_OBJ (ICOSAHEDRON_NUM_FACES*ICOSAHEDRON_NUM_EDGE_PER_FACE)
295 #define ICOSAHEDRON_VERT_ELEM_PER_OBJ (ICOSAHEDRON_VERT_PER_OBJ*3)
296 #define ICOSAHEDRON_VERT_PER_OBJ_TRI ICOSAHEDRON_VERT_PER_OBJ
297 /* Vertex Coordinates */
298 static GLdouble icosahedron_v[ICOSAHEDRON_NUM_VERT*3] =
301 0.447213595500, 0.894427191000, 0.0 ,
302 0.447213595500, 0.276393202252, 0.850650808354,
303 0.447213595500, -0.723606797748, 0.525731112119,
304 0.447213595500, -0.723606797748, -0.525731112119,
305 0.447213595500, 0.276393202252, -0.850650808354,
306 -0.447213595500, -0.894427191000, 0.0 ,
307 -0.447213595500, -0.276393202252, 0.850650808354,
308 -0.447213595500, 0.723606797748, 0.525731112119,
309 -0.447213595500, 0.723606797748, -0.525731112119,
310 -0.447213595500, -0.276393202252, -0.850650808354,
314 * icosahedron_n[i][0] = ( icosahedron_v[icosahedron_vi[i][1]][1] - icosahedron_v[icosahedron_vi[i][0]][1] ) * ( icosahedron_v[icosahedron_vi[i][2]][2] - icosahedron_v[icosahedron_vi[i][0]][2] ) - ( icosahedron_v[icosahedron_vi[i][1]][2] - icosahedron_v[icosahedron_vi[i][0]][2] ) * ( icosahedron_v[icosahedron_vi[i][2]][1] - icosahedron_v[icosahedron_vi[i][0]][1] ) ;
315 * icosahedron_n[i][1] = ( icosahedron_v[icosahedron_vi[i][1]][2] - icosahedron_v[icosahedron_vi[i][0]][2] ) * ( icosahedron_v[icosahedron_vi[i][2]][0] - icosahedron_v[icosahedron_vi[i][0]][0] ) - ( icosahedron_v[icosahedron_vi[i][1]][0] - icosahedron_v[icosahedron_vi[i][0]][0] ) * ( icosahedron_v[icosahedron_vi[i][2]][2] - icosahedron_v[icosahedron_vi[i][0]][2] ) ;
316 * icosahedron_n[i][2] = ( icosahedron_v[icosahedron_vi[i][1]][0] - icosahedron_v[icosahedron_vi[i][0]][0] ) * ( icosahedron_v[icosahedron_vi[i][2]][1] - icosahedron_v[icosahedron_vi[i][0]][1] ) - ( icosahedron_v[icosahedron_vi[i][1]][1] - icosahedron_v[icosahedron_vi[i][0]][1] ) * ( icosahedron_v[icosahedron_vi[i][2]][0] - icosahedron_v[icosahedron_vi[i][0]][0] ) ;
318 static GLdouble icosahedron_n[ICOSAHEDRON_NUM_FACES*3] =
320 0.760845213037948, 0.470228201835026, 0.341640786498800,
321 0.760845213036861, -0.179611190632978, 0.552786404500000,
322 0.760845213033849, -0.581234022404097, 0,
323 0.760845213036861, -0.179611190632978, -0.552786404500000,
324 0.760845213037948, 0.470228201835026, -0.341640786498800,
325 0.179611190628666, 0.760845213037948, 0.552786404498399,
326 0.179611190634277, -0.290617011204044, 0.894427191000000,
327 0.179611190633958, -0.940456403667806, 0,
328 0.179611190634278, -0.290617011204044, -0.894427191000000,
329 0.179611190628666, 0.760845213037948, -0.552786404498399,
330 -0.179611190633958, 0.940456403667806, 0,
331 -0.179611190634277, 0.290617011204044, 0.894427191000000,
332 -0.179611190628666, -0.760845213037948, 0.552786404498399,
333 -0.179611190628666, -0.760845213037948, -0.552786404498399,
334 -0.179611190634277, 0.290617011204044, -0.894427191000000,
335 -0.760845213036861, 0.179611190632978, -0.552786404500000,
336 -0.760845213033849, 0.581234022404097, 0,
337 -0.760845213036861, 0.179611190632978, 0.552786404500000,
338 -0.760845213037948, -0.470228201835026, 0.341640786498800,
339 -0.760845213037948, -0.470228201835026, -0.341640786498800,
343 static GLubyte icosahedron_vi[ICOSAHEDRON_VERT_PER_OBJ] =
366 DECLARE_SHAPE_CACHE(icosahedron,Icosahedron,ICOSAHEDRON);
368 /* -- Octahedron -- */
369 #define OCTAHEDRON_NUM_VERT 6
370 #define OCTAHEDRON_NUM_FACES 8
371 #define OCTAHEDRON_NUM_EDGE_PER_FACE 3
372 #define OCTAHEDRON_VERT_PER_OBJ (OCTAHEDRON_NUM_FACES*OCTAHEDRON_NUM_EDGE_PER_FACE)
373 #define OCTAHEDRON_VERT_ELEM_PER_OBJ (OCTAHEDRON_VERT_PER_OBJ*3)
374 #define OCTAHEDRON_VERT_PER_OBJ_TRI OCTAHEDRON_VERT_PER_OBJ
376 /* Vertex Coordinates */
377 static GLdouble octahedron_v[OCTAHEDRON_NUM_VERT*3] =
388 static GLdouble octahedron_n[OCTAHEDRON_NUM_FACES*3] =
390 0.577350269189, 0.577350269189, 0.577350269189, /* sqrt(1/3) */
391 0.577350269189, 0.577350269189,-0.577350269189,
392 0.577350269189,-0.577350269189, 0.577350269189,
393 0.577350269189,-0.577350269189,-0.577350269189,
394 -0.577350269189, 0.577350269189, 0.577350269189,
395 -0.577350269189, 0.577350269189,-0.577350269189,
396 -0.577350269189,-0.577350269189, 0.577350269189,
397 -0.577350269189,-0.577350269189,-0.577350269189
402 static GLubyte octahedron_vi[OCTAHEDRON_VERT_PER_OBJ] =
413 DECLARE_SHAPE_CACHE(octahedron,Octahedron,OCTAHEDRON);
415 /* -- RhombicDodecahedron -- */
416 #define RHOMBICDODECAHEDRON_NUM_VERT 14
417 #define RHOMBICDODECAHEDRON_NUM_FACES 12
418 #define RHOMBICDODECAHEDRON_NUM_EDGE_PER_FACE 4
419 #define RHOMBICDODECAHEDRON_VERT_PER_OBJ (RHOMBICDODECAHEDRON_NUM_FACES*RHOMBICDODECAHEDRON_NUM_EDGE_PER_FACE)
420 #define RHOMBICDODECAHEDRON_VERT_ELEM_PER_OBJ (RHOMBICDODECAHEDRON_VERT_PER_OBJ*3)
421 #define RHOMBICDODECAHEDRON_VERT_PER_OBJ_TRI (RHOMBICDODECAHEDRON_VERT_PER_OBJ+RHOMBICDODECAHEDRON_NUM_FACES*2) /* 2 extra edges per face when drawing quads as triangles */
423 /* Vertex Coordinates */
424 static GLdouble rhombicdodecahedron_v[RHOMBICDODECAHEDRON_NUM_VERT*3] =
427 0.707106781187, 0.0 , 0.5,
428 0.0 , 0.707106781187, 0.5,
429 -0.707106781187, 0.0 , 0.5,
430 0.0 , -0.707106781187, 0.5,
431 0.707106781187, 0.707106781187, 0.0,
432 -0.707106781187, 0.707106781187, 0.0,
433 -0.707106781187, -0.707106781187, 0.0,
434 0.707106781187, -0.707106781187, 0.0,
435 0.707106781187, 0.0 , -0.5,
436 0.0 , 0.707106781187, -0.5,
437 -0.707106781187, 0.0 , -0.5,
438 0.0 , -0.707106781187, -0.5,
442 static GLdouble rhombicdodecahedron_n[RHOMBICDODECAHEDRON_NUM_FACES*3] =
444 0.353553390594, 0.353553390594, 0.5,
445 -0.353553390594, 0.353553390594, 0.5,
446 -0.353553390594, -0.353553390594, 0.5,
447 0.353553390594, -0.353553390594, 0.5,
452 0.353553390594, 0.353553390594, -0.5,
453 -0.353553390594, 0.353553390594, -0.5,
454 -0.353553390594, -0.353553390594, -0.5,
455 0.353553390594, -0.353553390594, -0.5
459 static GLubyte rhombicdodecahedron_vi[RHOMBICDODECAHEDRON_VERT_PER_OBJ] =
474 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON);
476 /* -- Tetrahedron -- */
477 /* Magic Numbers: r0 = ( 1, 0, 0 )
478 * r1 = ( -1/3, 2 sqrt(2) / 3, 0 )
479 * r2 = ( -1/3, - sqrt(2) / 3, sqrt(6) / 3 )
480 * r3 = ( -1/3, - sqrt(2) / 3, -sqrt(6) / 3 )
481 * |r0| = |r1| = |r2| = |r3| = 1
482 * Distance between any two points is 2 sqrt(6) / 3
484 * Normals: The unit normals are simply the negative of the coordinates of the point not on the surface.
486 #define TETRAHEDRON_NUM_VERT 4
487 #define TETRAHEDRON_NUM_FACES 4
488 #define TETRAHEDRON_NUM_EDGE_PER_FACE 3
489 #define TETRAHEDRON_VERT_PER_OBJ (TETRAHEDRON_NUM_FACES*TETRAHEDRON_NUM_EDGE_PER_FACE)
490 #define TETRAHEDRON_VERT_ELEM_PER_OBJ (TETRAHEDRON_VERT_PER_OBJ*3)
491 #define TETRAHEDRON_VERT_PER_OBJ_TRI TETRAHEDRON_VERT_PER_OBJ
493 /* Vertex Coordinates */
494 static GLdouble tetrahedron_v[TETRAHEDRON_NUM_VERT*3] =
497 -0.333333333333, 0.942809041582, 0.0,
498 -0.333333333333, -0.471404520791, 0.816496580928,
499 -0.333333333333, -0.471404520791, -0.816496580928
502 static GLdouble tetrahedron_n[TETRAHEDRON_NUM_FACES*3] =
505 0.333333333333, -0.942809041582, 0.0,
506 0.333333333333, 0.471404520791, -0.816496580928,
507 0.333333333333, 0.471404520791, 0.816496580928
511 static GLubyte tetrahedron_vi[TETRAHEDRON_VERT_PER_OBJ] =
518 DECLARE_SHAPE_CACHE(tetrahedron,Tetrahedron,TETRAHEDRON);
520 /* -- Sierpinski Sponge -- */
521 static unsigned int ipow (int x, unsigned int y)
523 return y==0? 1: y==1? x: (y%2? x: 1) * ipow(x*x, y/2);
526 static void fghSierpinskiSpongeGenerate ( int numLevels, GLdouble offset[3], GLdouble scale, GLdouble* vertices, GLdouble* normals )
529 if ( numLevels == 0 )
531 for (i=0; i<TETRAHEDRON_NUM_FACES; i++)
534 int faceIdxVertIdx = i*TETRAHEDRON_NUM_EDGE_PER_FACE;
535 for (j=0; j<TETRAHEDRON_NUM_EDGE_PER_FACE; j++)
537 int outIdx = i*TETRAHEDRON_NUM_EDGE_PER_FACE*3+j*3;
538 int vertIdx = tetrahedron_vi[faceIdxVertIdx+j]*3;
540 vertices[outIdx ] = offset[0] + scale * tetrahedron_v[vertIdx ];
541 vertices[outIdx+1] = offset[1] + scale * tetrahedron_v[vertIdx+1];
542 vertices[outIdx+2] = offset[2] + scale * tetrahedron_v[vertIdx+2];
544 normals [outIdx ] = tetrahedron_n[normIdx ];
545 normals [outIdx+1] = tetrahedron_n[normIdx+1];
546 normals [outIdx+2] = tetrahedron_n[normIdx+2];
550 else if ( numLevels > 0 )
552 GLdouble local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */
553 unsigned int stride = ipow(4,--numLevels)*TETRAHEDRON_VERT_ELEM_PER_OBJ;
555 for ( i = 0 ; i < TETRAHEDRON_NUM_FACES ; i++ )
558 local_offset[0] = offset[0] + scale * tetrahedron_v[idx ];
559 local_offset[1] = offset[1] + scale * tetrahedron_v[idx+1];
560 local_offset[2] = offset[2] + scale * tetrahedron_v[idx+2];
561 fghSierpinskiSpongeGenerate ( numLevels, local_offset, scale, vertices+i*stride, normals+i*stride );
566 /* -- Now the various shapes involving circles -- */
568 * Compute lookup table of cos and sin values forming a circle
569 * (or half circle if halfCircle==TRUE)
572 * It is the responsibility of the caller to free these tables
573 * The size of the table is (n+1) to form a connected loop
574 * The last entry is exactly the same as the first
575 * The sign of n can be flipped to get the reverse loop
577 static void fghCircleTable(GLdouble **sint, GLdouble **cost, const int n, const GLboolean halfCircle)
581 /* Table size, the sign of n flips the circle direction */
582 const int size = abs(n);
584 /* Determine the angle between samples */
585 const GLdouble angle = (halfCircle?1:2)*M_PI/(GLdouble)( ( n == 0 ) ? 1 : n );
587 /* Allocate memory for n samples, plus duplicate of first entry at the end */
588 *sint = malloc(sizeof(GLdouble) * (size+1));
589 *cost = malloc(sizeof(GLdouble) * (size+1));
591 /* Bail out if memory allocation fails, fgError never returns */
592 if (!(*sint) || !(*cost))
596 fgError("Failed to allocate memory in fghCircleTable");
599 /* Compute cos and sin around the circle */
603 for (i=1; i<size; i++)
605 (*sint)[i] = sin(angle*i);
606 (*cost)[i] = cos(angle*i);
612 (*sint)[size] = 0.0; /* sin PI */
613 (*cost)[size] = -1.0; /* cos PI */
617 /* Last sample is duplicate of the first (sin or cos of 2 PI) */
618 (*sint)[size] = (*sint)[0];
619 (*cost)[size] = (*cost)[0];
624 /* -- INTERNAL DRAWING functions --------------------------------------- */
625 #define _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,vertIdxs)\
626 static void fgh##nameICaps( GLboolean useWireMode )\
630 fgh##nameICaps##Generate();\
631 name##Cached = GL_TRUE;\
636 fghDrawGeometryWire (name##_verts,name##_norms,\
637 nameCaps##_NUM_FACES,nameCaps##_NUM_EDGE_PER_FACE);\
641 fghDrawGeometrySolid(name##_verts,name##_norms,vertIdxs,\
642 nameCaps##_VERT_PER_OBJ_TRI, nameCaps##_NUM_EDGE_PER_FACE);\
645 #define DECLARE_INTERNAL_DRAW(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,NULL)
646 #define DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,name##_vertIdxs)
648 static void fghCube( GLdouble dSize, GLboolean useWireMode )
655 cubeCached = GL_TRUE;
660 /* Need to build new vertex list containing vertices for cube of different size */
663 vertices = malloc(CUBE_VERT_ELEM_PER_OBJ * sizeof(GLdouble));
665 /* Bail out if memory allocation fails, fgError never returns */
669 fgError("Failed to allocate memory in fghCube");
672 for (i=0; i<CUBE_VERT_ELEM_PER_OBJ; i++)
673 vertices[i] = dSize*cube_verts[i];
676 vertices = cube_verts;
679 fghDrawGeometryWire (vertices,cube_norms, CUBE_NUM_FACES,CUBE_NUM_EDGE_PER_FACE);
681 fghDrawGeometrySolid(vertices,cube_norms,cube_vertIdxs,CUBE_VERT_PER_OBJ_TRI, CUBE_NUM_EDGE_PER_FACE);
684 /* cleanup allocated memory */
688 DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON);
689 DECLARE_INTERNAL_DRAW(icosahedron,Icosahedron,ICOSAHEDRON);
690 DECLARE_INTERNAL_DRAW(octahedron,Octahedron,OCTAHEDRON);
691 DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON);
692 DECLARE_INTERNAL_DRAW(tetrahedron,Tetrahedron,TETRAHEDRON);
694 static void fghSierpinskiSponge ( int numLevels, GLdouble offset[3], GLdouble scale, GLboolean useWireMode )
698 GLsizei numTetr = numLevels<0? 0 : ipow(4,numLevels); /* No sponge for numLevels below 0 */
699 GLsizei numVert = numTetr*TETRAHEDRON_VERT_PER_OBJ;
700 GLsizei numFace = numTetr*TETRAHEDRON_NUM_FACES;
704 /* Allocate memory */
705 vertices = malloc(numVert*3 * sizeof(GLdouble));
706 normals = malloc(numVert*3 * sizeof(GLdouble));
707 /* Bail out if memory allocation fails, fgError never returns */
708 if (!vertices || !normals)
712 fgError("Failed to allocate memory in fghSierpinskiSponge");
715 /* Generate elements */
716 fghSierpinskiSpongeGenerate ( numLevels, offset, scale, vertices, normals );
718 /* Draw and cleanup */
720 fghDrawGeometryWire (vertices,normals, numFace,TETRAHEDRON_NUM_EDGE_PER_FACE);
722 fghDrawGeometrySolid(vertices,normals,NULL,numVert, TETRAHEDRON_NUM_EDGE_PER_FACE);
730 /* -- INTERFACE FUNCTIONS ---------------------------------------------- */
734 * Draws a solid sphere
736 void FGAPIENTRY glutSolidSphere(GLdouble radius, GLint slices, GLint stacks)
740 /* Adjust z and radius as stacks are drawn. */
745 /* Pre-computed circle */
747 GLdouble *sint1,*cost1;
748 GLdouble *sint2,*cost2;
750 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" );
752 fghCircleTable(&sint1,&cost1,-slices,FALSE);
753 fghCircleTable(&sint2,&cost2, stacks,TRUE);
755 /* The top stack is covered with a triangle fan */
758 z1 = cost2[(stacks>0)?1:0];
760 r1 = sint2[(stacks>0)?1:0];
762 glBegin(GL_TRIANGLE_FAN);
765 glVertex3d(0,0,radius);
767 for (j=slices; j>=0; j--)
769 glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 );
770 glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
775 /* Cover each stack with a quad strip, except the top and bottom stacks */
777 for( i=1; i<stacks-1; i++ )
779 z0 = z1; z1 = cost2[i+1];
780 r0 = r1; r1 = sint2[i+1];
782 glBegin(GL_QUAD_STRIP);
784 for(j=0; j<=slices; j++)
786 glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 );
787 glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
788 glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 );
789 glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
795 /* The bottom stack is covered with a triangle fan */
800 glBegin(GL_TRIANGLE_FAN);
803 glVertex3d(0,0,-radius);
805 for (j=0; j<=slices; j++)
807 glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 );
808 glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
813 /* Release sin and cos tables */
822 * Draws a wire sphere
824 void FGAPIENTRY glutWireSphere(GLdouble radius, GLint slices, GLint stacks)
828 /* Adjust z and radius as stacks and slices are drawn. */
833 /* Pre-computed circle */
835 GLdouble *sint1,*cost1;
836 GLdouble *sint2,*cost2;
838 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" );
840 fghCircleTable(&sint1,&cost1,-slices,FALSE);
841 fghCircleTable(&sint2,&cost2, stacks,TRUE);
843 /* Draw a line loop for each stack */
845 for (i=1; i<stacks; i++)
850 glBegin(GL_LINE_LOOP);
852 for(j=0; j<=slices; j++)
858 glVertex3d(x*r*radius,y*r*radius,z*radius);
864 /* Draw a line loop for each slice */
866 for (i=0; i<slices; i++)
868 glBegin(GL_LINE_STRIP);
870 for(j=0; j<=stacks; j++)
872 x = cost1[i]*sint2[j];
873 y = sint1[i]*sint2[j];
877 glVertex3d(x*radius,y*radius,z*radius);
883 /* Release sin and cos tables */
894 void FGAPIENTRY glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLint stacks )
898 /* Step in z and radius as stacks are drawn. */
903 const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
904 const GLdouble rStep = base / ( ( stacks > 0 ) ? stacks : 1 );
906 /* Scaling factors for vertex normals */
908 const GLdouble cosn = ( height / sqrt ( height * height + base * base ));
909 const GLdouble sinn = ( base / sqrt ( height * height + base * base ));
911 /* Pre-computed circle */
913 GLdouble *sint,*cost;
915 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" );
917 fghCircleTable(&sint,&cost,-slices,FALSE);
919 /* Cover the circular base with a triangle fan... */
927 glBegin(GL_TRIANGLE_FAN);
929 glNormal3d(0.0,0.0,-1.0);
930 glVertex3d(0.0,0.0, z0 );
932 for (j=0; j<=slices; j++)
933 glVertex3d(cost[j]*r0, sint[j]*r0, z0);
937 /* Cover each stack with a quad strip, except the top stack */
939 for( i=0; i<stacks-1; i++ )
941 glBegin(GL_QUAD_STRIP);
943 for(j=0; j<=slices; j++)
945 glNormal3d(cost[j]*cosn, sint[j]*cosn, sinn);
946 glVertex3d(cost[j]*r0, sint[j]*r0, z0 );
947 glVertex3d(cost[j]*r1, sint[j]*r1, z1 );
950 z0 = z1; z1 += zStep;
951 r0 = r1; r1 -= rStep;
956 /* The top stack is covered with individual triangles */
958 glBegin(GL_TRIANGLES);
960 glNormal3d(cost[0]*sinn, sint[0]*sinn, cosn);
962 for (j=0; j<slices; j++)
964 glVertex3d(cost[j+0]*r0, sint[j+0]*r0, z0 );
965 glVertex3d(0, 0, height);
966 glNormal3d(cost[j+1]*sinn, sint[j+1]*sinn, cosn );
967 glVertex3d(cost[j+1]*r0, sint[j+1]*r0, z0 );
972 /* Release sin and cos tables */
981 void FGAPIENTRY glutWireCone( GLdouble base, GLdouble height, GLint slices, GLint stacks)
985 /* Step in z and radius as stacks are drawn. */
990 const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
991 const GLdouble rStep = base / ( ( stacks > 0 ) ? stacks : 1 );
993 /* Scaling factors for vertex normals */
995 const GLdouble cosn = ( height / sqrt ( height * height + base * base ));
996 const GLdouble sinn = ( base / sqrt ( height * height + base * base ));
998 /* Pre-computed circle */
1000 GLdouble *sint,*cost;
1002 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" );
1004 fghCircleTable(&sint,&cost,-slices,FALSE);
1006 /* Draw the stacks... */
1008 for (i=0; i<stacks; i++)
1010 glBegin(GL_LINE_LOOP);
1012 for( j=0; j<slices; j++ )
1014 glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn);
1015 glVertex3d(cost[j]*r, sint[j]*r, z );
1024 /* Draw the slices */
1030 for (j=0; j<slices; j++)
1032 glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn );
1033 glVertex3d(cost[j]*r, sint[j]*r, 0.0 );
1034 glVertex3d(0.0, 0.0, height);
1039 /* Release sin and cos tables */
1047 * Draws a solid cylinder
1049 void FGAPIENTRY glutSolidCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks)
1053 /* Step in z and radius as stacks are drawn. */
1056 const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
1058 /* Pre-computed circle */
1060 GLdouble *sint,*cost;
1062 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCylinder" );
1064 fghCircleTable(&sint,&cost,-slices,FALSE);
1066 /* Cover the base and top */
1068 glBegin(GL_TRIANGLE_FAN);
1069 glNormal3d(0.0, 0.0, -1.0 );
1070 glVertex3d(0.0, 0.0, 0.0 );
1071 for (j=0; j<=slices; j++)
1072 glVertex3d(cost[j]*radius, sint[j]*radius, 0.0);
1075 glBegin(GL_TRIANGLE_FAN);
1076 glNormal3d(0.0, 0.0, 1.0 );
1077 glVertex3d(0.0, 0.0, height);
1078 for (j=slices; j>=0; j--)
1079 glVertex3d(cost[j]*radius, sint[j]*radius, height);
1087 for (i=1; i<=stacks; i++)
1092 glBegin(GL_QUAD_STRIP);
1093 for (j=0; j<=slices; j++ )
1095 glNormal3d(cost[j], sint[j], 0.0 );
1096 glVertex3d(cost[j]*radius, sint[j]*radius, z0 );
1097 glVertex3d(cost[j]*radius, sint[j]*radius, z1 );
1101 z0 = z1; z1 += zStep;
1104 /* Release sin and cos tables */
1111 * Draws a wire cylinder
1113 void FGAPIENTRY glutWireCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks)
1117 /* Step in z and radius as stacks are drawn. */
1120 const GLdouble zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
1122 /* Pre-computed circle */
1124 GLdouble *sint,*cost;
1126 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCylinder" );
1128 fghCircleTable(&sint,&cost,-slices,FALSE);
1130 /* Draw the stacks... */
1132 for (i=0; i<=stacks; i++)
1137 glBegin(GL_LINE_LOOP);
1139 for( j=0; j<slices; j++ )
1141 glNormal3d(cost[j], sint[j], 0.0);
1142 glVertex3d(cost[j]*radius, sint[j]*radius, z );
1150 /* Draw the slices */
1154 for (j=0; j<slices; j++)
1156 glNormal3d(cost[j], sint[j], 0.0 );
1157 glVertex3d(cost[j]*radius, sint[j]*radius, 0.0 );
1158 glVertex3d(cost[j]*radius, sint[j]*radius, height);
1163 /* Release sin and cos tables */
1170 * Draws a wire torus
1172 void FGAPIENTRY glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings )
1174 GLdouble iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi;
1175 GLdouble *vertex, *normal;
1177 GLdouble spsi, cpsi, sphi, cphi ;
1179 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireTorus" );
1181 if ( nSides < 1 ) nSides = 1;
1182 if ( nRings < 1 ) nRings = 1;
1184 /* Allocate the vertices array */
1185 vertex = (GLdouble *)calloc( sizeof(GLdouble), 3 * nSides * nRings );
1186 normal = (GLdouble *)calloc( sizeof(GLdouble), 3 * nSides * nRings );
1190 dpsi = 2.0 * M_PI / (GLdouble)nRings ;
1191 dphi = -2.0 * M_PI / (GLdouble)nSides ;
1194 for( j=0; j<nRings; j++ )
1196 cpsi = cos ( psi ) ;
1197 spsi = sin ( psi ) ;
1200 for( i=0; i<nSides; i++ )
1202 int offset = 3 * ( j * nSides + i ) ;
1203 cphi = cos ( phi ) ;
1204 sphi = sin ( phi ) ;
1205 *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
1206 *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
1207 *(vertex + offset + 2) = sphi * iradius ;
1208 *(normal + offset + 0) = cpsi * cphi ;
1209 *(normal + offset + 1) = spsi * cphi ;
1210 *(normal + offset + 2) = sphi ;
1217 for( i=0; i<nSides; i++ )
1219 glBegin( GL_LINE_LOOP );
1221 for( j=0; j<nRings; j++ )
1223 int offset = 3 * ( j * nSides + i ) ;
1224 glNormal3dv( normal + offset );
1225 glVertex3dv( vertex + offset );
1231 for( j=0; j<nRings; j++ )
1233 glBegin(GL_LINE_LOOP);
1235 for( i=0; i<nSides; i++ )
1237 int offset = 3 * ( j * nSides + i ) ;
1238 glNormal3dv( normal + offset );
1239 glVertex3dv( vertex + offset );
1251 * Draws a solid torus
1253 void FGAPIENTRY glutSolidTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings )
1255 GLdouble iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi;
1256 GLdouble *vertex, *normal;
1258 GLdouble spsi, cpsi, sphi, cphi ;
1260 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidTorus" );
1262 if ( nSides < 1 ) nSides = 1;
1263 if ( nRings < 1 ) nRings = 1;
1265 /* Increment the number of sides and rings to allow for one more point than surface */
1269 /* Allocate the vertices array */
1270 vertex = (GLdouble *)calloc( sizeof(GLdouble), 3 * nSides * nRings );
1271 normal = (GLdouble *)calloc( sizeof(GLdouble), 3 * nSides * nRings );
1275 dpsi = 2.0 * M_PI / (GLdouble)(nRings - 1) ;
1276 dphi = -2.0 * M_PI / (GLdouble)(nSides - 1) ;
1279 for( j=0; j<nRings; j++ )
1281 cpsi = cos ( psi ) ;
1282 spsi = sin ( psi ) ;
1285 for( i=0; i<nSides; i++ )
1287 int offset = 3 * ( j * nSides + i ) ;
1288 cphi = cos ( phi ) ;
1289 sphi = sin ( phi ) ;
1290 *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
1291 *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
1292 *(vertex + offset + 2) = sphi * iradius ;
1293 *(normal + offset + 0) = cpsi * cphi ;
1294 *(normal + offset + 1) = spsi * cphi ;
1295 *(normal + offset + 2) = sphi ;
1302 glBegin( GL_QUADS );
1303 for( i=0; i<nSides-1; i++ )
1305 for( j=0; j<nRings-1; j++ )
1307 int offset = 3 * ( j * nSides + i ) ;
1308 glNormal3dv( normal + offset );
1309 glVertex3dv( vertex + offset );
1310 glNormal3dv( normal + offset + 3 );
1311 glVertex3dv( vertex + offset + 3 );
1312 glNormal3dv( normal + offset + 3 * nSides + 3 );
1313 glVertex3dv( vertex + offset + 3 * nSides + 3 );
1314 glNormal3dv( normal + offset + 3 * nSides );
1315 glVertex3dv( vertex + offset + 3 * nSides );
1328 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
1329 /* Macro to generate interface functions */
1330 #define DECLARE_SHAPE_INTERFACE(nameICaps)\
1331 void FGAPIENTRY glutWire##nameICaps( void )\
1333 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWire"#nameICaps );\
1334 fgh##nameICaps( TRUE );\
1336 void FGAPIENTRY glutSolid##nameICaps( void )\
1338 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolid"#nameICaps );\
1339 fgh##nameICaps( FALSE );\
1342 void FGAPIENTRY glutWireCube( GLdouble dSize )
1344 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCube" );
1345 fghCube( dSize, TRUE );
1347 void FGAPIENTRY glutSolidCube( GLdouble dSize )
1349 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCube" );
1350 fghCube( dSize, FALSE );
1353 DECLARE_SHAPE_INTERFACE(Dodecahedron);
1354 DECLARE_SHAPE_INTERFACE(Icosahedron);
1355 DECLARE_SHAPE_INTERFACE(Octahedron);
1356 DECLARE_SHAPE_INTERFACE(RhombicDodecahedron);
1358 void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale )
1360 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSierpinskiSponge" );
1361 fghSierpinskiSponge ( num_levels, offset, scale, TRUE );
1363 void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale )
1365 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSierpinskiSponge" );
1366 fghSierpinskiSponge ( num_levels, offset, scale, FALSE );
1369 DECLARE_SHAPE_INTERFACE(Tetrahedron);
1372 /*** END OF FILE ***/