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"
34 * Need more types of polyhedra? See CPolyhedron in MRPT
37 /* VC++6 in C mode doesn't have C99's sinf/cos/sqrtf */
39 #define sinf(x) (float)sin((double)(x))
42 #define cosf(x) (float)cos((double)(x))
45 #define sqrtf(x) (float)sqrt((double)(x))
48 /* General functions for drawing geometry
49 * Solids are drawn by glDrawArrays if composed of triangles, or by
50 * glDrawElements if consisting of squares or pentagons that were
51 * decomposed into triangles (some vertices are repeated in that case).
52 * WireFrame drawing will have to be done per face, using GL_LINE_LOOP and
53 * issuing one draw call per face. Always use glDrawArrays as no triangle
54 * decomposition needed. We use the "first" parameter in glDrawArrays to go
58 /* Version for OpenGL (ES) 1.1 */
59 #ifndef GL_ES_VERSION_2_0
60 static void fghDrawGeometryWire11(GLfloat *vertices, GLfloat *normals,
61 GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode,
62 GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2
67 glEnableClientState(GL_VERTEX_ARRAY);
68 glEnableClientState(GL_NORMAL_ARRAY);
70 glVertexPointer(3, GL_FLOAT, 0, vertices);
71 glNormalPointer(GL_FLOAT, 0, normals);
75 /* Draw per face (TODO: could use glMultiDrawArrays if available) */
76 for (i=0; i<numParts; i++)
77 glDrawArrays(vertexMode, i*numVertPerPart, numVertPerPart);
79 for (i=0; i<numParts; i++)
80 glDrawElements(vertexMode,numVertPerPart,GL_UNSIGNED_SHORT,vertIdxs+i*numVertPerPart);
83 for (i=0; i<numParts2; i++)
84 glDrawElements(GL_LINE_LOOP,numVertPerPart2,GL_UNSIGNED_SHORT,vertIdxs2+i*numVertPerPart2);
86 glDisableClientState(GL_VERTEX_ARRAY);
87 glDisableClientState(GL_NORMAL_ARRAY);
91 /* Version for OpenGL (ES) >= 2.0 */
92 static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei numVertices,
93 GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode,
94 GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2,
95 GLint attribute_v_coord, GLint attribute_v_normal
98 GLuint vbo_coords = 0, vbo_normals = 0,
99 ibo_elements = 0, ibo_elements2 = 0;
100 GLsizei numVertIdxs = numParts * numVertPerPart;
101 GLsizei numVertIdxs2 = numParts2 * numVertPerPart2;
104 if (numVertices > 0 && attribute_v_coord != -1) {
105 fghGenBuffers(1, &vbo_coords);
106 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
107 fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
108 vertices, FGH_STATIC_DRAW);
111 if (numVertices > 0 && attribute_v_normal != -1) {
112 fghGenBuffers(1, &vbo_normals);
113 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
114 fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
115 normals, FGH_STATIC_DRAW);
118 if (vertIdxs != NULL) {
119 fghGenBuffers(1, &ibo_elements);
120 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
121 fghBufferData(FGH_ELEMENT_ARRAY_BUFFER, numVertIdxs * sizeof(vertIdxs[0]),
122 vertIdxs, FGH_STATIC_DRAW);
123 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
126 if (vertIdxs2 != NULL) {
127 fghGenBuffers(1, &ibo_elements2);
128 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements2);
129 fghBufferData(FGH_ELEMENT_ARRAY_BUFFER, numVertIdxs2 * sizeof(vertIdxs2[0]),
130 vertIdxs2, FGH_STATIC_DRAW);
131 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
135 fghEnableVertexAttribArray(attribute_v_coord);
136 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
137 fghVertexAttribPointer(
138 attribute_v_coord, /* attribute */
139 3, /* number of elements per vertex, here (x,y,z) */
140 GL_FLOAT, /* the type of each element */
141 GL_FALSE, /* take our values as-is */
142 0, /* no extra data between each position */
143 0 /* offset of first element */
145 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
149 fghEnableVertexAttribArray(attribute_v_normal);
150 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
151 fghVertexAttribPointer(
152 attribute_v_normal, /* attribute */
153 3, /* number of elements per vertex, here (x,y,z) */
154 GL_FLOAT, /* the type of each element */
155 GL_FALSE, /* take our values as-is */
156 0, /* no extra data between each position */
157 0 /* offset of first element */
159 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
163 /* Draw per face (TODO: could use glMultiDrawArrays if available) */
164 for (i=0; i<numParts; i++)
165 glDrawArrays(vertexMode, i*numVertPerPart, numVertPerPart);
167 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
168 for (i=0; i<numParts; i++)
169 glDrawElements(vertexMode, numVertPerPart,
170 GL_UNSIGNED_SHORT, (GLvoid*)(sizeof(vertIdxs[0])*i*numVertPerPart));
171 /* Clean existing bindings before clean-up */
172 /* Android showed instability otherwise */
173 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
177 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements2);
182 for (i=0; i<numParts2; i++)
183 glDrawElements(GL_LINE_LOOP, numVertPerPart2,
184 GL_UNSIGNED_SHORT, (GLvoid*)(sizeof(vertIdxs2[0])*i*numVertPerPart2));
185 /* Clean existing bindings before clean-up */
186 /* Android showed instability otherwise */
187 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
191 fghDisableVertexAttribArray(attribute_v_coord);
192 if (vbo_normals != 0)
193 fghDisableVertexAttribArray(attribute_v_normal);
196 fghDeleteBuffers(1, &vbo_coords);
197 if (vbo_normals != 0)
198 fghDeleteBuffers(1, &vbo_normals);
199 if (ibo_elements != 0)
200 fghDeleteBuffers(1, &ibo_elements);
201 if (ibo_elements2 != 0)
202 fghDeleteBuffers(1, &ibo_elements2);
205 static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numVertices,
206 GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode,
207 GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2
210 GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
211 GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
213 if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1))
214 /* User requested a 2.0 draw */
215 fghDrawGeometryWire20(vertices, normals, numVertices,
216 vertIdxs, numParts, numVertPerPart, vertexMode,
217 vertIdxs2, numParts2, numVertPerPart2,
218 attribute_v_coord, attribute_v_normal);
219 #ifndef GL_ES_VERSION_2_0
221 fghDrawGeometryWire11(vertices, normals,
222 vertIdxs, numParts, numVertPerPart, vertexMode,
223 vertIdxs2, numParts2, numVertPerPart2);
228 /* Draw the geometric shape with filled triangles
230 * - If the shape is naturally triangulated (numEdgePerFace==3), each
231 * vertex+normal pair is used only once, so no vertex indices.
233 * - If the shape was triangulated (DECOMPOSE_TO_TRIANGLE), some
234 * vertex+normal pairs are reused, so use vertex indices.
237 /* Version for OpenGL (ES) 1.1 */
238 #ifndef GL_ES_VERSION_2_0
239 static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
240 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart)
244 glEnableClientState(GL_VERTEX_ARRAY);
245 glEnableClientState(GL_NORMAL_ARRAY);
247 glVertexPointer(3, GL_FLOAT, 0, vertices);
248 glNormalPointer(GL_FLOAT, 0, normals);
249 if (vertIdxs == NULL)
250 glDrawArrays(GL_TRIANGLES, 0, numVertices);
253 for (i=0; i<numParts; i++)
254 glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_SHORT, vertIdxs+i*numVertIdxsPerPart);
256 glDrawElements(GL_TRIANGLES, numVertIdxsPerPart, GL_UNSIGNED_SHORT, vertIdxs);
258 glDisableClientState(GL_VERTEX_ARRAY);
259 glDisableClientState(GL_NORMAL_ARRAY);
263 /* Version for OpenGL (ES) >= 2.0 */
264 static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
265 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart,
266 GLint attribute_v_coord, GLint attribute_v_normal)
268 GLuint vbo_coords = 0, vbo_normals = 0, ibo_elements = 0;
269 GLsizei numVertIdxs = numParts * numVertIdxsPerPart;
272 if (numVertices > 0 && attribute_v_coord != -1) {
273 fghGenBuffers(1, &vbo_coords);
274 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
275 fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
276 vertices, FGH_STATIC_DRAW);
277 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
280 if (numVertices > 0 && attribute_v_normal != -1) {
281 fghGenBuffers(1, &vbo_normals);
282 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
283 fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
284 normals, FGH_STATIC_DRAW);
285 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
288 if (vertIdxs != NULL) {
289 fghGenBuffers(1, &ibo_elements);
290 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
291 fghBufferData(FGH_ELEMENT_ARRAY_BUFFER, numVertIdxs * sizeof(vertIdxs[0]),
292 vertIdxs, FGH_STATIC_DRAW);
293 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
297 fghEnableVertexAttribArray(attribute_v_coord);
298 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
299 fghVertexAttribPointer(
300 attribute_v_coord, /* attribute */
301 3, /* number of elements per vertex, here (x,y,z) */
302 GL_FLOAT, /* the type of each element */
303 GL_FALSE, /* take our values as-is */
304 0, /* no extra data between each position */
305 0 /* offset of first element */
307 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
311 fghEnableVertexAttribArray(attribute_v_normal);
312 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
313 fghVertexAttribPointer(
314 attribute_v_normal, /* attribute */
315 3, /* number of elements per vertex, here (x,y,z) */
316 GL_FLOAT, /* the type of each element */
317 GL_FALSE, /* take our values as-is */
318 0, /* no extra data between each position */
319 0 /* offset of first element */
321 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
324 if (vertIdxs == NULL) {
325 glDrawArrays(GL_TRIANGLES, 0, numVertices);
327 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
329 for (i=0; i<numParts; i++) {
330 glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_SHORT, (GLvoid*)(sizeof(vertIdxs[0])*i*numVertIdxsPerPart));
333 glDrawElements(GL_TRIANGLES, numVertIdxsPerPart, GL_UNSIGNED_SHORT, 0);
335 /* Clean existing bindings before clean-up */
336 /* Android showed instability otherwise */
337 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
341 fghDisableVertexAttribArray(attribute_v_coord);
342 if (vbo_normals != 0)
343 fghDisableVertexAttribArray(attribute_v_normal);
346 fghDeleteBuffers(1, &vbo_coords);
347 if (vbo_normals != 0)
348 fghDeleteBuffers(1, &vbo_normals);
349 if (ibo_elements != 0)
350 fghDeleteBuffers(1, &ibo_elements);
353 static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
354 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart)
356 GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
357 GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
359 if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1))
360 /* User requested a 2.0 draw */
361 fghDrawGeometrySolid20(vertices, normals, vertIdxs,
362 numVertices, numParts, numVertIdxsPerPart,
363 attribute_v_coord, attribute_v_normal);
364 #ifndef GL_ES_VERSION_2_0
366 fghDrawGeometrySolid11(vertices, normals, vertIdxs,
367 numVertices, numParts, numVertIdxsPerPart);
371 /* Shape decomposition to triangles
372 * We'll use glDrawElements to draw all shapes that are not naturally
373 * composed of triangles, so generate an index vector here, using the
374 * below sampling scheme.
375 * Be careful to keep winding of all triangles counter-clockwise,
376 * assuming that input has correct winding...
378 static GLubyte vert4Decomp[6] = {0,1,2, 0,2,3}; /* quad : 4 input vertices, 6 output (2 triangles) */
379 static GLubyte vert5Decomp[9] = {0,1,2, 0,2,4, 4,2,3}; /* pentagon: 5 input vertices, 9 output (3 triangles) */
381 static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace, GLfloat *vertices, GLubyte *vertIndices, GLfloat *normals, GLfloat *vertOut, GLfloat *normOut, GLushort *vertIdxOut)
383 int i,j,numEdgeIdxPerFace;
384 GLubyte *vertSamps = NULL;
385 switch (numEdgePerFace)
388 /* nothing to do here, we'll draw with glDrawArrays */
391 vertSamps = vert4Decomp;
392 numEdgeIdxPerFace = 6; /* 6 output vertices for each face */
395 vertSamps = vert5Decomp;
396 numEdgeIdxPerFace = 9; /* 9 output vertices for each face */
400 * Build array with vertices using vertex coordinates and vertex indices
401 * Do same for normals.
402 * Need to do this because of different normals at shared vertices.
404 for (i=0; i<numFaces; i++)
407 int faceIdxVertIdx = i*numEdgePerFace; /* index to first element of "row" in vertex indices */
408 for (j=0; j<numEdgePerFace; j++)
410 int outIdx = i*numEdgePerFace*3+j*3;
411 int vertIdx = vertIndices[faceIdxVertIdx+j]*3;
413 vertOut[outIdx ] = vertices[vertIdx ];
414 vertOut[outIdx+1] = vertices[vertIdx+1];
415 vertOut[outIdx+2] = vertices[vertIdx+2];
417 normOut[outIdx ] = normals [normIdx ];
418 normOut[outIdx+1] = normals [normIdx+1];
419 normOut[outIdx+2] = normals [normIdx+2];
422 /* generate vertex indices for each face */
424 for (j=0; j<numEdgeIdxPerFace; j++)
425 vertIdxOut[i*numEdgeIdxPerFace+j] = faceIdxVertIdx + vertSamps[j];
429 static void fghGenerateGeometry(int numFaces, int numEdgePerFace, GLfloat *vertices, GLubyte *vertIndices, GLfloat *normals, GLfloat *vertOut, GLfloat *normOut)
431 /* This function does the same as fghGenerateGeometryWithIndexArray, just skipping the index array generation... */
432 fghGenerateGeometryWithIndexArray(numFaces, numEdgePerFace, vertices, vertIndices, normals, vertOut, normOut, NULL);
436 /* -- INTERNAL SETUP OF GEOMETRY --------------------------------------- */
437 /* -- stuff that can be cached -- */
438 /* Cache of input to glDrawArrays or glDrawElements
439 * In general, we build arrays with all vertices or normals.
440 * We cant compress this and use glDrawElements as all combinations of
441 * vertices and normals are unique.
443 #define DECLARE_SHAPE_CACHE(name,nameICaps,nameCaps)\
444 static GLboolean name##Cached = FALSE;\
445 static GLfloat name##_verts[nameCaps##_VERT_ELEM_PER_OBJ];\
446 static GLfloat name##_norms[nameCaps##_VERT_ELEM_PER_OBJ];\
447 static void fgh##nameICaps##Generate()\
449 fghGenerateGeometry(nameCaps##_NUM_FACES, nameCaps##_NUM_EDGE_PER_FACE,\
450 name##_v, name##_vi, name##_n,\
451 name##_verts, name##_norms);\
453 #define DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(name,nameICaps,nameCaps)\
454 static GLboolean name##Cached = FALSE;\
455 static GLfloat name##_verts[nameCaps##_VERT_ELEM_PER_OBJ];\
456 static GLfloat name##_norms[nameCaps##_VERT_ELEM_PER_OBJ];\
457 static GLushort name##_vertIdxs[nameCaps##_VERT_PER_OBJ_TRI];\
458 static void fgh##nameICaps##Generate()\
460 fghGenerateGeometryWithIndexArray(nameCaps##_NUM_FACES, nameCaps##_NUM_EDGE_PER_FACE,\
461 name##_v, name##_vi, name##_n,\
462 name##_verts, name##_norms, name##_vertIdxs);\
466 #define CUBE_NUM_VERT 8
467 #define CUBE_NUM_FACES 6
468 #define CUBE_NUM_EDGE_PER_FACE 4
469 #define CUBE_VERT_PER_OBJ (CUBE_NUM_FACES*CUBE_NUM_EDGE_PER_FACE)
470 #define CUBE_VERT_ELEM_PER_OBJ (CUBE_VERT_PER_OBJ*3)
471 #define CUBE_VERT_PER_OBJ_TRI (CUBE_VERT_PER_OBJ+CUBE_NUM_FACES*2) /* 2 extra edges per face when drawing quads as triangles */
472 /* Vertex Coordinates */
473 static GLfloat cube_v[CUBE_NUM_VERT*3] =
485 static GLfloat cube_n[CUBE_NUM_FACES*3] =
495 /* Vertex indices, as quads, before triangulation */
496 static GLubyte cube_vi[CUBE_VERT_PER_OBJ] =
505 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(cube,Cube,CUBE)
507 /* -- Dodecahedron -- */
508 /* Magic Numbers: It is possible to create a dodecahedron by attaching two
509 * pentagons to each face of of a cube. The coordinates of the points are:
510 * (+-x,0, z); (+-1, 1, 1); (0, z, x )
511 * where x = (-1 + sqrt(5))/2, z = (1 + sqrt(5))/2 or
512 * x = 0.61803398875 and z = 1.61803398875.
514 #define DODECAHEDRON_NUM_VERT 20
515 #define DODECAHEDRON_NUM_FACES 12
516 #define DODECAHEDRON_NUM_EDGE_PER_FACE 5
517 #define DODECAHEDRON_VERT_PER_OBJ (DODECAHEDRON_NUM_FACES*DODECAHEDRON_NUM_EDGE_PER_FACE)
518 #define DODECAHEDRON_VERT_ELEM_PER_OBJ (DODECAHEDRON_VERT_PER_OBJ*3)
519 #define DODECAHEDRON_VERT_PER_OBJ_TRI (DODECAHEDRON_VERT_PER_OBJ+DODECAHEDRON_NUM_FACES*4) /* 4 extra edges per face when drawing pentagons as triangles */
520 /* Vertex Coordinates */
521 static GLfloat dodecahedron_v[DODECAHEDRON_NUM_VERT*3] =
523 0.0f, 1.61803398875f, 0.61803398875f,
525 -0.61803398875f, 0.0f, 1.61803398875f,
526 0.61803398875f, 0.0f, 1.61803398875f,
528 0.0f, 1.61803398875f, -0.61803398875f,
530 0.61803398875f, 0.0f, -1.61803398875f,
531 -0.61803398875f, 0.0f, -1.61803398875f,
532 - 1.0f, 1.0f, - 1.0f,
533 0.0f, -1.61803398875f, 0.61803398875f,
535 - 1.0f, - 1.0f, 1.0f,
536 0.0f, -1.61803398875f, -0.61803398875f,
537 - 1.0f, - 1.0f, - 1.0f,
538 1.0f, - 1.0f, - 1.0f,
539 1.61803398875f, -0.61803398875f, 0.0f,
540 1.61803398875f, 0.61803398875f, 0.0f,
541 -1.61803398875f, 0.61803398875f, 0.0f,
542 -1.61803398875f, -0.61803398875f, 0.0f
545 static GLfloat dodecahedron_n[DODECAHEDRON_NUM_FACES*3] =
547 0.0f, 0.525731112119f, 0.850650808354f,
548 0.0f, 0.525731112119f, -0.850650808354f,
549 0.0f, -0.525731112119f, 0.850650808354f,
550 0.0f, -0.525731112119f, -0.850650808354f,
552 0.850650808354f, 0.0f, 0.525731112119f,
553 -0.850650808354f, 0.0f, 0.525731112119f,
554 0.850650808354f, 0.0f, -0.525731112119f,
555 -0.850650808354f, 0.0f, -0.525731112119f,
557 0.525731112119f, 0.850650808354f, 0.0f,
558 0.525731112119f, -0.850650808354f, 0.0f,
559 -0.525731112119f, 0.850650808354f, 0.0f,
560 -0.525731112119f, -0.850650808354f, 0.0f,
564 static GLubyte dodecahedron_vi[DODECAHEDRON_VERT_PER_OBJ] =
581 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON)
584 /* -- Icosahedron -- */
585 #define ICOSAHEDRON_NUM_VERT 12
586 #define ICOSAHEDRON_NUM_FACES 20
587 #define ICOSAHEDRON_NUM_EDGE_PER_FACE 3
588 #define ICOSAHEDRON_VERT_PER_OBJ (ICOSAHEDRON_NUM_FACES*ICOSAHEDRON_NUM_EDGE_PER_FACE)
589 #define ICOSAHEDRON_VERT_ELEM_PER_OBJ (ICOSAHEDRON_VERT_PER_OBJ*3)
590 #define ICOSAHEDRON_VERT_PER_OBJ_TRI ICOSAHEDRON_VERT_PER_OBJ
591 /* Vertex Coordinates */
592 static GLfloat icosahedron_v[ICOSAHEDRON_NUM_VERT*3] =
595 0.447213595500f, 0.894427191000f, 0.0f,
596 0.447213595500f, 0.276393202252f, 0.850650808354f,
597 0.447213595500f, -0.723606797748f, 0.525731112119f,
598 0.447213595500f, -0.723606797748f, -0.525731112119f,
599 0.447213595500f, 0.276393202252f, -0.850650808354f,
600 -0.447213595500f, -0.894427191000f, 0.0f,
601 -0.447213595500f, -0.276393202252f, 0.850650808354f,
602 -0.447213595500f, 0.723606797748f, 0.525731112119f,
603 -0.447213595500f, 0.723606797748f, -0.525731112119f,
604 -0.447213595500f, -0.276393202252f, -0.850650808354f,
608 * 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] ) ;
609 * 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] ) ;
610 * 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] ) ;
612 static GLfloat icosahedron_n[ICOSAHEDRON_NUM_FACES*3] =
614 0.760845213037948f, 0.470228201835026f, 0.341640786498800f,
615 0.760845213036861f, -0.179611190632978f, 0.552786404500000f,
616 0.760845213033849f, -0.581234022404097f, 0.0f,
617 0.760845213036861f, -0.179611190632978f, -0.552786404500000f,
618 0.760845213037948f, 0.470228201835026f, -0.341640786498800f,
619 0.179611190628666f, 0.760845213037948f, 0.552786404498399f,
620 0.179611190634277f, -0.290617011204044f, 0.894427191000000f,
621 0.179611190633958f, -0.940456403667806f, 0.0f,
622 0.179611190634278f, -0.290617011204044f, -0.894427191000000f,
623 0.179611190628666f, 0.760845213037948f, -0.552786404498399f,
624 -0.179611190633958f, 0.940456403667806f, 0.0f,
625 -0.179611190634277f, 0.290617011204044f, 0.894427191000000f,
626 -0.179611190628666f, -0.760845213037948f, 0.552786404498399f,
627 -0.179611190628666f, -0.760845213037948f, -0.552786404498399f,
628 -0.179611190634277f, 0.290617011204044f, -0.894427191000000f,
629 -0.760845213036861f, 0.179611190632978f, -0.552786404500000f,
630 -0.760845213033849f, 0.581234022404097f, 0.0f,
631 -0.760845213036861f, 0.179611190632978f, 0.552786404500000f,
632 -0.760845213037948f, -0.470228201835026f, 0.341640786498800f,
633 -0.760845213037948f, -0.470228201835026f, -0.341640786498800f,
637 static GLubyte icosahedron_vi[ICOSAHEDRON_VERT_PER_OBJ] =
660 DECLARE_SHAPE_CACHE(icosahedron,Icosahedron,ICOSAHEDRON)
662 /* -- Octahedron -- */
663 #define OCTAHEDRON_NUM_VERT 6
664 #define OCTAHEDRON_NUM_FACES 8
665 #define OCTAHEDRON_NUM_EDGE_PER_FACE 3
666 #define OCTAHEDRON_VERT_PER_OBJ (OCTAHEDRON_NUM_FACES*OCTAHEDRON_NUM_EDGE_PER_FACE)
667 #define OCTAHEDRON_VERT_ELEM_PER_OBJ (OCTAHEDRON_VERT_PER_OBJ*3)
668 #define OCTAHEDRON_VERT_PER_OBJ_TRI OCTAHEDRON_VERT_PER_OBJ
670 /* Vertex Coordinates */
671 static GLfloat octahedron_v[OCTAHEDRON_NUM_VERT*3] =
682 static GLfloat octahedron_n[OCTAHEDRON_NUM_FACES*3] =
684 0.577350269189f, 0.577350269189f, 0.577350269189f, /* sqrt(1/3) */
685 0.577350269189f, 0.577350269189f,-0.577350269189f,
686 0.577350269189f,-0.577350269189f, 0.577350269189f,
687 0.577350269189f,-0.577350269189f,-0.577350269189f,
688 -0.577350269189f, 0.577350269189f, 0.577350269189f,
689 -0.577350269189f, 0.577350269189f,-0.577350269189f,
690 -0.577350269189f,-0.577350269189f, 0.577350269189f,
691 -0.577350269189f,-0.577350269189f,-0.577350269189f
696 static GLubyte octahedron_vi[OCTAHEDRON_VERT_PER_OBJ] =
707 DECLARE_SHAPE_CACHE(octahedron,Octahedron,OCTAHEDRON)
709 /* -- RhombicDodecahedron -- */
710 #define RHOMBICDODECAHEDRON_NUM_VERT 14
711 #define RHOMBICDODECAHEDRON_NUM_FACES 12
712 #define RHOMBICDODECAHEDRON_NUM_EDGE_PER_FACE 4
713 #define RHOMBICDODECAHEDRON_VERT_PER_OBJ (RHOMBICDODECAHEDRON_NUM_FACES*RHOMBICDODECAHEDRON_NUM_EDGE_PER_FACE)
714 #define RHOMBICDODECAHEDRON_VERT_ELEM_PER_OBJ (RHOMBICDODECAHEDRON_VERT_PER_OBJ*3)
715 #define RHOMBICDODECAHEDRON_VERT_PER_OBJ_TRI (RHOMBICDODECAHEDRON_VERT_PER_OBJ+RHOMBICDODECAHEDRON_NUM_FACES*2) /* 2 extra edges per face when drawing quads as triangles */
717 /* Vertex Coordinates */
718 static GLfloat rhombicdodecahedron_v[RHOMBICDODECAHEDRON_NUM_VERT*3] =
721 0.707106781187f, 0.0f, 0.5f,
722 0.0f, 0.707106781187f, 0.5f,
723 -0.707106781187f, 0.0f, 0.5f,
724 0.0f, -0.707106781187f, 0.5f,
725 0.707106781187f, 0.707106781187f, 0.0f,
726 -0.707106781187f, 0.707106781187f, 0.0f,
727 -0.707106781187f, -0.707106781187f, 0.0f,
728 0.707106781187f, -0.707106781187f, 0.0f,
729 0.707106781187f, 0.0f, -0.5f,
730 0.0f, 0.707106781187f, -0.5f,
731 -0.707106781187f, 0.0f, -0.5f,
732 0.0f, -0.707106781187f, -0.5f,
736 static GLfloat rhombicdodecahedron_n[RHOMBICDODECAHEDRON_NUM_FACES*3] =
738 0.353553390594f, 0.353553390594f, 0.5f,
739 -0.353553390594f, 0.353553390594f, 0.5f,
740 -0.353553390594f, -0.353553390594f, 0.5f,
741 0.353553390594f, -0.353553390594f, 0.5f,
746 0.353553390594f, 0.353553390594f, -0.5f,
747 -0.353553390594f, 0.353553390594f, -0.5f,
748 -0.353553390594f, -0.353553390594f, -0.5f,
749 0.353553390594f, -0.353553390594f, -0.5f
753 static GLubyte rhombicdodecahedron_vi[RHOMBICDODECAHEDRON_VERT_PER_OBJ] =
768 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON)
770 /* -- Tetrahedron -- */
771 /* Magic Numbers: r0 = ( 1, 0, 0 )
772 * r1 = ( -1/3, 2 sqrt(2) / 3, 0 )
773 * r2 = ( -1/3, - sqrt(2) / 3, sqrt(6) / 3 )
774 * r3 = ( -1/3, - sqrt(2) / 3, -sqrt(6) / 3 )
775 * |r0| = |r1| = |r2| = |r3| = 1
776 * Distance between any two points is 2 sqrt(6) / 3
778 * Normals: The unit normals are simply the negative of the coordinates of the point not on the surface.
780 #define TETRAHEDRON_NUM_VERT 4
781 #define TETRAHEDRON_NUM_FACES 4
782 #define TETRAHEDRON_NUM_EDGE_PER_FACE 3
783 #define TETRAHEDRON_VERT_PER_OBJ (TETRAHEDRON_NUM_FACES*TETRAHEDRON_NUM_EDGE_PER_FACE)
784 #define TETRAHEDRON_VERT_ELEM_PER_OBJ (TETRAHEDRON_VERT_PER_OBJ*3)
785 #define TETRAHEDRON_VERT_PER_OBJ_TRI TETRAHEDRON_VERT_PER_OBJ
787 /* Vertex Coordinates */
788 static GLfloat tetrahedron_v[TETRAHEDRON_NUM_VERT*3] =
791 -0.333333333333f, 0.942809041582f, 0.0f,
792 -0.333333333333f, -0.471404520791f, 0.816496580928f,
793 -0.333333333333f, -0.471404520791f, -0.816496580928f
796 static GLfloat tetrahedron_n[TETRAHEDRON_NUM_FACES*3] =
799 0.333333333333f, -0.942809041582f, 0.0f,
800 0.333333333333f, 0.471404520791f, -0.816496580928f,
801 0.333333333333f, 0.471404520791f, 0.816496580928f
805 static GLubyte tetrahedron_vi[TETRAHEDRON_VERT_PER_OBJ] =
812 DECLARE_SHAPE_CACHE(tetrahedron,Tetrahedron,TETRAHEDRON)
814 /* -- Sierpinski Sponge -- */
815 static unsigned int ipow (int x, unsigned int y)
817 return y==0? 1: y==1? x: (y%2? x: 1) * ipow(x*x, y/2);
820 static void fghSierpinskiSpongeGenerate ( int numLevels, double offset[3], GLfloat scale, GLfloat* vertices, GLfloat* normals )
823 if ( numLevels == 0 )
825 for (i=0; i<TETRAHEDRON_NUM_FACES; i++)
828 int faceIdxVertIdx = i*TETRAHEDRON_NUM_EDGE_PER_FACE;
829 for (j=0; j<TETRAHEDRON_NUM_EDGE_PER_FACE; j++)
831 int outIdx = i*TETRAHEDRON_NUM_EDGE_PER_FACE*3+j*3;
832 int vertIdx = tetrahedron_vi[faceIdxVertIdx+j]*3;
834 vertices[outIdx ] = (GLfloat)offset[0] + scale * tetrahedron_v[vertIdx ];
835 vertices[outIdx+1] = (GLfloat)offset[1] + scale * tetrahedron_v[vertIdx+1];
836 vertices[outIdx+2] = (GLfloat)offset[2] + scale * tetrahedron_v[vertIdx+2];
838 normals [outIdx ] = tetrahedron_n[normIdx ];
839 normals [outIdx+1] = tetrahedron_n[normIdx+1];
840 normals [outIdx+2] = tetrahedron_n[normIdx+2];
844 else if ( numLevels > 0 )
846 double local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */
847 unsigned int stride = ipow(4,--numLevels)*TETRAHEDRON_VERT_ELEM_PER_OBJ;
849 for ( i = 0 ; i < TETRAHEDRON_NUM_FACES ; i++ )
852 local_offset[0] = offset[0] + scale * tetrahedron_v[idx ];
853 local_offset[1] = offset[1] + scale * tetrahedron_v[idx+1];
854 local_offset[2] = offset[2] + scale * tetrahedron_v[idx+2];
855 fghSierpinskiSpongeGenerate ( numLevels, local_offset, scale, vertices+i*stride, normals+i*stride );
860 /* -- Now the various shapes involving circles -- */
862 * Compute lookup table of cos and sin values forming a circle
863 * (or half circle if halfCircle==TRUE)
866 * It is the responsibility of the caller to free these tables
867 * The size of the table is (n+1) to form a connected loop
868 * The last entry is exactly the same as the first
869 * The sign of n can be flipped to get the reverse loop
871 static void fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GLboolean halfCircle)
875 /* Table size, the sign of n flips the circle direction */
876 const int size = abs(n);
878 /* Determine the angle between samples */
879 const GLfloat angle = (halfCircle?1:2)*(GLfloat)M_PI/(GLfloat)( ( n == 0 ) ? 1 : n );
881 /* Allocate memory for n samples, plus duplicate of first entry at the end */
882 *sint = malloc(sizeof(GLfloat) * (size+1));
883 *cost = malloc(sizeof(GLfloat) * (size+1));
885 /* Bail out if memory allocation fails, fgError never returns */
886 if (!(*sint) || !(*cost))
890 fgError("Failed to allocate memory in fghCircleTable");
893 /* Compute cos and sin around the circle */
897 for (i=1; i<size; i++)
899 (*sint)[i] = sinf(angle*i);
900 (*cost)[i] = cosf(angle*i);
906 (*sint)[size] = 0.0f; /* sin PI */
907 (*cost)[size] = -1.0f; /* cos PI */
911 /* Last sample is duplicate of the first (sin or cos of 2 PI) */
912 (*sint)[size] = (*sint)[0];
913 (*cost)[size] = (*cost)[0];
917 static void fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat **vertices, GLfloat **normals, int* nVert)
920 int idx = 0; /* idx into vertex/normal buffer */
923 /* Pre-computed circle */
924 GLfloat *sint1,*cost1;
925 GLfloat *sint2,*cost2;
927 /* number of unique vertices */
928 if (slices==0 || stacks<2)
930 /* nothing to generate */
934 *nVert = slices*(stacks-1)+2;
935 if ((*nVert) > 65535) /* TODO: must have a better solution than this low limit, at least for architectures where gluint is available */
936 fgWarning("fghGenerateSphere: too many slices or stacks requested, indices will wrap");
938 /* precompute values on unit circle */
939 fghCircleTable(&sint1,&cost1,-slices,FALSE);
940 fghCircleTable(&sint2,&cost2, stacks,TRUE);
942 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
943 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
944 *normals = malloc((*nVert)*3*sizeof(GLfloat));
945 if (!(*vertices) || !(*normals))
949 fgError("Failed to allocate memory in fghGenerateSphere");
953 (*vertices)[0] = 0.f;
954 (*vertices)[1] = 0.f;
955 (*vertices)[2] = radius;
956 (*normals )[0] = 0.f;
957 (*normals )[1] = 0.f;
958 (*normals )[2] = 1.f;
962 for( i=1; i<stacks; i++ )
964 for(j=0; j<slices; j++, idx+=3)
966 x = cost1[j]*sint2[i];
967 y = sint1[j]*sint2[i];
970 (*vertices)[idx ] = x*radius;
971 (*vertices)[idx+1] = y*radius;
972 (*vertices)[idx+2] = z*radius;
973 (*normals )[idx ] = x;
974 (*normals )[idx+1] = y;
975 (*normals )[idx+2] = z;
980 (*vertices)[idx ] = 0.f;
981 (*vertices)[idx+1] = 0.f;
982 (*vertices)[idx+2] = -radius;
983 (*normals )[idx ] = 0.f;
984 (*normals )[idx+1] = 0.f;
985 (*normals )[idx+2] = -1.f;
987 /* Done creating vertices, release sin and cos tables */
994 void fghGenerateCone(
995 GLfloat base, GLfloat height, GLint slices, GLint stacks, /* input */
996 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
1000 int idx = 0; /* idx into vertex/normal buffer */
1002 /* Pre-computed circle */
1003 GLfloat *sint,*cost;
1005 /* Step in z and radius as stacks are drawn. */
1007 GLfloat r = (GLfloat)base;
1009 const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
1010 const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 );
1012 /* Scaling factors for vertex normals */
1013 const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base ));
1014 const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base ));
1018 /* number of unique vertices */
1019 if (slices==0 || stacks<1)
1021 /* nothing to generate */
1025 *nVert = slices*(stacks+2)+1; /* need an extra stack for closing off bottom with correct normals */
1027 if ((*nVert) > 65535)
1028 fgWarning("fghGenerateCone: too many slices or stacks requested, indices will wrap");
1030 /* Pre-computed circle */
1031 fghCircleTable(&sint,&cost,-slices,FALSE);
1033 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1034 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1035 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1036 if (!(*vertices) || !(*normals))
1040 fgError("Failed to allocate memory in fghGenerateSphere");
1044 (*vertices)[0] = 0.f;
1045 (*vertices)[1] = 0.f;
1047 (*normals )[0] = 0.f;
1048 (*normals )[1] = 0.f;
1049 (*normals )[2] = -1.f;
1051 /* other on bottom (get normals right) */
1052 for (j=0; j<slices; j++, idx+=3)
1054 (*vertices)[idx ] = cost[j]*r;
1055 (*vertices)[idx+1] = sint[j]*r;
1056 (*vertices)[idx+2] = z;
1057 (*normals )[idx ] = 0.f;
1058 (*normals )[idx+1] = 0.f;
1059 (*normals )[idx+2] = -1.f;
1063 for (i=0; i<stacks+1; i++ )
1065 for (j=0; j<slices; j++, idx+=3)
1067 (*vertices)[idx ] = cost[j]*r;
1068 (*vertices)[idx+1] = sint[j]*r;
1069 (*vertices)[idx+2] = z;
1070 (*normals )[idx ] = cost[j]*sinn;
1071 (*normals )[idx+1] = sint[j]*sinn;
1072 (*normals )[idx+2] = cosn;
1079 /* Release sin and cos tables */
1084 void fghGenerateCylinder(
1085 GLfloat radius, GLfloat height, GLint slices, GLint stacks, /* input */
1086 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
1090 int idx = 0; /* idx into vertex/normal buffer */
1092 /* Step in z as stacks are drawn. */
1093 GLfloat radf = (GLfloat)radius;
1095 const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
1097 /* Pre-computed circle */
1098 GLfloat *sint,*cost;
1100 /* number of unique vertices */
1101 if (slices==0 || stacks<1)
1103 /* nothing to generate */
1107 *nVert = slices*(stacks+3)+2; /* need two extra stacks for closing off top and bottom with correct normals */
1109 if ((*nVert) > 65535)
1110 fgWarning("fghGenerateCylinder: too many slices or stacks requested, indices will wrap");
1112 /* Pre-computed circle */
1113 fghCircleTable(&sint,&cost,-slices,FALSE);
1115 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1116 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1117 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1118 if (!(*vertices) || !(*normals))
1122 fgError("Failed to allocate memory in fghGenerateCylinder");
1127 (*vertices)[0] = 0.f;
1128 (*vertices)[1] = 0.f;
1129 (*vertices)[2] = 0.f;
1130 (*normals )[0] = 0.f;
1131 (*normals )[1] = 0.f;
1132 (*normals )[2] = -1.f;
1134 /* other on top (get normals right) */
1135 for (j=0; j<slices; j++, idx+=3)
1137 (*vertices)[idx ] = cost[j]*radf;
1138 (*vertices)[idx+1] = sint[j]*radf;
1139 (*vertices)[idx+2] = z;
1140 (*normals )[idx ] = 0.f;
1141 (*normals )[idx+1] = 0.f;
1142 (*normals )[idx+2] = -1.f;
1146 for (i=0; i<stacks+1; i++ )
1148 for (j=0; j<slices; j++, idx+=3)
1150 (*vertices)[idx ] = cost[j]*radf;
1151 (*vertices)[idx+1] = sint[j]*radf;
1152 (*vertices)[idx+2] = z;
1153 (*normals )[idx ] = cost[j];
1154 (*normals )[idx+1] = sint[j];
1155 (*normals )[idx+2] = 0.f;
1161 /* other on bottom (get normals right) */
1163 for (j=0; j<slices; j++, idx+=3)
1165 (*vertices)[idx ] = cost[j]*radf;
1166 (*vertices)[idx+1] = sint[j]*radf;
1167 (*vertices)[idx+2] = z;
1168 (*normals )[idx ] = 0.f;
1169 (*normals )[idx+1] = 0.f;
1170 (*normals )[idx+2] = 1.f;
1174 (*vertices)[idx ] = 0.f;
1175 (*vertices)[idx+1] = 0.f;
1176 (*vertices)[idx+2] = height;
1177 (*normals )[idx ] = 0.f;
1178 (*normals )[idx+1] = 0.f;
1179 (*normals )[idx+2] = 1.f;
1181 /* Release sin and cos tables */
1186 void fghGenerateTorus(
1187 double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, /* input */
1188 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
1191 GLfloat iradius = (float)dInnerRadius;
1192 GLfloat oradius = (float)dOuterRadius;
1195 /* Pre-computed circle */
1196 GLfloat *spsi, *cpsi;
1197 GLfloat *sphi, *cphi;
1199 /* number of unique vertices */
1200 if (nSides<2 || nRings<2)
1202 /* nothing to generate */
1206 *nVert = nSides * nRings;
1208 if ((*nVert) > 65535)
1209 fgWarning("fghGenerateTorus: too many slices or stacks requested, indices will wrap");
1211 /* precompute values on unit circle */
1212 fghCircleTable(&spsi,&cpsi, nRings,FALSE);
1213 fghCircleTable(&sphi,&cphi,-nSides,FALSE);
1215 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1216 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1217 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1218 if (!(*vertices) || !(*normals))
1222 fgError("Failed to allocate memory in fghGenerateTorus");
1225 for( j=0; j<nRings; j++ )
1227 for( i=0; i<nSides; i++ )
1229 int offset = 3 * ( j * nSides + i ) ;
1231 (*vertices)[offset ] = cpsi[j] * ( oradius + cphi[i] * iradius ) ;
1232 (*vertices)[offset+1] = spsi[j] * ( oradius + cphi[i] * iradius ) ;
1233 (*vertices)[offset+2] = sphi[i] * iradius ;
1234 (*normals )[offset ] = cpsi[j] * cphi[i] ;
1235 (*normals )[offset+1] = spsi[j] * cphi[i] ;
1236 (*normals )[offset+2] = sphi[i] ;
1240 /* Release sin and cos tables */
1247 /* -- INTERNAL DRAWING functions --------------------------------------- */
1248 #define _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,vertIdxs)\
1249 static void fgh##nameICaps( GLboolean useWireMode )\
1253 fgh##nameICaps##Generate();\
1254 name##Cached = GL_TRUE;\
1259 fghDrawGeometryWire (name##_verts,name##_norms,nameCaps##_VERT_PER_OBJ, \
1260 NULL,nameCaps##_NUM_FACES,nameCaps##_NUM_EDGE_PER_FACE,GL_LINE_LOOP,\
1265 fghDrawGeometrySolid(name##_verts,name##_norms,vertIdxs,\
1266 nameCaps##_VERT_PER_OBJ, 1, nameCaps##_VERT_PER_OBJ_TRI); \
1269 #define DECLARE_INTERNAL_DRAW(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,NULL)
1270 #define DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,name##_vertIdxs)
1272 static void fghCube( GLfloat dSize, GLboolean useWireMode )
1279 cubeCached = GL_TRUE;
1284 /* Need to build new vertex list containing vertices for cube of different size */
1287 vertices = malloc(CUBE_VERT_ELEM_PER_OBJ * sizeof(GLfloat));
1289 /* Bail out if memory allocation fails, fgError never returns */
1293 fgError("Failed to allocate memory in fghCube");
1296 for (i=0; i<CUBE_VERT_ELEM_PER_OBJ; i++)
1297 vertices[i] = dSize*cube_verts[i];
1300 vertices = cube_verts;
1303 fghDrawGeometryWire(vertices, cube_norms, CUBE_VERT_PER_OBJ,
1304 NULL,CUBE_NUM_FACES, CUBE_NUM_EDGE_PER_FACE,GL_LINE_LOOP,
1307 fghDrawGeometrySolid(vertices, cube_norms, cube_vertIdxs,
1308 CUBE_VERT_PER_OBJ, 1, CUBE_VERT_PER_OBJ_TRI);
1311 /* cleanup allocated memory */
1315 DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON)
1316 DECLARE_INTERNAL_DRAW(icosahedron,Icosahedron,ICOSAHEDRON)
1317 DECLARE_INTERNAL_DRAW(octahedron,Octahedron,OCTAHEDRON)
1318 DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON)
1319 DECLARE_INTERNAL_DRAW(tetrahedron,Tetrahedron,TETRAHEDRON)
1321 static void fghSierpinskiSponge ( int numLevels, double offset[3], GLfloat scale, GLboolean useWireMode )
1325 GLsizei numTetr = numLevels<0? 0 : ipow(4,numLevels); /* No sponge for numLevels below 0 */
1326 GLsizei numVert = numTetr*TETRAHEDRON_VERT_PER_OBJ;
1327 GLsizei numFace = numTetr*TETRAHEDRON_NUM_FACES;
1331 /* Allocate memory */
1332 vertices = malloc(numVert*3 * sizeof(GLfloat));
1333 normals = malloc(numVert*3 * sizeof(GLfloat));
1334 /* Bail out if memory allocation fails, fgError never returns */
1335 if (!vertices || !normals)
1339 fgError("Failed to allocate memory in fghSierpinskiSponge");
1342 /* Generate elements */
1343 fghSierpinskiSpongeGenerate ( numLevels, offset, scale, vertices, normals );
1345 /* Draw and cleanup */
1347 fghDrawGeometryWire (vertices,normals,numVert,
1348 NULL,numFace,TETRAHEDRON_NUM_EDGE_PER_FACE,GL_LINE_LOOP,
1351 fghDrawGeometrySolid(vertices,normals,NULL,numVert,1,0);
1359 static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useWireMode )
1362 GLfloat *vertices, *normals;
1364 /* Generate vertices and normals */
1365 fghGenerateSphere((GLfloat)radius,slices,stacks,&vertices,&normals,&nVert);
1368 /* nothing to draw */
1373 GLushort *sliceIdx, *stackIdx;
1374 /* First, generate vertex index arrays for drawing with glDrawElements
1375 * We have a bunch of line_loops to draw for each stack, and a
1376 * bunch for each slice.
1379 sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
1380 stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort));
1381 if (!(stackIdx) || !(sliceIdx))
1385 fgError("Failed to allocate memory in fghSphere");
1388 /* generate for each stack */
1389 for (i=0,idx=0; i<stacks-1; i++)
1391 GLushort offset = 1+i*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1392 for (j=0; j<slices; j++, idx++)
1394 stackIdx[idx] = offset+j;
1398 /* generate for each slice */
1399 for (i=0,idx=0; i<slices; i++)
1401 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1402 sliceIdx[idx++] = 0; /* vertex on top */
1403 for (j=0; j<stacks-1; j++, idx++)
1405 sliceIdx[idx] = offset+j*slices;
1407 sliceIdx[idx++] = nVert-1; /* zero based index, last element in array... */
1411 fghDrawGeometryWire(vertices,normals,nVert,
1412 sliceIdx,slices,stacks+1,GL_LINE_STRIP,
1413 stackIdx,stacks-1,slices);
1415 /* cleanup allocated memory */
1421 /* First, generate vertex index arrays for drawing with glDrawElements
1422 * All stacks, including top and bottom are covered with a triangle
1426 /* Create index vector */
1429 /* Allocate buffers for indices, bail out if memory allocation fails */
1430 stripIdx = malloc((slices+1)*2*(stacks)*sizeof(GLushort));
1434 fgError("Failed to allocate memory in fghSphere");
1438 for (j=0, idx=0; j<slices; j++, idx+=2)
1440 stripIdx[idx ] = j+1; /* 0 is top vertex, 1 is first for first stack */
1441 stripIdx[idx+1] = 0;
1443 stripIdx[idx ] = 1; /* repeat first slice's idx for closing off shape */
1444 stripIdx[idx+1] = 0;
1447 /* middle stacks: */
1448 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1449 for (i=0; i<stacks-2; i++, idx+=2)
1451 offset = 1+i*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
1452 for (j=0; j<slices; j++, idx+=2)
1454 stripIdx[idx ] = offset+j+slices;
1455 stripIdx[idx+1] = offset+j;
1457 stripIdx[idx ] = offset+slices; /* repeat first slice's idx for closing off shape */
1458 stripIdx[idx+1] = offset;
1462 offset = 1+(stacks-2)*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
1463 for (j=0; j<slices; j++, idx+=2)
1465 stripIdx[idx ] = nVert-1; /* zero based index, last element in array (bottom vertex)... */
1466 stripIdx[idx+1] = offset+j;
1468 stripIdx[idx ] = nVert-1; /* repeat first slice's idx for closing off shape */
1469 stripIdx[idx+1] = offset;
1473 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks,(slices+1)*2);
1475 /* cleanup allocated memory */
1479 /* cleanup allocated memory */
1484 static void fghCone( double base, double height, GLint slices, GLint stacks, GLboolean useWireMode )
1487 GLfloat *vertices, *normals;
1489 /* Generate vertices and normals */
1490 /* Note, (stacks+1)*slices vertices for side of object, slices+1 for top and bottom closures */
1491 fghGenerateCone((GLfloat)base,(GLfloat)height,slices,stacks,&vertices,&normals,&nVert);
1494 /* nothing to draw */
1499 GLushort *sliceIdx, *stackIdx;
1500 /* First, generate vertex index arrays for drawing with glDrawElements
1501 * We have a bunch of line_loops to draw for each stack, and a
1502 * bunch for each slice.
1505 stackIdx = malloc(slices*stacks*sizeof(GLushort));
1506 sliceIdx = malloc(slices*2 *sizeof(GLushort));
1507 if (!(stackIdx) || !(sliceIdx))
1511 fgError("Failed to allocate memory in fghCone");
1514 /* generate for each stack */
1515 for (i=0,idx=0; i<stacks; i++)
1517 GLushort offset = 1+(i+1)*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1518 for (j=0; j<slices; j++, idx++)
1520 stackIdx[idx] = offset+j;
1524 /* generate for each slice */
1525 for (i=0,idx=0; i<slices; i++)
1527 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1528 sliceIdx[idx++] = offset+slices;
1529 sliceIdx[idx++] = offset+(stacks+1)*slices;
1533 fghDrawGeometryWire(vertices,normals,nVert,
1534 sliceIdx,1,slices*2,GL_LINES,
1535 stackIdx,stacks,slices);
1537 /* cleanup allocated memory */
1543 /* First, generate vertex index arrays for drawing with glDrawElements
1544 * All stacks, including top and bottom are covered with a triangle
1548 /* Create index vector */
1551 /* Allocate buffers for indices, bail out if memory allocation fails */
1552 stripIdx = malloc((slices+1)*2*(stacks+1)*sizeof(GLushort)); /*stacks +1 because of closing off bottom */
1556 fgError("Failed to allocate memory in fghCone");
1560 for (j=0, idx=0; j<slices; j++, idx+=2)
1563 stripIdx[idx+1] = j+1; /* 0 is top vertex, 1 is first for first stack */
1565 stripIdx[idx ] = 0; /* repeat first slice's idx for closing off shape */
1566 stripIdx[idx+1] = 1;
1569 /* middle stacks: */
1570 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1571 for (i=0; i<stacks; i++, idx+=2)
1573 offset = 1+(i+1)*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
1574 for (j=0; j<slices; j++, idx+=2)
1576 stripIdx[idx ] = offset+j;
1577 stripIdx[idx+1] = offset+j+slices;
1579 stripIdx[idx ] = offset; /* repeat first slice's idx for closing off shape */
1580 stripIdx[idx+1] = offset+slices;
1584 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks+1,(slices+1)*2);
1586 /* cleanup allocated memory */
1590 /* cleanup allocated memory */
1595 static void fghCylinder( double radius, double height, GLint slices, GLint stacks, GLboolean useWireMode )
1598 GLfloat *vertices, *normals;
1600 /* Generate vertices and normals */
1601 /* Note, (stacks+1)*slices vertices for side of object, 2*slices+2 for top and bottom closures */
1602 fghGenerateCylinder((GLfloat)radius,(GLfloat)height,slices,stacks,&vertices,&normals,&nVert);
1605 /* nothing to draw */
1610 GLushort *sliceIdx, *stackIdx;
1611 /* First, generate vertex index arrays for drawing with glDrawElements
1612 * We have a bunch of line_loops to draw for each stack, and a
1613 * bunch for each slice.
1616 stackIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
1617 sliceIdx = malloc(slices*2 *sizeof(GLushort));
1618 if (!(stackIdx) || !(sliceIdx))
1622 fgError("Failed to allocate memory in fghCylinder");
1625 /* generate for each stack */
1626 for (i=0,idx=0; i<stacks+1; i++)
1628 GLushort offset = 1+(i+1)*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1629 for (j=0; j<slices; j++, idx++)
1631 stackIdx[idx] = offset+j;
1635 /* generate for each slice */
1636 for (i=0,idx=0; i<slices; i++)
1638 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1639 sliceIdx[idx++] = offset+slices;
1640 sliceIdx[idx++] = offset+(stacks+1)*slices;
1644 fghDrawGeometryWire(vertices,normals,nVert,
1645 sliceIdx,1,slices*2,GL_LINES,
1646 stackIdx,stacks+1,slices);
1648 /* cleanup allocated memory */
1654 /* First, generate vertex index arrays for drawing with glDrawElements
1655 * All stacks, including top and bottom are covered with a triangle
1659 /* Create index vector */
1662 /* Allocate buffers for indices, bail out if memory allocation fails */
1663 stripIdx = malloc((slices+1)*2*(stacks+2)*sizeof(GLushort)); /*stacks +2 because of closing off bottom and top */
1667 fgError("Failed to allocate memory in fghCylinder");
1671 for (j=0, idx=0; j<slices; j++, idx+=2)
1674 stripIdx[idx+1] = j+1; /* 0 is top vertex, 1 is first for first stack */
1676 stripIdx[idx ] = 0; /* repeat first slice's idx for closing off shape */
1677 stripIdx[idx+1] = 1;
1680 /* middle stacks: */
1681 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1682 for (i=0; i<stacks; i++, idx+=2)
1684 offset = 1+(i+1)*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
1685 for (j=0; j<slices; j++, idx+=2)
1687 stripIdx[idx ] = offset+j;
1688 stripIdx[idx+1] = offset+j+slices;
1690 stripIdx[idx ] = offset; /* repeat first slice's idx for closing off shape */
1691 stripIdx[idx+1] = offset+slices;
1695 offset = 1+(stacks+2)*slices;
1696 for (j=0; j<slices; j++, idx+=2)
1698 stripIdx[idx ] = offset+j;
1699 stripIdx[idx+1] = nVert-1; /* zero based index, last element in array (bottom vertex)... */
1701 stripIdx[idx ] = offset;
1702 stripIdx[idx+1] = nVert-1; /* repeat first slice's idx for closing off shape */
1705 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks+2,(slices+1)*2);
1707 /* cleanup allocated memory */
1711 /* cleanup allocated memory */
1716 static void fghTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, GLboolean useWireMode )
1719 GLfloat *vertices, *normals;
1721 /* Generate vertices and normals */
1722 fghGenerateTorus((GLfloat)dInnerRadius,(GLfloat)dOuterRadius,nSides,nRings, &vertices,&normals,&nVert);
1725 /* nothing to draw */
1730 GLushort *sideIdx, *ringIdx;
1731 /* First, generate vertex index arrays for drawing with glDrawElements
1732 * We have a bunch of line_loops to draw each side, and a
1733 * bunch for each ring.
1736 ringIdx = malloc(nRings*nSides*sizeof(GLushort));
1737 sideIdx = malloc(nSides*nRings*sizeof(GLushort));
1738 if (!(ringIdx) || !(sideIdx))
1742 fgError("Failed to allocate memory in fghTorus");
1745 /* generate for each ring */
1746 for( j=0,idx=0; j<nRings; j++ )
1747 for( i=0; i<nSides; i++, idx++ )
1748 ringIdx[idx] = j * nSides + i;
1750 /* generate for each side */
1751 for( i=0,idx=0; i<nSides; i++ )
1752 for( j=0; j<nRings; j++, idx++ )
1753 sideIdx[idx] = j * nSides + i;
1756 fghDrawGeometryWire(vertices,normals,nVert,
1757 ringIdx,nRings,nSides,GL_LINE_LOOP,
1758 sideIdx,nSides,nRings);
1760 /* cleanup allocated memory */
1766 /* First, generate vertex index arrays for drawing with glDrawElements
1767 * All stacks, including top and bottom are covered with a triangle
1772 /* Allocate buffers for indices, bail out if memory allocation fails */
1773 stripIdx = malloc((nRings+1)*2*nSides*sizeof(GLushort));
1777 fgError("Failed to allocate memory in fghTorus");
1780 for( i=0, idx=0; i<nSides; i++ )
1786 for( j=0; j<nRings; j++, idx+=2 )
1788 int offset = j * nSides + i;
1789 stripIdx[idx ] = offset;
1790 stripIdx[idx+1] = offset + ioff;
1792 /* repeat first to close off shape */
1794 stripIdx[idx+1] = i + ioff;
1799 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,nSides,(nRings+1)*2);
1801 /* cleanup allocated memory */
1805 /* cleanup allocated memory */
1811 /* -- INTERFACE FUNCTIONS ---------------------------------------------- */
1815 * Draws a solid sphere
1817 void FGAPIENTRY glutSolidSphere(double radius, GLint slices, GLint stacks)
1819 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" );
1821 fghSphere( radius, slices, stacks, FALSE );
1825 * Draws a wire sphere
1827 void FGAPIENTRY glutWireSphere(double radius, GLint slices, GLint stacks)
1829 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" );
1831 fghSphere( radius, slices, stacks, TRUE );
1836 * Draws a solid cone
1838 void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint stacks )
1840 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" );
1842 fghCone( base, height, slices, stacks, FALSE );
1848 void FGAPIENTRY glutWireCone( double base, double height, GLint slices, GLint stacks)
1850 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" );
1852 fghCone( base, height, slices, stacks, TRUE );
1857 * Draws a solid cylinder
1859 void FGAPIENTRY glutSolidCylinder(double radius, double height, GLint slices, GLint stacks)
1861 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCylinder" );
1863 fghCylinder( radius, height, slices, stacks, FALSE );
1867 * Draws a wire cylinder
1869 void FGAPIENTRY glutWireCylinder(double radius, double height, GLint slices, GLint stacks)
1871 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCylinder" );
1873 fghCylinder( radius, height, slices, stacks, TRUE );
1877 * Draws a wire torus
1879 void FGAPIENTRY glutWireTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings )
1881 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireTorus" );
1883 fghTorus(dInnerRadius, dOuterRadius, nSides, nRings, TRUE);
1887 * Draws a solid torus
1889 void FGAPIENTRY glutSolidTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings )
1891 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidTorus" );
1893 fghTorus(dInnerRadius, dOuterRadius, nSides, nRings, FALSE);
1898 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
1899 /* Macro to generate interface functions */
1900 #define DECLARE_SHAPE_INTERFACE(nameICaps)\
1901 void FGAPIENTRY glutWire##nameICaps( void )\
1903 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWire"#nameICaps );\
1904 fgh##nameICaps( TRUE );\
1906 void FGAPIENTRY glutSolid##nameICaps( void )\
1908 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolid"#nameICaps );\
1909 fgh##nameICaps( FALSE );\
1912 void FGAPIENTRY glutWireCube( double dSize )
1914 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCube" );
1915 fghCube( (GLfloat)dSize, TRUE );
1917 void FGAPIENTRY glutSolidCube( double dSize )
1919 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCube" );
1920 fghCube( (GLfloat)dSize, FALSE );
1923 DECLARE_SHAPE_INTERFACE(Dodecahedron)
1924 DECLARE_SHAPE_INTERFACE(Icosahedron)
1925 DECLARE_SHAPE_INTERFACE(Octahedron)
1926 DECLARE_SHAPE_INTERFACE(RhombicDodecahedron)
1928 void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, double offset[3], double scale )
1930 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSierpinskiSponge" );
1931 fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, TRUE );
1933 void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, double offset[3], double scale )
1935 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSierpinskiSponge" );
1936 fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, FALSE );
1939 DECLARE_SHAPE_INTERFACE(Tetrahedron)
1942 /*** END OF FILE ***/