-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2.cpp
1361 lines (1036 loc) · 27.9 KB
/
p2.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"
#include "heli.550"
// 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 2 -- Kaiyuan Fan" };
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 = { 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,
PERSPIN
};
//which viewmode:
enum ViewModes
{
OUTVIEW,
INSIDEVIEW
};
// 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
};
// 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:
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 or PERSPIN
int WhichViewMode; //OUTVIEW or INSIDEVIEW
int Xmouse, Ymouse; // mouse values
float Xrot, Yrot; // rotation angles in degrees
float BladeAngle; //rotation blade angles in degrees
float Time; //global floating-point variable
#define MS_IN_THE_ANIMATION_CYCLE 1000 //animation cycle
bool Frozen; //
// function prototypes:
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 DoViewModeMenu( 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 Axes( float );
void HsvRgb( float[3], float [3] );
void Top_Blade();
void Rear_Blade();
// 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( );
// 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
void
Animate()
{
// put animation stuff in here -- change some global variables
// for Display( ) to find:
int ms = glutGet(GLUT_ELAPSED_TIME); // milliseconds
ms %= MS_IN_THE_ANIMATION_CYCLE;
Time = (float)ms / (float)MS_IN_THE_ANIMATION_CYCLE; // [ 0., 1. )
BladeAngle = Time*360.;
// force a call to Display( ) next time it is convenient:
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
// 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:
if (WhichViewMode == INSIDEVIEW) {
gluLookAt(-0.4, 2, -4.9, 0, 0, -10, 0., 1., 0.);
}
else
gluLookAt(14., 2., 4., 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 )
{
glColor3fv( &Colors[WhichColor][0] );
glCallList( AxesList );
}
// since we are using glScalef( ), be sure normals get unitized:
glEnable( GL_NORMALIZE );
// draw the current object:
glCallList( BoxList );
//top blade
glPushMatrix();
glTranslatef(0., 2.9, -2.);
glRotatef(BladeAngle, 0., 1., 0.); // rotation
glRotatef(90., 1., 0, 0); // set correct orientation
Top_Blade();
glPopMatrix();
//rear blade
glPushMatrix();
glTranslatef(.5, 2.5, 9.);
glRotatef(3 * BladeAngle, 1., 0., 0.); //rotation
glRotatef(90., 0., 1., 0.);// set correct orientation
Rear_Blade();
glPopMatrix();
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 );
glPushMatrix();
glColor3f( 0., 1., 1. );
glTranslatef(0., 0, -10.);
DoRasterString( 0., 1., 0., "Satellite" );
glPopMatrix();
// 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., "Project 2 - Kaiyuan Fan" );
// 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
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( );
}
void
DoViewModeMenu(int id)
{
WhichViewMode = 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 viewmodemenu = glutCreateMenu(DoViewModeMenu);
glutAddMenuEntry("Outside", OUTVIEW);
glutAddMenuEntry("Inside", INSIDEVIEW);
int mainmenu = glutCreateMenu( DoMainMenu );
glutAddSubMenu("View Modes", viewmodemenu);
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); //set animation
// 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
}
//Left blade of the satellite
void leftBlade() {
glBegin(GL_QUADS);
glColor3f(0., 0., 1.);
glVertex3f(-1, 0,-1);
glVertex3f(-1,0,1);
glVertex3f(-4,0,1);
glVertex3f(-4,0,-1);
glEnd();
}
//Right blade of the satelite
void rightBlade() {
glBegin(GL_QUADS);
glColor3f(1, 0, 0.);
glVertex3f(1, 0, -1);
glVertex3f(1,0,1);
glVertex3f(4,0,1);
glVertex3f(4,0,-1);
glEnd();
}
void Head() {
glBegin(GL_TRIANGLES);
glColor3f(1, 1, 1);
glVertex3f(1, 0, -1);
glVertex3f(-1, 0, -1);
glVertex3f(0, 0, -3);
glEnd();
}
//top blade of helicopter
void Top_Blade() {
#define BLADE_RADIUS 5.
#define BLADE_WIDTH 1.5
// draw the helicopter blade with radius BLADE_RADIUS and
// width BLADE_WIDTH centered at (0.,0.,0.) in the XY plane
glColor3f(1, 1, 1); // white
glBegin(GL_TRIANGLES);
glVertex2f(BLADE_RADIUS, BLADE_WIDTH / 2.);
glVertex2f(0., 0.);
glVertex2f(BLADE_RADIUS, -BLADE_WIDTH / 2.);
glVertex2f(-BLADE_RADIUS, -BLADE_WIDTH / 2.);
glVertex2f(0., 0.);
glVertex2f(-BLADE_RADIUS, BLADE_WIDTH / 2.);
glEnd();
}
//rear blade of helicopter
void Rear_Blade() {
#define BLADE_RADIUS 1.5
#define BLADE_WIDTH 1
// draw the helicopter blade with radius BLADE_RADIUS and
// width BLADE_WIDTH centered at (0.,0.,0.) in the XY plane
glColor3f(1, 1, 1);
glBegin(GL_TRIANGLES);
glVertex2f(BLADE_RADIUS, BLADE_WIDTH / 2.);
glVertex2f(0., 0.);
glVertex2f(BLADE_RADIUS, -BLADE_WIDTH / 2.);
glVertex2f(-BLADE_RADIUS, -BLADE_WIDTH / 2.);
glVertex2f(0., 0.);
glVertex2f(-BLADE_RADIUS, BLADE_WIDTH / 2.);
glEnd();
}
//Lighting of Helicopter
float
Dot(float v1[3], float v2[3])
{
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
}
void
Cross(float v1[3], float v2[3], float vout[3])
{
float tmp[3];
tmp[0] = v1[1] * v2[2] - v2[1] * v1[2];
tmp[1] = v2[0] * v1[2] - v1[0] * v2[2];
tmp[2] = v1[0] * v2[1] - v2[0] * v1[1];
vout[0] = tmp[0];
vout[1] = tmp[1];
vout[2] = tmp[2];
}
float
Unit(float vin[3], float vout[3])
{
float dist = vin[0] * vin[0] + vin[1] * vin[1] + vin[2] * vin[2];
if (dist > 0.0)
{
dist = sqrt(dist);
vout[0] = vin[0] / dist;
vout[1] = vin[1] / dist;
vout[2] = vin[2] / dist;
}
else
{
vout[0] = vin[0];
vout[1] = vin[1];
vout[2] = vin[2];
}
return dist;
}
// 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 satellite:
BoxList = glGenLists( 1 );
glNewList( BoxList, GL_COMPILE );
glPushMatrix();
glTranslatef(0, 0, -10);
leftBlade();//leftblade
rightBlade();//rightblade
Head();//head
glColor3f(1, 1,0.);
glutSolidSphere (1, 36, 10);//middle sphere
//creating the tail
glBegin(GL_LINE_LOOP);
glColor3f(0., 1., 0.);
for (int i = 0; i < NUMSEGS; i++)
{
glVertex3f(1*cos(ang), 1*sin(ang), 1);
ang += dang;
}
glEnd();
glPopMatrix();
//helicopter
int i;
struct point *p0, *p1, *p2;
struct tri *tp;
float p01[3], p02[3], n[3];
glPushMatrix();
glTranslatef(0., -1., 0.);
glRotatef(97., 0., 1., 0.);
glRotatef(-15., 0., 0., 1.);
glBegin(GL_TRIANGLES);
for (i = 0, tp = Helitris; i < Helintris; i++, tp++)
{
p0 = &Helipoints[tp->p0];
p1 = &Helipoints[tp->p1];
p2 = &Helipoints[tp->p2];
// fake "lighting" from above:
p01[0] = p1->x - p0->x;
p01[1] = p1->y - p0->y;
p01[2] = p1->z - p0->z;
p02[0] = p2->x - p0->x;
p02[1] = p2->y - p0->y;
p02[2] = p2->z - p0->z;
Cross(p01, p02, n);
Unit(n, n);
n[1] = fabs(n[1]);
n[1] += .25;
if (n[1] > 1.)
n[1] = 1.;
glColor3f(0., n[1], 0.);
glVertex3f(p0->x, p0->y, p0->z);
glVertex3f(p1->x, p1->y, p1->z);
glVertex3f(p2->x, p2->y, p2->z);
}
glEnd();
glPopMatrix();
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 'f':
case 'F':
Frozen = !Frozen;
if (Frozen)
glutIdleFunc(NULL);
else
glutIdleFunc(Animate);// call rotation; unfreeze
break;
case 'o':
case 'O':