-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm.c
354 lines (300 loc) · 8.28 KB
/
mm.c
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
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GLUT/glut.h>
GLUquadricObj *cone, *base, *qsphere;
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef __sgi
#define trunc(x) ((double)((int)(x)))
#endif
int draw_passes = 8;
int headsUp = 0;
typedef struct {
GLfloat verts[4][3];
GLfloat scale[3];
GLfloat trans[3];
} Mirror;
Mirror mirrors[] = {
/* mirror on the left wall */
{{{-1., -.75, -.75}, {-1., .75, -.75}, {-1., .75, .75}, {-1, -.75, .75}},
{-1, 1, 1}, {2, 0, 0}},
/* mirror on right wall */
{{{1., -.75, .75}, {1., .75, .75}, {1., .75, -.75}, {1., -.75, -.75}},
{-1, 1, 1}, {-2, 0, 0}},
};
int nMirrors = 2;
void init(void)
{
static GLfloat lightpos[] = {.5, .75, 1.5, 1};
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glEnable(GL_CULL_FACE);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
cone = gluNewQuadric();
qsphere = gluNewQuadric();
}
void make_viewpoint(void)
{
if (headsUp) {
float width = (1 + 2*(draw_passes/nMirrors)) * 1.25;
float height = (width / tan((30./360.) * (2.*M_PI))) + 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, height - 3, height + 3);
gluLookAt(0, height, 0,
0, 0, 0,
0, 0, 1);
} else {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, .01, 4 + 2*(draw_passes / nMirrors));
gluLookAt(-2, 0, .75,
0, 0, 0,
0, 1, 0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
make_viewpoint();
}
void draw_room(void)
{
/* material for the walls, floor, ceiling */
static GLfloat wall_mat[] = {1.f, 1.f, 1.f, 1.f};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat);
glBegin(GL_QUADS);
/* floor */
glNormal3f(0, 1, 0);
glVertex3f(-1, -1, 1);
glVertex3f(1, -1, 1);
glVertex3f(1, -1, -1);
glVertex3f(-1, -1, -1);
/* ceiling */
glNormal3f(0, -1, 0);
glVertex3f(-1, 1, -1);
glVertex3f(1, 1, -1);
glVertex3f(1, 1, 1);
glVertex3f(-1, 1, 1);
/* left wall */
glNormal3f(1, 0, 0);
glVertex3f(-1, -1, -1);
glVertex3f(-1, 1, -1);
glVertex3f(-1, 1, 1);
glVertex3f(-1, -1, 1);
/* right wall */
glNormal3f(-1, 0, 0);
glVertex3f(1, -1, 1);
glVertex3f(1, 1, 1);
glVertex3f(1, 1, -1);
glVertex3f(1, -1, -1);
/* far wall */
glNormal3f(0, 0, 1);
glVertex3f(-1, -1, -1);
glVertex3f(1, -1, -1);
glVertex3f(1, 1, -1);
glVertex3f(-1, 1, -1);
/* back wall */
glNormal3f(0, 0, -1);
glVertex3f(-1, 1, 1);
glVertex3f(1, 1, 1);
glVertex3f(1, -1, 1);
glVertex3f(-1, -1, 1);
glEnd();
}
void draw_cone(void)
{
static GLfloat cone_mat[] = {0.f, .5f, 1.f, 1.f};
glPushMatrix();
glTranslatef(0, -1, 0);
glRotatef(-90, 1, 0, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat);
gluCylinder(cone, .3, 0, 1.25, 20, 1);
glPopMatrix();
}
void draw_sphere(GLdouble secs)
{
static GLfloat sphere_mat[] = {1.f, .5f, 0.f, 1.f};
GLfloat angle;
/* one revolution every 10 seconds... */
secs = secs - 10.*trunc(secs / 10.);
angle = (secs/10.) * (360.);
glPushMatrix();
glTranslatef(0, -.3, 0);
glRotatef(angle, 0, 1, 0);
glTranslatef(.6, 0, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat);
gluSphere(qsphere, .3, 20, 20);
glPopMatrix();
}
GLdouble get_secs(void)
{
return glutGet(GLUT_ELAPSED_TIME) / 1000.0;
}
void draw_mirror(Mirror *m)
{
glBegin(GL_QUADS);
glVertex3fv(m->verts[0]);
glVertex3fv(m->verts[1]);
glVertex3fv(m->verts[2]);
glVertex3fv(m->verts[3]);
glEnd();
}
/* A note on matrix management: it would be easier to use push and
* pop to save and restore the matrices, but the projection matrix stack
* is very shallow, so we just undo what we did. In the extreme this
* could lead to mathematic error. */
GLenum reflect_through_mirror(Mirror *m, GLenum cullFace)
{
GLenum newCullFace = ((cullFace == GL_FRONT) ? GL_BACK : GL_FRONT);
glMatrixMode(GL_PROJECTION);
glScalef(m->scale[0], m->scale[1], m->scale[2]);
glTranslatef(m->trans[0], m->trans[1], m->trans[2]);
glMatrixMode(GL_MODELVIEW);
/* must flip the cull face since reflection reverses the orientation
* of the polygons */
glCullFace(newCullFace);
return newCullFace;
}
void undo_reflect_through_mirror(Mirror *m, GLenum cullFace)
{
glMatrixMode(GL_PROJECTION);
glTranslatef(-m->trans[0], -m->trans[1], -m->trans[2]);
glScalef(1./m->scale[0], 1./m->scale[1], 1./m->scale[2]);
glMatrixMode(GL_MODELVIEW);
glCullFace(cullFace);
}
void draw_scene(GLdouble secs, int passes, GLenum cullFace,
GLuint stencilVal, GLuint mirror)
{
GLenum newCullFace;
int passesPerMirror, passesPerMirrorRem;
unsigned int curMirror, drawMirrors;
int i;
/* one pass to draw the real scene */
passes--;
/* only draw in my designated locations */
glStencilFunc(GL_EQUAL, stencilVal, 0xffffffff);
/* draw things which may obscure the mirrors first */
draw_sphere(secs);
draw_cone();
/* now draw the appropriate number of mirror reflections. for
* best results, we perform a depth-first traversal by allocating
* a number of passes for each of the mirrors. */
if (mirror != 0xffffffff) {
passesPerMirror = passes / (nMirrors - 1);
passesPerMirrorRem = passes % (nMirrors - 1);
if (passes > nMirrors - 1) drawMirrors = nMirrors - 1;
else drawMirrors = passes;
} else {
/* mirror == -1 means that this is the initial scene (there was no
* mirror) */
passesPerMirror = passes / nMirrors;
passesPerMirrorRem = passes % nMirrors;
if (passes > nMirrors) drawMirrors = nMirrors;
else drawMirrors = passes;
}
for (i = 0; drawMirrors > 0; i++) {
curMirror = i % nMirrors;
if (curMirror == mirror) continue;
drawMirrors--;
/* draw mirror into stencil buffer but not color or depth buffers */
glColorMask(0, 0, 0, 0);
glDepthMask(0);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
draw_mirror(&mirrors[curMirror]);
glColorMask(1, 1, 1, 1);
glDepthMask(1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
/* draw reflected scene */
newCullFace = reflect_through_mirror(&mirrors[curMirror], cullFace);
if (passesPerMirrorRem) {
draw_scene(secs, passesPerMirror + 1, newCullFace, stencilVal + 1,curMirror);
passesPerMirrorRem--;
} else {
draw_scene(secs, passesPerMirror, newCullFace, stencilVal + 1,curMirror);
}
undo_reflect_through_mirror(&mirrors[curMirror], cullFace);
/* back to our stencil value */
glStencilFunc(GL_EQUAL, stencilVal, 0xffffffff);
}
draw_room();
}
void draw(void)
{
GLenum err;
GLfloat secs = get_secs();
glDisable(GL_STENCIL_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
if (!headsUp) glEnable(GL_STENCIL_TEST);
draw_scene(secs, draw_passes, GL_BACK, 0, (unsigned)-1);
glDisable(GL_STENCIL_TEST);
if (headsUp) {
/* draw a red floor on the original scene */
glDisable(GL_LIGHTING);
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex3f(-1, -.95, 1);
glVertex3f(1, -.95, 1);
glVertex3f(1, -.95, -1);
glVertex3f(-1, -.95, -1);
glEnd();
glEnable(GL_LIGHTING);
}
err = glGetError();
if (err != GL_NO_ERROR) printf("Error: %s\n", gluErrorString(err));
glutSwapBuffers();
}
/* ARGSUSED1 */
void key(unsigned char key, int x, int y)
{
switch(key) {
case '.': case '>': case '+': case '=':
draw_passes++;
printf("Passes = %d\n", draw_passes);
make_viewpoint();
break;
case ',': case '<': case '-': case '_':
draw_passes--;
if (draw_passes < 1) draw_passes = 1;
printf("Passes = %d\n", draw_passes);
make_viewpoint();
break;
case 'h': case 'H':
/* heads up mode */
headsUp = (headsUp == 0);
make_viewpoint();
break;
case 27:
exit(0);
}
}
#define MIN_COLOR_BITS 4
#define MIN_DEPTH_BITS 8
main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(256, 256);
glutInitWindowPosition(0, 0);
if (argc > 1) {
glutInitDisplayString("samples stencil>=3 rgb depth");
} else {
glutInitDisplayString("samples stencil>=3 rgb double depth");
}
glutCreateWindow(argv[0]);
glutDisplayFunc(draw);
glutIdleFunc(draw);
glutKeyboardFunc(key);
glutReshapeFunc(reshape);
init();
glutMainLoop();
return 0;
}