-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.cpp
More file actions
1198 lines (884 loc) · 25.2 KB
/
sample.cpp
File metadata and controls
1198 lines (884 loc) · 25.2 KB
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <ctype.h>
#include "keytime.h"
#ifndef WIN32
#include <sys/time.h>
#endif
#ifdef WIN32
#pragma warning(disable:4996)
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include "glui.h"
// title of these windows:
const char *WINDOWTITLE = { "Project 6 -- Grace Todd" };
const char *GLUITITLE = { "User Interface Window" };
// what the glui package defines as true and false:
const int GLUITRUE = { true };
const int GLUIFALSE = { false };
// the escape key:
#define ESCAPE 0x1b
// initial window size:
const int INIT_WINDOW_SIZE = { 800 };
// multiplication factors for input interaction:
// (these are known from previous experience)
const float ANGFACT = { 1. };
const float SCLFACT = { 0.005f };
// minimum allowable scale factor:
const float MINSCALE = { 0.05f };
// active mouse buttons (or them together):
const int LEFT = { 4 };
const int MIDDLE = { 2 };
const int RIGHT = { 1 };
// which projection:
const int ORTHO = { GLUIFALSE };
const int PERSP = { GLUITRUE };
// which button:
#define RESET 0
#define QUIT 1
// window background color (rgba):
const float BACKCOLOR[ ] = { 0., 0., 0., 0. };
// color and line width for the axes:
const float AXES_COLOR[ ] = { 1., .5, 0. };
const float AXES_WIDTH = 3.;
// time in milliseconds for cycle to complete:
const int MSEC = { 10*1000 };
// handy to have around:
const int OFF = { 0 };
const int ON = { 1 };
#define FALSE 0
#define TRUE 1
// window background color (rgba): = {
const float BACKGROUND_COLOR[4] = { 0.,0.,0.,0. };
// torus size:
const float RADIUS1 = { 50. };
const float RADIUS2 = { 80. };
// projecting the shadow:
const float FLOOR = { -100. };
const float CEILING = { 1100. };
const float DSHADOW = { 10. };
const float LEFT_WALL = { -100. };
const float RIGHT_WALL = { 1100. };
const float BACK_WALL = { -100. };
const float FRONT_WALL = { 1100. };
const float FLOOR_COLOR[3] = { .5f,.5f,.5f };
const float LEFT_WALL_COLOR[3] = { .2f,.2f,.2f };
const float BACK_WALL_COLOR[3] = { .3f,.3f,.3f };
const float KEYTIME_COLOR[3] = { 0.6f,0.6f,0. };
// non-constant global variables:
int ActiveButton; // current button that is down
int AnimateOn; // TRUE means we are animating
int AxesList; // list to hold the axes
int AxesOn; // ON or OFF
int Debug; // ON means print debug info
GLUI * Glui; // instance of glui window
int GluiWindow; // the glut id for the glui window
int GrWindow; // window id for graphics window
int KeytimeList; // list to hold the keyframes
int KeytimeMenu; // id of the keyframe pop-up menu
int KeytimeOn; // ON or OFF
int ObjectList; // list to hold the object
float RotMatrix[4][4]; // set by glui rotation widget
float Scale, Scale2; // scaling factors
int WallList; // list to hold the wall
int WhichProjection; // ORTHO or PERSP
int Xmouse, Ymouse; // mouse values
float Xrot, Yrot; // rotation angles in degrees
float TransXYZ[3]; // set by glui translation widgets
GLUI_Rotation *GluiRot;
Keytimes Xpos, Ypos, Zpos;
Keytimes ThetaX, ThetaY, ThetaZ;
Keytimes Hue;
float White[ ] = { 1.,1.,1.,1. }; // handy to have around
// function prototypes:
void Animate( void );
float * Array3( float, float, float );
void Axes( float length );
void Buttons( int );
void Display( void );
void DoRasterString( float, float, float, char * );
void DoStrokeString( float, float, float, float, char * );
void HsvRgb( float [3], float [3] );
void InitGraphics( void );
void InitLists( void );
void InitGlui( void );
void Keyboard( unsigned char, int, int );
void MouseButton( int, int, int, int );
void MouseMotion( int, int );
float * MulArray3( float, float [3] );
void Reset( void );
void Resize( int, int );
void SetAnimate( int );
void Visibility( int );
// main program:
int
main( int argc, char *argv[ ] )
{
// turn on the glut package:
glutInit( &argc, argv );
// setup all the graphics stuff:
InitGraphics( );
// create the display structures that will not change:
InitLists( );
// init all the global variables used by Display( ):
// this will also post a redisplay
// it is important to call this before InitGlui( ):
// so that the variables that glui will control are correct
// when each glui widget is created
Reset( );
// setup all the user interface stuff:
InitGlui( );
// draw the scene once and wait for some interaction:
// (will never return)
glutMainLoop( );
// this is here to make the compiler happy:
return 0;
}
// this is where one would put code that is to be called
// everytime the glut main loop has nothing to do
//
// this is typically where animation parameters are set
//
// do not call Display( ) from here -- let glutMainLoop( ) do it
void
Animate( void )
{
// force a call to Display( ) next time it is convenient:
glutSetWindow( GrWindow );
glutPostRedisplay( );
}
// glui buttons callback:
void
Buttons( int id )
{
switch( id )
{
case RESET:
Reset( );
break;
case QUIT:
// gracefully close the glui window:
// gracefully close out the graphics:
// gracefully close the graphics window:
// gracefully exit the program:
Glui->close( );
glFinish( );
glutDestroyWindow( GrWindow );
exit( 0 );
}
Glui->sync_live( );
}
// draw the complete scene:
void
Display( void )
{
glutSetWindow( GrWindow );
// erase the background:
glDrawBuffer( GL_BACK );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable( GL_DEPTH_TEST );
// specify shading to be flat:
glShadeModel( GL_FLAT );
// set the viewport to a square centered in the window:
int dx = glutGet( GLUT_WINDOW_WIDTH );
int dy = glutGet( GLUT_WINDOW_HEIGHT );
int d = dx < dy ? dx : dy; // minimum dimension
int xl = ( dx - d ) / 2;
int yb = ( dy - d ) / 2;
glViewport( xl, yb, d, d );
// set the viewing volume:
// remember that the eye is at the origin looking in -Z
// remember that the Z values are actually
// given as DISTANCES IN FRONT OF THE EYE
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
if( WhichProjection == ORTHO )
glOrtho( -800., 800., -800., 800., 0.8, 4000. );
else
gluPerspective( 70., 1., 10., 4000. );
// place the object into the viewing volume:
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt( 0., 0., 2000., 0., 0., 0., 0., 1., 0. );
// light position:
glLightfv( GL_LIGHT0, GL_POSITION, Array3( 0., 1000., 0. ) );
glLightfv( GL_LIGHT1, GL_POSITION, Array3( 1000., 0., 0. ) );
// rotate the scene:
glRotatef( Yrot, 0., 1., 0. );
glRotatef( Xrot, 1., 0., 0. );
glMultMatrixf( (const GLfloat *) RotMatrix );
// scale the scene:
glScalef( Scale, Scale, Scale );
float scale2 = 1.f + Scale2; // because glui translation starts at 0.
if( scale2 < MINSCALE )
scale2 = MINSCALE;
glScalef( scale2, scale2, scale2 );
// possibly draw the axes:
glDisable( GL_LIGHTING );
glDisable( GL_LIGHT0 );
glDisable( GL_LIGHT1 );
if( AxesOn )
glCallList( AxesList );
// translate the object:
// note the minus sign on the z value
// this is to make the appearance of the glui z translate
// widget more intuitively match the translate behavior
glTranslatef( TransXYZ[0], TransXYZ[1], -TransXYZ[2] );
glTranslatef( -350., -350., -500. );
// draw the walls:
glCallList(WallList);
// the key positions:
if( KeytimeOn == ON )
{
glColor3fv( KEYTIME_COLOR );
glCallList( KeytimeList );
}
// turn # msec into the cycle ( 0 - MSEC-1 ):
int msec = glutGet( GLUT_ELAPSED_TIME ) % MSEC;
// turn that into a time:
float nowTime = (float)msec / 1000.f;
// material color:
float hsv[3], rgb[4];
hsv[0] = Hue.GetValue( nowTime );;
hsv[1] = hsv[2] = rgb[3] = 1.;
HsvRgb( hsv, rgb );
glMaterialfv( GL_FRONT, GL_AMBIENT, rgb );
glMaterialfv( GL_FRONT, GL_DIFFUSE, rgb );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHT1 );
float xpos = Xpos.GetValue( nowTime );
float ypos = Ypos.GetValue( nowTime );
float zpos = Zpos.GetValue( nowTime );
float ax = ThetaX.GetValue( nowTime );
float ay = ThetaY.GetValue( nowTime );
float az = ThetaZ.GetValue( nowTime );
glPushMatrix( );
glTranslatef( xpos, ypos, zpos );
glRotatef( ax, 1., 0., 0. );
glRotatef( ay, 0., 1., 0. );
glRotatef( az, 0., 0., 1. );
glCallList( ObjectList );
glPopMatrix( );
glDisable( GL_LIGHT1 );
glDisable( GL_LIGHT0 );
glDisable( GL_LIGHTING );
// swap the double-buffered framebuffers:
glutSwapBuffers( );
// be sure the graphics buffer has been sent:
glFlush( );
}
// use glut to display a string of characters using a raster font:
void
DoRasterString( float x, float y, float z, char *s )
{
char c; // one character to print
glRasterPos3f( x, y, z );
for( ; ( c = *s ) != '\0'; s++ )
{
glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_24, c );
}
}
// use glut to display a string of characters using a stroke font:
void
DoStrokeString( float x, float y, float z, float ht, char *s )
{
char c; // one character to print
glPushMatrix( );
glTranslatef( x, y, z );
float sf = ht / ( 119.05f + 33.33f );
glScalef( sf, sf, sf );
for( ; ( c = *s ) != '\0'; s++ )
{
glutStrokeCharacter( GLUT_STROKE_ROMAN, c );
}
glPopMatrix( );
}
// initialize the glut and OpenGL libraries:
// also setup display lists and callback functions
void
InitGraphics( void )
{
// setup the display mode:
// ( *must* be done before call to glutCreateWindow( ) )
// ask for color, double-buffering, and z-buffering:
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( INIT_WINDOW_SIZE, INIT_WINDOW_SIZE );
GrWindow = glutCreateWindow( WINDOWTITLE );
glutSetWindowTitle( WINDOWTITLE );
glClearColor( BACKCOLOR[0], BACKCOLOR[1], BACKCOLOR[2], BACKCOLOR[3] );
// setup the callback routines:
// DisplayFunc -- redraw the window
// ReshapeFunc -- handle the user resizing the window
// KeyboardFunc -- handle a keyboard input
// MouseFunc -- handle the mouse button going down or up
// MotionFunc -- handle the mouse moving with a button down
// PassiveMotionFunc -- handle the mouse moving with a button up*/
// VisibilityFunc -- handle a change in window visibility
// EntryFunc -- handle the cursor entering or leaving the window
// SpecialFunc -- handle special keys on the keyboard
// SpaceballMotionFunc -- handle spaceball translation
// SpaceballRotateFunc -- handle spaceball rotation
// SpaceballButtonFunc -- handle spaceball button hits
// ButtonBoxFunc -- handle button box hits
// DialsFunc -- handle dial rotations
// TabletMotionFunc -- handle digitizing tablet motion
// TabletButtonFunc -- handle digitizing tablet button hits
// MenuStateFunc -- declare when a pop-up menu is in use
// IdleFunc -- what to do when nothing else is going on
// TimerFunc -- trigger something to happen every so often
glutSetWindow( GrWindow );
glutDisplayFunc( Display );
glutReshapeFunc( Resize );
glutKeyboardFunc( Keyboard );
glutMouseFunc( MouseButton );
glutMotionFunc( MouseMotion );
glutPassiveMotionFunc( NULL );
glutVisibilityFunc( Visibility );
glutEntryFunc( NULL );
glutSpecialFunc( NULL );
glutSpaceballMotionFunc( NULL );
glutSpaceballRotateFunc( NULL );
glutSpaceballButtonFunc( NULL );
glutButtonBoxFunc( NULL );
glutDialsFunc( NULL );
glutTabletMotionFunc( NULL );
glutTabletButtonFunc( NULL );
glutMenuStateFunc( NULL );
// DO NOT SET THE GLUT IDLE FUNCTION HERE !!
// glutIdleFunc( NULL );
// let glui take care of it in InitGlui( )
glutTimerFunc( 0, NULL, 0 );
// init the lighting:
glShadeModel( GL_FLAT );
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, MulArray3( .2f, White ) );
glLightfv( GL_LIGHT0, GL_AMBIENT, Array3( 0., 0., 0. ) );
glLightf ( GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1. );
glLightf ( GL_LIGHT0, GL_LINEAR_ATTENUATION, 0. );
glLightf ( GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0. );
glLightfv( GL_LIGHT0, GL_DIFFUSE, Array3( 1., 1., 1. ) );
glLightfv( GL_LIGHT0, GL_SPECULAR, Array3( 1., 1., 1. ) );
glLightfv( GL_LIGHT1, GL_AMBIENT, Array3( 0., 0., 0. ) );
glLightf ( GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1. );
glLightf ( GL_LIGHT1, GL_LINEAR_ATTENUATION, 0. );
glLightf ( GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0. );
glLightfv( GL_LIGHT1, GL_DIFFUSE, Array3( .8f, .8f, .8f ) );
glLightfv( GL_LIGHT1, GL_SPECULAR, Array3( .8f, .8f, .8f ) );
glEnable( GL_NORMALIZE );
glLightModeli ( GL_LIGHT_MODEL_TWO_SIDE, TRUE );
glMaterialf ( GL_FRONT, GL_SHININESS, 8. );
glMaterialfv( GL_FRONT, GL_SPECULAR, MulArray3( .7f, Array3( 1., 1., 1. ) ) );
Xpos.AddTimeValue( 0.f, 0.f );
Xpos.AddTimeValue( 10.f, 800.f );
Ypos.AddTimeValue( 0.f, 0.f );
Ypos.AddTimeValue( 10.f, 500.f );
Zpos.AddTimeValue( 0.f, 0.f );
Zpos.AddTimeValue( 10.f, 1200.f );
ThetaX.AddTimeValue( 0.f, 0.f );
ThetaX.AddTimeValue( 10.f, 90.f );
ThetaY.AddTimeValue( 0.f, 0.f );
ThetaY.AddTimeValue( 10.f, 180.f );
ThetaZ.AddTimeValue( 0.f, 0.f );
ThetaZ.AddTimeValue( 10.f, 720.f );
}
// initialize the display lists that will not change:
void
InitLists( void )
{
// create the display of the keytimes:
KeytimeList = glGenLists( 1 );
glNewList( KeytimeList, GL_COMPILE );
// I only have 2 keytimes, you will have more
{;
float xpos = Xpos.GetValue( 0. );
float ypos = Ypos.GetValue( 0. );
float zpos = Zpos.GetValue( 0. );
float ax = ThetaX.GetValue( 0. );
float ay = ThetaY.GetValue( 0. );
float az = ThetaZ.GetValue( 0. );
glPushMatrix( );
glTranslatef( xpos, ypos, zpos );
glRotatef( ax, 1., 0., 0. );
glRotatef( ay, 0., 1., 0. );
glRotatef( az, 0., 0., 1. );
glScalef(50., 50., 50.);
glutWireDodecahedron();
glPopMatrix( );
}
{
float xpos = Xpos.GetValue(3.);
float ypos = Ypos.GetValue(3.);
float zpos = Zpos.GetValue(3.);
float ax = ThetaX.GetValue(3.);
float ay = ThetaY.GetValue(3.);
float az = ThetaZ.GetValue(3.);
glPushMatrix();
glTranslatef(xpos, ypos, zpos);
glRotatef(ax, 1., 0., 0.);
glRotatef(ay, 0., 1., 0.);
glRotatef(az, 0., 0., 1.);
glScalef(50., 50., 50.);
glutWireDodecahedron();
glPopMatrix();
}
{
float xpos = Xpos.GetValue(5.);
float ypos = Ypos.GetValue(5.);
float zpos = Zpos.GetValue(5.);
float ax = ThetaX.GetValue(5.);
float ay = ThetaY.GetValue(5.);
float az = ThetaZ.GetValue(5.);
glPushMatrix();
glTranslatef(xpos, ypos, zpos);
glRotatef(ax, 1., 0., 0.);
glRotatef(ay, 0., 1., 0.);
glRotatef(az, 0., 0., 1.);
glScalef(50., 50., 50.);
glutWireDodecahedron();
glPopMatrix();
}
{
float xpos = Xpos.GetValue(7.);
float ypos = Ypos.GetValue(7.);
float zpos = Zpos.GetValue(7.);
float ax = ThetaX.GetValue(7.);
float ay = ThetaY.GetValue(7.);
float az = ThetaZ.GetValue(7.);
glPushMatrix();
glTranslatef(xpos, ypos, zpos);
glRotatef(ax, 1., 0., 0.);
glRotatef(ay, 0., 1., 0.);
glRotatef(az, 0., 0., 1.);
glScalef(50., 50., 50.);
glutWireDodecahedron();
glPopMatrix();
}
{
float xpos = Xpos.GetValue(10.);
float ypos = Ypos.GetValue(10.);
float zpos = Zpos.GetValue(10.);
float ax = ThetaX.GetValue(10.);
float ay = ThetaY.GetValue(10.);
float az = ThetaZ.GetValue(10.);
glPushMatrix();
glTranslatef(xpos, ypos, zpos);
glRotatef(ax, 1., 0., 0.);
glRotatef(ay, 0., 1., 0.);
glRotatef(az, 0., 0., 1.);
glScalef(50., 50., 50.);
glutWireDodecahedron();
glPopMatrix();
}
glEndList( );
// create the object:
ObjectList = glGenLists( 1 );
glNewList( ObjectList, GL_COMPILE );
glMaterialfv( GL_BACK, GL_AMBIENT, MulArray3( .4f, White ) );
glMaterialfv( GL_BACK, GL_DIFFUSE, MulArray3( 1., White ) );
glMaterialfv( GL_BACK, GL_SPECULAR, Array3( 0., 0., 0. ) );
glMaterialf ( GL_BACK, GL_SHININESS, 5.f );
glMaterialfv( GL_BACK, GL_EMISSION, Array3( 0., 0., 0. ) );
glMaterialfv( GL_FRONT, GL_EMISSION, Array3( 0., 0., 0. ) );
glScalef(50., 50., 50.);
glutSolidDodecahedron();
glEndList( );
// create the walls:
WallList = glGenLists( 1 );
glNewList( WallList, GL_COMPILE );
glColor3fv( FLOOR_COLOR );
glBegin( GL_QUADS );
glVertex3f( LEFT_WALL, FLOOR, FRONT_WALL );
glVertex3f( LEFT_WALL, FLOOR, BACK_WALL );
glVertex3f( RIGHT_WALL, FLOOR, BACK_WALL );
glVertex3f( RIGHT_WALL, FLOOR, FRONT_WALL );
glEnd( );
glColor3fv( LEFT_WALL_COLOR );
glBegin( GL_QUADS );
glVertex3f( LEFT_WALL, FLOOR, FRONT_WALL );
glVertex3f( LEFT_WALL, FLOOR, BACK_WALL );
glVertex3f( LEFT_WALL, CEILING, BACK_WALL );
glVertex3f( LEFT_WALL, CEILING, FRONT_WALL );
glEnd( );
glEndList( );
// create the axes:
AxesList = glGenLists( 1 );
glNewList( AxesList, GL_COMPILE );
glColor3fv( AXES_COLOR );
glLineWidth( AXES_WIDTH );
Axes( 400. );
glLineWidth( 1. );
glEndList( );
}
// initialize the glui window:
void
InitGlui( void )
{
// setup the glui window:
glutInitWindowPosition( INIT_WINDOW_SIZE+50, 0 );
Glui = GLUI_Master.create_glui( (char *) GLUITITLE );
Glui->add_statictext( (char *) GLUITITLE );
Glui->add_separator( );
Glui->add_checkbox( "Axes", &AxesOn );
Glui->add_checkbox( "Perspective", &WhichProjection );
Glui->add_checkbox( "Keyframes", &KeytimeOn );
GLUI_Panel * panel = Glui->add_panel( "Animation" );
GLUI_RadioGroup *group = Glui->add_radiogroup_to_panel( panel, &AnimateOn, 0, (GLUI_Update_CB) SetAnimate );
Glui->add_radiobutton_to_group( group, "Off" );
Glui->add_radiobutton_to_group( group, "On" );
panel = Glui->add_panel( "Object Transformation" );
GluiRot = Glui->add_rotation_to_panel( panel, "Rotation", (float *) RotMatrix );
GluiRot->set_spin( 1. );
Glui->add_column_to_panel( panel, FALSE );
GLUI_Translation *scale = Glui->add_translation_to_panel( panel, "Scale", GLUI_TRANSLATION_Y , &Scale2 );
scale->set_speed( 0.01f );
Glui->add_column_to_panel( panel, FALSE );
GLUI_Translation *trans = Glui->add_translation_to_panel( panel, "Trans XY", GLUI_TRANSLATION_XY, &TransXYZ[0] );
trans->set_speed( 0.1f );
Glui->add_column_to_panel( panel, FALSE );
trans = Glui->add_translation_to_panel( panel, "Trans Z", GLUI_TRANSLATION_Z , &TransXYZ[2] );
trans->set_speed( 0.1f );
panel = Glui->add_panel( "", FALSE );
Glui->add_button_to_panel( panel, "Reset", RESET, (GLUI_Update_CB) Buttons );
Glui->add_column_to_panel( panel, FALSE );
Glui->add_button_to_panel( panel, "Quit", QUIT, (GLUI_Update_CB) Buttons );
Glui->add_column_to_panel( panel, FALSE );
Glui->add_checkbox_to_panel( panel, "Debug", &Debug );
// tell glui what graphics window it needs to post a redisplay to:
Glui->set_main_gfx_window( GrWindow );
// set the graphics window's idle function:
if (AnimateOn)
{
GLUI_Master.set_glutIdleFunc(Animate);
}
else
{
GLUI_Master.set_glutIdleFunc(NULL);
}
}
// the keyboard callback:
void
Keyboard( unsigned char c, int x, int y )
{
if( Debug )
fprintf( stderr, "DoKeyboard: '%c' (0x%0x)\n", c, c );
switch( c )
{
case 'q':
case 'Q':
case ESCAPE:
Buttons( QUIT ); // will not return here
break; // happy compiler
case 'f':
case 'F':
KeytimeOn = ! KeytimeOn;
break;
case 'o':
case 'O':
WhichProjection = ORTHO;
break;
case 'p':
case 'P':
WhichProjection = PERSP;
break;
default:
fprintf( stderr, "Don't know what to do with keyboard hit:: '%c' (0x%0x)\n", c, c );
}
// synchronize the GLUI display with the variables:
Glui->sync_live( );
// force a call to Display( ):
glutSetWindow( GrWindow );
glutPostRedisplay( );
}
// called when the mouse button transitions down or up:
void
MouseButton
(
int button, // GLUT_*_BUTTON
int state, // GLUT_UP or GLUT_DOWN
int x, int y // where mouse was when button hit
)
{
if( Debug )
fprintf( stderr, "MouseButton: %d, %d, %d, %d\n", button, state, x, y );
// get the proper button bit mask:
int b; // LEFT, MIDDLE, or RIGHT
switch( button )
{
case GLUT_LEFT_BUTTON:
b = LEFT; break;
case GLUT_MIDDLE_BUTTON:
b = MIDDLE; break;
case GLUT_RIGHT_BUTTON:
b = RIGHT; break;
default:
b = 0;
fprintf( stderr, "Unknown mouse button: %d\n", button );
}
// button down sets the bit, up clears the bit:
if( state == GLUT_DOWN )
{
Xmouse = x;
Ymouse = y;
ActiveButton |= b; // set the proper bit
}
else
ActiveButton &= ~b; // clear the proper bit
}
// called when the mouse moves while a button is down:
void
MouseMotion( int x, int y )
{
if( Debug )
fprintf( stderr, "MouseMotion: %d, %d\n", x, y );
int dx = x - Xmouse; // change in mouse coords
int dy = y - Ymouse;
if( ActiveButton & LEFT )
{
Xrot += ( ANGFACT*dy );
Yrot += ( ANGFACT*dx );
}
if( ActiveButton & MIDDLE )
{
Scale += SCLFACT * (float) ( dx - dy );
// keep object from turning inside-out or disappearing:
if( Scale < MINSCALE )
Scale = MINSCALE;
}
Xmouse = x; // new current position
Ymouse = y;
glutSetWindow( GrWindow );
glutPostRedisplay( );
}
// reset the transformations and the colors:
//
// this only sets the global variables --
// the glut main loop is responsible for redrawing the scene
void
Reset( void )
{
AnimateOn = TRUE;
ActiveButton = 0;
AxesOn = GLUITRUE;
Debug = GLUIFALSE;
Scale = 1.0;
Scale2 = 0.0; // because add 1. to it in Display( )
WhichProjection = PERSP;
Xrot = Yrot = 0.;
TransXYZ[0] = TransXYZ[1] = TransXYZ[2] = 0.;
RotMatrix[0][1] = RotMatrix[0][2] = RotMatrix[0][3] = 0.;
RotMatrix[1][0] = RotMatrix[1][2] = RotMatrix[1][3] = 0.;
RotMatrix[2][0] = RotMatrix[2][1] = RotMatrix[2][3] = 0.;
RotMatrix[3][0] = RotMatrix[3][1] = RotMatrix[3][3] = 0.;
RotMatrix[0][0] = RotMatrix[1][1] = RotMatrix[2][2] = RotMatrix[3][3] = 1.;
glutSetWindow( GrWindow );
glutPostRedisplay( );
}
// called when user resizes the window:
void
Resize( int width, int height )
{
// don't really need to do anything since window size is
// checked each time in Display( ):
glutSetWindow( GrWindow );
glutPostRedisplay( );
}
void
SetAnimate( int id )
{
if( AnimateOn )
{
GLUI_Master.set_glutIdleFunc( Animate );
}
else
{
GLUI_Master.set_glutIdleFunc( NULL );
}
}
// handle a change to the window's visibility:
void
Visibility( int state )
{
if( state == GLUT_VISIBLE )
{
glutSetWindow( GrWindow );
glutPostRedisplay( );
}
else
{
// could optimize by keeping track of the fact
// that the window is not visible and avoid
// animating or redrawing it ...
}
}
// the stroke characters 'X' 'Y' 'Z' :
static float xx[ ] = {
0., 1., 0., 1.
};
static float xy[ ] = {
-.5, .5, .5, -.5
};
static int xorder[ ] = {
1, 2, -3, 4
};
static float yx[ ] = {
0., 0., -.5, .5
};
static float yy[ ] = {
0., .6f, 1., 1.
};
static int yorder[ ] = {