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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/*
* 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.
*/
/*
* Ported to GLES2.
* Kristian Høgsberg <krh@bitplanet.net>
* May 3, 2010
*/
/*
* Command line options:
* -info print GL implementation information
*
*/
#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include "eglut.h"
struct gear {
GLfloat *vertices;
GLuint vbo;
int count;
};
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
static struct gear *gear1, *gear2, *gear3;
static GLfloat angle = 0.0;
static GLuint proj_location, light_location, color_location;
static GLfloat proj[16];
static GLfloat *
vert(GLfloat *p, GLfloat x, GLfloat y, GLfloat z, GLfloat *n)
{
p[0] = x;
p[1] = y;
p[2] = z;
p[3] = n[0];
p[4] = n[1];
p[5] = n[2];
return p + 6;
}
/* Draw a gear wheel. You'll probably want to call this function when
* building a display list since we do a lot of trig here.
*
* Input: 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 struct gear *
gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
GLint teeth, GLfloat tooth_depth)
{
GLint i;
GLfloat r0, r1, r2;
GLfloat da;
GLfloat *p, *v;
struct gear *gear;
double s[5], c[5];
GLfloat verts[3 * 14], normal[3];
const int tris_per_tooth = 20;
gear = malloc(sizeof *gear);
if (gear == NULL)
return NULL;
r0 = inner_radius;
r1 = outer_radius - tooth_depth / 2.0;
r2 = outer_radius + tooth_depth / 2.0;
da = 2.0 * M_PI / teeth / 4.0;
gear->vertices = calloc(teeth * tris_per_tooth * 3 * 6,
sizeof *gear->vertices);
s[4] = 0;
c[4] = 1;
v = gear->vertices;
for (i = 0; i < teeth; i++) {
s[0] = s[4];
c[0] = c[4];
sincos(i * 2.0 * M_PI / teeth + da, &s[1], &c[1]);
sincos(i * 2.0 * M_PI / teeth + da * 2, &s[2], &c[2]);
sincos(i * 2.0 * M_PI / teeth + da * 3, &s[3], &c[3]);
sincos(i * 2.0 * M_PI / teeth + da * 4, &s[4], &c[4]);
normal[0] = 0.0;
normal[1] = 0.0;
normal[2] = 1.0;
v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
v = vert(v, r2 * c[2], r2 * s[2], width * 0.5, normal);
v = vert(v, r1 * c[0], r1 * s[0], width * 0.5, normal);
v = vert(v, r1 * c[3], r1 * s[3], width * 0.5, normal);
v = vert(v, r0 * c[0], r0 * s[0], width * 0.5, normal);
v = vert(v, r1 * c[4], r1 * s[4], width * 0.5, normal);
v = vert(v, r0 * c[4], r0 * s[4], width * 0.5, normal);
v = vert(v, r0 * c[4], r0 * s[4], width * 0.5, normal);
v = vert(v, r0 * c[0], r0 * s[0], width * 0.5, normal);
v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
v = vert(v, r0 * c[0], r0 * s[0], -width * 0.5, normal);
normal[0] = 0.0;
normal[1] = 0.0;
normal[2] = -1.0;
v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
v = vert(v, r0 * c[0], r0 * s[0], -width * 0.5, normal);
v = vert(v, r1 * c[3], r1 * s[3], -width * 0.5, normal);
v = vert(v, r1 * c[0], r1 * s[0], -width * 0.5, normal);
v = vert(v, r2 * c[2], r2 * s[2], -width * 0.5, normal);
v = vert(v, r2 * c[1], r2 * s[1], -width * 0.5, normal);
v = vert(v, r1 * c[0], r1 * s[0], width * 0.5, normal);
v = vert(v, r1 * c[0], r1 * s[0], width * 0.5, normal);
v = vert(v, r1 * c[0], r1 * s[0], -width * 0.5, normal);
v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
v = vert(v, r2 * c[1], r2 * s[1], -width * 0.5, normal);
v = vert(v, r2 * c[2], r2 * s[2], width * 0.5, normal);
v = vert(v, r2 * c[2], r2 * s[2], -width * 0.5, normal);
v = vert(v, r1 * c[3], r1 * s[3], width * 0.5, normal);
v = vert(v, r1 * c[3], r1 * s[3], -width * 0.5, normal);
v = vert(v, r1 * c[4], r1 * s[4], width * 0.5, normal);
v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
}
gear->count = (v - gear->vertices) / 6;
glGenBuffers(1, &gear->vbo);
glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
glBufferData(GL_ARRAY_BUFFER, gear->count * 6 * 4,
gear->vertices, GL_STATIC_DRAW);
return gear;
}
static void
multiply(GLfloat *m, const GLfloat *n)
{
GLfloat tmp[16];
const GLfloat *row, *column;
div_t d;
int i, j;
for (i = 0; i < 16; i++) {
tmp[i] = 0;
d = div(i, 4);
row = n + d.quot * 4;
column = m + d.rem;
for (j = 0; j < 4; j++)
tmp[i] += row[j] * column[j * 4];
}
memcpy(m, &tmp, sizeof tmp);
}
static void
rotate(GLfloat *m, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
{
double s, c;
sincos(angle, &s, &c);
GLfloat r[16] = {
x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
0, 0, 0, 1
};
multiply(m, r);
}
static void
translate(GLfloat *m, GLfloat x, GLfloat y, GLfloat z)
{
GLfloat t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 };
multiply(m, t);
}
static const GLfloat light[3] = { 1.0, 1.0, -1.0 };
static void
draw_gear(struct gear *gear, GLfloat *m,
GLfloat x, GLfloat y, GLfloat angle, const GLfloat *color)
{
GLfloat tmp[16];
memcpy(tmp, m, sizeof tmp);
translate(tmp, x, y, 0);
rotate(tmp, 2 * M_PI * angle / 360.0, 0, 0, 1);
glUniformMatrix4fv(proj_location, 1, GL_FALSE, tmp);
glUniform3fv(light_location, 1, light);
glUniform4fv(color_location, 1, color);
glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
6 * sizeof(GLfloat), NULL);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
6 * sizeof(GLfloat), (GLfloat *) 0 + 3);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, gear->count);
}
static void
gears_draw(void)
{
const static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
const static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
const static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
GLfloat m[16];
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
memcpy(m, proj, sizeof m);
rotate(m, 2 * M_PI * view_rotx / 360.0, 1, 0, 0);
rotate(m, 2 * M_PI * view_roty / 360.0, 0, 1, 0);
rotate(m, 2 * M_PI * view_rotz / 360.0, 0, 0, 1);
draw_gear(gear1, m, -3.0, -2.0, angle, red);
draw_gear(gear2, m, 3.1, -2.0, -2 * angle - 9.0, green);
draw_gear(gear3, m, -3.1, 4.2, -2 * angle - 25.0, blue);
}
/* new window size or exposure */
static void
gears_reshape(int width, int height)
{
GLfloat ar, m[16] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.1, 0.0,
0.0, 0.0, 0.0, 1.0,
};
if (width < height)
ar = width;
else
ar = height;
m[0] = 0.1 * ar / width;
m[5] = 0.1 * ar / height;
memcpy(proj, m, sizeof proj);
glViewport(0, 0, (GLint) width, (GLint) height);
}
static void
gears_special(int special)
{
switch (special) {
case EGLUT_KEY_LEFT:
view_roty += 5.0;
break;
case EGLUT_KEY_RIGHT:
view_roty -= 5.0;
break;
case EGLUT_KEY_UP:
view_rotx += 5.0;
break;
case EGLUT_KEY_DOWN:
view_rotx -= 5.0;
break;
}
}
static void
gears_idle(void)
{
static double tRot0 = -1.0;
double dt, t = eglutGet(EGLUT_ELAPSED_TIME) / 1000.0;
if (tRot0 < 0.0)
tRot0 = t;
dt = t - tRot0;
tRot0 = t;
/* advance rotation for next frame */
angle += 70.0 * dt; /* 70 degrees per second */
if (angle > 3600.0)
angle -= 3600.0;
eglutPostRedisplay();
}
static const char vertex_shader[] =
"uniform mat4 proj;\n"
"attribute vec4 position;\n"
"attribute vec4 normal;\n"
"varying vec3 rotated_normal;\n"
"varying vec3 rotated_position;\n"
"vec4 tmp;\n"
"void main()\n"
"{\n"
" gl_Position = proj * position;\n"
" rotated_position = gl_Position.xyz;\n"
" tmp = proj * normal;\n"
" rotated_normal = tmp.xyz;\n"
"}\n";
static const char fragment_shader[] =
//"precision mediump float;\n"
"uniform vec4 color;\n"
"uniform vec3 light;\n"
"varying vec3 rotated_normal;\n"
"varying vec3 rotated_position;\n"
"vec3 light_direction;\n"
"vec4 white = vec4(1.0, 1.0, 1.0, 1.0);\n"
"void main()\n"
"{\n"
" light_direction = normalize(light - rotated_position);\n"
" gl_FragColor = color + white * dot(light_direction, rotated_normal);\n"
"}\n";
static void
gears_init(void)
{
GLuint v, f, program;
const char *p;
char msg[512];
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
p = vertex_shader;
v = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(v, 1, &p, NULL);
glCompileShader(v);
glGetShaderInfoLog(v, sizeof msg, NULL, msg);
printf("vertex shader info: %s\n", msg);
p = fragment_shader;
f = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(f, 1, &p, NULL);
glCompileShader(f);
glGetShaderInfoLog(f, sizeof msg, NULL, msg);
printf("fragment shader info: %s\n", msg);
program = glCreateProgram();
glAttachShader(program, v);
glAttachShader(program, f);
glBindAttribLocation(program, 0, "position");
glBindAttribLocation(program, 1, "normal");
glLinkProgram(program);
glGetProgramInfoLog(program, sizeof msg, NULL, msg);
printf("info: %s\n", msg);
glUseProgram(program);
proj_location = glGetUniformLocation(program, "proj");
light_location = glGetUniformLocation(program, "light");
color_location = glGetUniformLocation(program, "color");
/* make the gears */
gear1 = gear(1.0, 4.0, 1.0, 20, 0.7);
gear2 = gear(0.5, 2.0, 2.0, 10, 0.7);
gear3 = gear(1.3, 2.0, 0.5, 10, 0.7);
}
int
main(int argc, char *argv[])
{
eglutInitWindowSize(300, 300);
eglutInitAPIMask(EGLUT_OPENGL_ES2_BIT);
eglutInit(argc, argv);
eglutCreateWindow("es2gears");
eglutIdleFunc(gears_idle);
eglutReshapeFunc(gears_reshape);
eglutDisplayFunc(gears_draw);
eglutSpecialFunc(gears_special);
gears_init();
eglutMainLoop();
return 0;
}
|