| OLD | NEW |
| 1 // | 1 // |
| 2 // Book: OpenGL(R) ES 2.0 Programming Guide | 2 // Book: OpenGL(R) ES 2.0 Programming Guide |
| 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner | 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner |
| 4 // ISBN-10: 0321502795 | 4 // ISBN-10: 0321502795 |
| 5 // ISBN-13: 9780321502797 | 5 // ISBN-13: 9780321502797 |
| 6 // Publisher: Addison-Wesley Professional | 6 // Publisher: Addison-Wesley Professional |
| 7 // URLs: http://safari.informit.com/9780321563835 | 7 // URLs: http://safari.informit.com/9780321563835 |
| 8 // http://www.opengles-book.com | 8 // http://www.opengles-book.com |
| 9 // | 9 // |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 /// the results in the arrays. Generate index list for a TRIANGLE_STRIP | 45 /// the results in the arrays. Generate index list for a TRIANGLE_STRIP |
| 46 /// \param numSlices The number of slices in the sphere | 46 /// \param numSlices The number of slices in the sphere |
| 47 /// \param vertices If not NULL, will contain array of float3 positions | 47 /// \param vertices If not NULL, will contain array of float3 positions |
| 48 /// \param normals If not NULL, will contain array of float3 normals | 48 /// \param normals If not NULL, will contain array of float3 normals |
| 49 /// \param texCoords If not NULL, will contain array of float2 texCoords | 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 triang
le strip | 50 /// \param indices If not NULL, will contain the array of indices for the triang
le strip |
| 51 /// \return The number of indices required for rendering the buffers (the number
of indices stored in the indices array | 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 | 52 /// if it is not NULL ) as a GL_TRIANGLE_STRIP |
| 53 // | 53 // |
| 54 int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **nor
mals, | 54 int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **nor
mals, |
| 55 GLfloat **texCoords, GLuint **indices ) | 55 GLfloat **texCoords, GLushort **indices ) |
| 56 { | 56 { |
| 57 int i; | 57 int i; |
| 58 int j; | 58 int j; |
| 59 int numParallels = numSlices / 2; | 59 int numParallels = numSlices / 2; |
| 60 int numVertices = ( numParallels + 1 ) * ( numSlices + 1 ); | 60 int numVertices = ( numParallels + 1 ) * ( numSlices + 1 ); |
| 61 int numIndices = numParallels * numSlices * 6; | 61 int numIndices = numParallels * numSlices * 6; |
| 62 float angleStep = (2.0f * ES_PI) / ((float) numSlices); | 62 float angleStep = (2.0f * ES_PI) / ((float) numSlices); |
| 63 | 63 |
| 64 // Allocate memory for buffers | 64 // Allocate memory for buffers |
| 65 if ( vertices != NULL ) | 65 if ( vertices != NULL ) |
| 66 *vertices = malloc ( sizeof(GLfloat) * 3 * numVertices ); | 66 *vertices = malloc ( sizeof(GLfloat) * 3 * numVertices ); |
| 67 | 67 |
| 68 if ( normals != NULL ) | 68 if ( normals != NULL ) |
| 69 *normals = malloc ( sizeof(GLfloat) * 3 * numVertices ); | 69 *normals = malloc ( sizeof(GLfloat) * 3 * numVertices ); |
| 70 | 70 |
| 71 if ( texCoords != NULL ) | 71 if ( texCoords != NULL ) |
| 72 *texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices ); | 72 *texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices ); |
| 73 | 73 |
| 74 if ( indices != NULL ) | 74 if ( indices != NULL ) |
| 75 *indices = malloc ( sizeof(GLuint) * numIndices ); | 75 *indices = malloc ( sizeof(GLushort) * numIndices ); |
| 76 | 76 |
| 77 for ( i = 0; i < numParallels + 1; i++ ) | 77 for ( i = 0; i < numParallels + 1; i++ ) |
| 78 { | 78 { |
| 79 for ( j = 0; j < numSlices + 1; j++ ) | 79 for ( j = 0; j < numSlices + 1; j++ ) |
| 80 { | 80 { |
| 81 int vertex = ( i * (numSlices + 1) + j ) * 3; | 81 int vertex = ( i * (numSlices + 1) + j ) * 3; |
| 82 | 82 |
| 83 if ( vertices ) | 83 if ( vertices ) |
| 84 { | 84 { |
| 85 (*vertices)[vertex + 0] = radius * sinf ( angleStep * (float)i ) * | 85 (*vertices)[vertex + 0] = radius * sinf ( angleStep * (float)i ) * |
| (...skipping 15 matching lines...) Expand all Loading... |
| 101 int texIndex = ( i * (numSlices + 1) + j ) * 2; | 101 int texIndex = ( i * (numSlices + 1) + j ) * 2; |
| 102 (*texCoords)[texIndex + 0] = (float) j / (float) numSlices; | 102 (*texCoords)[texIndex + 0] = (float) j / (float) numSlices; |
| 103 (*texCoords)[texIndex + 1] = ( 1.0f - (float) i ) / (float) (numPara
llels - 1 ); | 103 (*texCoords)[texIndex + 1] = ( 1.0f - (float) i ) / (float) (numPara
llels - 1 ); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Generate the indices | 108 // Generate the indices |
| 109 if ( indices != NULL ) | 109 if ( indices != NULL ) |
| 110 { | 110 { |
| 111 GLuint *indexBuf = (*indices); | 111 GLushort *indexBuf = (*indices); |
| 112 for ( i = 0; i < numParallels ; i++ ) | 112 for ( i = 0; i < numParallels ; i++ ) |
| 113 { | 113 { |
| 114 for ( j = 0; j < numSlices; j++ ) | 114 for ( j = 0; j < numSlices; j++ ) |
| 115 { | 115 { |
| 116 *indexBuf++ = i * ( numSlices + 1 ) + j; | 116 *indexBuf++ = i * ( numSlices + 1 ) + j; |
| 117 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + j; | 117 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + j; |
| 118 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + ( j + 1 ); | 118 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + ( j + 1 ); |
| 119 | 119 |
| 120 *indexBuf++ = i * ( numSlices + 1 ) + j; | 120 *indexBuf++ = i * ( numSlices + 1 ) + j; |
| 121 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + ( j + 1 ); | 121 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + ( j + 1 ); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 132 /// the results in the arrays. Generate index list for a TRIANGLES | 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. | 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 | 134 /// \param vertices If not NULL, will contain array of float3 positions |
| 135 /// \param normals If not NULL, will contain array of float3 normals | 135 /// \param normals If not NULL, will contain array of float3 normals |
| 136 /// \param texCoords If not NULL, will contain array of float2 texCoords | 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 triang
le strip | 137 /// \param indices If not NULL, will contain the array of indices for the triang
le strip |
| 138 /// \return The number of indices required for rendering the buffers (the number
of indices stored in the indices array | 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 | 139 /// if it is not NULL ) as a GL_TRIANGLE_STRIP |
| 140 // | 140 // |
| 141 int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, | 141 int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, |
| 142 GLfloat **texCoords, GLuint **indices ) | 142 GLfloat **texCoords, GLushort **indices ) |
| 143 { | 143 { |
| 144 int i; | 144 int i; |
| 145 int numVertices = 24; | 145 int numVertices = 24; |
| 146 int numIndices = 36; | 146 int numIndices = 36; |
| 147 | 147 |
| 148 GLfloat cubeVerts[] = | 148 GLfloat cubeVerts[] = |
| 149 { | 149 { |
| 150 -0.5f, -0.5f, -0.5f, | 150 -0.5f, -0.5f, -0.5f, |
| 151 -0.5f, -0.5f, 0.5f, | 151 -0.5f, -0.5f, 0.5f, |
| 152 0.5f, -0.5f, 0.5f, | 152 0.5f, -0.5f, 0.5f, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 if ( texCoords != NULL ) | 249 if ( texCoords != NULL ) |
| 250 { | 250 { |
| 251 *texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices ); | 251 *texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices ); |
| 252 memcpy( *texCoords, cubeTex, sizeof( cubeTex ) ) ; | 252 memcpy( *texCoords, cubeTex, sizeof( cubeTex ) ) ; |
| 253 } | 253 } |
| 254 | 254 |
| 255 | 255 |
| 256 // Generate the indices | 256 // Generate the indices |
| 257 if ( indices != NULL ) | 257 if ( indices != NULL ) |
| 258 { | 258 { |
| 259 GLuint cubeIndices[] = | 259 GLushort cubeIndices[] = |
| 260 { | 260 { |
| 261 0, 2, 1, | 261 0, 2, 1, |
| 262 0, 3, 2, | 262 0, 3, 2, |
| 263 4, 5, 6, | 263 4, 5, 6, |
| 264 4, 6, 7, | 264 4, 6, 7, |
| 265 8, 9, 10, | 265 8, 9, 10, |
| 266 8, 10, 11, | 266 8, 10, 11, |
| 267 12, 15, 14, | 267 12, 15, 14, |
| 268 12, 14, 13, | 268 12, 14, 13, |
| 269 16, 17, 18, | 269 16, 17, 18, |
| 270 16, 18, 19, | 270 16, 18, 19, |
| 271 20, 23, 22, | 271 20, 23, 22, |
| 272 20, 22, 21 | 272 20, 22, 21 |
| 273 }; | 273 }; |
| 274 | 274 |
| 275 *indices = malloc ( sizeof(GLuint) * numIndices ); | 275 *indices = malloc ( sizeof(GLushort) * numIndices ); |
| 276 memcpy( *indices, cubeIndices, sizeof( cubeIndices ) ); | 276 memcpy( *indices, cubeIndices, sizeof( cubeIndices ) ); |
| 277 } | 277 } |
| 278 | 278 |
| 279 return numIndices; | 279 return numIndices; |
| 280 } | 280 } |
| OLD | NEW |