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,GL_UNSIGNED_SHORT,(void*)(long)(i*numVertPerPart));
170 /* Clean existing bindings before clean-up */
171 /* Android showed instability otherwise */
172 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
176 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements2);
177 for (i=0; i<numParts2; i++)
178 glDrawElements(GL_LINE_LOOP,numVertPerPart2,GL_UNSIGNED_SHORT,(void*)(long)(i*numVertPerPart2));
179 /* Clean existing bindings before clean-up */
180 /* Android showed instability otherwise */
181 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
185 fghDisableVertexAttribArray(attribute_v_coord);
186 if (vbo_normals != 0)
187 fghDisableVertexAttribArray(attribute_v_normal);
190 fghDeleteBuffers(1, &vbo_coords);
191 if (vbo_normals != 0)
192 fghDeleteBuffers(1, &vbo_normals);
193 if (ibo_elements != 0)
194 fghDeleteBuffers(1, &ibo_elements);
195 if (ibo_elements2 != 0)
196 fghDeleteBuffers(1, &ibo_elements2);
199 static void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numVertices,
200 GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode,
201 GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2
204 GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
205 GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
207 if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1))
208 /* User requested a 2.0 draw */
209 fghDrawGeometryWire20(vertices, normals, numVertices,
210 vertIdxs, numParts, numVertPerPart, vertexMode,
211 vertIdxs2, numParts2, numVertPerPart2,
212 attribute_v_coord, attribute_v_normal);
213 #ifndef GL_ES_VERSION_2_0
215 fghDrawGeometryWire11(vertices, normals,
216 vertIdxs, numParts, numVertPerPart, vertexMode,
217 vertIdxs2, numParts2, numVertPerPart2);
222 /* Draw the geometric shape with filled triangles
224 * - If the shape is naturally triangulated (numEdgePerFace==3), each
225 * vertex+normal pair is used only once, so no vertex indices.
227 * - If the shape was triangulated (DECOMPOSE_TO_TRIANGLE), some
228 * vertex+normal pairs are reused, so use vertex indices.
231 /* Version for OpenGL (ES) 1.1 */
232 #ifndef GL_ES_VERSION_2_0
233 static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
234 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart)
238 glEnableClientState(GL_VERTEX_ARRAY);
239 glEnableClientState(GL_NORMAL_ARRAY);
241 glVertexPointer(3, GL_FLOAT, 0, vertices);
242 glNormalPointer(GL_FLOAT, 0, normals);
243 if (vertIdxs == NULL)
244 glDrawArrays(GL_TRIANGLES, 0, numVertices);
247 for (i=0; i<numParts; i++)
248 glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_SHORT, vertIdxs+i*numVertIdxsPerPart);
250 glDrawElements(GL_TRIANGLES, numVertIdxsPerPart, GL_UNSIGNED_SHORT, vertIdxs);
252 glDisableClientState(GL_VERTEX_ARRAY);
253 glDisableClientState(GL_NORMAL_ARRAY);
257 /* Version for OpenGL (ES) >= 2.0 */
258 static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
259 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart,
260 GLint attribute_v_coord, GLint attribute_v_normal)
262 GLuint vbo_coords = 0, vbo_normals = 0, ibo_elements = 0;
263 GLsizei numVertIdxs = numParts * numVertIdxsPerPart;
266 if (numVertices > 0 && attribute_v_coord != -1) {
267 fghGenBuffers(1, &vbo_coords);
268 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
269 fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(vertices[0]),
270 vertices, FGH_STATIC_DRAW);
271 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
274 if (numVertices > 0 && attribute_v_normal != -1) {
275 fghGenBuffers(1, &vbo_normals);
276 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
277 fghBufferData(FGH_ARRAY_BUFFER, numVertices * 3 * sizeof(normals[0]),
278 normals, FGH_STATIC_DRAW);
279 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
282 if (vertIdxs != NULL) {
283 fghGenBuffers(1, &ibo_elements);
284 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
285 fghBufferData(FGH_ELEMENT_ARRAY_BUFFER, numVertIdxs * sizeof(vertIdxs[0]),
286 vertIdxs, FGH_STATIC_DRAW);
287 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
291 fghEnableVertexAttribArray(attribute_v_coord);
292 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_coords);
293 fghVertexAttribPointer(
294 attribute_v_coord, /* attribute */
295 3, /* number of elements per vertex, here (x,y,z) */
296 GL_FLOAT, /* the type of each element */
297 GL_FALSE, /* take our values as-is */
298 0, /* no extra data between each position */
299 0 /* offset of first element */
301 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
305 fghEnableVertexAttribArray(attribute_v_normal);
306 fghBindBuffer(FGH_ARRAY_BUFFER, vbo_normals);
307 fghVertexAttribPointer(
308 attribute_v_normal, /* attribute */
309 3, /* number of elements per vertex, here (x,y,z) */
310 GL_FLOAT, /* the type of each element */
311 GL_FALSE, /* take our values as-is */
312 0, /* no extra data between each position */
313 0 /* offset of first element */
315 fghBindBuffer(FGH_ARRAY_BUFFER, 0);
318 if (vertIdxs == NULL) {
319 glDrawArrays(GL_TRIANGLES, 0, numVertices);
321 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, ibo_elements);
323 for (i=0; i<numParts; i++) {
324 glDrawElements(GL_TRIANGLE_STRIP, numVertIdxsPerPart, GL_UNSIGNED_SHORT, (void*)(long)(i*numVertIdxsPerPart));
327 glDrawElements(GL_TRIANGLES, numVertIdxsPerPart, GL_UNSIGNED_SHORT, 0);
329 /* Clean existing bindings before clean-up */
330 /* Android showed instability otherwise */
331 fghBindBuffer(FGH_ELEMENT_ARRAY_BUFFER, 0);
335 fghDisableVertexAttribArray(attribute_v_coord);
336 if (vbo_normals != 0)
337 fghDisableVertexAttribArray(attribute_v_normal);
340 fghDeleteBuffers(1, &vbo_coords);
341 if (vbo_normals != 0)
342 fghDeleteBuffers(1, &vbo_normals);
343 if (ibo_elements != 0)
344 fghDeleteBuffers(1, &ibo_elements);
347 static void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLushort *vertIdxs,
348 GLsizei numVertices, GLsizei numParts, GLsizei numVertIdxsPerPart)
350 GLint attribute_v_coord = fgStructure.CurrentWindow->Window.attribute_v_coord;
351 GLint attribute_v_normal = fgStructure.CurrentWindow->Window.attribute_v_normal;
353 if (fgState.HasOpenGL20 && (attribute_v_coord != -1 || attribute_v_normal != -1))
354 /* User requested a 2.0 draw */
355 fghDrawGeometrySolid20(vertices, normals, vertIdxs,
356 numVertices, numParts, numVertIdxsPerPart,
357 attribute_v_coord, attribute_v_normal);
358 #ifndef GL_ES_VERSION_2_0
360 fghDrawGeometrySolid11(vertices, normals, vertIdxs,
361 numVertices, numParts, numVertIdxsPerPart);
365 /* Shape decomposition to triangles
366 * We'll use glDrawElements to draw all shapes that are not naturally
367 * composed of triangles, so generate an index vector here, using the
368 * below sampling scheme.
369 * Be careful to keep winding of all triangles counter-clockwise,
370 * assuming that input has correct winding...
372 static GLubyte vert4Decomp[6] = {0,1,2, 0,2,3}; /* quad : 4 input vertices, 6 output (2 triangles) */
373 static GLubyte vert5Decomp[9] = {0,1,2, 0,2,4, 4,2,3}; /* pentagon: 5 input vertices, 9 output (3 triangles) */
375 static void fghGenerateGeometryWithIndexArray(int numFaces, int numEdgePerFace, GLfloat *vertices, GLubyte *vertIndices, GLfloat *normals, GLfloat *vertOut, GLfloat *normOut, GLushort *vertIdxOut)
377 int i,j,numEdgeIdxPerFace;
378 GLubyte *vertSamps = NULL;
379 switch (numEdgePerFace)
382 /* nothing to do here, we'll draw with glDrawArrays */
385 vertSamps = vert4Decomp;
386 numEdgeIdxPerFace = 6; /* 6 output vertices for each face */
389 vertSamps = vert5Decomp;
390 numEdgeIdxPerFace = 9; /* 9 output vertices for each face */
394 * Build array with vertices using vertex coordinates and vertex indices
395 * Do same for normals.
396 * Need to do this because of different normals at shared vertices.
398 for (i=0; i<numFaces; i++)
401 int faceIdxVertIdx = i*numEdgePerFace; /* index to first element of "row" in vertex indices */
402 for (j=0; j<numEdgePerFace; j++)
404 int outIdx = i*numEdgePerFace*3+j*3;
405 int vertIdx = vertIndices[faceIdxVertIdx+j]*3;
407 vertOut[outIdx ] = vertices[vertIdx ];
408 vertOut[outIdx+1] = vertices[vertIdx+1];
409 vertOut[outIdx+2] = vertices[vertIdx+2];
411 normOut[outIdx ] = normals [normIdx ];
412 normOut[outIdx+1] = normals [normIdx+1];
413 normOut[outIdx+2] = normals [normIdx+2];
416 /* generate vertex indices for each face */
418 for (j=0; j<numEdgeIdxPerFace; j++)
419 vertIdxOut[i*numEdgeIdxPerFace+j] = faceIdxVertIdx + vertSamps[j];
423 static void fghGenerateGeometry(int numFaces, int numEdgePerFace, GLfloat *vertices, GLubyte *vertIndices, GLfloat *normals, GLfloat *vertOut, GLfloat *normOut)
425 /* This function does the same as fghGenerateGeometryWithIndexArray, just skipping the index array generation... */
426 fghGenerateGeometryWithIndexArray(numFaces, numEdgePerFace, vertices, vertIndices, normals, vertOut, normOut, NULL);
430 /* -- INTERNAL SETUP OF GEOMETRY --------------------------------------- */
431 /* -- stuff that can be cached -- */
432 /* Cache of input to glDrawArrays or glDrawElements
433 * In general, we build arrays with all vertices or normals.
434 * We cant compress this and use glDrawElements as all combinations of
435 * vertices and normals are unique.
437 #define DECLARE_SHAPE_CACHE(name,nameICaps,nameCaps)\
438 static GLboolean name##Cached = FALSE;\
439 static GLfloat name##_verts[nameCaps##_VERT_ELEM_PER_OBJ];\
440 static GLfloat name##_norms[nameCaps##_VERT_ELEM_PER_OBJ];\
441 static void fgh##nameICaps##Generate()\
443 fghGenerateGeometry(nameCaps##_NUM_FACES, nameCaps##_NUM_EDGE_PER_FACE,\
444 name##_v, name##_vi, name##_n,\
445 name##_verts, name##_norms);\
447 #define DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(name,nameICaps,nameCaps)\
448 static GLboolean name##Cached = FALSE;\
449 static GLfloat name##_verts[nameCaps##_VERT_ELEM_PER_OBJ];\
450 static GLfloat name##_norms[nameCaps##_VERT_ELEM_PER_OBJ];\
451 static GLushort name##_vertIdxs[nameCaps##_VERT_PER_OBJ_TRI];\
452 static void fgh##nameICaps##Generate()\
454 fghGenerateGeometryWithIndexArray(nameCaps##_NUM_FACES, nameCaps##_NUM_EDGE_PER_FACE,\
455 name##_v, name##_vi, name##_n,\
456 name##_verts, name##_norms, name##_vertIdxs);\
460 #define CUBE_NUM_VERT 8
461 #define CUBE_NUM_FACES 6
462 #define CUBE_NUM_EDGE_PER_FACE 4
463 #define CUBE_VERT_PER_OBJ (CUBE_NUM_FACES*CUBE_NUM_EDGE_PER_FACE)
464 #define CUBE_VERT_ELEM_PER_OBJ (CUBE_VERT_PER_OBJ*3)
465 #define CUBE_VERT_PER_OBJ_TRI (CUBE_VERT_PER_OBJ+CUBE_NUM_FACES*2) /* 2 extra edges per face when drawing quads as triangles */
466 /* Vertex Coordinates */
467 static GLfloat cube_v[CUBE_NUM_VERT*3] =
479 static GLfloat cube_n[CUBE_NUM_FACES*3] =
489 /* Vertex indices, as quads, before triangulation */
490 static GLubyte cube_vi[CUBE_VERT_PER_OBJ] =
499 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(cube,Cube,CUBE)
501 /* -- Dodecahedron -- */
502 /* Magic Numbers: It is possible to create a dodecahedron by attaching two
503 * pentagons to each face of of a cube. The coordinates of the points are:
504 * (+-x,0, z); (+-1, 1, 1); (0, z, x )
505 * where x = (-1 + sqrt(5))/2, z = (1 + sqrt(5))/2 or
506 * x = 0.61803398875 and z = 1.61803398875.
508 #define DODECAHEDRON_NUM_VERT 20
509 #define DODECAHEDRON_NUM_FACES 12
510 #define DODECAHEDRON_NUM_EDGE_PER_FACE 5
511 #define DODECAHEDRON_VERT_PER_OBJ (DODECAHEDRON_NUM_FACES*DODECAHEDRON_NUM_EDGE_PER_FACE)
512 #define DODECAHEDRON_VERT_ELEM_PER_OBJ (DODECAHEDRON_VERT_PER_OBJ*3)
513 #define DODECAHEDRON_VERT_PER_OBJ_TRI (DODECAHEDRON_VERT_PER_OBJ+DODECAHEDRON_NUM_FACES*4) /* 4 extra edges per face when drawing pentagons as triangles */
514 /* Vertex Coordinates */
515 static GLfloat dodecahedron_v[DODECAHEDRON_NUM_VERT*3] =
517 0.0f, 1.61803398875f, 0.61803398875f,
519 -0.61803398875f, 0.0f, 1.61803398875f,
520 0.61803398875f, 0.0f, 1.61803398875f,
522 0.0f, 1.61803398875f, -0.61803398875f,
524 0.61803398875f, 0.0f, -1.61803398875f,
525 -0.61803398875f, 0.0f, -1.61803398875f,
526 - 1.0f, 1.0f, - 1.0f,
527 0.0f, -1.61803398875f, 0.61803398875f,
529 - 1.0f, - 1.0f, 1.0f,
530 0.0f, -1.61803398875f, -0.61803398875f,
531 - 1.0f, - 1.0f, - 1.0f,
532 1.0f, - 1.0f, - 1.0f,
533 1.61803398875f, -0.61803398875f, 0.0f,
534 1.61803398875f, 0.61803398875f, 0.0f,
535 -1.61803398875f, 0.61803398875f, 0.0f,
536 -1.61803398875f, -0.61803398875f, 0.0f
539 static GLfloat dodecahedron_n[DODECAHEDRON_NUM_FACES*3] =
541 0.0f, 0.525731112119f, 0.850650808354f,
542 0.0f, 0.525731112119f, -0.850650808354f,
543 0.0f, -0.525731112119f, 0.850650808354f,
544 0.0f, -0.525731112119f, -0.850650808354f,
546 0.850650808354f, 0.0f, 0.525731112119f,
547 -0.850650808354f, 0.0f, 0.525731112119f,
548 0.850650808354f, 0.0f, -0.525731112119f,
549 -0.850650808354f, 0.0f, -0.525731112119f,
551 0.525731112119f, 0.850650808354f, 0.0f,
552 0.525731112119f, -0.850650808354f, 0.0f,
553 -0.525731112119f, 0.850650808354f, 0.0f,
554 -0.525731112119f, -0.850650808354f, 0.0f,
558 static GLubyte dodecahedron_vi[DODECAHEDRON_VERT_PER_OBJ] =
575 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON)
578 /* -- Icosahedron -- */
579 #define ICOSAHEDRON_NUM_VERT 12
580 #define ICOSAHEDRON_NUM_FACES 20
581 #define ICOSAHEDRON_NUM_EDGE_PER_FACE 3
582 #define ICOSAHEDRON_VERT_PER_OBJ (ICOSAHEDRON_NUM_FACES*ICOSAHEDRON_NUM_EDGE_PER_FACE)
583 #define ICOSAHEDRON_VERT_ELEM_PER_OBJ (ICOSAHEDRON_VERT_PER_OBJ*3)
584 #define ICOSAHEDRON_VERT_PER_OBJ_TRI ICOSAHEDRON_VERT_PER_OBJ
585 /* Vertex Coordinates */
586 static GLfloat icosahedron_v[ICOSAHEDRON_NUM_VERT*3] =
589 0.447213595500f, 0.894427191000f, 0.0f,
590 0.447213595500f, 0.276393202252f, 0.850650808354f,
591 0.447213595500f, -0.723606797748f, 0.525731112119f,
592 0.447213595500f, -0.723606797748f, -0.525731112119f,
593 0.447213595500f, 0.276393202252f, -0.850650808354f,
594 -0.447213595500f, -0.894427191000f, 0.0f,
595 -0.447213595500f, -0.276393202252f, 0.850650808354f,
596 -0.447213595500f, 0.723606797748f, 0.525731112119f,
597 -0.447213595500f, 0.723606797748f, -0.525731112119f,
598 -0.447213595500f, -0.276393202252f, -0.850650808354f,
602 * 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] ) ;
603 * 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] ) ;
604 * 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] ) ;
606 static GLfloat icosahedron_n[ICOSAHEDRON_NUM_FACES*3] =
608 0.760845213037948f, 0.470228201835026f, 0.341640786498800f,
609 0.760845213036861f, -0.179611190632978f, 0.552786404500000f,
610 0.760845213033849f, -0.581234022404097f, 0.0f,
611 0.760845213036861f, -0.179611190632978f, -0.552786404500000f,
612 0.760845213037948f, 0.470228201835026f, -0.341640786498800f,
613 0.179611190628666f, 0.760845213037948f, 0.552786404498399f,
614 0.179611190634277f, -0.290617011204044f, 0.894427191000000f,
615 0.179611190633958f, -0.940456403667806f, 0.0f,
616 0.179611190634278f, -0.290617011204044f, -0.894427191000000f,
617 0.179611190628666f, 0.760845213037948f, -0.552786404498399f,
618 -0.179611190633958f, 0.940456403667806f, 0.0f,
619 -0.179611190634277f, 0.290617011204044f, 0.894427191000000f,
620 -0.179611190628666f, -0.760845213037948f, 0.552786404498399f,
621 -0.179611190628666f, -0.760845213037948f, -0.552786404498399f,
622 -0.179611190634277f, 0.290617011204044f, -0.894427191000000f,
623 -0.760845213036861f, 0.179611190632978f, -0.552786404500000f,
624 -0.760845213033849f, 0.581234022404097f, 0.0f,
625 -0.760845213036861f, 0.179611190632978f, 0.552786404500000f,
626 -0.760845213037948f, -0.470228201835026f, 0.341640786498800f,
627 -0.760845213037948f, -0.470228201835026f, -0.341640786498800f,
631 static GLubyte icosahedron_vi[ICOSAHEDRON_VERT_PER_OBJ] =
654 DECLARE_SHAPE_CACHE(icosahedron,Icosahedron,ICOSAHEDRON)
656 /* -- Octahedron -- */
657 #define OCTAHEDRON_NUM_VERT 6
658 #define OCTAHEDRON_NUM_FACES 8
659 #define OCTAHEDRON_NUM_EDGE_PER_FACE 3
660 #define OCTAHEDRON_VERT_PER_OBJ (OCTAHEDRON_NUM_FACES*OCTAHEDRON_NUM_EDGE_PER_FACE)
661 #define OCTAHEDRON_VERT_ELEM_PER_OBJ (OCTAHEDRON_VERT_PER_OBJ*3)
662 #define OCTAHEDRON_VERT_PER_OBJ_TRI OCTAHEDRON_VERT_PER_OBJ
664 /* Vertex Coordinates */
665 static GLfloat octahedron_v[OCTAHEDRON_NUM_VERT*3] =
676 static GLfloat octahedron_n[OCTAHEDRON_NUM_FACES*3] =
678 0.577350269189f, 0.577350269189f, 0.577350269189f, /* sqrt(1/3) */
679 0.577350269189f, 0.577350269189f,-0.577350269189f,
680 0.577350269189f,-0.577350269189f, 0.577350269189f,
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
690 static GLubyte octahedron_vi[OCTAHEDRON_VERT_PER_OBJ] =
701 DECLARE_SHAPE_CACHE(octahedron,Octahedron,OCTAHEDRON)
703 /* -- RhombicDodecahedron -- */
704 #define RHOMBICDODECAHEDRON_NUM_VERT 14
705 #define RHOMBICDODECAHEDRON_NUM_FACES 12
706 #define RHOMBICDODECAHEDRON_NUM_EDGE_PER_FACE 4
707 #define RHOMBICDODECAHEDRON_VERT_PER_OBJ (RHOMBICDODECAHEDRON_NUM_FACES*RHOMBICDODECAHEDRON_NUM_EDGE_PER_FACE)
708 #define RHOMBICDODECAHEDRON_VERT_ELEM_PER_OBJ (RHOMBICDODECAHEDRON_VERT_PER_OBJ*3)
709 #define RHOMBICDODECAHEDRON_VERT_PER_OBJ_TRI (RHOMBICDODECAHEDRON_VERT_PER_OBJ+RHOMBICDODECAHEDRON_NUM_FACES*2) /* 2 extra edges per face when drawing quads as triangles */
711 /* Vertex Coordinates */
712 static GLfloat rhombicdodecahedron_v[RHOMBICDODECAHEDRON_NUM_VERT*3] =
715 0.707106781187f, 0.0f, 0.5f,
716 0.0f, 0.707106781187f, 0.5f,
717 -0.707106781187f, 0.0f, 0.5f,
718 0.0f, -0.707106781187f, 0.5f,
719 0.707106781187f, 0.707106781187f, 0.0f,
720 -0.707106781187f, 0.707106781187f, 0.0f,
721 -0.707106781187f, -0.707106781187f, 0.0f,
722 0.707106781187f, -0.707106781187f, 0.0f,
723 0.707106781187f, 0.0f, -0.5f,
724 0.0f, 0.707106781187f, -0.5f,
725 -0.707106781187f, 0.0f, -0.5f,
726 0.0f, -0.707106781187f, -0.5f,
730 static GLfloat rhombicdodecahedron_n[RHOMBICDODECAHEDRON_NUM_FACES*3] =
732 0.353553390594f, 0.353553390594f, 0.5f,
733 -0.353553390594f, 0.353553390594f, 0.5f,
734 -0.353553390594f, -0.353553390594f, 0.5f,
735 0.353553390594f, -0.353553390594f, 0.5f,
740 0.353553390594f, 0.353553390594f, -0.5f,
741 -0.353553390594f, 0.353553390594f, -0.5f,
742 -0.353553390594f, -0.353553390594f, -0.5f,
743 0.353553390594f, -0.353553390594f, -0.5f
747 static GLubyte rhombicdodecahedron_vi[RHOMBICDODECAHEDRON_VERT_PER_OBJ] =
762 DECLARE_SHAPE_CACHE_DECOMPOSE_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON)
764 /* -- Tetrahedron -- */
765 /* Magic Numbers: r0 = ( 1, 0, 0 )
766 * r1 = ( -1/3, 2 sqrt(2) / 3, 0 )
767 * r2 = ( -1/3, - sqrt(2) / 3, sqrt(6) / 3 )
768 * r3 = ( -1/3, - sqrt(2) / 3, -sqrt(6) / 3 )
769 * |r0| = |r1| = |r2| = |r3| = 1
770 * Distance between any two points is 2 sqrt(6) / 3
772 * Normals: The unit normals are simply the negative of the coordinates of the point not on the surface.
774 #define TETRAHEDRON_NUM_VERT 4
775 #define TETRAHEDRON_NUM_FACES 4
776 #define TETRAHEDRON_NUM_EDGE_PER_FACE 3
777 #define TETRAHEDRON_VERT_PER_OBJ (TETRAHEDRON_NUM_FACES*TETRAHEDRON_NUM_EDGE_PER_FACE)
778 #define TETRAHEDRON_VERT_ELEM_PER_OBJ (TETRAHEDRON_VERT_PER_OBJ*3)
779 #define TETRAHEDRON_VERT_PER_OBJ_TRI TETRAHEDRON_VERT_PER_OBJ
781 /* Vertex Coordinates */
782 static GLfloat tetrahedron_v[TETRAHEDRON_NUM_VERT*3] =
785 -0.333333333333f, 0.942809041582f, 0.0f,
786 -0.333333333333f, -0.471404520791f, 0.816496580928f,
787 -0.333333333333f, -0.471404520791f, -0.816496580928f
790 static GLfloat tetrahedron_n[TETRAHEDRON_NUM_FACES*3] =
793 0.333333333333f, -0.942809041582f, 0.0f,
794 0.333333333333f, 0.471404520791f, -0.816496580928f,
795 0.333333333333f, 0.471404520791f, 0.816496580928f
799 static GLubyte tetrahedron_vi[TETRAHEDRON_VERT_PER_OBJ] =
806 DECLARE_SHAPE_CACHE(tetrahedron,Tetrahedron,TETRAHEDRON)
808 /* -- Sierpinski Sponge -- */
809 static unsigned int ipow (int x, unsigned int y)
811 return y==0? 1: y==1? x: (y%2? x: 1) * ipow(x*x, y/2);
814 static void fghSierpinskiSpongeGenerate ( int numLevels, double offset[3], GLfloat scale, GLfloat* vertices, GLfloat* normals )
817 if ( numLevels == 0 )
819 for (i=0; i<TETRAHEDRON_NUM_FACES; i++)
822 int faceIdxVertIdx = i*TETRAHEDRON_NUM_EDGE_PER_FACE;
823 for (j=0; j<TETRAHEDRON_NUM_EDGE_PER_FACE; j++)
825 int outIdx = i*TETRAHEDRON_NUM_EDGE_PER_FACE*3+j*3;
826 int vertIdx = tetrahedron_vi[faceIdxVertIdx+j]*3;
828 vertices[outIdx ] = (GLfloat)offset[0] + scale * tetrahedron_v[vertIdx ];
829 vertices[outIdx+1] = (GLfloat)offset[1] + scale * tetrahedron_v[vertIdx+1];
830 vertices[outIdx+2] = (GLfloat)offset[2] + scale * tetrahedron_v[vertIdx+2];
832 normals [outIdx ] = tetrahedron_n[normIdx ];
833 normals [outIdx+1] = tetrahedron_n[normIdx+1];
834 normals [outIdx+2] = tetrahedron_n[normIdx+2];
838 else if ( numLevels > 0 )
840 double local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */
841 unsigned int stride = ipow(4,--numLevels)*TETRAHEDRON_VERT_ELEM_PER_OBJ;
843 for ( i = 0 ; i < TETRAHEDRON_NUM_FACES ; i++ )
846 local_offset[0] = offset[0] + scale * tetrahedron_v[idx ];
847 local_offset[1] = offset[1] + scale * tetrahedron_v[idx+1];
848 local_offset[2] = offset[2] + scale * tetrahedron_v[idx+2];
849 fghSierpinskiSpongeGenerate ( numLevels, local_offset, scale, vertices+i*stride, normals+i*stride );
854 /* -- Now the various shapes involving circles -- */
856 * Compute lookup table of cos and sin values forming a circle
857 * (or half circle if halfCircle==TRUE)
860 * It is the responsibility of the caller to free these tables
861 * The size of the table is (n+1) to form a connected loop
862 * The last entry is exactly the same as the first
863 * The sign of n can be flipped to get the reverse loop
865 static void fghCircleTable(GLfloat **sint, GLfloat **cost, const int n, const GLboolean halfCircle)
869 /* Table size, the sign of n flips the circle direction */
870 const int size = abs(n);
872 /* Determine the angle between samples */
873 const GLfloat angle = (halfCircle?1:2)*(GLfloat)M_PI/(GLfloat)( ( n == 0 ) ? 1 : n );
875 /* Allocate memory for n samples, plus duplicate of first entry at the end */
876 *sint = malloc(sizeof(GLfloat) * (size+1));
877 *cost = malloc(sizeof(GLfloat) * (size+1));
879 /* Bail out if memory allocation fails, fgError never returns */
880 if (!(*sint) || !(*cost))
884 fgError("Failed to allocate memory in fghCircleTable");
887 /* Compute cos and sin around the circle */
891 for (i=1; i<size; i++)
893 (*sint)[i] = sinf(angle*i);
894 (*cost)[i] = cosf(angle*i);
900 (*sint)[size] = 0.0f; /* sin PI */
901 (*cost)[size] = -1.0f; /* cos PI */
905 /* Last sample is duplicate of the first (sin or cos of 2 PI) */
906 (*sint)[size] = (*sint)[0];
907 (*cost)[size] = (*cost)[0];
911 static void fghGenerateSphere(GLfloat radius, GLint slices, GLint stacks, GLfloat **vertices, GLfloat **normals, int* nVert)
914 int idx = 0; /* idx into vertex/normal buffer */
917 /* Pre-computed circle */
918 GLfloat *sint1,*cost1;
919 GLfloat *sint2,*cost2;
921 /* number of unique vertices */
922 if (slices==0 || stacks<2)
924 /* nothing to generate */
928 *nVert = slices*(stacks-1)+2;
929 if ((*nVert) > 65535) /* TODO: must have a better solution than this low limit, at least for architectures where gluint is available */
930 fgWarning("fghGenerateSphere: too many slices or stacks requested, indices will wrap");
932 /* precompute values on unit circle */
933 fghCircleTable(&sint1,&cost1,-slices,FALSE);
934 fghCircleTable(&sint2,&cost2, stacks,TRUE);
936 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
937 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
938 *normals = malloc((*nVert)*3*sizeof(GLfloat));
939 if (!(*vertices) || !(*normals))
943 fgError("Failed to allocate memory in fghGenerateSphere");
947 (*vertices)[0] = 0.f;
948 (*vertices)[1] = 0.f;
949 (*vertices)[2] = radius;
950 (*normals )[0] = 0.f;
951 (*normals )[1] = 0.f;
952 (*normals )[2] = 1.f;
956 for( i=1; i<stacks; i++ )
958 for(j=0; j<slices; j++, idx+=3)
960 x = cost1[j]*sint2[i];
961 y = sint1[j]*sint2[i];
964 (*vertices)[idx ] = x*radius;
965 (*vertices)[idx+1] = y*radius;
966 (*vertices)[idx+2] = z*radius;
967 (*normals )[idx ] = x;
968 (*normals )[idx+1] = y;
969 (*normals )[idx+2] = z;
974 (*vertices)[idx ] = 0.f;
975 (*vertices)[idx+1] = 0.f;
976 (*vertices)[idx+2] = -radius;
977 (*normals )[idx ] = 0.f;
978 (*normals )[idx+1] = 0.f;
979 (*normals )[idx+2] = -1.f;
981 /* Done creating vertices, release sin and cos tables */
988 void fghGenerateCone(
989 GLfloat base, GLfloat height, GLint slices, GLint stacks, /* input */
990 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
994 int idx = 0; /* idx into vertex/normal buffer */
996 /* Pre-computed circle */
999 /* Step in z and radius as stacks are drawn. */
1001 GLfloat r = (GLfloat)base;
1003 const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
1004 const GLfloat rStep = (GLfloat)base / ( ( stacks > 0 ) ? stacks : 1 );
1006 /* Scaling factors for vertex normals */
1007 const GLfloat cosn = ( (GLfloat)height / sqrtf( height * height + base * base ));
1008 const GLfloat sinn = ( (GLfloat)base / sqrtf( height * height + base * base ));
1012 /* number of unique vertices */
1013 if (slices==0 || stacks<1)
1015 /* nothing to generate */
1019 *nVert = slices*(stacks+2)+1; /* need an extra stack for closing off bottom with correct normals */
1021 if ((*nVert) > 65535)
1022 fgWarning("fghGenerateCone: too many slices or stacks requested, indices will wrap");
1024 /* Pre-computed circle */
1025 fghCircleTable(&sint,&cost,-slices,FALSE);
1027 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1028 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1029 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1030 if (!(*vertices) || !(*normals))
1034 fgError("Failed to allocate memory in fghGenerateSphere");
1038 (*vertices)[0] = 0.f;
1039 (*vertices)[1] = 0.f;
1041 (*normals )[0] = 0.f;
1042 (*normals )[1] = 0.f;
1043 (*normals )[2] = -1.f;
1045 /* other on bottom (get normals right) */
1046 for (j=0; j<slices; j++, idx+=3)
1048 (*vertices)[idx ] = cost[j]*r;
1049 (*vertices)[idx+1] = sint[j]*r;
1050 (*vertices)[idx+2] = z;
1051 (*normals )[idx ] = 0.f;
1052 (*normals )[idx+1] = 0.f;
1053 (*normals )[idx+2] = -1.f;
1057 for (i=0; i<stacks+1; i++ )
1059 for (j=0; j<slices; j++, idx+=3)
1061 (*vertices)[idx ] = cost[j]*r;
1062 (*vertices)[idx+1] = sint[j]*r;
1063 (*vertices)[idx+2] = z;
1064 (*normals )[idx ] = cost[j]*sinn;
1065 (*normals )[idx+1] = sint[j]*sinn;
1066 (*normals )[idx+2] = cosn;
1073 /* Release sin and cos tables */
1078 void fghGenerateCylinder(
1079 GLfloat radius, GLfloat height, GLint slices, GLint stacks, /* input */
1080 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
1084 int idx = 0; /* idx into vertex/normal buffer */
1086 /* Step in z as stacks are drawn. */
1087 GLfloat radf = (GLfloat)radius;
1089 const GLfloat zStep = (GLfloat)height / ( ( stacks > 0 ) ? stacks : 1 );
1091 /* Pre-computed circle */
1092 GLfloat *sint,*cost;
1094 /* number of unique vertices */
1095 if (slices==0 || stacks<1)
1097 /* nothing to generate */
1101 *nVert = slices*(stacks+3)+2; /* need two extra stacks for closing off top and bottom with correct normals */
1103 if ((*nVert) > 65535)
1104 fgWarning("fghGenerateCylinder: too many slices or stacks requested, indices will wrap");
1106 /* Pre-computed circle */
1107 fghCircleTable(&sint,&cost,-slices,FALSE);
1109 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1110 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1111 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1112 if (!(*vertices) || !(*normals))
1116 fgError("Failed to allocate memory in fghGenerateCylinder");
1121 (*vertices)[0] = 0.f;
1122 (*vertices)[1] = 0.f;
1123 (*vertices)[2] = 0.f;
1124 (*normals )[0] = 0.f;
1125 (*normals )[1] = 0.f;
1126 (*normals )[2] = -1.f;
1128 /* other on top (get normals right) */
1129 for (j=0; j<slices; j++, idx+=3)
1131 (*vertices)[idx ] = cost[j]*radf;
1132 (*vertices)[idx+1] = sint[j]*radf;
1133 (*vertices)[idx+2] = z;
1134 (*normals )[idx ] = 0.f;
1135 (*normals )[idx+1] = 0.f;
1136 (*normals )[idx+2] = -1.f;
1140 for (i=0; i<stacks+1; i++ )
1142 for (j=0; j<slices; j++, idx+=3)
1144 (*vertices)[idx ] = cost[j]*radf;
1145 (*vertices)[idx+1] = sint[j]*radf;
1146 (*vertices)[idx+2] = z;
1147 (*normals )[idx ] = cost[j];
1148 (*normals )[idx+1] = sint[j];
1149 (*normals )[idx+2] = 0.f;
1155 /* other on bottom (get normals right) */
1157 for (j=0; j<slices; j++, idx+=3)
1159 (*vertices)[idx ] = cost[j]*radf;
1160 (*vertices)[idx+1] = sint[j]*radf;
1161 (*vertices)[idx+2] = z;
1162 (*normals )[idx ] = 0.f;
1163 (*normals )[idx+1] = 0.f;
1164 (*normals )[idx+2] = 1.f;
1168 (*vertices)[idx ] = 0.f;
1169 (*vertices)[idx+1] = 0.f;
1170 (*vertices)[idx+2] = height;
1171 (*normals )[idx ] = 0.f;
1172 (*normals )[idx+1] = 0.f;
1173 (*normals )[idx+2] = 1.f;
1175 /* Release sin and cos tables */
1180 void fghGenerateTorus(
1181 double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, /* input */
1182 GLfloat **vertices, GLfloat **normals, int* nVert /* output */
1185 GLfloat iradius = (float)dInnerRadius;
1186 GLfloat oradius = (float)dOuterRadius;
1189 /* Pre-computed circle */
1190 GLfloat *spsi, *cpsi;
1191 GLfloat *sphi, *cphi;
1193 /* number of unique vertices */
1194 if (nSides<2 || nRings<2)
1196 /* nothing to generate */
1200 *nVert = nSides * nRings;
1202 if ((*nVert) > 65535)
1203 fgWarning("fghGenerateTorus: too many slices or stacks requested, indices will wrap");
1205 /* precompute values on unit circle */
1206 fghCircleTable(&spsi,&cpsi, nRings,FALSE);
1207 fghCircleTable(&sphi,&cphi,-nSides,FALSE);
1209 /* Allocate vertex and normal buffers, bail out if memory allocation fails */
1210 *vertices = malloc((*nVert)*3*sizeof(GLfloat));
1211 *normals = malloc((*nVert)*3*sizeof(GLfloat));
1212 if (!(*vertices) || !(*normals))
1216 fgError("Failed to allocate memory in fghGenerateTorus");
1219 for( j=0; j<nRings; j++ )
1221 for( i=0; i<nSides; i++ )
1223 int offset = 3 * ( j * nSides + i ) ;
1225 (*vertices)[offset ] = cpsi[j] * ( oradius + cphi[i] * iradius ) ;
1226 (*vertices)[offset+1] = spsi[j] * ( oradius + cphi[i] * iradius ) ;
1227 (*vertices)[offset+2] = sphi[i] * iradius ;
1228 (*normals )[offset ] = cpsi[j] * cphi[i] ;
1229 (*normals )[offset+1] = spsi[j] * cphi[i] ;
1230 (*normals )[offset+2] = sphi[i] ;
1234 /* Release sin and cos tables */
1241 /* -- INTERNAL DRAWING functions --------------------------------------- */
1242 #define _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,vertIdxs)\
1243 static void fgh##nameICaps( GLboolean useWireMode )\
1247 fgh##nameICaps##Generate();\
1248 name##Cached = GL_TRUE;\
1253 fghDrawGeometryWire (name##_verts,name##_norms,nameCaps##_VERT_PER_OBJ, \
1254 NULL,nameCaps##_NUM_FACES,nameCaps##_NUM_EDGE_PER_FACE,GL_LINE_LOOP,\
1259 fghDrawGeometrySolid(name##_verts,name##_norms,vertIdxs,\
1260 nameCaps##_VERT_PER_OBJ, 1, nameCaps##_VERT_PER_OBJ_TRI); \
1263 #define DECLARE_INTERNAL_DRAW(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,NULL)
1264 #define DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(name,nameICaps,nameCaps) _DECLARE_INTERNAL_DRAW_DO_DECLARE(name,nameICaps,nameCaps,name##_vertIdxs)
1266 static void fghCube( GLfloat dSize, GLboolean useWireMode )
1273 cubeCached = GL_TRUE;
1278 /* Need to build new vertex list containing vertices for cube of different size */
1281 vertices = malloc(CUBE_VERT_ELEM_PER_OBJ * sizeof(GLfloat));
1283 /* Bail out if memory allocation fails, fgError never returns */
1287 fgError("Failed to allocate memory in fghCube");
1290 for (i=0; i<CUBE_VERT_ELEM_PER_OBJ; i++)
1291 vertices[i] = dSize*cube_verts[i];
1294 vertices = cube_verts;
1297 fghDrawGeometryWire(vertices, cube_norms, CUBE_VERT_PER_OBJ,
1298 NULL,CUBE_NUM_FACES, CUBE_NUM_EDGE_PER_FACE,GL_LINE_LOOP,
1301 fghDrawGeometrySolid(vertices, cube_norms, cube_vertIdxs,
1302 CUBE_VERT_PER_OBJ, 1, CUBE_VERT_PER_OBJ_TRI);
1305 /* cleanup allocated memory */
1309 DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(dodecahedron,Dodecahedron,DODECAHEDRON)
1310 DECLARE_INTERNAL_DRAW(icosahedron,Icosahedron,ICOSAHEDRON)
1311 DECLARE_INTERNAL_DRAW(octahedron,Octahedron,OCTAHEDRON)
1312 DECLARE_INTERNAL_DRAW_DECOMPOSED_TO_TRIANGLE(rhombicdodecahedron,RhombicDodecahedron,RHOMBICDODECAHEDRON)
1313 DECLARE_INTERNAL_DRAW(tetrahedron,Tetrahedron,TETRAHEDRON)
1315 static void fghSierpinskiSponge ( int numLevels, double offset[3], GLfloat scale, GLboolean useWireMode )
1319 GLsizei numTetr = numLevels<0? 0 : ipow(4,numLevels); /* No sponge for numLevels below 0 */
1320 GLsizei numVert = numTetr*TETRAHEDRON_VERT_PER_OBJ;
1321 GLsizei numFace = numTetr*TETRAHEDRON_NUM_FACES;
1325 /* Allocate memory */
1326 vertices = malloc(numVert*3 * sizeof(GLfloat));
1327 normals = malloc(numVert*3 * sizeof(GLfloat));
1328 /* Bail out if memory allocation fails, fgError never returns */
1329 if (!vertices || !normals)
1333 fgError("Failed to allocate memory in fghSierpinskiSponge");
1336 /* Generate elements */
1337 fghSierpinskiSpongeGenerate ( numLevels, offset, scale, vertices, normals );
1339 /* Draw and cleanup */
1341 fghDrawGeometryWire (vertices,normals,numVert,
1342 NULL,numFace,TETRAHEDRON_NUM_EDGE_PER_FACE,GL_LINE_LOOP,
1345 fghDrawGeometrySolid(vertices,normals,NULL,numVert,1,0);
1353 static void fghSphere( double radius, GLint slices, GLint stacks, GLboolean useWireMode )
1356 GLfloat *vertices, *normals;
1358 /* Generate vertices and normals */
1359 fghGenerateSphere((GLfloat)radius,slices,stacks,&vertices,&normals,&nVert);
1362 /* nothing to draw */
1367 GLushort *sliceIdx, *stackIdx;
1368 /* First, generate vertex index arrays for drawing with glDrawElements
1369 * We have a bunch of line_loops to draw for each stack, and a
1370 * bunch for each slice.
1373 sliceIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
1374 stackIdx = malloc(slices*(stacks-1)*sizeof(GLushort));
1375 if (!(stackIdx) || !(sliceIdx))
1379 fgError("Failed to allocate memory in fghSphere");
1382 /* generate for each stack */
1383 for (i=0,idx=0; i<stacks-1; i++)
1385 GLushort offset = 1+i*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1386 for (j=0; j<slices; j++, idx++)
1388 stackIdx[idx] = offset+j;
1392 /* generate for each slice */
1393 for (i=0,idx=0; i<slices; i++)
1395 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1396 sliceIdx[idx++] = 0; /* vertex on top */
1397 for (j=0; j<stacks-1; j++, idx++)
1399 sliceIdx[idx] = offset+j*slices;
1401 sliceIdx[idx++] = nVert-1; /* zero based index, last element in array... */
1405 fghDrawGeometryWire(vertices,normals,nVert,
1406 sliceIdx,slices,stacks+1,GL_LINE_STRIP,
1407 stackIdx,stacks-1,slices);
1409 /* cleanup allocated memory */
1415 /* First, generate vertex index arrays for drawing with glDrawElements
1416 * All stacks, including top and bottom are covered with a triangle
1420 /* Create index vector */
1423 /* Allocate buffers for indices, bail out if memory allocation fails */
1424 stripIdx = malloc((slices+1)*2*(stacks)*sizeof(GLushort));
1428 fgError("Failed to allocate memory in fghSphere");
1432 for (j=0, idx=0; j<slices; j++, idx+=2)
1434 stripIdx[idx ] = j+1; /* 0 is top vertex, 1 is first for first stack */
1435 stripIdx[idx+1] = 0;
1437 stripIdx[idx ] = 1; /* repeat first slice's idx for closing off shape */
1438 stripIdx[idx+1] = 0;
1441 /* middle stacks: */
1442 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1443 for (i=0; i<stacks-2; i++, idx+=2)
1445 offset = 1+i*slices; /* triangle_strip indices start at 1 (0 is top vertex), and we advance one stack down as we go along */
1446 for (j=0; j<slices; j++, idx+=2)
1448 stripIdx[idx ] = offset+j+slices;
1449 stripIdx[idx+1] = offset+j;
1451 stripIdx[idx ] = offset+slices; /* repeat first slice's idx for closing off shape */
1452 stripIdx[idx+1] = offset;
1456 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 */
1457 for (j=0; j<slices; j++, idx+=2)
1459 stripIdx[idx ] = nVert-1; /* zero based index, last element in array (bottom vertex)... */
1460 stripIdx[idx+1] = offset+j;
1462 stripIdx[idx ] = nVert-1; /* repeat first slice's idx for closing off shape */
1463 stripIdx[idx+1] = offset;
1467 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks,(slices+1)*2);
1469 /* cleanup allocated memory */
1473 /* cleanup allocated memory */
1478 static void fghCone( double base, double height, GLint slices, GLint stacks, GLboolean useWireMode )
1481 GLfloat *vertices, *normals;
1483 /* Generate vertices and normals */
1484 /* Note, (stacks+1)*slices vertices for side of object, slices+1 for top and bottom closures */
1485 fghGenerateCone((GLfloat)base,(GLfloat)height,slices,stacks,&vertices,&normals,&nVert);
1488 /* nothing to draw */
1493 GLushort *sliceIdx, *stackIdx;
1494 /* First, generate vertex index arrays for drawing with glDrawElements
1495 * We have a bunch of line_loops to draw for each stack, and a
1496 * bunch for each slice.
1499 stackIdx = malloc(slices*stacks*sizeof(GLushort));
1500 sliceIdx = malloc(slices*2 *sizeof(GLushort));
1501 if (!(stackIdx) || !(sliceIdx))
1505 fgError("Failed to allocate memory in fghCone");
1508 /* generate for each stack */
1509 for (i=0,idx=0; i<stacks; i++)
1511 GLushort offset = 1+(i+1)*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1512 for (j=0; j<slices; j++, idx++)
1514 stackIdx[idx] = offset+j;
1518 /* generate for each slice */
1519 for (i=0,idx=0; i<slices; i++)
1521 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1522 sliceIdx[idx++] = offset+slices;
1523 sliceIdx[idx++] = offset+(stacks+1)*slices;
1527 fghDrawGeometryWire(vertices,normals,nVert,
1528 sliceIdx,1,slices*2,GL_LINES,
1529 stackIdx,stacks,slices);
1531 /* cleanup allocated memory */
1537 /* First, generate vertex index arrays for drawing with glDrawElements
1538 * All stacks, including top and bottom are covered with a triangle
1542 /* Create index vector */
1545 /* Allocate buffers for indices, bail out if memory allocation fails */
1546 stripIdx = malloc((slices+1)*2*(stacks+1)*sizeof(GLushort)); /*stacks +1 because of closing off bottom */
1550 fgError("Failed to allocate memory in fghCone");
1554 for (j=0, idx=0; j<slices; j++, idx+=2)
1557 stripIdx[idx+1] = j+1; /* 0 is top vertex, 1 is first for first stack */
1559 stripIdx[idx ] = 0; /* repeat first slice's idx for closing off shape */
1560 stripIdx[idx+1] = 1;
1563 /* middle stacks: */
1564 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1565 for (i=0; i<stacks; i++, idx+=2)
1567 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 */
1568 for (j=0; j<slices; j++, idx+=2)
1570 stripIdx[idx ] = offset+j;
1571 stripIdx[idx+1] = offset+j+slices;
1573 stripIdx[idx ] = offset; /* repeat first slice's idx for closing off shape */
1574 stripIdx[idx+1] = offset+slices;
1578 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks+1,(slices+1)*2);
1580 /* cleanup allocated memory */
1584 /* cleanup allocated memory */
1589 static void fghCylinder( double radius, double height, GLint slices, GLint stacks, GLboolean useWireMode )
1592 GLfloat *vertices, *normals;
1594 /* Generate vertices and normals */
1595 /* Note, (stacks+1)*slices vertices for side of object, 2*slices+2 for top and bottom closures */
1596 fghGenerateCylinder((GLfloat)radius,(GLfloat)height,slices,stacks,&vertices,&normals,&nVert);
1599 /* nothing to draw */
1604 GLushort *sliceIdx, *stackIdx;
1605 /* First, generate vertex index arrays for drawing with glDrawElements
1606 * We have a bunch of line_loops to draw for each stack, and a
1607 * bunch for each slice.
1610 stackIdx = malloc(slices*(stacks+1)*sizeof(GLushort));
1611 sliceIdx = malloc(slices*2 *sizeof(GLushort));
1612 if (!(stackIdx) || !(sliceIdx))
1616 fgError("Failed to allocate memory in fghCylinder");
1619 /* generate for each stack */
1620 for (i=0,idx=0; i<stacks+1; i++)
1622 GLushort offset = 1+(i+1)*slices; /* start at 1 (0 is top vertex), and we advance one stack down as we go along */
1623 for (j=0; j<slices; j++, idx++)
1625 stackIdx[idx] = offset+j;
1629 /* generate for each slice */
1630 for (i=0,idx=0; i<slices; i++)
1632 GLushort offset = 1+i; /* start at 1 (0 is top vertex), and we advance one slice as we go along */
1633 sliceIdx[idx++] = offset+slices;
1634 sliceIdx[idx++] = offset+(stacks+1)*slices;
1638 fghDrawGeometryWire(vertices,normals,nVert,
1639 sliceIdx,1,slices*2,GL_LINES,
1640 stackIdx,stacks+1,slices);
1642 /* cleanup allocated memory */
1648 /* First, generate vertex index arrays for drawing with glDrawElements
1649 * All stacks, including top and bottom are covered with a triangle
1653 /* Create index vector */
1656 /* Allocate buffers for indices, bail out if memory allocation fails */
1657 stripIdx = malloc((slices+1)*2*(stacks+2)*sizeof(GLushort)); /*stacks +2 because of closing off bottom and top */
1661 fgError("Failed to allocate memory in fghCylinder");
1665 for (j=0, idx=0; j<slices; j++, idx+=2)
1668 stripIdx[idx+1] = j+1; /* 0 is top vertex, 1 is first for first stack */
1670 stripIdx[idx ] = 0; /* repeat first slice's idx for closing off shape */
1671 stripIdx[idx+1] = 1;
1674 /* middle stacks: */
1675 /* Strip indices are relative to first index belonging to strip, NOT relative to first vertex/normal pair in array */
1676 for (i=0; i<stacks; i++, idx+=2)
1678 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 */
1679 for (j=0; j<slices; j++, idx+=2)
1681 stripIdx[idx ] = offset+j;
1682 stripIdx[idx+1] = offset+j+slices;
1684 stripIdx[idx ] = offset; /* repeat first slice's idx for closing off shape */
1685 stripIdx[idx+1] = offset+slices;
1689 offset = 1+(stacks+2)*slices;
1690 for (j=0; j<slices; j++, idx+=2)
1692 stripIdx[idx ] = offset+j;
1693 stripIdx[idx+1] = nVert-1; /* zero based index, last element in array (bottom vertex)... */
1695 stripIdx[idx ] = offset;
1696 stripIdx[idx+1] = nVert-1; /* repeat first slice's idx for closing off shape */
1699 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,stacks+2,(slices+1)*2);
1701 /* cleanup allocated memory */
1705 /* cleanup allocated memory */
1710 static void fghTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings, GLboolean useWireMode )
1713 GLfloat *vertices, *normals;
1715 /* Generate vertices and normals */
1716 fghGenerateTorus((GLfloat)dInnerRadius,(GLfloat)dOuterRadius,nSides,nRings, &vertices,&normals,&nVert);
1719 /* nothing to draw */
1724 GLushort *sideIdx, *ringIdx;
1725 /* First, generate vertex index arrays for drawing with glDrawElements
1726 * We have a bunch of line_loops to draw each side, and a
1727 * bunch for each ring.
1730 ringIdx = malloc(nRings*nSides*sizeof(GLushort));
1731 sideIdx = malloc(nSides*nRings*sizeof(GLushort));
1732 if (!(ringIdx) || !(sideIdx))
1736 fgError("Failed to allocate memory in fghTorus");
1739 /* generate for each ring */
1740 for( j=0,idx=0; j<nRings; j++ )
1741 for( i=0; i<nSides; i++, idx++ )
1742 ringIdx[idx] = j * nSides + i;
1744 /* generate for each side */
1745 for( i=0,idx=0; i<nSides; i++ )
1746 for( j=0; j<nRings; j++, idx++ )
1747 sideIdx[idx] = j * nSides + i;
1750 fghDrawGeometryWire(vertices,normals,nVert,
1751 ringIdx,nRings,nSides,GL_LINE_LOOP,
1752 sideIdx,nSides,nRings);
1754 /* cleanup allocated memory */
1760 /* First, generate vertex index arrays for drawing with glDrawElements
1761 * All stacks, including top and bottom are covered with a triangle
1766 /* Allocate buffers for indices, bail out if memory allocation fails */
1767 stripIdx = malloc((nRings+1)*2*nSides*sizeof(GLushort));
1771 fgError("Failed to allocate memory in fghTorus");
1774 for( i=0, idx=0; i<nSides; i++ )
1780 for( j=0; j<nRings; j++, idx+=2 )
1782 int offset = j * nSides + i;
1783 stripIdx[idx ] = offset;
1784 stripIdx[idx+1] = offset + ioff;
1786 /* repeat first to close off shape */
1788 stripIdx[idx+1] = i + ioff;
1793 fghDrawGeometrySolid(vertices,normals,stripIdx,nVert,nSides,(nRings+1)*2);
1795 /* cleanup allocated memory */
1799 /* cleanup allocated memory */
1805 /* -- INTERFACE FUNCTIONS ---------------------------------------------- */
1809 * Draws a solid sphere
1811 void FGAPIENTRY glutSolidSphere(double radius, GLint slices, GLint stacks)
1813 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" );
1815 fghSphere( radius, slices, stacks, FALSE );
1819 * Draws a wire sphere
1821 void FGAPIENTRY glutWireSphere(double radius, GLint slices, GLint stacks)
1823 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" );
1825 fghSphere( radius, slices, stacks, TRUE );
1830 * Draws a solid cone
1832 void FGAPIENTRY glutSolidCone( double base, double height, GLint slices, GLint stacks )
1834 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCone" );
1836 fghCone( base, height, slices, stacks, FALSE );
1842 void FGAPIENTRY glutWireCone( double base, double height, GLint slices, GLint stacks)
1844 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCone" );
1846 fghCone( base, height, slices, stacks, TRUE );
1851 * Draws a solid cylinder
1853 void FGAPIENTRY glutSolidCylinder(double radius, double height, GLint slices, GLint stacks)
1855 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCylinder" );
1857 fghCylinder( radius, height, slices, stacks, FALSE );
1861 * Draws a wire cylinder
1863 void FGAPIENTRY glutWireCylinder(double radius, double height, GLint slices, GLint stacks)
1865 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCylinder" );
1867 fghCylinder( radius, height, slices, stacks, TRUE );
1871 * Draws a wire torus
1873 void FGAPIENTRY glutWireTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings )
1875 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireTorus" );
1877 fghTorus(dInnerRadius, dOuterRadius, nSides, nRings, TRUE);
1881 * Draws a solid torus
1883 void FGAPIENTRY glutSolidTorus( double dInnerRadius, double dOuterRadius, GLint nSides, GLint nRings )
1885 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidTorus" );
1887 fghTorus(dInnerRadius, dOuterRadius, nSides, nRings, FALSE);
1892 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
1893 /* Macro to generate interface functions */
1894 #define DECLARE_SHAPE_INTERFACE(nameICaps)\
1895 void FGAPIENTRY glutWire##nameICaps( void )\
1897 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWire"#nameICaps );\
1898 fgh##nameICaps( TRUE );\
1900 void FGAPIENTRY glutSolid##nameICaps( void )\
1902 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolid"#nameICaps );\
1903 fgh##nameICaps( FALSE );\
1906 void FGAPIENTRY glutWireCube( double dSize )
1908 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireCube" );
1909 fghCube( (GLfloat)dSize, TRUE );
1911 void FGAPIENTRY glutSolidCube( double dSize )
1913 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidCube" );
1914 fghCube( (GLfloat)dSize, FALSE );
1917 DECLARE_SHAPE_INTERFACE(Dodecahedron)
1918 DECLARE_SHAPE_INTERFACE(Icosahedron)
1919 DECLARE_SHAPE_INTERFACE(Octahedron)
1920 DECLARE_SHAPE_INTERFACE(RhombicDodecahedron)
1922 void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, double offset[3], double scale )
1924 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSierpinskiSponge" );
1925 fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, TRUE );
1927 void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, double offset[3], double scale )
1929 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSierpinskiSponge" );
1930 fghSierpinskiSponge ( num_levels, offset, (GLfloat)scale, FALSE );
1933 DECLARE_SHAPE_INTERFACE(Tetrahedron)
1936 /*** END OF FILE ***/