1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
* Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
*
* Based on eglgears by
* Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <GLES/gl.h>
#include "winsys.h"
#ifndef M_PI
#define M_PI 3.14159265
#endif
struct gear {
GLuint vbo;
GLfloat *vertices;
GLsizei stride;
GLint num_teeth;
};
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
static struct gear gears[3];
static GLfloat angle = 0.0;
/*
* Initialize a gear wheel.
*
* Input: gear - gear to initialize
* inner_radius - radius of hole at center
* outer_radius - radius at center of teeth
* width - width of gear
* teeth - number of teeth
* tooth_depth - depth of tooth
*/
static void
init_gear(struct gear *gear, GLfloat inner_radius, GLfloat outer_radius,
GLfloat width, GLint teeth, GLfloat tooth_depth)
{
GLfloat r0, r1, r2;
GLfloat a0, da;
GLint verts_per_tooth, total_verts, total_size;
GLint count, i;
GLfloat *verts;
r0 = inner_radius;
r1 = outer_radius - tooth_depth / 2.0;
r2 = outer_radius + tooth_depth / 2.0;
a0 = 2.0 * M_PI / teeth;
da = a0 / 4.0;
gear->vbo = 0;
gear->vertices = NULL;
gear->stride = sizeof(GLfloat) * 6; /* XYZ + normal */
gear->num_teeth = teeth;
verts_per_tooth = 10 + 4;
total_verts = teeth * verts_per_tooth;
total_size = total_verts * gear->stride;
verts = malloc(total_size);
if (!verts) {
printf("failed to allocate vertices\n");
return;
}
#define GEAR_VERT(r, n, sign) \
do { \
verts[count * 6 + 0] = (r) * vx[n]; \
verts[count * 6 + 1] = (r) * vy[n]; \
verts[count * 6 + 2] = (sign) * width * 0.5; \
verts[count * 6 + 3] = normal[0]; \
verts[count * 6 + 4] = normal[1]; \
verts[count * 6 + 5] = normal[2]; \
count++; \
} while (0)
count = 0;
for (i = 0; i < teeth; i++) {
GLfloat normal[3];
GLfloat vx[5], vy[5];
GLfloat u, v;
normal[0] = 0.0;
normal[1] = 0.0;
normal[2] = 0.0;
vx[0] = cos(i * a0 + 0 * da);
vy[0] = sin(i * a0 + 0 * da);
vx[1] = cos(i * a0 + 1 * da);
vy[1] = sin(i * a0 + 1 * da);
vx[2] = cos(i * a0 + 2 * da);
vy[2] = sin(i * a0 + 2 * da);
vx[3] = cos(i * a0 + 3 * da);
vy[3] = sin(i * a0 + 3 * da);
vx[4] = cos(i * a0 + 4 * da);
vy[4] = sin(i * a0 + 4 * da);
/* outward faces of a tooth, 10 verts */
normal[0] = vx[0];
normal[1] = vy[0];
GEAR_VERT(r1, 0, 1);
GEAR_VERT(r1, 0, -1);
u = r2 * vx[1] - r1 * vx[0];
v = r2 * vy[1] - r1 * vy[0];
normal[0] = v;
normal[1] = -u;
GEAR_VERT(r2, 1, 1);
GEAR_VERT(r2, 1, -1);
normal[0] = vx[0];
normal[1] = vy[0];
GEAR_VERT(r2, 2, 1);
GEAR_VERT(r2, 2, -1);
u = r1 * vx[3] - r2 * vx[2];
v = r1 * vy[3] - r2 * vy[2];
normal[0] = v;
normal[1] = -u;
GEAR_VERT(r1, 3, 1);
GEAR_VERT(r1, 3, -1);
normal[0] = vx[0];
normal[1] = vy[0];
GEAR_VERT(r1, 4, 1);
GEAR_VERT(r1, 4, -1);
/* inside radius cylinder, 4 verts */
normal[0] = -vx[4];
normal[1] = -vy[4];
GEAR_VERT(r0, 4, 1);
GEAR_VERT(r0, 4, -1);
normal[0] = -vx[0];
normal[1] = -vy[0];
GEAR_VERT(r0, 0, 1);
GEAR_VERT(r0, 0, -1);
assert(count % verts_per_tooth == 0);
}
assert(count == total_verts);
#undef GEAR_VERT
gear->vertices = verts;
/* setup VBO */
glGenBuffers(1, &gear->vbo);
if (gear->vbo) {
glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
glBufferData(GL_ARRAY_BUFFER, total_size, verts, GL_STATIC_DRAW);
}
}
static void
draw_gear(const struct gear *gear)
{
GLint i;
if (!gear->vbo && !gear->vertices) {
printf("nothing to be drawn\n");
return;
}
if (gear->vbo) {
glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
glVertexPointer(3, GL_FLOAT, gear->stride, (const GLvoid *) 0);
glNormalPointer(GL_FLOAT, gear->stride, (const GLvoid *) (sizeof(GLfloat) * 3));
} else {
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexPointer(3, GL_FLOAT, gear->stride, gear->vertices);
glNormalPointer(GL_FLOAT, gear->stride, gear->vertices + 3);
}
glEnableClientState(GL_VERTEX_ARRAY);
for (i = 0; i < gear->num_teeth; i++) {
const GLint base = (10 + 4) * i;
GLushort indices[7];
glShadeModel(GL_FLAT);
/* front face */
indices[0] = base + 12;
indices[1] = base + 0;
indices[2] = base + 2;
indices[3] = base + 4;
indices[4] = base + 6;
indices[5] = base + 8;
indices[6] = base + 10;
glNormal3f(0.0, 0.0, 1.0);
glDrawElements(GL_TRIANGLE_FAN, 7, GL_UNSIGNED_SHORT, indices);
/* back face */
indices[0] = base + 13;
indices[1] = base + 11;
indices[2] = base + 9;
indices[3] = base + 7;
indices[4] = base + 5;
indices[5] = base + 3;
indices[6] = base + 1;
glNormal3f(0.0, 0.0, -1.0);
glDrawElements(GL_TRIANGLE_FAN, 7, GL_UNSIGNED_SHORT, indices);
glEnableClientState(GL_NORMAL_ARRAY);
/* outward face of a tooth */
glDrawArrays(GL_TRIANGLE_STRIP, base, 10);
/* inside radius cylinder */
glShadeModel(GL_SMOOTH);
glDrawArrays(GL_TRIANGLE_STRIP, base + 10, 4);
glDisableClientState(GL_NORMAL_ARRAY);
}
glDisableClientState(GL_VERTEX_ARRAY);
}
static void
gears_draw(void *data)
{
static const GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
static const GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
static const GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(-3.0, -2.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
draw_gear(&gears[0]);
glPopMatrix();
glPushMatrix();
glTranslatef(3.1, -2.0, 0.0);
glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green);
draw_gear(&gears[1]);
glPopMatrix();
glPushMatrix();
glTranslatef(-3.1, 4.2, 0.0);
glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
draw_gear(&gears[2]);
glPopMatrix();
glPopMatrix();
/* advance rotation for next frame */
angle += 0.5; /* 0.5 degree per frame */
if (angle > 3600.0)
angle -= 3600.0;
}
static void gears_fini(void)
{
GLint i;
for (i = 0; i < 3; i++) {
struct gear *gear = &gears[i];
if (gear->vbo) {
glDeleteBuffers(1, &gear->vbo);
gear->vbo = 0;
}
if (gear->vertices) {
free(gear->vertices);
gear->vertices = NULL;
}
}
}
static void gears_init(void)
{
static const GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
init_gear(&gears[0], 1.0, 4.0, 1.0, 20, 0.7);
init_gear(&gears[1], 0.5, 2.0, 2.0, 10, 0.7);
init_gear(&gears[2], 1.3, 2.0, 0.5, 10, 0.7);
}
/* new window size or exposure */
static void
gears_reshape(int width, int height)
{
GLfloat h = (GLfloat) height / (GLfloat) width;
glViewport(0, 0, (GLint) width, (GLint) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-1.0, 1.0, -h, h, 5.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -40.0);
}
static void gears_run(void)
{
winsysRun(5.0, gears_draw, NULL);
}
int
main(int argc, char *argv[])
{
EGLint width, height;
if (!winsysInitScreen())
exit(1);
winsysQueryScreenSize(&width, &height);
gears_init();
gears_reshape(width, height);
gears_run();
gears_fini();
winsysFiniScreen();
return 0;
}
|