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);
178 for (i=0; i<numParts2; i++)
179 glDrawElements(GL_LINE_LOOP, numVertPerPart2,
180 GL_UNSIGNED_SHORT, (GLvoid*)(sizeof(vertIdxs2[0])*i*numVertPerPart2));
181 /* Clean existing bindings before clean-up */
182 /* Android showed instability otherwise */
183 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
187 fghDisableVertexAttribArray(attribute_v_coord);
188 if (vbo_normals != 0)
189 fghDisableVertexAttribArray(attribute_v_normal);
192 fghDeleteBuffers(1, &vbo_coords);
193 if (vbo_normals != 0)
194 fghDeleteBuffers(1, &vbo_normals);
195 if (ibo_elements != 0)
196 fghDeleteBuffers(1, &ibo_elements);
197 if (ibo_elements2 != 0)
198 fghDeleteBuffers(1, &ibo_elements2);
201 static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numVertices,
202 GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode,
203 GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2
206 GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
207 GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
209 if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1))
210 /* User requested a 2.0 draw */
211 fghDrawGeometryWire20(vertices, normals, numVertices,
212 vertIdxs, numParts, numVertPerPart, vertexMode,
213 vertIdxs2, numParts2, numVertPerPart2,
214 attribute_v_coord, attribute_v_normal);
215 #ifndef GL_ES_VERSION_2_0
217 fghDrawGeometryWire11(vertices, normals,
218 vertIdxs, numParts, numVertPerPart, vertexMode,
219 vertIdxs2, numParts2, numVertPerPart2);
224 /* Draw the geometric shape with filled triangles
226 * - If the shape is naturally triangulated (numEdgePerFace==3), each
227 * vertex+normal pair is used only once, so no vertex indices.
229 * - If the shape was triangulated (DECOMPOSE_TO_TRIANGLE), some
230 * vertex+normal pairs are reused, so use vertex indices.
233 /* Version for OpenGL (ES) 1.1 */
234 #ifndef GL_ES_VERSION_2_0
235 static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
236 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart)
240 glEnableClientState(GL_VERTEX_ARRAY);
241 glEnableClientState(GL_NORMAL_ARRAY);
243 glVertexPointer(3, GL_FLOAT, 0, vertices);
244 glNormalPointer(GL_FLOAT, 0, normals);
245 if (vertIdxs == NULL)
246 glDrawArrays(GL_TRIANGLES, 0, numVertices);
249 for (i=0; i<numParts; i++)
250 glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_SHORT, vertIdxs+i*numVertIdxsPerPart);
252 glDrawElements(GL_TRIANGLES, numVertIdxsPerPart, GL_UNSIGNED_SHORT, vertIdxs);
254 glDisableClientState(GL_VERTEX_ARRAY);
255 glDisableClientState(GL_NORMAL_ARRAY);
259 /* Version for OpenGL (ES) >= 2.0 */
260 static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
261 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart,
262 GLint attribute_v_coord, GLint attribute_v_normal)
264 GLuint vbo_coords = 0, vbo_normals = 0, ibo_elements = 0;
265 GLsizei numVertIdxs = numParts * numVertIdxsPerPart;
268 if (numVertices > 0 && attribute_v_coord != -1) {
269 fghGenBuffers(1, &vbo_coords);
270 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
271 fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
272 vertices, FGH_STATIC_DRAW);
273 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
276 if (numVertices > 0 && attribute_v_normal != -1) {
277 fghGenBuffers(1, &vbo_normals);
278 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
279 fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
280 normals, FGH_STATIC_DRAW);
281 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
284 if (vertIdxs != NULL) {
285 fghGenBuffers(1, &ibo_elements);
286 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
287 fghBufferData(FGH_ELEMENT_ARRAY_BUFFER, numVertIdxs * sizeof(vertIdxs[0]),
288 vertIdxs, FGH_STATIC_DRAW);
289 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
293 fghEnableVertexAttribArray(attribute_v_coord);
294 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
295 fghVertexAttribPointer(
296 attribute_v_coord, /* attribute */
297 3, /* number of elements per vertex, here (x,y,z) */
298 GL_FLOAT, /* the type of each element */
299 GL_FALSE, /* take our values as-is */
300 0, /* no extra data between each position */
301 0 /* offset of first element */
303 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
307 fghEnableVertexAttribArray(attribute_v_normal);
308 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
309 fghVertexAttribPointer(
310 attribute_v_normal, /* attribute */
311 3, /* number of elements per vertex, here (x,y,z) */
312 GL_FLOAT, /* the type of each element */
313 GL_FALSE, /* take our values as-is */
314 0, /* no extra data between each position */
315 0 /* offset of first element */
317 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
320 if (vertIdxs == NULL) {
321 glDrawArrays(GL_TRIANGLES, 0, numVertices);
323 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
325 for (i=0; i<numParts; i++) {
326 glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_SHORT, (GLvoid*)(sizeof(vertIdxs[0])*i*numVertIdxsPerPart));
329 glDrawElements(GL_TRIANGLES, numVertIdxsPerPart, GL_UNSIGNED_SHORT, 0);
331 /* Clean existing bindings before clean-up */
332 /* Android showed instability otherwise */
333 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
337 fghDisableVertexAttribArray(attribute_v_coord);
338 if (vbo_normals != 0)
339 fghDisableVertexAttribArray(attribute_v_normal);
342 fghDeleteBuffers(1, &vbo_coords);
343 if (vbo_normals != 0)
344 fghDeleteBuffers(1, &vbo_normals);
345 if (ibo_elements != 0)
346 fghDeleteBuffers(1, &ibo_elements);
349 static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
350 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart)
352 GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
353 GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
355 if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1))
356 /* User requested a 2.0 draw */
357 fghDrawGeometrySolid20(vertices, normals, vertIdxs,
358 numVertices, numParts, numVertIdxsPerPart,
359 attribute_v_coord, attribute_v_normal);
360 #ifndef GL_ES_VERSION_2_0
362 fghDrawGeometrySolid11(vertices, normals, vertIdxs,
363 numVertices, numParts, numVertIdxsPerPart);
367 /* Shape decomposition to triangles
368 * We'll use glDrawElements to draw all shapes that are not naturally
369 * composed of triangles, so generate an index vector here, using the
370 * below sampling scheme.
371 * Be careful to keep winding of all triangles counter-clockwise,
372 * assuming that input has correct winding...
374 static GLubyte vert4Decomp[6] = {0,1,2, 0,2,3}; /* quad : 4 input vertices, 6 output (2 triangles) */
375 static GLubyte vert5Decomp[9] = {0,1,2, 0,2,4, 4,2,3}; /* pentagon: 5 input vertices, 9 output (3 triangles) */
377 static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace, GLfloat *vertices, GLubyte *vertIndices, GLfloat *normals, GLfloat *vertOut, GLfloat *normOut, GLushort *vertIdxOut)
379 int i,j,numEdgeIdxPerFace;
380 GLubyte *vertSamps = NULL;
381 switch (numEdgePerFace)
384 /* nothing to do here, we'll draw with glDrawArrays */
387 vertSamps = vert4Decomp;
388 numEdgeIdxPerFace = 6; /* 6 output vertices for each face */
391 vertSamps = vert5Decomp;
392 numEdgeIdxPerFace = 9; /* 9 output vertices for each face */
396 * Build array with vertices using vertex coordinates and vertex indices
397 * Do same for normals.
398 * Need to do this because of different normals at shared vertices.
400 for (i=0; i<numFaces; i++)
403 int faceIdxVertIdx = i*numEdgePerFace; /* index to first element of "row" in vertex indices */
404 for (j=0; j<numEdgePerFace; j++)
406 int outIdx = i*numEdgePerFace*3+j*3;
407 int vertIdx = vertIndices[faceIdxVertIdx+j]*3;
409 vertOut[outIdx ] = vertices[vertIdx ];
410 vertOut[outIdx+1] = vertices[vertIdx+1];
411 vertOut[outIdx+2] = vertices[vertIdx+2];
413 normOut[outIdx ] = normals [normIdx ];
414 normOut[outIdx+1] = normals [normIdx+1];
415 normOut[outIdx+2] = normals [normIdx+2];
418 /* generate vertex indices for each face */
420 for (j=0; j<numEdgeIdxPerFace; j++)
421 vertIdxOut[i*numEdgeIdxPerFace+j] = faceIdxVertIdx + vertSamps[j];
425 static void fghGenerateGeometry(int numFaces, int numEdgePerFace, GLfloat *vertices, GLubyte *vertIndices, GLfloat *normals, GLfloat *vertOut, GLfloat *normOut)
427 /* This function does the same as fghGenerateGeometryWithIndexArray, just skipping the index array generation... */
428 fghGenerateGeometryWithIndexArray(numFaces, numEdgePerFace, vertices, vertIndices, normals, vertOut, normOut, NULL);
432 /* -- INTERNAL SETUP OF GEOMETRY --------------------------------------- */
433 /* -- stuff that can be cached -- */
434 /* Cache of input to glDrawArrays or glDrawElements
435 * In general, we build arrays with all vertices or normals.
436 * We cant compress this and use glDrawElements as all combinations of
437 * vertices and normals are unique.
439 #define DECLARE_SHAPE_CACHE(name,nameICaps,nameCaps)\
440 static GLboolean name##Cached = FALSE;\
441 static GLfloat name##_verts[nameCaps##_VERT_ELEM_PER_OBJ];\
442 static GLfloat name##_norms[nameCaps##_VERT_ELEM_PER_OBJ];\
443 static void fgh##nameICaps##Generate()\
445 fghGenerateGeometry(nameCaps##_NUM_FACES, nameCaps##_NUM_EDGE_PER_FACE,\
446 name##_v, name##_vi, name##_n,\
447 name##_verts, name##_norms);\
449 #define DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(name,nameICaps,nameCaps)\
450 static GLboolean name##Cached = FALSE;\
451 static GLfloat name##_verts[nameCaps##_VERT_ELEM_PER_OBJ];\
452 static GLfloat name##_norms[nameCaps##_VERT_ELEM_PER_OBJ];\
453 static GLushort name##_vertIdxs[nameCaps##_VERT_PER_OBJ_TRI];\
454 static void fgh##nameICaps##Generate()\
456 fghGenerateGeometryWithIndexArray(nameCaps##_NUM_FACES, nameCaps##_NUM_EDGE_PER_FACE,\
457 name##_v, name##_vi, name##_n,\
458 name##_verts, name##_norms, name##_vertIdxs);\
462 #define CUBE_NUM_VERT 8
463 #define CUBE_NUM_FACES 6
464 #define CUBE_NUM_EDGE_PER_FACE 4
465 #define CUBE_VERT_PER_OBJ (CUBE_NUM_FACES*CUBE_NUM_EDGE_PER_FACE)
466 #define CUBE_VERT_ELEM_PER_OBJ (CUBE_VERT_PER_OBJ*3)
467 #define CUBE_VERT_PER_OBJ_TRI (CUBE_VERT_PER_OBJ+CUBE_NUM_FACES*2) /* 2 extra edges per face when drawing quads as triangles */
468 /* Vertex Coordinates */
469 static GLfloat cube_v[CUBE_NUM_VERT*3] =
481 static GLfloat cube_n[CUBE_NUM_FACES*3] =
491 /* Vertex indices, as quads, before triangulation */
492 static GLubyte cube_vi[CUBE_VERT_PER_OBJ] =
501 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(cube,Cube,CUBE)
503 /* -- Dodecahedron -- */
504 /* Magic Numbers: It is possible to create a dodecahedron by attaching two
505 * pentagons to each face of of a cube. The coordinates of the points are:
506 * (+-x,0, z); (+-1, 1, 1); (0, z, x )
507 * where x = (-1 + sqrt(5))/2, z = (1 + sqrt(5))/2 or
508 * x = 0.61803398875 and z = 1.61803398875.
510 #define DODECAHEDRON_NUM_VERT 20
511 #define DODECAHEDRON_NUM_FACES 12
512 #define DODECAHEDRON_NUM_EDGE_PER_FACE 5
513 #define DODECAHEDRON_VERT_PER_OBJ (DODECAHEDRON_NUM_FACES*DODECAHEDRON_NUM_EDGE_PER_FACE)
514 #define DODECAHEDRON_VERT_ELEM_PER_OBJ (DODECAHEDRON_VERT_PER_OBJ*3)
515 #define DODECAHEDRON_VERT_PER_OBJ_TRI (DODECAHEDRON_VERT_PER_OBJ+DODECAHEDRON_NUM_FACES*4) /* 4 extra edges per face when drawing pentagons as triangles */
516 /* Vertex Coordinates */
517 static GLfloat dodecahedron_v[DODECAHEDRON_NUM_VERT*3] =
519 0.0f, 1.61803398875f, 0.61803398875f,
521 -0.61803398875f, 0.0f, 1.61803398875f,
522 0.61803398875f, 0.0f, 1.61803398875f,
524 0.0f, 1.61803398875f, -0.61803398875f,
526 0.61803398875f, 0.0f, -1.61803398875f,
527 -0.61803398875f, 0.0f, -1.61803398875f,
528 - 1.0f, 1.0f, - 1.0f,
529 0.0f, -1.61803398875f, 0.61803398875f,
531 - 1.0f, - 1.0f, 1.0f,
532 0.0f, -1.61803398875f, -0.61803398875f,
533 - 1.0f, - 1.0f, - 1.0f,
534 1.0f, - 1.0f, - 1.0f,
535 1.61803398875f, -0.61803398875f, 0.0f,
536 1.61803398875f, 0.61803398875f, 0.0f,
537 -1.61803398875f, 0.61803398875f, 0.0f,
538 -1.61803398875f, -0.61803398875f, 0.0f
541 static GLfloat dodecahedron_n[DODECAHEDRON_NUM_FACES*3] =
543 0.0f, 0.525731112119f, 0.850650808354f,
544 0.0f, 0.525731112119f, -0.850650808354f,
545 0.0f, -0.525731112119f, 0.850650808354f,
546 0.0f, -0.525731112119f, -0.850650808354f,
548 0.850650808354f, 0.0f, 0.525731112119f,
549 -0.850650808354f, 0.0f, 0.525731112119f,
550 0.850650808354f, 0.0f, -0.525731112119f,
551 -0.850650808354f, 0.0f, -0.525731112119f,
553 0.525731112119f, 0.850650808354f, 0.0f,
554 0.525731112119f, -0.850650808354f, 0.0f,
555 -0.525731112119f, 0.850650808354f, 0.0f,
556 -0.525731112119f, -0.850650808354f, 0.0f,
560 static GLubyte dodecahedron_vi[DODECAHEDRON_VERT_PER_OBJ] =
577 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON)
580 /* -- Icosahedron -- */
581 #define ICOSAHEDRON_NUM_VERT 12
582 #define ICOSAHEDRON_NUM_FACES 20
583 #define ICOSAHEDRON_NUM_EDGE_PER_FACE 3
584 #define ICOSAHEDRON_VERT_PER_OBJ (ICOSAHEDRON_NUM_FACES*ICOSAHEDRON_NUM_EDGE_PER_FACE)
585 #define ICOSAHEDRON_VERT_ELEM_PER_OBJ (ICOSAHEDRON_VERT_PER_OBJ*3)
586 #define ICOSAHEDRON_VERT_PER_OBJ_TRI ICOSAHEDRON_VERT_PER_OBJ
587 /* Vertex Coordinates */
588 static GLfloat icosahedron_v[ICOSAHEDRON_NUM_VERT*3] =
591 0.447213595500f, 0.894427191000f, 0.0f,
592 0.447213595500f, 0.276393202252f, 0.850650808354f,
593 0.447213595500f, -0.723606797748f, 0.525731112119f,
594 0.447213595500f, -0.723606797748f, -0.525731112119f,
595 0.447213595500f, 0.276393202252f, -0.850650808354f,
596 -0.447213595500f, -0.894427191000f, 0.0f,
597 -0.447213595500f, -0.276393202252f, 0.850650808354f,
598 -0.447213595500f, 0.723606797748f, 0.525731112119f,
599 -0.447213595500f, 0.723606797748f, -0.525731112119f,
600 -0.447213595500f, -0.276393202252f, -0.850650808354f,
604 * 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] ) ;
605 * 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] ) ;
606 * 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] ) ;
608 static GLfloat icosahedron_n[ICOSAHEDRON_NUM_FACES*3] =
610 0.760845213037948f, 0.470228201835026f, 0.341640786498800f,
611 0.760845213036861f, -0.179611190632978f, 0.552786404500000f,
612 0.760845213033849f, -0.581234022404097f, 0.0f,
613 0.760845213036861f, -0.179611190632978f, -0.552786404500000f,
614 0.760845213037948f, 0.470228201835026f, -0.341640786498800f,
615 0.179611190628666f, 0.760845213037948f, 0.552786404498399f,
616 0.179611190634277f, -0.290617011204044f, 0.894427191000000f,
617 0.179611190633958f, -0.940456403667806f, 0.0f,
618 0.179611190634278f, -0.290617011204044f, -0.894427191000000f,
619 0.179611190628666f, 0.760845213037948f, -0.552786404498399f,
620 -0.179611190633958f, 0.940456403667806f, 0.0f,
621 -0.179611190634277f, 0.290617011204044f, 0.894427191000000f,
622 -0.179611190628666f, -0.760845213037948f, 0.552786404498399f,
623 -0.179611190628666f, -0.760845213037948f, -0.552786404498399f,
624 -0.179611190634277f, 0.290617011204044f, -0.894427191000000f,
625 -0.760845213036861f, 0.179611190632978f, -0.552786404500000f,
626 -0.760845213033849f, 0.581234022404097f, 0.0f,
627 -0.760845213036861f, 0.179611190632978f, 0.552786404500000f,
628 -0.760845213037948f, -0.470228201835026f, 0.341640786498800f,
629 -0.760845213037948f, -0.470228201835026f, -0.341640786498800f,
633 static GLubyte icosahedron_vi[ICOSAHEDRON_VERT_PER_OBJ] =
656 DECLARE_SHAPE_CACHE(icosahedron,Icosahedron,ICOSAHEDRON)
658 /* -- Octahedron -- */
659 #define OCTAHEDRON_NUM_VERT 6
660 #define OCTAHEDRON_NUM_FACES 8
661 #define OCTAHEDRON_NUM_EDGE_PER_FACE 3
662 #define OCTAHEDRON_VERT_PER_OBJ (OCTAHEDRON_NUM_FACES*OCTAHEDRON_NUM_EDGE_PER_FACE)
663 #define OCTAHEDRON_VERT_ELEM_PER_OBJ (OCTAHEDRON_VERT_PER_OBJ*3)
664 #define OCTAHEDRON_VERT_PER_OBJ_TRI OCTAHEDRON_VERT_PER_OBJ
666 /* Vertex Coordinates */
667 static GLfloat octahedron_v[OCTAHEDRON_NUM_VERT*3] =
678 static GLfloat octahedron_n[OCTAHEDRON_NUM_FACES*3] =
680 0.577350269189f, 0.577350269189f, 0.577350269189f, /* sqrt(1/3) */
681 0.577350269189f, 0.577350269189f,-0.577350269189f,
682 0.577350269189f,-0.577350269189f, 0.577350269189f,
683 0.577350269189f,-0.577350269189f,-0.577350269189f,
684 -0.577350269189f, 0.577350269189f, 0.577350269189f,
685 -0.577350269189f, 0.577350269189f,-0.577350269189f,
686 -0.577350269189f,-0.577350269189f, 0.577350269189f,
687 -0.577350269189f,-0.577350269189f,-0.577350269189f
692 static GLubyte octahedron_vi[OCTAHEDRON_VERT_PER_OBJ] =
703 DECLARE_SHAPE_CACHE(octahedron,Octahedron,OCTAHEDRON)
705 /* -- RhombicDodecahedron -- */
706 #define RHOMBICDODECAHEDRON_NUM_VERT 14
707 #define RHOMBICDODECAHEDRON_NUM_FACES 12
708 #define RHOMBICDODECAHEDRON_NUM_EDGE_PER_FACE 4
709 #define RHOMBICDODECAHEDRON_VERT_PER_OBJ (RHOMBICDODECAHEDRON_NUM_FACES*RHOMBICDODECAHEDRON_NUM_EDGE_PER_FACE)
710 #define RHOMBICDODECAHEDRON_VERT_ELEM_PER_OBJ (RHOMBICDODECAHEDRON_VERT_PER_OBJ*3)
711 #define RHOMBICDODECAHEDRON_VERT_PER_OBJ_TRI (RHOMBICDODECAHEDRON_VERT_PER_OBJ+RHOMBICDODECAHEDRON_NUM_FACES*2) /* 2 extra edges per face when drawing quads as triangles */
713 /* Vertex Coordinates */
714 static GLfloat rhombicdodecahedron_v[RHOMBICDODECAHEDRON_NUM_VERT*3] =
717 0.707106781187f, 0.0f, 0.5f,
718 0.0f, 0.707106781187f, 0.5f,
719 -0.707106781187f, 0.0f, 0.5f,
720 0.0f, -0.707106781187f, 0.5f,
721 0.707106781187f, 0.707106781187f, 0.0f,
722 -0.707106781187f, 0.707106781187f, 0.0f,
723 -0.707106781187f, -0.707106781187f, 0.0f,
724 0.707106781187f, -0.707106781187f, 0.0f,
725 0.707106781187f, 0.0f, -0.5f,
726 0.0f, 0.707106781187f, -0.5f,
727 -0.707106781187f, 0.0f, -0.5f,
728 0.0f, -0.707106781187f, -0.5f,
732 static GLfloat rhombicdodecahedron_n[RHOMBICDODECAHEDRON_NUM_FACES*3] =
734 0.353553390594f, 0.353553390594f, 0.5f,
735 -0.353553390594f, 0.353553390594f, 0.5f,
736 -0.353553390594f, -0.353553390594f, 0.5f,
737 0.353553390594f, -0.353553390594f, 0.5f,
742 0.353553390594f, 0.353553390594f, -0.5f,
743 -0.353553390594f, 0.353553390594f, -0.5f,
744 -0.353553390594f, -0.353553390594f, -0.5f,
745 0.353553390594f, -0.353553390594f, -0.5f
749 static GLubyte rhombicdodecahedron_vi[RHOMBICDODECAHEDRON_VERT_PER_OBJ] =
764 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON)
766 /* -- Tetrahedron -- */
767 /* Magic Numbers: r0 = ( 1, 0, 0 )
768 * r1 = ( -1/3, 2 sqrt(2) / 3, 0 )
769 * r2 = ( -1/3, - sqrt(2) / 3, sqrt(6) / 3 )
770 * r3 = ( -1/3, - sqrt(2) / 3, -sqrt(6) / 3 )
771 * |r0| = |r1| = |r2| = |r3| = 1
772 * Distance between any two points is 2 sqrt(6) / 3
774 * Normals: The unit normals are simply the negative of the coordinates of the point not on the surface.
776 #define TETRAHEDRON_NUM_VERT 4
777 #define TETRAHEDRON_NUM_FACES 4
778 #define TETRAHEDRON_NUM_EDGE_PER_FACE 3
779 #define TETRAHEDRON_VERT_PER_OBJ (TETRAHEDRON_NUM_FACES*TETRAHEDRON_NUM_EDGE_PER_FACE)
780 #define TETRAHEDRON_VERT_ELEM_PER_OBJ (TETRAHEDRON_VERT_PER_OBJ*3)
781 #define TETRAHEDRON_VERT_PER_OBJ_TRI TETRAHEDRON_VERT_PER_OBJ
783 /* Vertex Coordinates */
784 static GLfloat tetrahedron_v[TETRAHEDRON_NUM_VERT*3] =
787 -0.333333333333f, 0.942809041582f, 0.0f,
788 -0.333333333333f, -0.471404520791f, 0.816496580928f,
789 -0.333333333333f, -0.471404520791f, -0.816496580928f
792 static GLfloat tetrahedron_n[TETRAHEDRON_NUM_FACES*3] =
795 0.333333333333f, -0.942809041582f, 0.0f,
796 0.333333333333f, 0.471404520791f, -0.816496580928f,
797 0.333333333333f, 0.471404520791f, 0.816496580928f
801 static GLubyte tetrahedron_vi[TETRAHEDRON_VERT_PER_OBJ] =
808 DECLARE_SHAPE_CACHE(tetrahedron,Tetrahedron,TETRAHEDRON)
810 /* -- Sierpinski Sponge -- */
811 static unsigned int ipow (int x, unsigned int y)
813 return y==0? 1: y==1? x: (y%2? x: 1) * ipow(x*x, y/2);
816 static void fghSierpinskiSpongeGenerate ( int numLevels, double offset[3], GLfloat scale, GLfloat* vertices, GLfloat* normals )
819 if ( numLevels == 0 )
821 for (i=0; i<TETRAHEDRON_NUM_FACES; i++)
824 int faceIdxVertIdx = i*TETRAHEDRON_NUM_EDGE_PER_FACE;
825 for (j=0; j<TETRAHEDRON_NUM_EDGE_PER_FACE; j++)
827 int outIdx = i*TETRAHEDRON_NUM_EDGE_PER_FACE*3+j*3;
828 int vertIdx = tetrahedron_vi[faceIdxVertIdx+j]*3;
830 vertices[outIdx ] = (GLfloat)offset[0] + scale * tetrahedron_v[vertIdx ];
831 vertices[outIdx+1] = (GLfloat)offset[1] + scale * tetrahedron_v[vertIdx+1];
832 vertices[outIdx+2] = (GLfloat)offset[2] + scale * tetrahedron_v[vertIdx+2];
834 normals [outIdx ] = tetrahedron_n[normIdx ];
835 normals [outIdx+1] = tetrahedron_n[normIdx+1];
836 normals [outIdx+2] = tetrahedron_n[normIdx+2];
840 else if ( numLevels > 0 )
842 double local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */
843 unsigned int stride = ipow(4,--numLevels)*TETRAHEDRON_VERT_ELEM_PER_OBJ;
845 for ( i = 0 ; i < TETRAHEDRON_NUM_FACES ; i++ )
848 local_offset[0] = offset[0] + scale * tetrahedron_v[idx ];
849 local_offset[1] = offset[1] + scale * tetrahedron_v[idx+1];
850 local_offset[2] = offset[2] + scale * tetrahedron_v[idx+2];
851 fghSierpinskiSpongeGenerate ( numLevels, local_offset, scale, vertices+i*stride, normals+i*stride );
856 /* -- Now the various shapes involving circles -- */
858 * Compute lookup table of cos and sin values forming a circle
859 * (or half circle if halfCircle==TRUE)
862 * It is the responsibility of the caller to free these tables
863 * The size of the table is (n+1) to form a connected loop
864 * The last entry is exactly the same as the first
865 * The sign of n can be flipped to get the reverse loop
867 static void fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GLboolean halfCircle)
871 /* Table size, the sign of n flips the circle direction */
872 const int size = abs(n);
874 /* Determine the angle between samples */
875 const GLfloat angle = (halfCircle?1:2)*(GLfloat)M_PI/(GLfloat)( ( n == 0 ) ? 1 : n );
877 /* Allocate memory for n samples, plus duplicate of first entry at the end */
878 *sint = malloc(sizeof(GLfloat) * (size+1));
879 *cost = malloc(sizeof(GLfloat) * (size+1));
881 /* Bail out if memory allocation fails, fgError never returns */
882 if (!(*sint) || !(*cost))
886 fgError("Failed to allocate memory in fghCircleTable");
889 /* Compute cos and sin around the circle */
893 for (i=1; i<size; i++)
895 (*sint)[i] = sinf(angle*i);
896 (*cost)[i] = cosf(angle*i);
902 (*sint)[size] = 0.0f; /* sin PI */
903 (*cost)[size] = -1.0f; /* cos PI */
907 /* Last sample is duplicate of the first (sin or cos of 2 PI) */
908 (*sint)[size] = (*sint)[0];
909 (*cost)[size] = (*cost)[0];
913 static void fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat **vertices, GLfloat **normals, int* nVert)
916 int idx = 0; /* idx into vertex/normal buffer */
919 /* Pre-computed circle */
920 GLfloat *sint1,*cost1;
921 GLfloat *sint2,*cost2;
923 /* number of unique vertices */
924 if (slices==0 || stacks<2)
926 /* nothing to generate */
930 *nVert = slices*(stacks-1)+2;
931 if ((*nVert) > 65535) /* TODO: must have a better solution than this low limit, at least for architectures where gluint is available */
932 fgWarning("fghGenerateSphere: too many slices or stacks requested, indices will wrap");
934 /* precompute values on unit circle */
935 fghCircleTable(&sint1,&cost1,-slices,FALSE);
936 fghCircleTable(&sint2,&cost2, stacks,TRUE);
938 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
939 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
940 *normals = malloc((*nVert)*3*sizeof(GLfloat));
941 if (!(*vertices) || !(*normals))
945 fgError("Failed to allocate memory in fghGenerateSphere");
949 (*vertices)[0] = 0.f;
950 (*vertices)[1] = 0.f;
951 (*vertices)[2] = radius;
952 (*normals )[0] = 0.f;
953 (*normals )[1] = 0.f;
954 (*normals )[2] = 1.f;
958 for( i=1; i<stacks; i++ )
960 for(j=0; j<slices; j++, idx+=3)
962 x = cost1[j]*sint2[i];
963 y = sint1[j]*sint2[i];
966 (*vertices)[idx ] = x*radius;
967 (*vertices)[idx+1] = y*radius;
968 (*vertices)[idx+2] = z*radius;
969 (*normals )[idx ] = x;
970 (*normals )[idx+1] = y;
971 (*normals )[idx+2] = z;
976 (*vertices)[idx ] = 0.f;
977 (*vertices)[idx+1] = 0.f;
978 (*vertices)[idx+2] = -radius;
979 (*normals )[idx ] = 0.f;
980 (*normals )[idx+1] = 0.f;
981 (*normals )[idx+2] = -1.f;
983 /* Done creating vertices, release sin and cos tables */
990 void fghGenerateCone(
991 GLfloat base, GLfloat height, GLint slices, GLint stacks, /* input */
992 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
996 int idx = 0; /* idx into vertex/normal buffer */
998 /* Pre-computed circle */
1001 /* Step in z and radius as stacks are drawn. */
1003 GLfloat r = (GLfloat)base;
1005 const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
1006 const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 );
1008 /* Scaling factors for vertex normals */
1009 const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base ));
1010 const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base ));
1014 /* number of unique vertices */
1015 if (slices==0 || stacks<1)
1017 /* nothing to generate */
1021 *nVert = slices*(stacks+2)+1; /* need an extra stack for closing off bottom with correct normals */
1023 if ((*nVert) > 65535)
1024 fgWarning("fghGenerateCone: too many slices or stacks requested, indices will wrap");
1026 /* Pre-computed circle */
1027 fghCircleTable(&sint,&cost,-slices,FALSE);
1029 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1030 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1031 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1032 if (!(*vertices) || !(*normals))
1036 fgError("Failed to allocate memory in fghGenerateSphere");
1040 (*vertices)[0] = 0.f;
1041 (*vertices)[1] = 0.f;
1043 (*normals )[0] = 0.f;
1044 (*normals )[1] = 0.f;
1045 (*normals )[2] = -1.f;
1047 /* other on bottom (get normals right) */
1048 for (j=0; j<slices; j++, idx+=3)
1050 (*vertices)[idx ] = cost[j]*r;
1051 (*vertices)[idx+1] = sint[j]*r;
1052 (*vertices)[idx+2] = z;
1053 (*normals )[idx ] = 0.f;
1054 (*normals )[idx+1] = 0.f;
1055 (*normals )[idx+2] = -1.f;
1059 for (i=0; i<stacks+1; i++ )
1061 for (j=0; j<slices; j++, idx+=3)
1063 (*vertices)[idx ] = cost[j]*r;
1064 (*vertices)[idx+1] = sint[j]*r;
1065 (*vertices)[idx+2] = z;
1066 (*normals )[idx ] = cost[j]*sinn;
1067 (*normals )[idx+1] = sint[j]*sinn;
1068 (*normals )[idx+2] = cosn;
1075 /* Release sin and cos tables */
1080 void fghGenerateCylinder(
1081 GLfloat radius, GLfloat height, GLint slices, GLint stacks, /* input */
1082 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
1086 int idx = 0; /* idx into vertex/normal buffer */
1088 /* Step in z as stacks are drawn. */
1089 GLfloat radf = (GLfloat)radius;
1091 const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
1093 /* Pre-computed circle */
1094 GLfloat *sint,*cost;
1096 /* number of unique vertices */
1097 if (slices==0 || stacks<1)
1099 /* nothing to generate */
1103 *nVert = slices*(stacks+3)+2; /* need two extra stacks for closing off top and bottom with correct normals */
1105 if ((*nVert) > 65535)
1106 fgWarning("fghGenerateCylinder: too many slices or stacks requested, indices will wrap");
1108 /* Pre-computed circle */
1109 fghCircleTable(&sint,&cost,-slices,FALSE);
1111 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1112 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1113 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1114 if (!(*vertices) || !(*normals))
1118 fgError("Failed to allocate memory in fghGenerateCylinder");
1123 (*vertices)[0] = 0.f;
1124 (*vertices)[1] = 0.f;
1125 (*vertices)[2] = 0.f;
1126 (*normals )[0] = 0.f;
1127 (*normals )[1] = 0.f;
1128 (*normals )[2] = -1.f;
1130 /* other on top (get normals right) */
1131 for (j=0; j<slices; j++, idx+=3)
1133 (*vertices)[idx ] = cost[j]*radf;
1134 (*vertices)[idx+1] = sint[j]*radf;
1135 (*vertices)[idx+2] = z;
1136 (*normals )[idx ] = 0.f;
1137 (*normals )[idx+1] = 0.f;
1138 (*normals )[idx+2] = -1.f;
1142 for (i=0; i<stacks+1; i++ )
1144 for (j=0; j<slices; j++, idx+=3)
1146 (*vertices)[idx ] = cost[j]*radf;
1147 (*vertices)[idx+1] = sint[j]*radf;
1148 (*vertices)[idx+2] = z;
1149 (*normals )[idx ] = cost[j];
1150 (*normals )[idx+1] = sint[j];
1151 (*normals )[idx+2] = 0.f;
1157 /* other on bottom (get normals right) */
1159 for (j=0; j<slices; j++, idx+=3)
1161 (*vertices)[idx ] = cost[j]*radf;
1162 (*vertices)[idx+1] = sint[j]*radf;
1163 (*vertices)[idx+2] = z;
1164 (*normals )[idx ] = 0.f;
1165 (*normals )[idx+1] = 0.f;
1166 (*normals )[idx+2] = 1.f;
1170 (*vertices)[idx ] = 0.f;
1171 (*vertices)[idx+1] = 0.f;
1172 (*vertices)[idx+2] = height;
1173 (*normals )[idx ] = 0.f;
1174 (*normals )[idx+1] = 0.f;
1175 (*normals )[idx+2] = 1.f;
1177 /* Release sin and cos tables */
1182 void fghGenerateTorus(
1183 double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, /* input */
1184 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
1187 GLfloat iradius = (float)dInnerRadius;
1188 GLfloat oradius = (float)dOuterRadius;
1191 /* Pre-computed circle */
1192 GLfloat *spsi, *cpsi;
1193 GLfloat *sphi, *cphi;
1195 /* number of unique vertices */
1196 if (nSides<2 || nRings<2)
1198 /* nothing to generate */
1202 *nVert = nSides * nRings;
1204 if ((*nVert) > 65535)
1205 fgWarning("fghGenerateTorus: too many slices or stacks requested, indices will wrap");
1207 /* precompute values on unit circle */
1208 fghCircleTable(&spsi,&cpsi, nRings,FALSE);
1209 fghCircleTable(&sphi,&cphi,-nSides,FALSE);
1211 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1212 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1213 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1214 if (!(*vertices) || !(*normals))
1218 fgError("Failed to allocate memory in fghGenerateTorus");
1221 for( j=0; j<nRings; j++ )
1223 for( i=0; i<nSides; i++ )
1225 int offset = 3 * ( j * nSides + i ) ;
1227 (*vertices)[offset ] = cpsi[j] * ( oradius + cphi[i] * iradius ) ;
1228 (*vertices)[offset+1] = spsi[j] * ( oradius + cphi[i] * iradius ) ;
1229 (*vertices)[offset+2] = sphi[i] * iradius ;
1230 (*normals )[offset ] = cpsi[j] * cphi[i] ;
1231 (*normals )[offset+1] = spsi[j] * cphi[i] ;
1232 (*normals )[offset+2] = sphi[i] ;
1236 /* Release sin and cos tables */
1243 /* -- INTERNAL DRAWING functions --------------------------------------- */
1244 #define _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,vertIdxs)\
1245 static void fgh##nameICaps( GLboolean useWireMode )\
1249 fgh##nameICaps##Generate();\
1250 name##Cached = GL_TRUE;\
1255 fghDrawGeometryWire (name##_verts,name##_norms,nameCaps##_VERT_PER_OBJ, \
1256 NULL,nameCaps##_NUM_FACES,nameCaps##_NUM_EDGE_PER_FACE,GL_LINE_LOOP,\
1261 fghDrawGeometrySolid(name##_verts,name##_norms,vertIdxs,\
1262 nameCaps##_VERT_PER_OBJ, 1, nameCaps##_VERT_PER_OBJ_TRI); \
1265 #define DECLARE_INTERNAL_DRAW(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,NULL)
1266 #define DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,name##_vertIdxs)
1268 static void fghCube( GLfloat dSize, GLboolean useWireMode )
1275 cubeCached = GL_TRUE;
1280 /* Need to build new vertex list containing vertices for cube of different size */
1283 vertices = malloc(CUBE_VERT_ELEM_PER_OBJ * sizeof(GLfloat));
1285 /* Bail out if memory allocation fails, fgError never returns */
1289 fgError("Failed to allocate memory in fghCube");
1292 for (i=0; i<CUBE_VERT_ELEM_PER_OBJ; i++)
1293 vertices[i] = dSize*cube_verts[i];
1296 vertices = cube_verts;
1299 fghDrawGeometryWire(vertices, cube_norms, CUBE_VERT_PER_OBJ,
1300 NULL,CUBE_NUM_FACES, CUBE_NUM_EDGE_PER_FACE,GL_LINE_LOOP,
1303 fghDrawGeometrySolid(vertices, cube_norms, cube_vertIdxs,
1304 CUBE_VERT_PER_OBJ, 1, CUBE_VERT_PER_OBJ_TRI);
1307 /* cleanup allocated memory */
1311 DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON)
1312 DECLARE_INTERNAL_DRAW(icosahedron,Icosahedron,ICOSAHEDRON)
1313 DECLARE_INTERNAL_DRAW(octahedron,Octahedron,OCTAHEDRON)
1314 DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON)
1315 DECLARE_INTERNAL_DRAW(tetrahedron,Tetrahedron,TETRAHEDRON)
1317 static void fghSierpinskiSponge ( int numLevels, double offset[3], GLfloat scale, GLboolean useWireMode )
1321 GLsizei numTetr = numLevels<0? 0 : ipow(4,numLevels); /* No sponge for numLevels below 0 */
1322 GLsizei numVert = numTetr*TETRAHEDRON_VERT_PER_OBJ;
1323 GLsizei numFace = numTetr*TETRAHEDRON_NUM_FACES;
1327 /* Allocate memory */
1328 vertices = malloc(numVert*3 * sizeof(GLfloat));
1329 normals = malloc(numVert*3 * sizeof(GLfloat));
1330 /* Bail out if memory allocation fails, fgError never returns */
1331 if (!vertices || !normals)
1335 fgError("Failed to allocate memory in fghSierpinskiSponge");
1338 /* Generate elements */
1339 fghSierpinskiSpongeGenerate ( numLevels, offset, scale, vertices, normals );
1341 /* Draw and cleanup */
1343 fghDrawGeometryWire (vertices,normals,numVert,
1344 NULL,numFace,TETRAHEDRON_NUM_EDGE_PER_FACE,GL_LINE_LOOP,
1347 fghDrawGeometrySolid(vertices,normals,NULL,numVert,1,0);
1355 static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useWireMode )
1358 GLfloat *vertices, *normals;
1360 /* Generate vertices and normals */
1361 fghGenerateSphere((GLfloat)radius,slices,stacks,&vertices,&normals,&nVert);
1364 /* nothing to draw */
1369 GLushort *sliceIdx, *stackIdx;
1370 /* First, generate vertex index arrays for drawing with glDrawElements
1371 * We have a bunch of line_loops to draw for each stack, and a
1372 * bunch for each slice.
1375 sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
1376 stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort));
1377 if (!(stackIdx) || !(sliceIdx))
1381 fgError("Failed to allocate memory in fghSphere");
1384 /* generate for each stack */
1385 for (i=0,idx=0; i<stacks-1; i++)
1387 GLushort offset = 1+i*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1388 for (j=0; j<slices; j++, idx++)
1390 stackIdx[idx] = offset+j;
1394 /* generate for each slice */
1395 for (i=0,idx=0; i<slices; i++)
1397 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1398 sliceIdx[idx++] = 0; /* vertex on top */
1399 for (j=0; j<stacks-1; j++, idx++)
1401 sliceIdx[idx] = offset+j*slices;
1403 sliceIdx[idx++] = nVert-1; /* zero based index, last element in array... */
1407 fghDrawGeometryWire(vertices,normals,nVert,
1408 sliceIdx,slices,stacks+1,GL_LINE_STRIP,
1409 stackIdx,stacks-1,slices);
1411 /* cleanup allocated memory */
1417 /* First, generate vertex index arrays for drawing with glDrawElements
1418 * All stacks, including top and bottom are covered with a triangle
1422 /* Create index vector */
1425 /* Allocate buffers for indices, bail out if memory allocation fails */
1426 stripIdx = malloc((slices+1)*2*(stacks)*sizeof(GLushort));
1430 fgError("Failed to allocate memory in fghSphere");
1434 for (j=0, idx=0; j<slices; j++, idx+=2)
1436 stripIdx[idx ] = j+1; /* 0 is top vertex, 1 is first for first stack */
1437 stripIdx[idx+1] = 0;
1439 stripIdx[idx ] = 1; /* repeat first slice's idx for closing off shape */
1440 stripIdx[idx+1] = 0;
1443 /* middle stacks: */
1444 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1445 for (i=0; i<stacks-2; i++, idx+=2)
1447 offset = 1+i*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
1448 for (j=0; j<slices; j++, idx+=2)
1450 stripIdx[idx ] = offset+j+slices;
1451 stripIdx[idx+1] = offset+j;
1453 stripIdx[idx ] = offset+slices; /* repeat first slice's idx for closing off shape */
1454 stripIdx[idx+1] = offset;
1458 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 */
1459 for (j=0; j<slices; j++, idx+=2)
1461 stripIdx[idx ] = nVert-1; /* zero based index, last element in array (bottom vertex)... */
1462 stripIdx[idx+1] = offset+j;
1464 stripIdx[idx ] = nVert-1; /* repeat first slice's idx for closing off shape */
1465 stripIdx[idx+1] = offset;
1469 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks,(slices+1)*2);
1471 /* cleanup allocated memory */
1475 /* cleanup allocated memory */
1480 static void fghCone( double base, double height, GLint slices, GLint stacks, GLboolean useWireMode )
1483 GLfloat *vertices, *normals;
1485 /* Generate vertices and normals */
1486 /* Note, (stacks+1)*slices vertices for side of object, slices+1 for top and bottom closures */
1487 fghGenerateCone((GLfloat)base,(GLfloat)height,slices,stacks,&vertices,&normals,&nVert);
1490 /* nothing to draw */
1495 GLushort *sliceIdx, *stackIdx;
1496 /* First, generate vertex index arrays for drawing with glDrawElements
1497 * We have a bunch of line_loops to draw for each stack, and a
1498 * bunch for each slice.
1501 stackIdx = malloc(slices*stacks*sizeof(GLushort));
1502 sliceIdx = malloc(slices*2 *sizeof(GLushort));
1503 if (!(stackIdx) || !(sliceIdx))
1507 fgError("Failed to allocate memory in fghCone");
1510 /* generate for each stack */
1511 for (i=0,idx=0; i<stacks; i++)
1513 GLushort offset = 1+(i+1)*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1514 for (j=0; j<slices; j++, idx++)
1516 stackIdx[idx] = offset+j;
1520 /* generate for each slice */
1521 for (i=0,idx=0; i<slices; i++)
1523 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1524 sliceIdx[idx++] = offset+slices;
1525 sliceIdx[idx++] = offset+(stacks+1)*slices;
1529 fghDrawGeometryWire(vertices,normals,nVert,
1530 sliceIdx,1,slices*2,GL_LINES,
1531 stackIdx,stacks,slices);
1533 /* cleanup allocated memory */
1539 /* First, generate vertex index arrays for drawing with glDrawElements
1540 * All stacks, including top and bottom are covered with a triangle
1544 /* Create index vector */
1547 /* Allocate buffers for indices, bail out if memory allocation fails */
1548 stripIdx = malloc((slices+1)*2*(stacks+1)*sizeof(GLushort)); /*stacks +1 because of closing off bottom */
1552 fgError("Failed to allocate memory in fghCone");
1556 for (j=0, idx=0; j<slices; j++, idx+=2)
1559 stripIdx[idx+1] = j+1; /* 0 is top vertex, 1 is first for first stack */
1561 stripIdx[idx ] = 0; /* repeat first slice's idx for closing off shape */
1562 stripIdx[idx+1] = 1;
1565 /* middle stacks: */
1566 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1567 for (i=0; i<stacks; i++, idx+=2)
1569 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 */
1570 for (j=0; j<slices; j++, idx+=2)
1572 stripIdx[idx ] = offset+j;
1573 stripIdx[idx+1] = offset+j+slices;
1575 stripIdx[idx ] = offset; /* repeat first slice's idx for closing off shape */
1576 stripIdx[idx+1] = offset+slices;
1580 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks+1,(slices+1)*2);
1582 /* cleanup allocated memory */
1586 /* cleanup allocated memory */
1591 static void fghCylinder( double radius, double height, GLint slices, GLint stacks, GLboolean useWireMode )
1594 GLfloat *vertices, *normals;
1596 /* Generate vertices and normals */
1597 /* Note, (stacks+1)*slices vertices for side of object, 2*slices+2 for top and bottom closures */
1598 fghGenerateCylinder((GLfloat)radius,(GLfloat)height,slices,stacks,&vertices,&normals,&nVert);
1601 /* nothing to draw */
1606 GLushort *sliceIdx, *stackIdx;
1607 /* First, generate vertex index arrays for drawing with glDrawElements
1608 * We have a bunch of line_loops to draw for each stack, and a
1609 * bunch for each slice.
1612 stackIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
1613 sliceIdx = malloc(slices*2 *sizeof(GLushort));
1614 if (!(stackIdx) || !(sliceIdx))
1618 fgError("Failed to allocate memory in fghCylinder");
1621 /* generate for each stack */
1622 for (i=0,idx=0; i<stacks+1; i++)
1624 GLushort offset = 1+(i+1)*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1625 for (j=0; j<slices; j++, idx++)
1627 stackIdx[idx] = offset+j;
1631 /* generate for each slice */
1632 for (i=0,idx=0; i<slices; i++)
1634 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1635 sliceIdx[idx++] = offset+slices;
1636 sliceIdx[idx++] = offset+(stacks+1)*slices;
1640 fghDrawGeometryWire(vertices,normals,nVert,
1641 sliceIdx,1,slices*2,GL_LINES,
1642 stackIdx,stacks+1,slices);
1644 /* cleanup allocated memory */
1650 /* First, generate vertex index arrays for drawing with glDrawElements
1651 * All stacks, including top and bottom are covered with a triangle
1655 /* Create index vector */
1658 /* Allocate buffers for indices, bail out if memory allocation fails */
1659 stripIdx = malloc((slices+1)*2*(stacks+2)*sizeof(GLushort)); /*stacks +2 because of closing off bottom and top */
1663 fgError("Failed to allocate memory in fghCylinder");
1667 for (j=0, idx=0; j<slices; j++, idx+=2)
1670 stripIdx[idx+1] = j+1; /* 0 is top vertex, 1 is first for first stack */
1672 stripIdx[idx ] = 0; /* repeat first slice's idx for closing off shape */
1673 stripIdx[idx+1] = 1;
1676 /* middle stacks: */
1677 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1678 for (i=0; i<stacks; i++, idx+=2)
1680 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 */
1681 for (j=0; j<slices; j++, idx+=2)
1683 stripIdx[idx ] = offset+j;
1684 stripIdx[idx+1] = offset+j+slices;
1686 stripIdx[idx ] = offset; /* repeat first slice's idx for closing off shape */
1687 stripIdx[idx+1] = offset+slices;
1691 offset = 1+(stacks+2)*slices;
1692 for (j=0; j<slices; j++, idx+=2)
1694 stripIdx[idx ] = offset+j;
1695 stripIdx[idx+1] = nVert-1; /* zero based index, last element in array (bottom vertex)... */
1697 stripIdx[idx ] = offset;
1698 stripIdx[idx+1] = nVert-1; /* repeat first slice's idx for closing off shape */
1701 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks+2,(slices+1)*2);
1703 /* cleanup allocated memory */
1707 /* cleanup allocated memory */
1712 static void fghTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, GLboolean useWireMode )
1715 GLfloat *vertices, *normals;
1717 /* Generate vertices and normals */
1718 fghGenerateTorus((GLfloat)dInnerRadius,(GLfloat)dOuterRadius,nSides,nRings, &vertices,&normals,&nVert);
1721 /* nothing to draw */
1726 GLushort *sideIdx, *ringIdx;
1727 /* First, generate vertex index arrays for drawing with glDrawElements
1728 * We have a bunch of line_loops to draw each side, and a
1729 * bunch for each ring.
1732 ringIdx = malloc(nRings*nSides*sizeof(GLushort));
1733 sideIdx = malloc(nSides*nRings*sizeof(GLushort));
1734 if (!(ringIdx) || !(sideIdx))
1738 fgError("Failed to allocate memory in fghTorus");
1741 /* generate for each ring */
1742 for( j=0,idx=0; j<nRings; j++ )
1743 for( i=0; i<nSides; i++, idx++ )
1744 ringIdx[idx] = j * nSides + i;
1746 /* generate for each side */
1747 for( i=0,idx=0; i<nSides; i++ )
1748 for( j=0; j<nRings; j++, idx++ )
1749 sideIdx[idx] = j * nSides + i;
1752 fghDrawGeometryWire(vertices,normals,nVert,
1753 ringIdx,nRings,nSides,GL_LINE_LOOP,
1754 sideIdx,nSides,nRings);
1756 /* cleanup allocated memory */
1762 /* First, generate vertex index arrays for drawing with glDrawElements
1763 * All stacks, including top and bottom are covered with a triangle
1768 /* Allocate buffers for indices, bail out if memory allocation fails */
1769 stripIdx = malloc((nRings+1)*2*nSides*sizeof(GLushort));
1773 fgError("Failed to allocate memory in fghTorus");
1776 for( i=0, idx=0; i<nSides; i++ )
1782 for( j=0; j<nRings; j++, idx+=2 )
1784 int offset = j * nSides + i;
1785 stripIdx[idx ] = offset;
1786 stripIdx[idx+1] = offset + ioff;
1788 /* repeat first to close off shape */
1790 stripIdx[idx+1] = i + ioff;
1795 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,nSides,(nRings+1)*2);
1797 /* cleanup allocated memory */
1801 /* cleanup allocated memory */
1807 /* -- INTERFACE FUNCTIONS ---------------------------------------------- */
1811 * Draws a solid sphere
1813 void FGAPIENTRY glutSolidSphere(double radius, GLint slices, GLint stacks)
1815 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" );
1817 fghSphere( radius, slices, stacks, FALSE );
1821 * Draws a wire sphere
1823 void FGAPIENTRY glutWireSphere(double radius, GLint slices, GLint stacks)
1825 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" );
1827 fghSphere( radius, slices, stacks, TRUE );
1832 * Draws a solid cone
1834 void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint stacks )
1836 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" );
1838 fghCone( base, height, slices, stacks, FALSE );
1844 void FGAPIENTRY glutWireCone( double base, double height, GLint slices, GLint stacks)
1846 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" );
1848 fghCone( base, height, slices, stacks, TRUE );
1853 * Draws a solid cylinder
1855 void FGAPIENTRY glutSolidCylinder(double radius, double height, GLint slices, GLint stacks)
1857 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCylinder" );
1859 fghCylinder( radius, height, slices, stacks, FALSE );
1863 * Draws a wire cylinder
1865 void FGAPIENTRY glutWireCylinder(double radius, double height, GLint slices, GLint stacks)
1867 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCylinder" );
1869 fghCylinder( radius, height, slices, stacks, TRUE );
1873 * Draws a wire torus
1875 void FGAPIENTRY glutWireTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings )
1877 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireTorus" );
1879 fghTorus(dInnerRadius, dOuterRadius, nSides, nRings, TRUE);
1883 * Draws a solid torus
1885 void FGAPIENTRY glutSolidTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings )
1887 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidTorus" );
1889 fghTorus(dInnerRadius, dOuterRadius, nSides, nRings, FALSE);
1894 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
1895 /* Macro to generate interface functions */
1896 #define DECLARE_SHAPE_INTERFACE(nameICaps)\
1897 void FGAPIENTRY glutWire##nameICaps( void )\
1899 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWire"#nameICaps );\
1900 fgh##nameICaps( TRUE );\
1902 void FGAPIENTRY glutSolid##nameICaps( void )\
1904 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolid"#nameICaps );\
1905 fgh##nameICaps( FALSE );\
1908 void FGAPIENTRY glutWireCube( double dSize )
1910 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCube" );
1911 fghCube( (GLfloat)dSize, TRUE );
1913 void FGAPIENTRY glutSolidCube( double dSize )
1915 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCube" );
1916 fghCube( (GLfloat)dSize, FALSE );
1919 DECLARE_SHAPE_INTERFACE(Dodecahedron)
1920 DECLARE_SHAPE_INTERFACE(Icosahedron)
1921 DECLARE_SHAPE_INTERFACE(Octahedron)
1922 DECLARE_SHAPE_INTERFACE(RhombicDodecahedron)
1924 void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, double offset[3], double scale )
1926 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSierpinskiSponge" );
1927 fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, TRUE );
1929 void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, double offset[3], double scale )
1931 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSierpinskiSponge" );
1932 fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, FALSE );
1935 DECLARE_SHAPE_INTERFACE(Tetrahedron)
1938 /*** END OF FILE ***/