2 * Simple engine demo (crankshaft, pistons, connecting rods)
8 #define GL_GLEXT_PROTOTYPES
16 #include "trackball.h"
19 #define DEG_TO_RAD(DEG) ((DEG) * M_PI / 180.0)
21 #define TEXTURE_FILE "../images/reflect.rgb"
23 /* Target engine speed: */
24 const int RPM
= 100.0;
40 float CrankPlateThickness
;
42 float CrankJournalRadius
;
43 float CrankJournalLength
;
44 float ConnectingRodLength
;
45 float ConnectingRodThickness
;
46 /* display list IDs */
58 /* When mouse is moving: */
59 GLboolean Rotating
, Translating
;
90 static GLfloat Theta
= 0.0;
92 static const GLfloat PistonColor
[4] = { 1.0, 0.5, 0.5, 1.0 };
93 static const GLfloat ConnRodColor
[4] = { 0.7, 1.0, 0.7, 1.0 };
94 static const GLfloat CrankshaftColor
[4] = { 0.7, 0.7, 1.0, 1.0 };
95 static const GLfloat BlockColor
[4] = {0.8, 0.8, 0.8, 0.75 };
97 static GLuint TextureObj
;
98 static GLint WinWidth
= 800, WinHeight
= 500;
100 static ViewInfo View
;
101 static RenderInfo Render
;
103 #define NUM_ENGINES 3
104 static Engine Engines
[NUM_ENGINES
] =
111 0.5, /* PistonRadius */
112 0.6, /* PistonHeight */
113 0.1, /* WristPinRadius */
115 0.2, /* CrankPlateThickness */
116 0.25, /* CrankPinRadius */
117 0.3, /* CrankJournalRadius */
118 0.4, /* CrankJournalLength */
119 1.5, /* ConnectingRodLength */
120 0.1 /* ConnectingRodThickness */
127 0.5, /* PistonRadius */
128 0.6, /* PistonHeight */
129 0.1, /* WristPinRadius */
131 0.2, /* CrankPlateThickness */
132 0.25, /* CrankPinRadius */
133 0.3, /* CrankJournalRadius */
134 0.4, /* CrankJournalLength */
135 1.5, /* ConnectingRodLength */
136 0.1 /* ConnectingRodThickness */
143 0.5, /* PistonRadius */
144 0.6, /* PistonHeight */
145 0.1, /* WristPinRadius */
147 0.2, /* CrankPlateThickness */
148 0.25, /* CrankPinRadius */
149 0.3, /* CrankJournalRadius */
150 0.4, /* CrankJournalLength */
151 1.5, /* ConnectingRodLength */
152 0.1 /* ConnectingRodThickness */
156 static int CurEngine
= 0;
161 InitViewInfo(ViewInfo
*view
)
163 view
->Rotating
= GL_FALSE
;
164 view
->Translating
= GL_FALSE
;
165 view
->StartX
= view
->StartY
= 0;
166 view
->Distance
= 12.0;
167 view
->StartDistance
= 0.0;
168 view
->CurQuat
[0] = -0.194143;
169 view
->CurQuat
[1] = 0.507848;
170 view
->CurQuat
[2] = 0.115245;
171 view
->CurQuat
[3] = 0.831335;
176 InitRenderInfo(RenderInfo
*render
)
179 render
->Anim
= GL_TRUE
;
180 render
->Wireframe
= GL_FALSE
;
181 render
->Blend
= GL_FALSE
;
182 render
->Antialias
= GL_FALSE
;
183 render
->Texture
= GL_FALSE
;
184 render
->DrawBox
= GL_FALSE
;
185 render
->ShowInfo
= GL_TRUE
;
186 render
->ShowBlock
= GL_FALSE
;
187 render
->UseLists
= GL_FALSE
;
192 * Set GL for given rendering mode.
195 SetRenderState(RenderMode mode
)
197 static const GLfloat gray2
[4] = { 0.2, 0.2, 0.2, 1.0 };
198 static const GLfloat gray4
[4] = { 0.4, 0.4, 0.4, 1.0 };
201 glDisable(GL_LIGHTING
);
202 glDisable(GL_TEXTURE_2D
);
204 glDisable(GL_LINE_SMOOTH
);
205 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
206 glDisable(GL_TEXTURE_GEN_S
);
207 glDisable(GL_TEXTURE_GEN_T
);
208 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, gray2
);
212 glEnable(GL_LIGHTING
);
215 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
216 glEnable(GL_LINE_SMOOTH
);
221 glEnable(GL_LIGHTING
);
222 glEnable(GL_TEXTURE_2D
);
223 glEnable(GL_TEXTURE_GEN_S
);
224 glEnable(GL_TEXTURE_GEN_T
);
225 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, gray4
);
234 * Animate the engine parts.
239 /* convert degrees per millisecond to RPM: */
240 const float m
= 360.0 / 1000.0 / 60.0;
241 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
242 Theta
= ((int) (t
* RPM
* m
)) % 360;
248 * Compute piston's position along its stroke.
251 PistonStrokePosition(float throwDist
, float crankAngle
, float connRodLength
)
253 float x
= throwDist
* cos(DEG_TO_RAD(crankAngle
));
254 float y
= throwDist
* sin(DEG_TO_RAD(crankAngle
));
255 float pos
= y
+ sqrt(connRodLength
* connRodLength
- x
* x
);
261 * Compute position of nth piston along the crankshaft.
264 PistonShaftPosition(const Engine
*eng
, int piston
)
266 const int i
= piston
/ (eng
->Pistons
/ eng
->Cranks
);
268 assert(piston
< eng
->Pistons
);
269 z
= 1.5 * eng
->CrankJournalLength
+ eng
->CrankPlateThickness
270 + i
* (2.0 * (eng
->CrankJournalLength
+ eng
->CrankPlateThickness
));
271 if (eng
->Pistons
> eng
->Cranks
) {
273 z
+= eng
->ConnectingRodThickness
;
275 z
-= eng
->ConnectingRodThickness
;
282 * Compute distance between two adjacent pistons
285 PistonSpacing(const Engine
*eng
)
287 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
288 const float z0
= PistonShaftPosition(eng
, 0);
289 const float z1
= PistonShaftPosition(eng
, pistonsPerCrank
);
295 * (x0, y0) = position of big end on crankshaft
296 * (x1, y1) = position of small end on piston
299 ComputeConnectingRodPosition(float throwDist
, float crankAngle
,
301 float *x0
, float *y0
, float *x1
, float *y1
)
303 *x0
= throwDist
* cos(DEG_TO_RAD(crankAngle
));
304 *y0
= throwDist
* sin(DEG_TO_RAD(crankAngle
));
306 *y1
= PistonStrokePosition(throwDist
, crankAngle
, connRodLength
);
311 * Compute total length of the crankshaft.
314 CrankshaftLength(const Engine
*eng
)
316 float len
= (eng
->Cranks
* 2 + 1) * eng
->CrankJournalLength
317 + 2 * eng
->Cranks
* eng
->CrankPlateThickness
;
324 * Axis of piston = Z axis. Wrist pin is centered on (0, 0, 0).
327 DrawPiston(const Engine
*eng
)
329 const int slices
= 30, stacks
= 4, loops
= 4;
330 const float innerRadius
= 0.9 * eng
->PistonRadius
;
331 const float innerHeight
= eng
->PistonHeight
- 0.15;
332 const float wristPinLength
= 1.8 * eng
->PistonRadius
;
337 glTranslatef(0, 0, -1.1 * eng
->WristPinRadius
);
339 gluQuadricOrientation(Q
, GLU_INSIDE
);
342 gluDisk(Q
, innerRadius
, eng
->PistonRadius
, slices
, 1/*loops*/);
345 gluCylinder(Q
, innerRadius
, innerRadius
, innerHeight
, slices
, stacks
);
349 glTranslatef(0, 0, innerHeight
);
350 gluDisk(Q
, 0, innerRadius
, slices
, loops
);
353 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
356 gluCylinder(Q
, eng
->PistonRadius
, eng
->PistonRadius
, eng
->PistonHeight
,
360 glTranslatef(0, 0, eng
->PistonHeight
);
361 gluDisk(Q
, 0, eng
->PistonRadius
, slices
, loops
);
367 glTranslatef(0, 0.5 * wristPinLength
, 0.0);
368 glRotatef(90, 1, 0, 0);
369 gluCylinder(Q
, eng
->WristPinRadius
, eng
->WristPinRadius
, wristPinLength
,
376 * Draw piston at particular position.
379 DrawPositionedPiston(const Engine
*eng
, float crankAngle
)
381 const float pos
= PistonStrokePosition(eng
->Throw
, crankAngle
,
382 eng
->ConnectingRodLength
);
384 glRotatef(-90, 1, 0, 0);
385 glTranslatef(0, 0, pos
);
392 * Draw connector plate. Used for crankshaft and connecting rods.
395 DrawConnector(float length
, float thickness
,
396 float bigEndRadius
, float smallEndRadius
)
398 const float bigRadius
= 1.2 * bigEndRadius
;
399 const float smallRadius
= 1.2 * smallEndRadius
;
400 const float z0
= -0.5 * thickness
, z1
= -z0
;
401 GLfloat points
[36][2], normals
[36][2];
404 /* compute vertex locations, normals */
405 for (i
= 0; i
< 36; i
++) {
406 const int angle
= i
* 10;
407 float x
= cos(DEG_TO_RAD(angle
));
408 float y
= sin(DEG_TO_RAD(angle
));
411 if (angle
>= 0 && angle
<= 180) {
413 y
= y
* smallRadius
+ length
;
426 for (i
= 0; i
< 36; i
++) {
427 glVertex3f(points
[i
][0], points
[i
][1], z1
);
432 glNormal3f(0, 0, -1);
434 for (i
= 0; i
< 36; i
++) {
435 glVertex3f(points
[35-i
][0], points
[35-i
][1], z0
);
440 glBegin(GL_QUAD_STRIP
);
441 for (i
= 0; i
<= 36; i
++) {
442 const int j
= i
% 36;
443 glNormal3f(normals
[j
][0], normals
[j
][1], 0);
444 glVertex3f(points
[j
][0], points
[j
][1], z0
);
445 glVertex3f(points
[j
][0], points
[j
][1], z1
);
452 * Draw a crankshaft. Shaft lies along +Z axis, starting at zero.
455 DrawCrankshaft(const Engine
*eng
)
457 const int slices
= 20, stacks
= 2;
458 const int n
= eng
->Cranks
* 4 + 1;
459 const float phiStep
= 360 / eng
->Cranks
;
464 for (i
= 0; i
< n
; i
++) {
466 glTranslatef(0, 0, z
);
468 /* draw a crank plate */
469 glRotatef(phi
, 0, 0, 1);
470 glTranslatef(0, 0, 0.5 * eng
->CrankPlateThickness
);
471 DrawConnector(eng
->Throw
, eng
->CrankPlateThickness
,
472 eng
->CrankJournalRadius
, eng
->CrankPinRadius
);
477 else if (i
% 4 == 0) {
478 /* draw crank journal segment */
479 gluCylinder(Q
, eng
->CrankJournalRadius
, eng
->CrankJournalRadius
,
480 eng
->CrankJournalLength
, slices
, stacks
);
481 z
+= eng
->CrankJournalLength
;
483 else if (i
% 4 == 2) {
484 /* draw crank pin segment */
485 glRotatef(phi
, 0, 0, 1);
486 glTranslatef(0, eng
->Throw
, 0);
487 gluCylinder(Q
, eng
->CrankPinRadius
, eng
->CrankPinRadius
,
488 eng
->CrankJournalLength
, slices
, stacks
);
489 z
+= eng
->CrankJournalLength
;
497 * Draw crankshaft at a particular rotation.
498 * \param crankAngle current crankshaft rotation, in radians
501 DrawPositionedCrankshaft(const Engine
*eng
, float crankAngle
)
504 glRotatef(crankAngle
, 0, 0, 1);
506 glCallList(eng
->CrankList
);
514 * Draw a connecting rod at particular position.
515 * \param eng description of connecting rod to draw
516 * \param crankAngle current crankshaft rotation, in radians
519 DrawPositionedConnectingRod(const Engine
*eng
, float crankAngle
)
521 float x0
, y0
, x1
, y1
;
524 ComputeConnectingRodPosition(eng
->Throw
, crankAngle
,
525 eng
->ConnectingRodLength
,
527 d
= sqrt(eng
->ConnectingRodLength
* eng
->ConnectingRodLength
- x0
* x0
);
528 phi
= atan(x0
/ d
) * 180.0 / M_PI
;
531 glTranslatef(x0
, y0
, 0);
532 glRotatef(phi
, 0, 0, 1);
533 if (eng
->ConnRodList
)
534 glCallList(eng
->ConnRodList
);
536 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
537 eng
->CrankPinRadius
, eng
->WristPinRadius
);
543 * Draw a square with a hole in middle.
546 SquareWithHole(float squareSize
, float holeRadius
)
549 glBegin(GL_QUAD_STRIP
);
551 for (i
= 0; i
<= 360; i
+= 5) {
552 const float x1
= holeRadius
* cos(DEG_TO_RAD(i
));
553 const float y1
= holeRadius
* sin(DEG_TO_RAD(i
));
555 if (i
> 315 || i
<= 45) {
557 y2
= squareSize
* tan(DEG_TO_RAD(i
));
559 else if (i
> 45 && i
<= 135) {
560 x2
= -squareSize
* tan(DEG_TO_RAD(i
- 90));
563 else if (i
> 135 && i
<= 225) {
565 y2
= -squareSize
* tan(DEG_TO_RAD(i
-180));
567 else if (i
> 225 && i
<= 315) {
568 x2
= squareSize
* tan(DEG_TO_RAD(i
- 270));
571 glVertex2f(x1
, y1
); /* inner circle */
572 glVertex2f(x2
, y2
); /* outer square */
579 * Draw block with hole through middle.
580 * Hole is centered on Z axis.
581 * Bottom of block is at z=0, top of block is at z = blockHeight.
582 * index is in [0, count - 1] to determine which block faces are drawn.
585 DrawBlockWithHole(float blockSize
, float blockHeight
, float holeRadius
,
586 int index
, int count
)
588 const int slices
= 30, stacks
= 4;
589 const float x
= blockSize
;
590 const float y
= blockSize
;
592 const float z1
= blockHeight
;
594 assert(index
< count
);
596 gluQuadricOrientation(Q
, GLU_INSIDE
);
601 glVertex3f( x
, -y
, z0
);
602 glVertex3f( x
, y
, z0
);
603 glVertex3f( x
, y
, z1
);
604 glVertex3f( x
, -y
, z1
);
606 glNormal3f(-1, 0, 0);
607 glVertex3f(-x
, -y
, z1
);
608 glVertex3f(-x
, y
, z1
);
609 glVertex3f(-x
, y
, z0
);
610 glVertex3f(-x
, -y
, z0
);
614 glVertex3f(-x
, y
, z1
);
615 glVertex3f( x
, y
, z1
);
616 glVertex3f( x
, y
, z0
);
617 glVertex3f(-x
, y
, z0
);
619 if (index
== count
- 1) {
621 glNormal3f(0, -1, 0);
622 glVertex3f(-x
, -y
, z0
);
623 glVertex3f( x
, -y
, z0
);
624 glVertex3f( x
, -y
, z1
);
625 glVertex3f(-x
, -y
, z1
);
629 /* cylinder / hole */
630 gluCylinder(Q
, holeRadius
, holeRadius
, blockHeight
, slices
, stacks
);
634 glRotatef(180, 1, 0, 0);
635 SquareWithHole(blockSize
, holeRadius
);
639 glTranslatef(0, 0, z1
);
640 SquareWithHole(blockSize
, holeRadius
);
642 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
647 * Draw the engine block.
650 DrawEngineBlock(const Engine
*eng
)
652 const float blockHeight
= eng
->Throw
+ 1.5 * eng
->PistonHeight
;
653 const float cylRadius
= 1.01 * eng
->PistonRadius
;
654 const float blockSize
= 0.5 * PistonSpacing(eng
);
655 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
658 for (i
= 0; i
< eng
->Pistons
; i
++) {
659 const float z
= PistonShaftPosition(eng
, i
);
660 const int crank
= i
/ pistonsPerCrank
;
664 glTranslatef(0, 0, z
);
666 /* additional rotation for kth piston per crank */
667 k
= i
% pistonsPerCrank
;
668 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
671 glRotatef(-90, 1, 0, 0);
672 glTranslatef(0, 0, eng
->Throw
* 2);
673 DrawBlockWithHole(blockSize
, blockHeight
, cylRadius
,
681 * Generate display lists for engine parts.
684 GenerateDisplayLists(Engine
*eng
)
686 eng
->CrankList
= glGenLists(1);
687 glNewList(eng
->CrankList
, GL_COMPILE
);
691 eng
->ConnRodList
= glGenLists(1);
692 glNewList(eng
->ConnRodList
, GL_COMPILE
);
693 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
694 eng
->CrankPinRadius
, eng
->WristPinRadius
);
697 eng
->PistonList
= glGenLists(1);
698 glNewList(eng
->PistonList
, GL_COMPILE
);
702 eng
->BlockList
= glGenLists(1);
703 glNewList(eng
->BlockList
, GL_COMPILE
);
704 DrawEngineBlock(eng
);
710 * Free engine display lists (render with immediate mode).
713 FreeDisplayLists(Engine
*eng
)
715 glDeleteLists(eng
->CrankList
, 1);
717 glDeleteLists(eng
->ConnRodList
, 1);
718 eng
->ConnRodList
= 0;
719 glDeleteLists(eng
->PistonList
, 1);
721 glDeleteLists(eng
->BlockList
, 1);
727 * Draw complete engine.
728 * \param eng description of engine to draw
729 * \param crankAngle current crankshaft angle, in radians
732 DrawEngine(const Engine
*eng
, float crankAngle
)
734 const float crankDelta
= 360.0 / eng
->Cranks
;
735 const float crankLen
= CrankshaftLength(eng
);
736 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
740 glRotatef(eng
->V_Angle
* 0.5, 0, 0, 1);
741 glTranslatef(0, 0, -0.5 * crankLen
);
744 glMaterialfv(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, CrankshaftColor
);
745 glColor4fv(CrankshaftColor
);
746 DrawPositionedCrankshaft(eng
, crankAngle
);
748 for (i
= 0; i
< eng
->Pistons
; i
++) {
749 const float z
= PistonShaftPosition(eng
, i
);
750 const int crank
= i
/ pistonsPerCrank
;
751 float rot
= crankAngle
+ crank
* crankDelta
;
755 glTranslatef(0, 0, z
);
757 /* additional rotation for kth piston per crank */
758 k
= i
% pistonsPerCrank
;
759 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
760 rot
+= k
* eng
->V_Angle
;
763 glMaterialfv(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, PistonColor
);
764 glColor4fv(PistonColor
);
765 DrawPositionedPiston(eng
, rot
);
768 glMaterialfv(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, ConnRodColor
);
769 glColor4fv(ConnRodColor
);
770 DrawPositionedConnectingRod(eng
, rot
);
774 if (Render
.ShowBlock
) {
775 const GLboolean blend
= glIsEnabled(GL_BLEND
);
777 glDepthMask(GL_FALSE
);
781 glEnable(GL_CULL_FACE
);
783 glMaterialfv(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, BlockColor
);
784 glColor4fv(BlockColor
);
786 glCallList(eng
->BlockList
);
788 DrawEngineBlock(eng
);
790 glDisable(GL_CULL_FACE
);
791 glDepthMask(GL_TRUE
);
804 const float xmin
= -3.0, xmax
= 3.0;
805 const float ymin
= -1.0, ymax
= 3.0;
806 const float zmin
= -4.0, zmax
= 4.0;
807 const float step
= 0.5;
808 const float d
= 0.01;
810 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
811 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
813 glDisable(GL_LIGHTING
);
814 glDisable(GL_TEXTURE_2D
);
820 for (x
= xmin
; x
<= xmax
; x
+= step
) {
821 glVertex3f(x
, ymin
, zmin
);
822 glVertex3f(x
, ymax
, zmin
);
826 for (y
= ymin
; y
<= ymax
; y
+= step
) {
827 glVertex3f(xmin
, y
, zmin
);
828 glVertex3f(xmax
, y
, zmin
);
834 for (x
= xmin
; x
<= xmax
; x
+= step
) {
835 glVertex3f(x
, ymin
, zmin
);
836 glVertex3f(x
, ymin
, zmax
);
840 for (z
= zmin
; z
<= zmax
; z
+= step
) {
841 glVertex3f(xmin
, ymin
, z
);
842 glVertex3f(xmax
, ymin
, z
);
848 for (y
= ymin
; y
<= ymax
; y
+= step
) {
849 glVertex3f(xmin
, y
, zmin
);
850 glVertex3f(xmin
, y
, zmax
);
854 for (z
= zmin
; z
<= zmax
; z
+= step
) {
855 glVertex3f(xmin
, ymin
, z
);
856 glVertex3f(xmin
, ymax
, z
);
860 glColor3f(0.4, 0.4, 0.6);
863 glVertex3f(xmin
-d
, ymin
, zmin
);
864 glVertex3f(xmin
-d
, ymax
, zmin
);
865 glVertex3f(xmin
-d
, ymax
, zmax
);
866 glVertex3f(xmin
-d
, ymin
, zmax
);
868 glVertex3f(xmin
, ymin
-d
, zmin
);
869 glVertex3f(xmax
, ymin
-d
, zmin
);
870 glVertex3f(xmax
, ymin
-d
, zmax
);
871 glVertex3f(xmin
, ymin
-d
, zmax
);
873 glVertex3f(xmin
, ymin
, zmin
-d
);
874 glVertex3f(xmax
, ymin
, zmin
-d
);
875 glVertex3f(xmax
, ymax
, zmin
-d
);
876 glVertex3f(xmin
, ymax
, zmin
-d
);
880 glEnable(GL_LIGHTING
);
882 glEnable(GL_TEXTURE_2D
);
887 PrintString(const char *s
)
890 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
899 static double t0
= -1.0;
900 static int frames
= 0;
901 double t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
910 else if (t
- t0
>= 1.0) {
911 fps
= (int) (frames
/ (t
- t0
) + 0.5);
926 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
930 glTranslatef(0.0, 0.0, -View
.Distance
);
931 build_rotmatrix(rot
, View
.CurQuat
);
932 glMultMatrixf(&rot
[0][0]);
935 glTranslatef(0, -0.75, 0);
938 DrawEngine(Engines
+ CurEngine
, Theta
);
944 if (Render
.ShowInfo
) {
945 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
946 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
948 sprintf(s
, "%s %d FPS %s", Engines
[CurEngine
].Name
, fps
,
949 Render
.UseLists
? "Display Lists" : "Immediate mode");
950 glDisable(GL_LIGHTING
);
951 glDisable(GL_TEXTURE_2D
);
953 glWindowPos2iARB(10, 10);
956 glEnable(GL_LIGHTING
);
958 glEnable(GL_TEXTURE_2D
);
966 * Handle window resize.
969 Reshape(int width
, int height
)
971 float ar
= (float) width
/ height
;
973 glViewport(0, 0, width
, height
);
974 glMatrixMode(GL_PROJECTION
);
976 glFrustum(-ar
* s
, ar
* s
, -s
, s
, 2.0, 50.0);
977 glMatrixMode(GL_MODELVIEW
);
985 * Handle mouse button.
988 Mouse(int button
, int state
, int x
, int y
)
990 if (button
== GLUT_LEFT_BUTTON
) {
991 if (state
== GLUT_DOWN
) {
994 View
.Rotating
= GL_TRUE
;
996 else if (state
== GLUT_UP
) {
997 View
.Rotating
= GL_FALSE
;
1000 else if (button
== GLUT_MIDDLE_BUTTON
) {
1001 if (state
== GLUT_DOWN
) {
1004 View
.StartDistance
= View
.Distance
;
1005 View
.Translating
= GL_TRUE
;
1007 else if (state
== GLUT_UP
) {
1008 View
.Translating
= GL_FALSE
;
1015 * Handle mouse motion
1018 Motion(int x
, int y
)
1021 if (View
.Rotating
) {
1022 float x0
= (2.0 * View
.StartX
- WinWidth
) / WinWidth
;
1023 float y0
= (WinHeight
- 2.0 * View
.StartY
) / WinHeight
;
1024 float x1
= (2.0 * x
- WinWidth
) / WinWidth
;
1025 float y1
= (WinHeight
- 2.0 * y
) / WinHeight
;
1028 trackball(q
, x0
, y0
, x1
, y1
);
1031 for (i
= 0; i
< 1; i
++)
1032 add_quats(q
, View
.CurQuat
, View
.CurQuat
);
1034 glutPostRedisplay();
1036 else if (View
.Translating
) {
1037 float dz
= 0.01 * (y
- View
.StartY
);
1038 View
.Distance
= View
.StartDistance
+ dz
;
1039 glutPostRedisplay();
1051 Render
.Anim
= !Render
.Anim
;
1059 OptChangeEngine(void)
1061 CurEngine
= (CurEngine
+ 1) % NUM_ENGINES
;
1068 if (Render
.Mode
> TEXTURED
)
1070 SetRenderState(Render
.Mode
);
1074 OptDisplayLists(void)
1077 Render
.UseLists
= !Render
.UseLists
;
1078 if (Render
.UseLists
) {
1079 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1080 GenerateDisplayLists(Engines
+ i
);
1084 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1085 FreeDisplayLists(Engines
+ i
);
1093 Render
.ShowBlock
= !Render
.ShowBlock
;
1099 Render
.ShowInfo
= !Render
.ShowInfo
;
1105 Render
.DrawBox
= !Render
.DrawBox
;
1122 * Define menu entries (w/ keyboard shortcuts)
1129 void (*Function
)(void);
1132 static const MenuInfo MenuItems
[] = {
1133 { "Animation", 'a', OptAnimation
},
1134 { "Change Engine", 'e', OptChangeEngine
},
1135 { "Rendering Style", 'm', OptRenderMode
},
1136 { "Display Lists", 'd', OptDisplayLists
},
1137 { "Show Block", 'b', OptShowBlock
},
1138 { "Show Info", 'i', OptShowInfo
},
1139 { "Show Box", 'x', OptShowBox
},
1140 { "Exit", 27, OptExit
},
1141 { NULL
, 'r', OptRotate
},
1147 * Handle menu selection.
1150 MenuHandler(int entry
)
1152 MenuItems
[entry
].Function();
1153 glutPostRedisplay();
1164 glutCreateMenu(MenuHandler
);
1165 for (i
= 0; MenuItems
[i
].Text
; i
++) {
1166 glutAddMenuEntry(MenuItems
[i
].Text
, i
);
1168 glutAttachMenu(GLUT_RIGHT_BUTTON
);
1173 * Handle keyboard event.
1176 Key(unsigned char key
, int x
, int y
)
1180 for (i
= 0; MenuItems
[i
].Key
; i
++) {
1181 if (MenuItems
[i
].Key
== key
) {
1182 MenuItems
[i
].Function();
1183 glutPostRedisplay();
1191 void LoadTexture(void)
1193 GLboolean convolve
= GL_FALSE
;
1195 glGenTextures(1, &TextureObj
);
1196 glBindTexture(GL_TEXTURE_2D
, TextureObj
);
1197 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1198 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1199 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1200 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1203 #define FILTER_SIZE 7
1204 /* use convolution to blur the texture to simulate a dull finish
1210 GLfloat filter
[FILTER_SIZE
][FILTER_SIZE
][4];
1212 for (h
= 0; h
< FILTER_SIZE
; h
++) {
1213 for (w
= 0; w
< FILTER_SIZE
; w
++) {
1214 const GLfloat k
= 1.0 / (FILTER_SIZE
* FILTER_SIZE
);
1215 filter
[h
][w
][0] = k
;
1216 filter
[h
][w
][1] = k
;
1217 filter
[h
][w
][2] = k
;
1218 filter
[h
][w
][3] = k
;
1222 glEnable(GL_CONVOLUTION_2D
);
1223 glConvolutionParameteri(GL_CONVOLUTION_2D
,
1224 GL_CONVOLUTION_BORDER_MODE
, GL_CONSTANT_BORDER
);
1225 glConvolutionFilter2D(GL_CONVOLUTION_2D
, GL_RGBA
,
1226 FILTER_SIZE
, FILTER_SIZE
,
1227 GL_RGBA
, GL_FLOAT
, filter
);
1229 img
= LoadRGBImage(TEXTURE_FILE
, &w
, &h
, &format
);
1231 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1235 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, w
, h
, 0,
1236 format
, GL_UNSIGNED_BYTE
, img
);
1240 if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
1241 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1251 const GLfloat lightColor
[4] = { 0.7, 0.7, 0.7, 1.0 };
1252 const GLfloat specular
[4] = { 0.8, 0.8, 0.8, 1.0 };
1254 Q
= gluNewQuadric();
1255 gluQuadricNormals(Q
, GLU_SMOOTH
);
1259 glClearColor(0.3, 0.3, 0.3, 0.0);
1260 glEnable(GL_DEPTH_TEST
);
1261 glEnable(GL_LIGHTING
);
1262 glEnable(GL_LIGHT0
);
1263 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, lightColor
);
1264 glMaterialf(GL_FRONT_AND_BACK
, GL_SHININESS
, 40);
1265 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, specular
);
1266 glEnable(GL_NORMALIZE
);
1268 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1270 InitViewInfo(&View
);
1271 InitRenderInfo(&Render
);
1276 main(int argc
, char *argv
[])
1278 glutInit(&argc
, argv
);
1279 glutInitWindowSize(WinWidth
, WinHeight
);
1280 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
1281 glutCreateWindow("OpenGL Engine Demo");
1282 glutReshapeFunc(Reshape
);
1283 glutMouseFunc(Mouse
);
1284 glutMotionFunc(Motion
);
1285 glutKeyboardFunc(Key
);
1286 glutDisplayFunc(Draw
);