2
// Book: OpenGL(R) ES 2.0 Programming Guide
3
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
5
// ISBN-13: 9780321502797
6
// Publisher: Addison-Wesley Professional
7
// URLs: http://safari.informit.com/9780321563835
8
// http://www.opengles-book.com
13
// Utility functions for generating shapes
27
#define ES_PI (3.14159265f)
29
//////////////////////////////////////////////////////////////////
37
//////////////////////////////////////////////////////////////////
44
/// \brief Generates geometry for a sphere. Allocates memory for the vertex data and stores
45
/// the results in the arrays. Generate index list for a TRIANGLE_STRIP
46
/// \param numSlices The number of slices in the sphere
47
/// \param vertices If not NULL, will contain array of float3 positions
48
/// \param normals If not NULL, will contain array of float3 normals
49
/// \param texCoords If not NULL, will contain array of float2 texCoords
50
/// \param indices If not NULL, will contain the array of indices for the triangle strip
51
/// \return The number of indices required for rendering the buffers (the number of indices stored in the indices array
52
/// if it is not NULL ) as a GL_TRIANGLE_STRIP
54
int ESUTIL_API esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **normals,
55
GLfloat **texCoords, GLushort **indices )
59
int numParallels = numSlices;
60
int numVertices = ( numParallels + 1 ) * ( numSlices + 1 );
61
int numIndices = numParallels * numSlices * 6;
62
float angleStep = (2.0f * ES_PI) / ((float) numSlices);
64
// Allocate memory for buffers
65
if ( vertices != NULL )
66
*vertices = malloc ( sizeof(GLfloat) * 3 * numVertices );
68
if ( normals != NULL )
69
*normals = malloc ( sizeof(GLfloat) * 3 * numVertices );
71
if ( texCoords != NULL )
72
*texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices );
74
if ( indices != NULL )
75
*indices = malloc ( sizeof(GLushort) * numIndices );
77
for ( i = 0; i < numParallels + 1; i++ )
79
for ( j = 0; j < numSlices + 1; j++ )
81
int vertex = ( i * (numSlices + 1) + j ) * 3;
85
(*vertices)[vertex + 0] = radius * sinf ( angleStep * (float)i ) *
86
sinf ( angleStep * (float)j );
87
(*vertices)[vertex + 1] = radius * cosf ( angleStep * (float)i );
88
(*vertices)[vertex + 2] = radius * sinf ( angleStep * (float)i ) *
89
cosf ( angleStep * (float)j );
94
(*normals)[vertex + 0] = (*vertices)[vertex + 0] / radius;
95
(*normals)[vertex + 1] = (*vertices)[vertex + 1] / radius;
96
(*normals)[vertex + 2] = (*vertices)[vertex + 2] / radius;
101
int texIndex = ( i * (numSlices + 1) + j ) * 2;
102
(*texCoords)[texIndex + 0] = (float) j / (float) numSlices;
103
(*texCoords)[texIndex + 1] = ( 1.0f - (float) i ) / (float) (numParallels - 1 );
108
// Generate the indices
109
if ( indices != NULL )
111
GLushort *indexBuf = (*indices);
112
for ( i = 0; i < numParallels ; i++ )
114
for ( j = 0; j < numSlices; j++ )
116
*indexBuf++ = i * ( numSlices + 1 ) + j;
117
*indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + j;
118
*indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + ( j + 1 );
120
*indexBuf++ = i * ( numSlices + 1 ) + j;
121
*indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + ( j + 1 );
122
*indexBuf++ = i * ( numSlices + 1 ) + ( j + 1 );
131
/// \brief Generates geometry for a cube. Allocates memory for the vertex data and stores
132
/// the results in the arrays. Generate index list for a TRIANGLES
133
/// \param scale The size of the cube, use 1.0 for a unit cube.
134
/// \param vertices If not NULL, will contain array of float3 positions
135
/// \param normals If not NULL, will contain array of float3 normals
136
/// \param texCoords If not NULL, will contain array of float2 texCoords
137
/// \param indices If not NULL, will contain the array of indices for the triangle strip
138
/// \return The number of indices required for rendering the buffers (the number of indices stored in the indices array
139
/// if it is not NULL ) as a GL_TRIANGLE_STRIP
141
int ESUTIL_API esGenCube ( float scale, GLfloat **vertices, GLfloat **normals,
142
GLfloat **texCoords, GLushort **indices )
145
int numVertices = 24;
148
GLfloat cubeVerts[] =
176
GLfloat cubeNormals[] =
232
// Allocate memory for buffers
233
if ( vertices != NULL )
235
*vertices = malloc ( sizeof(GLfloat) * 3 * numVertices );
236
memcpy( *vertices, cubeVerts, sizeof( cubeVerts ) );
237
for ( i = 0; i < numVertices * 3; i++ )
239
(*vertices)[i] *= scale;
243
if ( normals != NULL )
245
*normals = malloc ( sizeof(GLfloat) * 3 * numVertices );
246
memcpy( *normals, cubeNormals, sizeof( cubeNormals ) );
249
if ( texCoords != NULL )
251
*texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices );
252
memcpy( *texCoords, cubeTex, sizeof( cubeTex ) ) ;
256
// Generate the indices
257
if ( indices != NULL )
259
GLushort cubeIndices[] =
275
*indices = malloc ( sizeof(GLushort) * numIndices );
276
memcpy( *indices, cubeIndices, sizeof( cubeIndices ) );