-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4.cpp
1654 lines (1277 loc) · 34.7 KB
/
p4.cpp
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>
#include <ctype.h>
#define _USE_MATH_DEFINES
#include <math.h>
#ifdef WIN32
#include <windows.h>
#pragma warning(disable:4996)
#include "glew.h"
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
// This is a sample OpenGL / GLUT program
//
// The objective is to draw a 3d object and change the color of the axes
// with a glut menu
//
// The left mouse button does rotation
// The middle mouse button does scaling
// The user interface allows:
// 1. The axes to be turned on and off
// 2. The color of the axes to be changed
// 3. Debugging to be turned on and off
// 4. Depth cueing to be turned on and off
// 5. The projection to be changed
// 6. The transformations to be reset
// 7. The program to quit
//
// Author: Joe Graphics
// NOTE: There are a lot of good reasons to use const variables instead
// of #define's. However, Visual C++ does not allow a const variable
// to be used as an array size or as the case in a switch( ) statement. So in
// the following, all constants are const variables except those which need to
// be array sizes or cases in switch( ) statements. Those are #defines.
// title of these windows:
const char *WINDOWTITLE = { "Project 4 -- Kaiyuan Fan" };
const char *GLUITITLE = { "User Interface Window" };
const char * Texture0 = "worldtex.bmp";
// 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 = { 600 };
// size of the box:
const float BOXSIZE = { 2.f };
// 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:
enum Projections
{
ORTHO,
PERSP
};
// which button:
enum ButtonVals
{
RESET,
QUIT
};
// window background color (rgba):
const GLfloat BACKCOLOR[ ] = { 0., 0., 0., 1. };
// line width for the axes:
const GLfloat AXES_WIDTH = { 3. };
// the color numbers:
// this order must match the radio button order
enum Colors
{
RED,
YELLOW,
GREEN,
CYAN,
BLUE,
MAGENTA,
WHITE,
BLACK
};
char * ColorNames[ ] =
{
"Red",
"Yellow",
"Green",
"Cyan",
"Blue",
"Magenta",
"White",
"Black"
};
// the color definitions:
// this order must match the menu order
const GLfloat Colors[ ][3] =
{
{ 1., 0., 0. }, // red
{ 1., 1., 0. }, // yellow
{ 0., 1., 0. }, // green
{ 0., 1., 1. }, // cyan
{ 0., 0., 1. }, // blue
{ 1., 0., 1. }, // magenta
{ 1., 1., 1. }, // white
{ 0., 0., 0. }, // black
};
struct bmfh
{
short bfType;
int bfSize;
short bfReserved1;
short bfReserved2;
int bfOffBits;
} FileHeader;
struct bmih
{
int biSize;
int biWidth;
int biHeight;
short biPlanes;
short biBitCount;
int biCompression;
int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
int biClrUsed;
int biClrImportant;
} InfoHeader;
const int birgb = { 0 };
// fog parameters:
const GLfloat FOGCOLOR[4] = { .0, .0, .0, 1. };
const GLenum FOGMODE = { GL_LINEAR };
const GLfloat FOGDENSITY = { 0.30f };
const GLfloat FOGSTART = { 1.5 };
const GLfloat FOGEND = { 4. };
// non-constant global variables:
float Time;
#define MS_PER_CYCLE 10000;
int ActiveButton; // current button that is down
GLuint AxesList; // list to hold the axes
int AxesOn; // != 0 means to draw the axes
int DebugOn; // != 0 means to print debugging info
int DepthCueOn; // != 0 means to use intensity depth cueing
int DepthBufferOn; // != 0 means to use the z-buffer
int DepthFightingOn; // != 0 means to use the z-buffer
//GLuint BoxList; // object display list
int MainWindow; // window id for main graphics window
float Scale; // scaling factor
int WhichColor; // index into Colors[ ]
int WhichProjection; // ORTHO or PERSP
int Xmouse, Ymouse; // mouse values
float Xrot, Yrot; // rotation angles in degrees
int ReadInt(FILE *);
short ReadShort(FILE *);
//new variables
bool Freeze;
bool Distort; // global -- true means to distort the texture
GLuint tex0;
bool Light0On, Light1On, Light2On;
float Light0x, Light0y, Light0z;
// function prototypes:
unsigned char * BmpToTexture(char *, int *, int *);
struct point * PtsPointer(int, int);
void MjbSphere(float, int, int);
void DrawPoint(struct point *p);
void Animate( );
void Display( );
void DoAxesMenu( int );
void DoColorMenu( int );
void DoDepthBufferMenu( int );
void DoDepthFightingMenu( int );
void DoDepthMenu( int );
void DoDebugMenu( int );
void DoMainMenu( int );
void DoProjectMenu( int );
void DoRasterString( float, float, float, char * );
void DoStrokeString( float, float, float, float, char * );
float ElapsedSeconds( );
void InitGraphics( );
void InitLists( );
void InitMenus( );
void Keyboard( unsigned char, int, int );
void MouseButton( int, int, int, int );
void MouseMotion( int, int );
void Reset( );
void Resize( int, int );
void Visibility( int );
void init_texture();
void Axes( float );
void HsvRgb( float[3], float [3] );
//void set_texture(const char *, GLuint *);
// main program:
int
main( int argc, char *argv[ ] )
{
// turn on the glut package:
// (do this before checking argc and argv since it might
// pull some command line arguments out)
glutInit( &argc, argv );
// setup all the graphics stuff:
InitGraphics( );
//ini texture
init_texture();
// create the display structures that will not change:
InitLists( );
// init all the global variables used by Display( ):
// this will also post a redisplay
Reset( );
// setup all the user interface stuff:
InitMenus( );
// draw the scene once and wait for some interaction:
// (this will never return)
glutSetWindow( MainWindow );
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
float White[] = { 1.,1.,1.,1. };
float *
MulArray3(float factor, float array0[3])
{
static float array[4];
array[0] = factor * array0[0];
array[1] = factor * array0[1];
array[2] = factor * array0[2];
array[3] = 1.;
return array;
}
float *
Array3(float a, float b, float c)
{
static float array[4];
array[0] = a;
array[1] = b;
array[2] = c;
array[3] = 1.;
return array;
}
void
SetMaterial(float r, float g, float b, float shininess)
{
glMaterialfv(GL_BACK, GL_EMISSION, Array3(0., 0., 0.));
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_FRONT, GL_EMISSION, Array3(0., 0., 0.));
glMaterialfv(GL_FRONT, GL_AMBIENT, Array3(r, g, b));
glMaterialfv(GL_FRONT, GL_DIFFUSE, Array3(r, g, b));
glMaterialfv(GL_FRONT, GL_SPECULAR, MulArray3(.8f, White));
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
}
void
SetPointLight(int ilight, float x, float y, float z, float r, float g, float b)
{
glLightfv(ilight, GL_POSITION, Array3(x, y, z));
glLightfv(ilight, GL_AMBIENT, Array3(0., 0., 0.));
glLightfv(ilight, GL_DIFFUSE, Array3(r, g, b));
glLightfv(ilight, GL_SPECULAR, Array3(r, g, b));
glLightf(ilight, GL_CONSTANT_ATTENUATION, 1.);
glLightf(ilight, GL_LINEAR_ATTENUATION, 0.);
glLightf(ilight, GL_QUADRATIC_ATTENUATION, 0.);
glEnable(ilight);
}
void
SetSpotLight(int ilight, float x, float y, float z, float xdir, float ydir, float zdir, float r, float g, float b)
{
glLightfv(ilight, GL_POSITION, Array3(x, y, z));
glLightfv(ilight, GL_SPOT_DIRECTION, Array3(xdir, ydir, zdir));
glLightf(ilight, GL_SPOT_EXPONENT, 1.);
glLightf(ilight, GL_SPOT_CUTOFF, 45.);
glLightfv(ilight, GL_AMBIENT, Array3(0., 0., 0.));
glLightfv(ilight, GL_DIFFUSE, Array3(r, g, b));
glLightfv(ilight, GL_SPECULAR, Array3(r, g, b));
glLightf(ilight, GL_CONSTANT_ATTENUATION, 1.);
glLightf(ilight, GL_LINEAR_ATTENUATION, 0.);
glLightf(ilight, GL_QUADRATIC_ATTENUATION, 0.);
glEnable(ilight);
}
void Lighting() {
//point, moving, BLUE, position= 0., 0., 7.4 - 18. * Time
SetPointLight(GL_LIGHT0, 0., 0., 7.4 - 18. * Time, 0.f, 0.f, 1.f);
// point, stationary, WHITE, position = 0.f, 3.f, -5.f
SetPointLight(GL_LIGHT1, 0.f, 3.f, -5.f, 1.f, 1.f, 1.f);
// spotlight, stationary, RED, position= 0.f, 2.f, 3.f
SetSpotLight(GL_LIGHT2, 0.f, 2.f, 3.f, 0., 0., -1., 1.f, 0.f, 0.f);
if (Light0On==TRUE) {
glEnable(GL_LIGHT0);
}
else{
glDisable(GL_LIGHT0);
}
if (Light1On == TRUE) {
glEnable(GL_LIGHT1);
}
else{
glDisable(GL_LIGHT1);
}
if (Light2On == TRUE) {
glEnable(GL_LIGHT2);
}
else{
glDisable(GL_LIGHT2);
}
}
void Objects() {
//Torus
//stationary, GL_SMOOTH, shiny
glShadeModel(GL_SMOOTH);
SetMaterial(0., 1., 0, 64);
glPushMatrix();
glTranslatef(0., 0., -3.);
glutSolidTorus(1., 3., 100, 100);
glPopMatrix();
// Cube
//moving, GL_FLAT, shiny
glShadeModel(GL_FLAT);
SetMaterial(1., 0., 0., 64);
glPushMatrix();
glTranslatef(0., 0., 8.f - 18.f * Time);
glutSolidCube(1.);
glPopMatrix();
//Earth sphere with texture
//stationary,GL_FLAT, DULL
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex0);
glShadeModel(GL_FLAT);
SetMaterial(1., 1., 1., 0.f);
glTranslatef(5.f, 0.f, 3.f);
MjbSphere(1.f, 100, 100);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
//light0 sphere
glPushMatrix();
glColor3f(0., 0., 1.); //BLUE
glTranslatef(0., 0., 7.4 - 18. * Time);
glutSolidSphere(.1, 100, 100);
glPopMatrix();
//light1 sphere
glPushMatrix();
glColor3f(1.f, 1.f, 1.f);//WHITE
glTranslatef(0.f, 3.f, -5.f);
glutSolidSphere(.1, 100, 100);
glPopMatrix();
//light2 sphere
glPushMatrix();
glColor3f(1.f, 0.f, 0.f);//RED
glTranslatef(0.f, 2.f, 3.f);
glutSolidSphere(.1, 100, 100);
glPopMatrix();
glEnable(GL_LIGHTING);
}
void
Animate( )
{
// put animation stuff in here -- change some global variables
// for Display( ) to find:
int ms = glutGet(GLUT_ELAPSED_TIME);
ms %= MS_PER_CYCLE;
Time = (float)ms / (float)MS_PER_CYCLE; // [0.,1.)
// force a call to Display( ) next time it is convenient:
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
void init_texture() {
int tex0_width;
int tex0_height;
unsigned char * Texture0 = BmpToTexture("worldtex.bmp", &tex0_width, &tex0_height);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &tex0); // assign binding ¡°handles¡±
glBindTexture(GL_TEXTURE_2D, tex0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex0_width, tex0_height, 0, GL_RGB, GL_UNSIGNED_BYTE, Texture0);
}
// draw the complete scene:
//
void
Display( )
{
if( DebugOn != 0 )
{
fprintf( stderr, "Display\n" );
}
// set which window we want to do the graphics into:
glutSetWindow( MainWindow );
// erase the background:
glDrawBuffer( GL_BACK );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
if( DepthBufferOn != 0 )
glEnable( GL_DEPTH_TEST );
else
glDisable( GL_DEPTH_TEST );
// specify shading to be flat:
glShadeModel( GL_FLAT );
// set the viewport to a square centered in the window:
GLsizei vx = glutGet( GLUT_WINDOW_WIDTH );
GLsizei vy = glutGet( GLUT_WINDOW_HEIGHT );
GLsizei v = vx < vy ? vx : vy; // minimum dimension
GLint xl = ( vx - v ) / 2;
GLint yb = ( vy - v ) / 2;
glViewport( xl, yb, v, v );
// set the viewing volume:
// remember that the Z clipping values are actually
// given as DISTANCES IN FRONT OF THE EYE
// USE gluOrtho2D( ) IF YOU ARE DOING 2D !
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
if( WhichProjection == ORTHO )
glOrtho( -3., 3., -3., 3., 0.1, 1000. );
else
gluPerspective( 90., 1., 0.1, 1000. );
// place the objects into the scene:
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
// set the eye position, look-at position, and up-vector:
gluLookAt( 12., 0., 3., 0., 0., 0., 0., 1., 0. );
// rotate the scene:
glRotatef( (GLfloat)Yrot, 0., 1., 0. );
glRotatef( (GLfloat)Xrot, 1., 0., 0. );
// uniformly scale the scene:
if( Scale < MINSCALE )
Scale = MINSCALE;
glScalef( (GLfloat)Scale, (GLfloat)Scale, (GLfloat)Scale );
// set the fog parameters:
if( DepthCueOn != 0 )
{
glFogi( GL_FOG_MODE, FOGMODE );
glFogfv( GL_FOG_COLOR, FOGCOLOR );
glFogf( GL_FOG_DENSITY, FOGDENSITY );
glFogf( GL_FOG_START, FOGSTART );
glFogf( GL_FOG_END, FOGEND );
glEnable( GL_FOG );
}
else
{
glDisable( GL_FOG );
}
// possibly draw the axes:
if( AxesOn != 0 )
{
glColor3f( 0.f,0.f,0. );
glCallList( AxesList );
}
// since we are using glScalef( ), be sure normals get unitized:
glEnable( GL_NORMALIZE );
// draw the current object:
//glCallList( BoxList );
//call lighting functions
Lighting();
Objects();
//glEnable(GL_LIGHTING);
if( DepthFightingOn != 0 )
{
glPushMatrix( );
glRotatef( 90., 0., 1., 0. );
//glCallList( BoxList );
glPopMatrix( );
}
// draw some gratuitous text that just rotates on top of the scene:
glDisable( GL_DEPTH_TEST );
glColor3f( 0., 1., 1. );
DoRasterString( 0., 1., 0., "" );
// draw some gratuitous text that is fixed on the screen:
//
// the projection matrix is reset to define a scene whose
// world coordinate system goes from 0-100 in each axis
//
// this is called "percent units", and is just a convenience
//
// the modelview matrix is reset to identity as we don't
// want to transform these coordinates
glDisable( GL_DEPTH_TEST );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluOrtho2D( 0., 100., 0., 100. );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
glColor3f( 1., 1., 1. );
DoRasterString( 5., 5., 0., "Lighting" );
// swap the double-buffered framebuffers:
glutSwapBuffers( );
// be sure the graphics buffer has been sent:
// note: be sure to use glFlush( ) here, not glFinish( ) !
glFlush( );
}
void
DoAxesMenu( int id )
{
AxesOn = id;
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
void
DoColorMenu( int id )
{
WhichColor = id - RED;
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
void
DoDebugMenu( int id )
{
DebugOn = id;
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
void
DoTexMenu(int id)
{
glutSetWindow(MainWindow);
glutPostRedisplay();
}
void
DoDepthBufferMenu( int id )
{
DepthBufferOn = id;
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
void
DoDepthFightingMenu( int id )
{
DepthFightingOn = id;
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
void
DoDepthMenu( int id )
{
DepthCueOn = id;
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
// main menu callback:
void
DoMainMenu( int id )
{
switch( id )
{
case RESET:
Reset( );
break;
case QUIT:
// gracefully close out the graphics:
// gracefully close the graphics window:
// gracefully exit the program:
glutSetWindow( MainWindow );
glFinish( );
glutDestroyWindow( MainWindow );
exit( 0 );
break;
default:
fprintf( stderr, "Don't know what to do with Main Menu ID %d\n", id );
}
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
void
DoProjectMenu( int id )
{
WhichProjection = id;
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
// use glut to display a string of characters using a raster font:
void
DoRasterString( float x, float y, float z, char *s )
{
glRasterPos3f( (GLfloat)x, (GLfloat)y, (GLfloat)z );
char c; // one character to print
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 )
{
glPushMatrix( );
glTranslatef( (GLfloat)x, (GLfloat)y, (GLfloat)z );
float sf = ht / ( 119.05f + 33.33f );
glScalef( (GLfloat)sf, (GLfloat)sf, (GLfloat)sf );
char c; // one character to print
for( ; ( c = *s ) != '\0'; s++ )
{
glutStrokeCharacter( GLUT_STROKE_ROMAN, c );
}
glPopMatrix( );
}
// return the number of seconds since the start of the program:
float
ElapsedSeconds( )
{
// get # of milliseconds since the start of the program:
int ms = glutGet( GLUT_ELAPSED_TIME );
// convert it to seconds:
return (float)ms / 1000.f;
}
// initialize the glui window:
void
InitMenus( )
{
glutSetWindow( MainWindow );
int numColors = sizeof( Colors ) / ( 3*sizeof(int) );
int colormenu = glutCreateMenu( DoColorMenu );
for( int i = 0; i < numColors; i++ )
{
glutAddMenuEntry( ColorNames[i], i );
}
int axesmenu = glutCreateMenu( DoAxesMenu );
glutAddMenuEntry( "Off", 0 );
glutAddMenuEntry( "On", 1 );
int depthcuemenu = glutCreateMenu( DoDepthMenu );
glutAddMenuEntry( "Off", 0 );
glutAddMenuEntry( "On", 1 );
int depthbuffermenu = glutCreateMenu( DoDepthBufferMenu );
glutAddMenuEntry( "Off", 0 );
glutAddMenuEntry( "On", 1 );
int depthfightingmenu = glutCreateMenu( DoDepthFightingMenu );
glutAddMenuEntry( "Off", 0 );
glutAddMenuEntry( "On", 1 );
int debugmenu = glutCreateMenu( DoDebugMenu );
glutAddMenuEntry( "Off", 0 );
glutAddMenuEntry( "On", 1 );
int projmenu = glutCreateMenu( DoProjectMenu );
glutAddMenuEntry( "Orthographic", ORTHO );
glutAddMenuEntry( "Perspective", PERSP );
int mainmenu = glutCreateMenu( DoMainMenu );
glutAddSubMenu( "Axes", axesmenu);
glutAddSubMenu( "Colors", colormenu);
glutAddSubMenu( "Depth Buffer", depthbuffermenu);
glutAddSubMenu( "Depth Fighting",depthfightingmenu);
glutAddSubMenu( "Depth Cue", depthcuemenu);
glutAddSubMenu( "Projection", projmenu );
glutAddMenuEntry( "Reset", RESET );
glutAddSubMenu( "Debug", debugmenu);
glutAddMenuEntry( "Quit", QUIT );
// attach the pop-up menu to the right mouse button:
glutAttachMenu( GLUT_RIGHT_BUTTON );
}
// initialize the glut and OpenGL libraries:
// also setup display lists and callback functions
void
InitGraphics( )
{
// request the display modes:
// ask for red-green-blue-alpha color, double-buffering, and z-buffering:
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
// set the initial window configuration:
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( INIT_WINDOW_SIZE, INIT_WINDOW_SIZE );
// open the window and set its title:
MainWindow = glutCreateWindow( WINDOWTITLE );
glutSetWindowTitle( WINDOWTITLE );
// set the framebuffer clear values:
glClearColor( BACKCOLOR[0], BACKCOLOR[1], BACKCOLOR[2], BACKCOLOR[3] );
// setup the callback functions:
// 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
// TimerFunc -- trigger something to happen a certain time from now
// IdleFunc -- what to do when nothing else is going on
glutSetWindow( MainWindow );
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 );
glutTimerFunc( -1, NULL, 0 );
glutIdleFunc( Animate );
// init glew (a window must be open to do this):
#ifdef WIN32
GLenum err = glewInit( );
if( err != GLEW_OK )
{
fprintf( stderr, "glewInit Error\n" );
}
else
fprintf( stderr, "GLEW initialized OK\n" );
fprintf( stderr, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
#endif
}
// initialize the display lists that will not change:
// (a display list is a way to store opengl commands in
// memory so that they can be played back efficiently at a later time
// with a call to glCallList( )
void
InitLists( )
{
float NUMSEGS = 100;
float dang = 2. * M_PI / ((float)(NUMSEGS - 1));
float ang = 0.;
glutSetWindow( MainWindow );
// create the object:
//BoxList = glGenLists( 1 );
//glNewList( BoxList, GL_COMPILE );
/*set_texture();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 1);
MjbSphere(1., 100, 100); // draw the sphere.*/
//glEndList( );
// create the axes:
AxesList = glGenLists( 1 );
glNewList( AxesList, GL_COMPILE );
glLineWidth( AXES_WIDTH );
Axes( 1.5 );
glLineWidth( 1. );
glEndList( );
}
// the keyboard callback:
void
Keyboard( unsigned char c, int x, int y )
{
if( DebugOn != 0 )
fprintf( stderr, "Keyboard: '%c' (0x%0x)\n", c, c );
switch( c )
{
case 'o':
case 'O':
WhichProjection = ORTHO;
break;
case 'p':
case 'P':
WhichProjection = PERSP;
break;
case 'q':
case 'Q':
case ESCAPE:
DoMainMenu( QUIT ); // will not return here
break; // happy compiler
case 'f':
case 'F':
Freeze = !Freeze;
if (Freeze)
glutIdleFunc(NULL);
else
glutIdleFunc(Animate);
break;
case '0':