-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.c
executable file
·525 lines (462 loc) · 16.4 KB
/
callbacks.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
# include <OpenGL/gl3.h>
#else
# include <GL/gl3.h>
#endif
#include "spot.h"
#include "types.h"
#include "callbacks.h"
#include "matrixFunctions.h"
extern context_t *gctx;
extern void setScene(int i);
extern int contextDraw(context_t *ctx);
extern void setUnilocs();
extern int programIds[NUM_PROGRAMS+1];
extern const char *vertFnames[NUM_PROGRAMS], *fragFnames[NUM_PROGRAMS];
extern int updateTweakBarVars(int scene);
extern int sceneGeomOffset;
#include <AntTweakBar.h>
#define UP 283
#define DOWN 284
#define LEFT 285
#define RIGHT 286
#define SPACE 32
void callbackKeyboard(int key, int action)
{
/* give AntTweakBar first pass at handling with key event */
if (TwEventKeyGLFW(key, action)) {
/* the event was handled by AntTweakBar; nothing more for us to do */
return;
}
FILE *file;
spotImage *shot;
int test, testMax=99999;
char fname[128]; /* long enough to hold filename */
if (GLFW_PRESS != action) {
GLfloat v;
switch (key) {
case 'D':
shot = spotImageNew();
/* copy image from render window; last argument controls whether
image is retreived with (SPOT_TRUE) or without (SPOT_FALSE) an
alpha channel */
if (spotImageScreenshot(shot, SPOT_TRUE)) {
//fprintf(stderr, "%s: trouble getting image:\n", me);
spotErrorPrint(); spotErrorClear();
break;
}
/* find unused filename */
for (test=0, file=NULL; test<=testMax; test++) {
/* feel free to change the filename format used here! */
sprintf(fname, "%05d.png", test);
if (!(file = fopen(fname, "rb"))) {
/* couldn't open fname => it didn't exist => we can use fname, done */
break;
}
/* else we *could* open it => already used => close it try again */
fclose(file);
file = NULL;
}
if (test > testMax) {
//fprintf(stderr, "%s: unable to find unused filename to write to!", me);
spotImageNix(shot);
break;
}
/* save image */
if (spotImageSavePNG(fname, shot)) {
//fprintf(stderr, "%s: trouble saving to %s:\n", me, fname);
spotImageNix(shot); spotErrorPrint(); spotErrorClear();
break;
}
spotImageNix(shot);
break;
// Quit the application
case 'Q': gctx->running=0; break;
case 'U':
gctx->camera.fixed ^= 1;
fprintf(stderr, gctx->camera.fixed ? "Up vector fixed\n" : "Up vector free\n");
break;
// Toggle between orthographic and perspective projection matrices
case 'P':
gctx->camera.ortho ^= 1;
fprintf(stderr, gctx->camera.ortho ? "Orthographic\n" : "Perspective\n");
callbackResize(gctx->winSizeX, gctx->winSizeY);
break;
// Enter View Mode
case 'V':
gctx->viewMode = 1;
gctx->modelMode = gctx->lightMode = 0;
fprintf(stderr,"View Mode\n");
break;
// Enter Model Mode
case 'M':
gctx->modelMode = 1;
gctx->viewMode = gctx->lightMode = 0;
fprintf(stderr,"Model Mode\n");
break;
// Enter Light Mode
case 'L':
gctx->lightMode = 1;
gctx->viewMode = gctx->modelMode = 0;
fprintf(stderr,"Light Mode\n");
break;
// Rotate around the U-axis of the viewport
case UP:
updateScene(gctx->time, 1);
break;
// Rotate around the U-axis of the viewport
case DOWN:
updateScene(gctx->time, -1);
break;
// Rotate around the V-axis of the viewport
case LEFT:
v= -0.125;
if (gctx->modelMode) {
rotate_model_V(-0.05);
} else if (gctx->lightMode) {
rotate_spotlight_V(v);
} else {
rotate_view_V(v);
rotate_spotlight_V(v);
}
break;
// Rotate around the V-axis of the viewport
case RIGHT:
v = 0.125;
if (gctx->modelMode) {
rotate_model_V(0.05);
} else if (gctx->lightMode) {
rotate_spotlight_V(v);
} else {
rotate_view_V(v);
rotate_spotlight_V(v);
}
break;
case SPACE:
gctx->paused ^= 1;
break;
// Print keycode for debugging purposes
default:
fprintf(stderr, "Caught key code: %d\n", key);
}
}
}
#define FIFTH 0.2
#define VERTICAL 1
#define HORIZONTAL 0
void callbackMouseButton(int button, int action)
{
/* give AntTweakBar first pass at handling mouse event */
if (TwEventMouseButtonGLFW(button, action)) {
/* AntTweakBar has handled event, nothing more for us to do,
not even recording buttonDown */
return;
}
(void)(button);
int xx, yy;
float xf, yf;
gctx->buttonDown = 1;
gctx->shiftDown = GLFW_PRESS == glfwGetKey(GLFW_KEY_LSHIFT)
|| GLFW_PRESS == glfwGetKey(GLFW_KEY_RSHIFT);
glfwGetMousePos(&xx, &yy);
gctx->lastX = xx;
gctx->lastY = yy;
gctx->mouseFun.m = gctx->geom[0]->modelMatrix; // To be overwritten
gctx->mouseFun.f = identity;
gctx->mouseFun.offset = 0;
gctx->mouseFun.multiplier = 1;
gctx->onlyN = 0;
if (GLFW_PRESS == action) {
xf = (float) xx / gctx->winSizeX;
yf = (float) yy / gctx->winSizeY;
if (yf > 1 - FIFTH) {
gctx->mouseFun.i = HORIZONTAL;
if (!gctx->shiftDown) {
if (gctx->viewMode) {
printf(" ... (mode V) rotates eye point around N\n");
gctx->mouseFun.m = NULL;
gctx->mouseFun.f = m_rotate_view_N;
} else if (gctx->lightMode) {
if (gctx->program != programIds[ID_SPOTLIGHT]) {
printf(" ... (mode L) rotates light direction around N\n");
gctx->mouseFun.m = gctx->lightDir;
gctx->mouseFun.f = m_rotate_3rd_V3;
gctx->mouseFun.multiplier = 10;
} else {
printf(" ... (mode L) rotates spotlight direction around N\n");
gctx->mouseFun.m = NULL;
gctx->mouseFun.f = m_rotate_spotlight_N;
}
} else if (gctx->modelMode) {
printf(" ... (mode L) rotates model around N\n");
// Do nothing here
gctx->onlyN = 1;
gctx->mouseFun.m = NULL;
gctx->mouseFun.f = m_rotate_model_N;
}
} else {
if (gctx->modelMode) {
printf(" ... (move M) translates object along N\n");
gctx->mouseFun.m = gctx->geom[gctx->gi]->modelMatrix;
gctx->mouseFun.f = translate_model_N;
gctx->mouseFun.multiplier = 4;
int j; for (j=0; j<gctx->geomNum; j++) {
updateNormals(gctx->geom[j]->normalMatrix, gctx->geom[j]->modelMatrix);
}
} else if (gctx->viewMode) {
printf(" ... (move V) translates eye and look-at along N\n");
gctx->mouseFun.m = gctx->camera.uvn; // Never accessed
gctx->mouseFun.f = translate_view_N;
gctx->mouseFun.multiplier = -20;
}
}
} else if (xf < FIFTH) {
gctx->mouseFun.i = VERTICAL;
if (!gctx->shiftDown) {
printf(" ... (mode V) zooms in or out\n");
if (gctx->viewMode) {
gctx->mouseFun.m = &gctx->camera.fov;
gctx->mouseFun.f = scale_1D;
gctx->mouseFun.offset = 1;
gctx->mouseFun.multiplier = 0.25;
callbackResize(gctx->winSizeX, gctx->winSizeY);
fprintf(stderr, "fov: %f\n", gctx->camera.fov);
}
} else {
printf(" ... (mode V) shrinks or grows (far distance) - (near distance)\n");
if (gctx->viewMode) {
gctx->mouseFun.m = NULL;
gctx->mouseFun.f = scale_near_far;
gctx->mouseFun.multiplier = 0.25;
} else if (gctx->lightMode) {
gctx->mouseFun.m = NULL;
gctx->mouseFun.f = scale_near_far2;
gctx->mouseFun.multiplier = 0.25;
}
}
} else {
if (!gctx->shiftDown) {
if (gctx->viewMode) {
printf(" ... (mode V) rotates eye point around U and V\n");
gctx->mouseFun.m = NULL;
gctx->mouseFun.f = m_rotate_view_UV;
gctx->mouseFun.multiplier = 2;
} else if (gctx->lightMode) {
if (gctx->program != programIds[ID_SPOTLIGHT]) {
printf(" ... (mode L) rotates light direction around U and V\n");
gctx->mouseFun.m = gctx->lightDir;
gctx->mouseFun.f = m_rotate_1st_2nd_V3;
} else {
printf(" ... (mode L) rotates spotlight direction around U and V\n");
gctx->mouseFun.m = NULL;
gctx->mouseFun.f = m_rotate_spotlight_UV;
gctx->mouseFun.multiplier = 2;
}
} else if (gctx->modelMode) {
printf(" ... (mode L) rotates model around U and V\n");
gctx->mouseFun.m = NULL;
gctx->mouseFun.f = m_rotate_model_UV;
}
} else {
if (gctx->viewMode) {
printf(" ... (mode V) translates eye and look-at along U and V\n");
gctx->mouseFun.m = gctx->camera.uvn;
gctx->mouseFun.f = translate_view_UV;
gctx->mouseFun.multiplier = -32;
} else if (gctx->modelMode) {
printf(" ... (mode M) translates object along U and V\n");
gctx->mouseFun.m = gctx->geom[gctx->gi]->modelMatrix;
gctx->mouseFun.f = translate_model_UV;
gctx->mouseFun.multiplier = 4;
}
}
}
} else {
gctx->buttonDown = gctx->shiftDown = 0;
}
}
void callbackMousePos(int xx, int yy)
{
GLfloat s[5];
/*
// animation
double toc = spotTime();
if (gctx->ticMouse == -1)
gctx->ticMouse = toc;
// Change in time
double dt = toc - gctx->ticMouse;
*/
if (gctx->buttonDown) {
GLfloat xf = (float) xx / gctx->winSizeX;
GLfloat yf = (float) yy / gctx->winSizeY;
s[0] = ((float) gctx->lastX / gctx->winSizeX) - xf;
s[1] = ((float) gctx->lastY / gctx->winSizeY) - yf;
s[0] = gctx->mouseFun.multiplier * s[0] + gctx->mouseFun.offset;
s[1] = gctx->mouseFun.multiplier * s[1] + gctx->mouseFun.offset;
(gctx->mouseFun.f)(gctx->mouseFun.m, s, gctx->mouseFun.i);
/*
if (gctx->modelMode && !gctx->shiftDown) {
// Change in angle
GLfloat dau, dav;
dau = (float)(xx - gctx->lastX)/(gctx->winSizeX)*2*M_PI;
dav = (float)(yy - gctx->lastY)/(gctx->winSizeY)*2*M_PI;
if (!gctx->onlyN) {
if (fabs(dau) < 0.009) {
gctx->thetaPerSecU = 0;
} else if (dt > 0) {
gctx->thetaPerSecU = dau/dt;
}
if (fabs(dav) < 0.009) {
gctx->thetaPerSecV = 0;
} else if (dt > 0) {
gctx->thetaPerSecV = dav/dt;
}
} else {
if (fabs(dau) < 0.009) {
gctx->thetaPerSecN = 0;
} else if (dt > 0) {
gctx->thetaPerSecN = dau/dt;
}
}
gctx->ticMouse = toc;
}
// printf("thetaPerSecond: %.6f, %.6f\n", gctx->thetaPerSecU, gctx->thetaPerSecV);
*/
// NOTE: We update lastX and lastY in both callbackMouseButton and callbackMousePos; We believe
// this produces better motion.
gctx->lastX = xx;
gctx->lastY = yy;
} else {
TwEventMousePosGLFW(xx, yy);
}
}
void callbackResize(int w, int h)
{
const char me[]="callbackResize";
char buff[128];
// Recalculated w and h values (using camera aspect ratio and fov); for projection matrix
GLfloat wf, hf;
// NOTE: glfwSetWindowSizeCallback sometimes sends negative numbers, causing the program to
// crash. To counteract this, clamp w and h above 0.
// NOTE: Still doesn't fix it... seems to bug out only when glfwSwapBuffers gets called...
if (w<=0) w = 1;
if (h<=0) h = 1;
/* Set Viewport to window dimensions */
glViewport(0, 0, w, h);
/* let AntTweakBar know about new window dimensions */
TwWindowSize(w, h);
gctx->winSizeX = w;
gctx->winSizeY = h;
// Update aspect ratio
gctx->camera.aspect = (GLfloat) w / h;
// Calculate hf and wf
if (!gctx->camera.ortho) {
hf = 0.5 * gctx->camera.near * tanf(0.5 * gctx->camera.fov);
wf = gctx->camera.aspect * hf;
} else {
wf = gctx->camera.aspect;
hf = 1;
}
// Normalize
// wf /= hf;
// hf = 1;
updateProj(gctx->camera.proj, wf, hf, gctx->camera.near, gctx->camera.far, gctx->camera.ortho);
/* By default the tweak bar maintains its position relative to the
LEFT edge of the window, which we are using for camera control.
So, move the tweak bar to a new position, fixed relative to RIGHT
edge of the window. You can remove/modify this based on your
preferences. */
sprintf(buff, TBAR_NAME " position='%d %d' ",
gctx->winSizeX - gctx->tbarSizeX - gctx->tbarMargin,
gctx->tbarMargin);
TwDefine(buff);
/* redraw now with new window size to permit feedback during resizing */
if (contextDraw(gctx)) {
fprintf(stderr, "%s: trouble drawing during resize:\n", me);
spotErrorPrint();
spotErrorClear();
gctx->running = 0;
}
if (!TwDraw()) {
fprintf(stderr, "%s: AntTweakBar error: %s\n", me, TwGetLastError());
gctx->running = 0;
}
glfwSwapBuffers();
return;
}
void setScene(int sceneNum)
{
int i;
if (sceneNum == 1) {
for (i = 0; i < gctx->geomNum; i ++) {
// set each object to be the same size
SPOT_M4_IDENTITY(gctx->geom[i]->modelMatrix);
scaleGeom(gctx->geom[i], 0.125);
}
// set objects so that there is one on each axis
// Red: is in the center
translateGeomN(gctx->geom[6], 2.5); // Yellow: placing in the back
translateGeomU(gctx->geom[2], 2.5); // Orange: placing on the right
translateGeomV(gctx->geom[3], -2.5); // Green: placing on the bottom
translateGeomU(gctx->geom[4], -2.5); // Blue: placing on left
translateGeomN(gctx->geom[5], -2.5); // Purple: placing in the front
translateGeomV(gctx->geom[1], 2.5); // White: placing on top
// set to orthographic mode
gctx->camera.ortho = 1;
callbackResize(gctx->winSizeX, gctx->winSizeY);
// move from so that overlooks objects
gctx->camera.from[0] = 1;
gctx->camera.from[1] = 0.5;
gctx->camera.from[2] = -1;
fprintf(stderr,
"Based on placement, we know that the white sphere belongs on top,the blue cone on the left, orange cone on the right, yellow in the back, purple in the front and the green sphere on the bottom. Knowing that our model is centered at (0,0,0) it makes sense to take a look at the space from (1, 0.5, 1), that we are in orthographic mode, and that our from point is at (0, 1, -1) we know that our model view and orthographic transform are working properly.\n");
} else if (sceneNum == 2) {
for (i = 0; i < gctx->geomNum; i ++) {
// set each object to be the same size
SPOT_M4_IDENTITY(gctx->geom[i]->modelMatrix);
scaleGeom(gctx->geom[i], 0.125);
// set each object farther and farther away
translateGeomN(gctx->geom[i], i*5);
}
// set to perspective mode
gctx->camera.ortho = 0;
callbackResize(gctx->winSizeX, gctx->winSizeY);
// move from so that puts objects along a path
gctx->camera.from[0] = 0.75;
gctx->camera.from[1] = 0.75;
gctx->camera.from[2] = -2;
fprintf(stderr,
"To ensure that prospective transform is correct, the best way to look at objects right behind each other. Thus the arrangement here is all of our objects in a row, one after another, looking at them almost head on. Note that each object is the same size, yet in the picture the objects get smaller as they are placed farther back.");
} else if (sceneNum == 3) {
for (i = 0; i < gctx->geomNum; i ++) {
SPOT_M4_IDENTITY(gctx->geom[i]->modelMatrix);
// set each object except for the sphere and ellipsoid to dissapear
if (i != 0 && i != 1) {
scaleGeom(gctx->geom[i], 0);
} else {
scaleGeom(gctx->geom[i], 0.125);
}
}
translateGeomV(gctx->geom[1], 1);
// scale the sphere by 1.0 along the X, 0.5 along the Y, 0.2 along the Z
scaleGeomX(gctx->geom[1], 1);
scaleGeomY(gctx->geom[1], 0.5);
scaleGeomZ(gctx->geom[1], 0.2);
// set both to the same color
SPOT_V3_SET(gctx->geom[0]->objColor, 1.0f, 0.0f, 0.0f); // Red
SPOT_V3_SET(gctx->geom[1]->objColor, 1.0f, 0.0f, 0.0f); // Red
// set to orthographic mode
gctx->camera.ortho = 1;
callbackResize(gctx->winSizeX, gctx->winSizeY);
// set from so that overlooks objects
gctx->camera.from[0] = 0;
gctx->camera.from[1] = -1;
gctx->camera.from[2] = -1;
fprintf(stderr,
"To ensure normals were calculated correctly, an ellipsoid whose normals are correct was placed beside a sphere that was stretched to be the same size as the sllipsoid. They were placed side by side.\n");
} else {
fprintf(stderr, "Cannot set up scene %d because scene %d does not exist!\n", sceneNum, sceneNum);
}
}