Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: third_party/gles_book_examples/Common/Source/esShapes.c

Issue 543002: Renamed gles_book_examples to gles2_book to make it shorter and more correct.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 //
2 // Book: OpenGL(R) ES 2.0 Programming Guide
3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
4 // ISBN-10: 0321502795
5 // ISBN-13: 9780321502797
6 // Publisher: Addison-Wesley Professional
7 // URLs: http://safari.informit.com/9780321563835
8 // http://www.opengles-book.com
9 //
10
11 // ESShapes.c
12 //
13 // Utility functions for generating shapes
14 //
15
16 ///
17 // Includes
18 //
19 #include "esUtil.h"
20 #include <stdlib.h>
21 #include <math.h>
22 #include <string.h>
23
24 ///
25 // Defines
26 //
27 #define ES_PI (3.14159265f)
28
29 //////////////////////////////////////////////////////////////////
30 //
31 // Private Functions
32 //
33 //
34
35
36
37 //////////////////////////////////////////////////////////////////
38 //
39 // Public Functions
40 //
41 //
42
43 //
44 /// \brief Generates geometry for a sphere. Allocates memory for the vertex dat a 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 triang le 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
53 //
54 int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **nor mals,
55 GLfloat **texCoords, GLuint **indices )
56 {
57 int i;
58 int j;
59 int numParallels = numSlices / 2;
60 int numVertices = ( numParallels + 1 ) * ( numSlices + 1 );
61 int numIndices = numParallels * numSlices * 6;
62 float angleStep = (2.0f * ES_PI) / ((float) numSlices);
63
64 // Allocate memory for buffers
65 if ( vertices != NULL )
66 *vertices = malloc ( sizeof(GLfloat) * 3 * numVertices );
67
68 if ( normals != NULL )
69 *normals = malloc ( sizeof(GLfloat) * 3 * numVertices );
70
71 if ( texCoords != NULL )
72 *texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices );
73
74 if ( indices != NULL )
75 *indices = malloc ( sizeof(GLuint) * numIndices );
76
77 for ( i = 0; i < numParallels + 1; i++ )
78 {
79 for ( j = 0; j < numSlices + 1; j++ )
80 {
81 int vertex = ( i * (numSlices + 1) + j ) * 3;
82
83 if ( vertices )
84 {
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 );
90 }
91
92 if ( normals )
93 {
94 (*normals)[vertex + 0] = (*vertices)[vertex + 0] / radius;
95 (*normals)[vertex + 1] = (*vertices)[vertex + 1] / radius;
96 (*normals)[vertex + 2] = (*vertices)[vertex + 2] / radius;
97 }
98
99 if ( texCoords )
100 {
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) (numPara llels - 1 );
104 }
105 }
106 }
107
108 // Generate the indices
109 if ( indices != NULL )
110 {
111 GLuint *indexBuf = (*indices);
112 for ( i = 0; i < numParallels ; i++ )
113 {
114 for ( j = 0; j < numSlices; j++ )
115 {
116 *indexBuf++ = i * ( numSlices + 1 ) + j;
117 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + j;
118 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + ( j + 1 );
119
120 *indexBuf++ = i * ( numSlices + 1 ) + j;
121 *indexBuf++ = ( i + 1 ) * ( numSlices + 1 ) + ( j + 1 );
122 *indexBuf++ = i * ( numSlices + 1 ) + ( j + 1 );
123 }
124 }
125 }
126
127 return numIndices;
128 }
129
130 //
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 triang le 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
140 //
141 int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals,
142 GLfloat **texCoords, GLuint **indices )
143 {
144 int i;
145 int numVertices = 24;
146 int numIndices = 36;
147
148 GLfloat cubeVerts[] =
149 {
150 -0.5f, -0.5f, -0.5f,
151 -0.5f, -0.5f, 0.5f,
152 0.5f, -0.5f, 0.5f,
153 0.5f, -0.5f, -0.5f,
154 -0.5f, 0.5f, -0.5f,
155 -0.5f, 0.5f, 0.5f,
156 0.5f, 0.5f, 0.5f,
157 0.5f, 0.5f, -0.5f,
158 -0.5f, -0.5f, -0.5f,
159 -0.5f, 0.5f, -0.5f,
160 0.5f, 0.5f, -0.5f,
161 0.5f, -0.5f, -0.5f,
162 -0.5f, -0.5f, 0.5f,
163 -0.5f, 0.5f, 0.5f,
164 0.5f, 0.5f, 0.5f,
165 0.5f, -0.5f, 0.5f,
166 -0.5f, -0.5f, -0.5f,
167 -0.5f, -0.5f, 0.5f,
168 -0.5f, 0.5f, 0.5f,
169 -0.5f, 0.5f, -0.5f,
170 0.5f, -0.5f, -0.5f,
171 0.5f, -0.5f, 0.5f,
172 0.5f, 0.5f, 0.5f,
173 0.5f, 0.5f, -0.5f,
174 };
175
176 GLfloat cubeNormals[] =
177 {
178 0.0f, -1.0f, 0.0f,
179 0.0f, -1.0f, 0.0f,
180 0.0f, -1.0f, 0.0f,
181 0.0f, -1.0f, 0.0f,
182 0.0f, 1.0f, 0.0f,
183 0.0f, 1.0f, 0.0f,
184 0.0f, 1.0f, 0.0f,
185 0.0f, 1.0f, 0.0f,
186 0.0f, 0.0f, -1.0f,
187 0.0f, 0.0f, -1.0f,
188 0.0f, 0.0f, -1.0f,
189 0.0f, 0.0f, -1.0f,
190 0.0f, 0.0f, 1.0f,
191 0.0f, 0.0f, 1.0f,
192 0.0f, 0.0f, 1.0f,
193 0.0f, 0.0f, 1.0f,
194 -1.0f, 0.0f, 0.0f,
195 -1.0f, 0.0f, 0.0f,
196 -1.0f, 0.0f, 0.0f,
197 -1.0f, 0.0f, 0.0f,
198 1.0f, 0.0f, 0.0f,
199 1.0f, 0.0f, 0.0f,
200 1.0f, 0.0f, 0.0f,
201 1.0f, 0.0f, 0.0f,
202 };
203
204 GLfloat cubeTex[] =
205 {
206 0.0f, 0.0f,
207 0.0f, 1.0f,
208 1.0f, 1.0f,
209 1.0f, 0.0f,
210 1.0f, 0.0f,
211 1.0f, 1.0f,
212 0.0f, 1.0f,
213 0.0f, 0.0f,
214 0.0f, 0.0f,
215 0.0f, 1.0f,
216 1.0f, 1.0f,
217 1.0f, 0.0f,
218 0.0f, 0.0f,
219 0.0f, 1.0f,
220 1.0f, 1.0f,
221 1.0f, 0.0f,
222 0.0f, 0.0f,
223 0.0f, 1.0f,
224 1.0f, 1.0f,
225 1.0f, 0.0f,
226 0.0f, 0.0f,
227 0.0f, 1.0f,
228 1.0f, 1.0f,
229 1.0f, 0.0f,
230 };
231
232 // Allocate memory for buffers
233 if ( vertices != NULL )
234 {
235 *vertices = malloc ( sizeof(GLfloat) * 3 * numVertices );
236 memcpy( *vertices, cubeVerts, sizeof( cubeVerts ) );
237 for ( i = 0; i < numVertices; i++ )
238 {
239 (*vertices)[i] *= scale;
240 }
241 }
242
243 if ( normals != NULL )
244 {
245 *normals = malloc ( sizeof(GLfloat) * 3 * numVertices );
246 memcpy( *normals, cubeNormals, sizeof( cubeNormals ) );
247 }
248
249 if ( texCoords != NULL )
250 {
251 *texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices );
252 memcpy( *texCoords, cubeTex, sizeof( cubeTex ) ) ;
253 }
254
255
256 // Generate the indices
257 if ( indices != NULL )
258 {
259 GLuint cubeIndices[] =
260 {
261 0, 2, 1,
262 0, 3, 2,
263 4, 5, 6,
264 4, 6, 7,
265 8, 9, 10,
266 8, 10, 11,
267 12, 15, 14,
268 12, 14, 13,
269 16, 17, 18,
270 16, 18, 19,
271 20, 23, 22,
272 20, 22, 21
273 };
274
275 *indices = malloc ( sizeof(GLuint) * numIndices );
276 memcpy( *indices, cubeIndices, sizeof( cubeIndices ) );
277 }
278
279 return numIndices;
280 }
OLDNEW
« no previous file with comments | « third_party/gles_book_examples/Common/Source/esShader.c ('k') | third_party/gles_book_examples/Common/Source/esTransform.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698