forked from QianMo/GPU-Gems-Book-Source-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.c
677 lines (520 loc) · 15.4 KB
/
mouse.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
=============================================================================
mouse.c --- handles all mouse events and display events
-----------------------------------------------------------------------------
Author : Jos Stam ([email protected])
=============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <GL/glut.h>
#include <math.h>
#include "mouse.h"
/*
-----------------------------------------------------------------------------
constants
-----------------------------------------------------------------------------
*/
#define TRACK_RADIUS 0.4f
#define MOUSE_SPEEDZ 5.0f
#define MOUSE_SPEEDXY 2.0f
#define UP 0
#define DOWN 1
#ifdef _WIN32
/* math defines from math.h */
#define M_SQRT2 1.41421356237309504880
#define M_SQRT1_2 0.70710678118654752440
#endif
/*
-----------------------------------------------------------------------------
static variables
-----------------------------------------------------------------------------
*/
static float RotVec[4]; /* Euler rotation parameters */
static float RotMat[16]; /* Corresponding rotation matrix */
static float Trans[4]; /* translational part of viewing transformation */
static long OrgX, OrgY; /* origin of window */
static long SizeX, SizeY; /* size of window */
static float BackGndR, BackGndG, BackGndB;
/* background color */
static int already_set_view=0;
static short LeftMouse; /* status of left mouse button (UP or DOWN) */
static short MiddleMouse; /* status of middle mouse button */
static short RightMouse; /* status of right mouse button */
static int MouseX; /* current X mouse coordinate */
static int MouseY; /* current Y mouse coordinate */
static float Zoom; /* zoom factor for the display */
static time_t StartTime; /* used to compute frames per second */
static int winID; /* GLUT identifier of the window */
/*
-----------------------------------------------------------------------------
simple vector routines
-----------------------------------------------------------------------------
*/
static void vcopy ( float * v1, float * v2 )
{
int i;
for ( i=0 ; i<3 ; i++ ) {
v2[i] = v1[i];
}
}
static void vset ( float * v, float x, float y, float z )
{
v[0] = x;
v[1] = y;
v[2] = z;
}
static void vzero ( float * v )
{
v[0] = 0;
v[1] = 0;
v[2] = 0;
}
static float vlength ( float * v )
{
return ( (float)sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] ) );
}
static void vscale ( float * v, float div )
{
v[0] *= div;
v[1] *= div;
v[2] *= div;
}
static void vnormal ( float * v )
{
vscale ( v, 1/vlength(v) );
}
static void vmult ( float * src1, float * src2, float * dst )
{
dst[0] = src1[0] * src2[0];
dst[1] = src1[1] * src2[1];
dst[2] = src1[2] * src2[2];
}
static void vadd ( float * src1, float * src2, float * dst )
{
dst[0] = src1[0] + src2[0];
dst[1] = src1[1] + src2[1];
dst[2] = src1[2] + src2[2];
}
static void vsub ( float * src1, float * src2, float * dst )
{
dst[0] = src1[0] - src2[0];
dst[1] = src1[1] - src2[1];
dst[2] = src1[2] - src2[2];
}
static float vdot ( float * v1, float * v2 )
{
return ( v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] );
}
static void vcross ( float * v1, float * v2, float * cross )
{
float temp[3];
temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
vcopy ( temp, cross );
}
/*
-----------------------------------------------------------------------------
quaternion/euler angle handling routines
-----------------------------------------------------------------------------
*/
/*
* Euler paramaters always obey: a^2 + b^2 + c^2 + d^2 = 1.0
* We'll normalize based on this formula. Also, normalize greatest
* component, to avoid problems that occur when the component we're
* normalizing gets close to zero (and the other components may add up
* to more than 1.0 because of rounding error).
*/
static void normalize_euler ( float * e )
{
int which, i;
float gr;
which = 0;
gr = e[which];
for ( i=1 ; i<4 ; i++ ) {
if ( fabs(e[i]) > fabs(gr) ) {
gr = e[i];
which = i;
}
}
e[which] = 0;
e[which] = (float) sqrt(1 - (e[0]*e[0] + e[1]*e[1] + e[2]*e[2] + e[3]*e[3]));
/* Check to see if we need negative square root */
if ( gr < 0.0f ) {
e[which] = -e[which];
}
}
/*
* Given two rotations, e1 and e2, expressed as Euler paramaters,
* figure out the equivalent single rotation and stuff it into dest.
*
* This routine also normalizes the result every COUNT times it is
* called, to keep error from creeping in.
*/
#define COUNT 100
static void add_eulers ( float * e1, float * e2, float * dest )
{
static int count=0;
int i;
float t1[3], t2[3], t3[3];
float tf[4];
vcopy ( e1, t1 ); vscale ( t1, e2[3] );
vcopy ( e2, t2 ); vscale ( t2, e1[3] );
vcross ( e2, e1, t3 );
vadd ( t1, t2, tf );
vadd ( t3, tf, tf );
tf[3] = e1[3] * e2[3] - vdot(e1,e2);
for ( i=0 ; i<4 ; i++ ) {
dest[i] = tf[i];
}
if ( ++count > COUNT ) {
count = 0;
normalize_euler ( dest );
}
}
/*
* Build a rotation matrix, given Euler paramaters.
*/
static void build_rotmatrix ( float * m, float * e )
{
m[0 ] = 1 - 2*(e[1]*e[1] + e[2]*e[2]);
m[1 ] = 2*(e[0]*e[1] - e[2]*e[3]);
m[2 ] = 2*(e[2]*e[0] + e[1]*e[3]);
m[3 ] = 0;
m[4 ] = 2*(e[0]*e[1] + e[2]*e[3]);
m[5 ] = 1 - 2*(e[2]*e[2] + e[0]*e[0]);
m[6 ] = 2*(e[1]*e[2] - e[0]*e[3]);
m[7 ] = 0;
m[8 ] = 2*(e[2]*e[0] - e[1]*e[3]);
m[9 ] = 2*(e[1]*e[2] + e[0]*e[3]);
m[10] = 1 - 2*(e[1]*e[1] + e[0]*e[0]);
m[11] = 0;
m[12] = 0;
m[13] = 0;
m[14] = 0;
m[15] = 1;
}
/*
-----------------------------------------------------------------------------
GetScreenFrame --- get the frame projected into the view
-----------------------------------------------------------------------------
*/
void GetScreenFrame ( float * S, float * T, float * N )
{
S[0] = RotMat[0 ];
S[1] = RotMat[4 ];
S[2] = RotMat[8 ];
T[0] = RotMat[1 ];
T[1] = RotMat[5 ];
T[2] = RotMat[9 ];
N[0] = RotMat[2 ];
N[1] = RotMat[6 ];
N[2] = RotMat[10];
}
/*
-----------------------------------------------------------------------------
InitMouseVars --- Init global variables
-----------------------------------------------------------------------------
*/
void InitMouseVars ()
{
int i;
FILE * f;
/* all buttons up at the beginning */
LeftMouse = UP;
MiddleMouse = UP;
RightMouse = UP;
if ( already_set_view ) return;
/* set default values for viewing parameters */
for ( i=0 ; i<16 ; i++ ) {
RotMat[i] = 0;
}
for ( i=0 ; i<4 ; i++ ) {
RotMat[4*i] = 1;
RotVec[i] = 0;
Trans[i] = 0;
}
RotVec[3] = 1;
Trans[2] = 60;
/* try to read view parameters from the view.param file */
if ( (f=fopen(".view.param","r")) ) {
fscanf ( f, "%f %f %f ", &Trans[0], &Trans[1], &Trans[2] );
fscanf ( f, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f ",
&RotMat[0 ], &RotMat[1 ], &RotMat[2 ], &RotMat[3 ],
&RotMat[4 ], &RotMat[5 ], &RotMat[6 ], &RotMat[7 ],
&RotMat[8 ], &RotMat[9 ], &RotMat[10], &RotMat[11],
&RotMat[12], &RotMat[13], &RotMat[14], &RotMat[15] );
fscanf ( f, "%f %f %f %f ", &RotVec[0], &RotVec[1], &RotVec[2], &RotVec[3] );
fclose ( f );
} else {
fprintf ( stderr, "cannot open view file\n" );
}
}
/*
-----------------------------------------------------------------------------
SaveView --- save viewing parameters in a file called .view.param
-----------------------------------------------------------------------------
*/
int SaveView ()
{
FILE * f;
if ( !(f=fopen(".view.param","w")) ) {
return ( FALSE );
}
fprintf ( f, "%f %f %f\n", Trans[0], Trans[1], Trans[2] );
fprintf ( f, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n",
RotMat[0 ], RotMat[1 ], RotMat[2 ], RotMat[3 ],
RotMat[4 ], RotMat[5 ], RotMat[6 ], RotMat[7 ],
RotMat[8 ], RotMat[9 ], RotMat[10], RotMat[11],
RotMat[12], RotMat[13], RotMat[14], RotMat[15] );
fprintf ( f, "%f %f %f %f\n", RotVec[0], RotVec[1], RotVec[2], RotVec[3] );
fclose ( f );
return ( TRUE );
}
/*
-----------------------------------------------------------------------------
callback routines for keyboard for mouse events
-----------------------------------------------------------------------------
*/
void MouseStart ( int button, int state, int x, int y )
{
int button_status;
button_status = state == GLUT_DOWN ? DOWN : UP;
switch ( button )
{
case GLUT_LEFT_BUTTON:
LeftMouse = button_status;
break;
case GLUT_MIDDLE_BUTTON:
MiddleMouse = button_status;
break;
case GLUT_RIGHT_BUTTON:
RightMouse = button_status;
break;
}
MouseX = x;
MouseY = y;
}
/*
-----------------------------------------------------------------------------
UpdateViewing --- well the title says it all...
-----------------------------------------------------------------------------
*/
static void UpdateViewing ( float * r, float * t )
{
vadd ( t,Trans, Trans );
add_eulers ( r, RotVec, RotVec );
build_rotmatrix ( RotMat, RotVec );
}
/*
-----------------------------------------------------------------------------
TranslateXY --- do XY translation from mouse movement
-----------------------------------------------------------------------------
*/
static void TranslateXY ( int mx, int my )
{
float r[4], t[3];
vzero ( r ); r[3] = 1;
vset( t, (float)(mx-MouseX)/(float)SizeX*MOUSE_SPEEDXY,
(float)(MouseY-my)/(float)SizeY*MOUSE_SPEEDXY,
0.0f );
UpdateViewing ( r, t );
MouseX = mx;
MouseY = my;
}
/*
-----------------------------------------------------------------------------
TranslateZ -- do Z translation from mouse movement
-----------------------------------------------------------------------------
*/
static void TranslateZ ( int mx, int my )
{
float r[4], t[3];
vzero ( r ); r[3] = 1.0f;
vzero ( t );
t[2] = (float)(mx-MouseX)/(float)SizeX + (float)(MouseY-my)/(float)SizeY;
t[2] *= MOUSE_SPEEDZ;
UpdateViewing ( r, t );
MouseX = mx;
MouseY = my;
}
/*
-----------------------------------------------------------------------------
project_to_sphere --- project (x,y) on sphere (or hyperbola)
-----------------------------------------------------------------------------
*/
static float project_to_sphere ( float x, float y )
{
float d, t, z;
d = sqrt ( x*x + y*y );
if ( d < TRACK_RADIUS*M_SQRT1_2 ) {
z = (float) sqrt ( TRACK_RADIUS*TRACK_RADIUS - d*d );
} else {
t = TRACK_RADIUS / M_SQRT2;
z = t*t / d;
}
return ( z );
}
/*
-----------------------------------------------------------------------------
CalcRotate --- calculate rotation from mouse movement
-----------------------------------------------------------------------------
*/
static void CalcRotate ( float * r, float p1x, float p1y, float p2x,
float p2y )
{
float p1[3], p2[3];
float dp[3];
float axis[3];
float phi;
vzero ( axis );
if ( p1x == p2x && p1y == p2y ) {
vzero ( r );
r[3] = 1;
return;
}
vset ( p1, p1x, p1y, project_to_sphere(p1x,p1y) );
vset ( p2, p2x, p2y, project_to_sphere(p2x,p2y) );
vcross ( p2, p1, axis );
vsub ( p1, p2, dp );
phi = (float) sin(vlength(dp) / (2*TRACK_RADIUS));
vnormal ( axis );
vcopy ( axis, r );
vscale ( r, (float)sin(phi/2) );
r[3] = (float)cos(phi/2);
}
/*
-----------------------------------------------------------------------------
Rotate --- do rotation from mouse movement
-----------------------------------------------------------------------------
*/
static void Rotate ( int mx, int my )
{
float r[4], t[3];
float p1x, p1y, p2x, p2y;
vzero ( t );
p1x = 2*(MouseX-OrgX)/(float)SizeX - 1;
p1y = -2*(MouseY-OrgY)/(float)SizeY + 1;
p2x = 2*(mx -OrgX)/(float)SizeX - 1;
p2y = -2*(my -OrgY)/(float)SizeY + 1;
CalcRotate ( r, p1x, p1y, p2x, p2y );
UpdateViewing ( r, t );
MouseX = mx;
MouseY = my;
}
/*
-----------------------------------------------------------------------------
MouseHandle --- handles current state of the mouse (motion callback)
-----------------------------------------------------------------------------
*/
void MouseHandle ( int x, int y )
{
if ( LeftMouse == UP && RightMouse == DOWN ) {
TranslateXY ( x, y );
}
if ( LeftMouse == DOWN && RightMouse == DOWN ) {
TranslateZ ( x, y );
}
if ( LeftMouse == DOWN && RightMouse == UP ) {
Rotate ( x, y );
}
}
/*
-----------------------------------------------------------------------------
SetBackGnd -- set the background colour
-----------------------------------------------------------------------------
*/
void SetBackGnd ( float r, float g, float b )
{
BackGndR = r;
BackGndG = g;
BackGndB = b;
}
/*
-----------------------------------------------------------------------------
StartDisplay --- clean up before displaying
-----------------------------------------------------------------------------
*/
void StartDisplay ( void )
{
float aspect;
StartTime = clock ();
aspect = (float)SizeX / (float)SizeY;
glViewport ( 0, 0, SizeX, SizeY );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ();
gluPerspective ( Zoom, aspect, 0.01f, 200.0f );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef ( Trans[0], Trans[1], Trans[2] );
glMultMatrixf ( RotMat );
glClearColor ( BackGndR, BackGndG, BackGndB, 1.0f );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}
/*
-----------------------------------------------------------------------------
EndDisplay --- clean up at end of display
-----------------------------------------------------------------------------
*/
void EndDisplay ( void )
{
char wname[256];
time_t t;
float dt;
t = clock ();
dt = (t-StartTime)/(float)CLOCKS_PER_SEC;
if ( dt > 0.0f ) {
sprintf ( wname, "Real-Time Diffraction by Jos Stam FPS : %6.2f", 1.0f/dt );
glutSetWindowTitle( wname );
}
glutSwapBuffers ();
}
/*
-----------------------------------------------------------------------------
ReshapeFunc -- reshape callback function
-----------------------------------------------------------------------------
*/
static void ReshapeFunc ( int width, int height )
{
SizeX = width;
SizeY = height;
}
/*
-----------------------------------------------------------------------------
InitWindow --- open working window and set viewing parameters
-----------------------------------------------------------------------------
*/
int InitWindow ( long oX, long oY, long sX, long sY, float zoom )
{
GLfloat lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
OrgX = oX;
OrgY = oY;
SizeX = sX;
SizeY = sY;
Zoom = zoom;
InitMouseVars ();
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition ( OrgX, OrgY );
glutInitWindowSize ( SizeX, SizeY );
winID = glutCreateWindow ( "Real-Time Diffraction by Jos Stam FPS :" );
glEnable ( GL_LIGHT0 );
glEnable ( GL_LIGHTING );
glShadeModel ( GL_SMOOTH );
glEnable ( GL_DEPTH_TEST );
glDisable ( GL_CULL_FACE );
glCullFace ( GL_BACK );
glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, lmodel_ambient );
glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glutSwapBuffers ();
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glutSwapBuffers ();
glLineWidth ( 5.0f );
glutReshapeFunc ( ReshapeFunc );
SetBackGnd ( 0.0f, 0.0f, 0.0f );
glEnable ( GL_POLYGON_OFFSET_LINE );
glPolygonOffset ( 1, 1e-6f );
return ( winID );
}