2 * Simple engine demo (crankshaft, pistons, connecting rods)
15 #include "trackball.h"
19 #define M_PI 3.14159265358979323846
22 #define DEG_TO_RAD(DEG) ((DEG) * M_PI / 180.0)
24 #define TEXTURE_FILE "../images/reflect.rgb"
26 /* Target engine speed: */
27 const int RPM
= 100.0;
43 float CrankPlateThickness
;
45 float CrankJournalRadius
;
46 float CrankJournalLength
;
47 float ConnectingRodLength
;
48 float ConnectingRodThickness
;
49 /* display list IDs */
61 /* When mouse is moving: */
62 GLboolean Rotating
, Translating
;
93 static GLfloat Theta
= 0.0;
95 static const GLfloat PistonColor
[4] = { 1.0, 0.5, 0.5, 1.0 };
96 static const GLfloat ConnRodColor
[4] = { 0.7, 1.0, 0.7, 1.0 };
97 static const GLfloat CrankshaftColor
[4] = { 0.7, 0.7, 1.0, 1.0 };
98 static const GLfloat BlockColor
[4] = {0.8, 0.8, 0.8, 0.75 };
100 static GLuint TextureObj
;
101 static GLint WinWidth
= 800, WinHeight
= 500;
103 static ViewInfo View
;
104 static RenderInfo Render
;
106 #define NUM_ENGINES 3
107 static Engine Engines
[NUM_ENGINES
] =
114 0.5, /* PistonRadius */
115 0.6, /* PistonHeight */
116 0.1, /* WristPinRadius */
118 0.2, /* CrankPlateThickness */
119 0.25, /* CrankPinRadius */
120 0.3, /* CrankJournalRadius */
121 0.4, /* CrankJournalLength */
122 1.5, /* ConnectingRodLength */
123 0.1 /* ConnectingRodThickness */
130 0.5, /* PistonRadius */
131 0.6, /* PistonHeight */
132 0.1, /* WristPinRadius */
134 0.2, /* CrankPlateThickness */
135 0.25, /* CrankPinRadius */
136 0.3, /* CrankJournalRadius */
137 0.4, /* CrankJournalLength */
138 1.5, /* ConnectingRodLength */
139 0.1 /* ConnectingRodThickness */
146 0.5, /* PistonRadius */
147 0.6, /* PistonHeight */
148 0.1, /* WristPinRadius */
150 0.2, /* CrankPlateThickness */
151 0.25, /* CrankPinRadius */
152 0.3, /* CrankJournalRadius */
153 0.4, /* CrankJournalLength */
154 1.5, /* ConnectingRodLength */
155 0.1 /* ConnectingRodThickness */
159 static int CurEngine
= 0;
164 InitViewInfo(ViewInfo
*view
)
166 view
->Rotating
= GL_FALSE
;
167 view
->Translating
= GL_FALSE
;
168 view
->StartX
= view
->StartY
= 0;
169 view
->Distance
= 12.0;
170 view
->StartDistance
= 0.0;
171 view
->CurQuat
[0] = -0.194143;
172 view
->CurQuat
[1] = 0.507848;
173 view
->CurQuat
[2] = 0.115245;
174 view
->CurQuat
[3] = 0.831335;
179 InitRenderInfo(RenderInfo
*render
)
182 render
->Anim
= GL_TRUE
;
183 render
->Wireframe
= GL_FALSE
;
184 render
->Blend
= GL_FALSE
;
185 render
->Antialias
= GL_FALSE
;
186 render
->Texture
= GL_FALSE
;
187 render
->DrawBox
= GL_FALSE
;
188 render
->ShowInfo
= GL_TRUE
;
189 render
->ShowBlock
= GL_FALSE
;
190 render
->UseLists
= GL_FALSE
;
195 * Set GL for given rendering mode.
198 SetRenderState(RenderMode mode
)
200 static const GLfloat gray2
[4] = { 0.2, 0.2, 0.2, 1.0 };
201 static const GLfloat gray4
[4] = { 0.4, 0.4, 0.4, 1.0 };
204 glDisable(GL_LIGHTING
);
205 glDisable(GL_TEXTURE_2D
);
207 glDisable(GL_LINE_SMOOTH
);
208 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
209 glDisable(GL_TEXTURE_GEN_S
);
210 glDisable(GL_TEXTURE_GEN_T
);
211 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, gray2
);
215 glEnable(GL_LIGHTING
);
218 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
219 glEnable(GL_LINE_SMOOTH
);
224 glEnable(GL_LIGHTING
);
225 glEnable(GL_TEXTURE_2D
);
226 glEnable(GL_TEXTURE_GEN_S
);
227 glEnable(GL_TEXTURE_GEN_T
);
228 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, gray4
);
237 * Animate the engine parts.
242 /* convert degrees per millisecond to RPM: */
243 const float m
= 360.0 / 1000.0 / 60.0;
244 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
245 Theta
= ((int) (t
* RPM
* m
)) % 360;
251 * Compute piston's position along its stroke.
254 PistonStrokePosition(float throwDist
, float crankAngle
, float connRodLength
)
256 float x
= throwDist
* cos(DEG_TO_RAD(crankAngle
));
257 float y
= throwDist
* sin(DEG_TO_RAD(crankAngle
));
258 float pos
= y
+ sqrt(connRodLength
* connRodLength
- x
* x
);
264 * Compute position of nth piston along the crankshaft.
267 PistonShaftPosition(const Engine
*eng
, int piston
)
269 const int i
= piston
/ (eng
->Pistons
/ eng
->Cranks
);
271 assert(piston
< eng
->Pistons
);
272 z
= 1.5 * eng
->CrankJournalLength
+ eng
->CrankPlateThickness
273 + i
* (2.0 * (eng
->CrankJournalLength
+ eng
->CrankPlateThickness
));
274 if (eng
->Pistons
> eng
->Cranks
) {
276 z
+= eng
->ConnectingRodThickness
;
278 z
-= eng
->ConnectingRodThickness
;
285 * Compute distance between two adjacent pistons
288 PistonSpacing(const Engine
*eng
)
290 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
291 const float z0
= PistonShaftPosition(eng
, 0);
292 const float z1
= PistonShaftPosition(eng
, pistonsPerCrank
);
298 * (x0, y0) = position of big end on crankshaft
299 * (x1, y1) = position of small end on piston
302 ComputeConnectingRodPosition(float throwDist
, float crankAngle
,
304 float *x0
, float *y0
, float *x1
, float *y1
)
306 *x0
= throwDist
* cos(DEG_TO_RAD(crankAngle
));
307 *y0
= throwDist
* sin(DEG_TO_RAD(crankAngle
));
309 *y1
= PistonStrokePosition(throwDist
, crankAngle
, connRodLength
);
314 * Compute total length of the crankshaft.
317 CrankshaftLength(const Engine
*eng
)
319 float len
= (eng
->Cranks
* 2 + 1) * eng
->CrankJournalLength
320 + 2 * eng
->Cranks
* eng
->CrankPlateThickness
;
327 * Axis of piston = Z axis. Wrist pin is centered on (0, 0, 0).
330 DrawPiston(const Engine
*eng
)
332 const int slices
= 30, stacks
= 4, loops
= 4;
333 const float innerRadius
= 0.9 * eng
->PistonRadius
;
334 const float innerHeight
= eng
->PistonHeight
- 0.15;
335 const float wristPinLength
= 1.8 * eng
->PistonRadius
;
340 glTranslatef(0, 0, -1.1 * eng
->WristPinRadius
);
342 gluQuadricOrientation(Q
, GLU_INSIDE
);
345 gluDisk(Q
, innerRadius
, eng
->PistonRadius
, slices
, 1/*loops*/);
348 gluCylinder(Q
, innerRadius
, innerRadius
, innerHeight
, slices
, stacks
);
352 glTranslatef(0, 0, innerHeight
);
353 gluDisk(Q
, 0, innerRadius
, slices
, loops
);
356 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
359 gluCylinder(Q
, eng
->PistonRadius
, eng
->PistonRadius
, eng
->PistonHeight
,
363 glTranslatef(0, 0, eng
->PistonHeight
);
364 gluDisk(Q
, 0, eng
->PistonRadius
, slices
, loops
);
370 glTranslatef(0, 0.5 * wristPinLength
, 0.0);
371 glRotatef(90, 1, 0, 0);
372 gluCylinder(Q
, eng
->WristPinRadius
, eng
->WristPinRadius
, wristPinLength
,
379 * Draw piston at particular position.
382 DrawPositionedPiston(const Engine
*eng
, float crankAngle
)
384 const float pos
= PistonStrokePosition(eng
->Throw
, crankAngle
,
385 eng
->ConnectingRodLength
);
387 glRotatef(-90, 1, 0, 0);
388 glTranslatef(0, 0, pos
);
390 glCallList(eng
->PistonList
);
398 * Draw connector plate. Used for crankshaft and connecting rods.
401 DrawConnector(float length
, float thickness
,
402 float bigEndRadius
, float smallEndRadius
)
404 const float bigRadius
= 1.2 * bigEndRadius
;
405 const float smallRadius
= 1.2 * smallEndRadius
;
406 const float z0
= -0.5 * thickness
, z1
= -z0
;
407 GLfloat points
[36][2], normals
[36][2];
410 /* compute vertex locations, normals */
411 for (i
= 0; i
< 36; i
++) {
412 const int angle
= i
* 10;
413 float x
= cos(DEG_TO_RAD(angle
));
414 float y
= sin(DEG_TO_RAD(angle
));
417 if (angle
>= 0 && angle
<= 180) {
419 y
= y
* smallRadius
+ length
;
432 for (i
= 0; i
< 36; i
++) {
433 glVertex3f(points
[i
][0], points
[i
][1], z1
);
438 glNormal3f(0, 0, -1);
440 for (i
= 0; i
< 36; i
++) {
441 glVertex3f(points
[35-i
][0], points
[35-i
][1], z0
);
446 glBegin(GL_QUAD_STRIP
);
447 for (i
= 0; i
<= 36; i
++) {
448 const int j
= i
% 36;
449 glNormal3f(normals
[j
][0], normals
[j
][1], 0);
450 glVertex3f(points
[j
][0], points
[j
][1], z1
);
451 glVertex3f(points
[j
][0], points
[j
][1], z0
);
458 * Draw a crankshaft. Shaft lies along +Z axis, starting at zero.
461 DrawCrankshaft(const Engine
*eng
)
463 const int slices
= 20, stacks
= 2;
464 const int n
= eng
->Cranks
* 4 + 1;
465 const float phiStep
= 360 / eng
->Cranks
;
470 for (i
= 0; i
< n
; i
++) {
472 glTranslatef(0, 0, z
);
474 /* draw a crank plate */
475 glRotatef(phi
, 0, 0, 1);
476 glTranslatef(0, 0, 0.5 * eng
->CrankPlateThickness
);
477 DrawConnector(eng
->Throw
, eng
->CrankPlateThickness
,
478 eng
->CrankJournalRadius
, eng
->CrankPinRadius
);
483 else if (i
% 4 == 0) {
484 /* draw crank journal segment */
485 gluCylinder(Q
, eng
->CrankJournalRadius
, eng
->CrankJournalRadius
,
486 eng
->CrankJournalLength
, slices
, stacks
);
487 z
+= eng
->CrankJournalLength
;
489 else if (i
% 4 == 2) {
490 /* draw crank pin segment */
491 glRotatef(phi
, 0, 0, 1);
492 glTranslatef(0, eng
->Throw
, 0);
493 gluCylinder(Q
, eng
->CrankPinRadius
, eng
->CrankPinRadius
,
494 eng
->CrankJournalLength
, slices
, stacks
);
495 z
+= eng
->CrankJournalLength
;
503 * Draw crankshaft at a particular rotation.
504 * \param crankAngle current crankshaft rotation, in radians
507 DrawPositionedCrankshaft(const Engine
*eng
, float crankAngle
)
510 glRotatef(crankAngle
, 0, 0, 1);
512 glCallList(eng
->CrankList
);
520 * Draw a connecting rod at particular position.
521 * \param eng description of connecting rod to draw
522 * \param crankAngle current crankshaft rotation, in radians
525 DrawPositionedConnectingRod(const Engine
*eng
, float crankAngle
)
527 float x0
, y0
, x1
, y1
;
530 ComputeConnectingRodPosition(eng
->Throw
, crankAngle
,
531 eng
->ConnectingRodLength
,
533 d
= sqrt(eng
->ConnectingRodLength
* eng
->ConnectingRodLength
- x0
* x0
);
534 phi
= atan(x0
/ d
) * 180.0 / M_PI
;
537 glTranslatef(x0
, y0
, 0);
538 glRotatef(phi
, 0, 0, 1);
539 if (eng
->ConnRodList
)
540 glCallList(eng
->ConnRodList
);
542 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
543 eng
->CrankPinRadius
, eng
->WristPinRadius
);
549 * Draw a square with a hole in middle.
552 SquareWithHole(float squareSize
, float holeRadius
)
555 glBegin(GL_QUAD_STRIP
);
557 for (i
= 0; i
<= 360; i
+= 5) {
558 const float x1
= holeRadius
* cos(DEG_TO_RAD(i
));
559 const float y1
= holeRadius
* sin(DEG_TO_RAD(i
));
560 float x2
= 0.0F
, y2
= 0.0F
;
561 if (i
> 315 || i
<= 45) {
563 y2
= squareSize
* tan(DEG_TO_RAD(i
));
565 else if (i
> 45 && i
<= 135) {
566 x2
= -squareSize
* tan(DEG_TO_RAD(i
- 90));
569 else if (i
> 135 && i
<= 225) {
571 y2
= -squareSize
* tan(DEG_TO_RAD(i
-180));
573 else if (i
> 225 && i
<= 315) {
574 x2
= squareSize
* tan(DEG_TO_RAD(i
- 270));
577 glVertex2f(x1
, y1
); /* inner circle */
578 glVertex2f(x2
, y2
); /* outer square */
585 * Draw block with hole through middle.
586 * Hole is centered on Z axis.
587 * Bottom of block is at z=0, top of block is at z = blockHeight.
588 * index is in [0, count - 1] to determine which block faces are drawn.
591 DrawBlockWithHole(float blockSize
, float blockHeight
, float holeRadius
,
592 int index
, int count
)
594 const int slices
= 30, stacks
= 4;
595 const float x
= blockSize
;
596 const float y
= blockSize
;
598 const float z1
= blockHeight
;
600 assert(index
< count
);
602 gluQuadricOrientation(Q
, GLU_INSIDE
);
607 glVertex3f( x
, -y
, z0
);
608 glVertex3f( x
, y
, z0
);
609 glVertex3f( x
, y
, z1
);
610 glVertex3f( x
, -y
, z1
);
612 glNormal3f(-1, 0, 0);
613 glVertex3f(-x
, -y
, z1
);
614 glVertex3f(-x
, y
, z1
);
615 glVertex3f(-x
, y
, z0
);
616 glVertex3f(-x
, -y
, z0
);
620 glVertex3f(-x
, y
, z1
);
621 glVertex3f( x
, y
, z1
);
622 glVertex3f( x
, y
, z0
);
623 glVertex3f(-x
, y
, z0
);
625 if (index
== count
- 1) {
627 glNormal3f(0, -1, 0);
628 glVertex3f(-x
, -y
, z0
);
629 glVertex3f( x
, -y
, z0
);
630 glVertex3f( x
, -y
, z1
);
631 glVertex3f(-x
, -y
, z1
);
635 /* cylinder / hole */
636 gluCylinder(Q
, holeRadius
, holeRadius
, blockHeight
, slices
, stacks
);
640 glRotatef(180, 1, 0, 0);
641 SquareWithHole(blockSize
, holeRadius
);
645 glTranslatef(0, 0, z1
);
646 SquareWithHole(blockSize
, holeRadius
);
648 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
653 * Draw the engine block.
656 DrawEngineBlock(const Engine
*eng
)
658 const float blockHeight
= eng
->Throw
+ 1.5 * eng
->PistonHeight
;
659 const float cylRadius
= 1.01 * eng
->PistonRadius
;
660 const float blockSize
= 0.5 * PistonSpacing(eng
);
661 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
664 for (i
= 0; i
< eng
->Pistons
; i
++) {
665 const float z
= PistonShaftPosition(eng
, i
);
666 const int crank
= i
/ pistonsPerCrank
;
670 glTranslatef(0, 0, z
);
672 /* additional rotation for kth piston per crank */
673 k
= i
% pistonsPerCrank
;
674 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
677 glRotatef(-90, 1, 0, 0);
678 glTranslatef(0, 0, eng
->Throw
* 2);
679 DrawBlockWithHole(blockSize
, blockHeight
, cylRadius
,
687 * Generate display lists for engine parts.
690 GenerateDisplayLists(Engine
*eng
)
692 eng
->CrankList
= glGenLists(1);
693 glNewList(eng
->CrankList
, GL_COMPILE
);
697 eng
->ConnRodList
= glGenLists(1);
698 glNewList(eng
->ConnRodList
, GL_COMPILE
);
699 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
700 eng
->CrankPinRadius
, eng
->WristPinRadius
);
703 eng
->PistonList
= glGenLists(1);
704 glNewList(eng
->PistonList
, GL_COMPILE
);
708 eng
->BlockList
= glGenLists(1);
709 glNewList(eng
->BlockList
, GL_COMPILE
);
710 DrawEngineBlock(eng
);
716 * Free engine display lists (render with immediate mode).
719 FreeDisplayLists(Engine
*eng
)
721 glDeleteLists(eng
->CrankList
, 1);
723 glDeleteLists(eng
->ConnRodList
, 1);
724 eng
->ConnRodList
= 0;
725 glDeleteLists(eng
->PistonList
, 1);
727 glDeleteLists(eng
->BlockList
, 1);
733 * Draw complete engine.
734 * \param eng description of engine to draw
735 * \param crankAngle current crankshaft angle, in radians
738 DrawEngine(const Engine
*eng
, float crankAngle
)
740 const float crankDelta
= 360.0 / eng
->Cranks
;
741 const float crankLen
= CrankshaftLength(eng
);
742 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
746 glRotatef(eng
->V_Angle
* 0.5, 0, 0, 1);
747 glTranslatef(0, 0, -0.5 * crankLen
);
750 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, CrankshaftColor
);
751 glColor4fv(CrankshaftColor
);
752 DrawPositionedCrankshaft(eng
, crankAngle
);
754 for (i
= 0; i
< eng
->Pistons
; i
++) {
755 const float z
= PistonShaftPosition(eng
, i
);
756 const int crank
= i
/ pistonsPerCrank
;
757 float rot
= crankAngle
+ crank
* crankDelta
;
761 glTranslatef(0, 0, z
);
763 /* additional rotation for kth piston per crank */
764 k
= i
% pistonsPerCrank
;
765 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
766 rot
+= k
* eng
->V_Angle
;
769 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, PistonColor
);
770 glColor4fv(PistonColor
);
771 DrawPositionedPiston(eng
, rot
);
774 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, ConnRodColor
);
775 glColor4fv(ConnRodColor
);
776 DrawPositionedConnectingRod(eng
, rot
);
780 if (Render
.ShowBlock
) {
781 const GLboolean blend
= glIsEnabled(GL_BLEND
);
783 glDepthMask(GL_FALSE
);
787 glEnable(GL_CULL_FACE
);
789 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, BlockColor
);
790 glColor4fv(BlockColor
);
792 glCallList(eng
->BlockList
);
794 DrawEngineBlock(eng
);
796 glDisable(GL_CULL_FACE
);
797 glDepthMask(GL_TRUE
);
810 const float xmin
= -3.0, xmax
= 3.0;
811 const float ymin
= -1.0, ymax
= 3.0;
812 const float zmin
= -4.0, zmax
= 4.0;
813 const float step
= 0.5;
814 const float d
= 0.01;
816 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
817 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
819 glDisable(GL_LIGHTING
);
820 glDisable(GL_TEXTURE_2D
);
826 for (x
= xmin
; x
<= xmax
; x
+= step
) {
827 glVertex3f(x
, ymin
, zmin
);
828 glVertex3f(x
, ymax
, zmin
);
832 for (y
= ymin
; y
<= ymax
; y
+= step
) {
833 glVertex3f(xmin
, y
, zmin
);
834 glVertex3f(xmax
, y
, zmin
);
840 for (x
= xmin
; x
<= xmax
; x
+= step
) {
841 glVertex3f(x
, ymin
, zmin
);
842 glVertex3f(x
, ymin
, zmax
);
846 for (z
= zmin
; z
<= zmax
; z
+= step
) {
847 glVertex3f(xmin
, ymin
, z
);
848 glVertex3f(xmax
, ymin
, z
);
854 for (y
= ymin
; y
<= ymax
; y
+= step
) {
855 glVertex3f(xmin
, y
, zmin
);
856 glVertex3f(xmin
, y
, zmax
);
860 for (z
= zmin
; z
<= zmax
; z
+= step
) {
861 glVertex3f(xmin
, ymin
, z
);
862 glVertex3f(xmin
, ymax
, z
);
866 glColor3f(0.4, 0.4, 0.6);
869 glVertex3f(xmin
-d
, ymin
, zmin
);
870 glVertex3f(xmin
-d
, ymax
, zmin
);
871 glVertex3f(xmin
-d
, ymax
, zmax
);
872 glVertex3f(xmin
-d
, ymin
, zmax
);
874 glVertex3f(xmin
, ymin
-d
, zmin
);
875 glVertex3f(xmax
, ymin
-d
, zmin
);
876 glVertex3f(xmax
, ymin
-d
, zmax
);
877 glVertex3f(xmin
, ymin
-d
, zmax
);
879 glVertex3f(xmin
, ymin
, zmin
-d
);
880 glVertex3f(xmax
, ymin
, zmin
-d
);
881 glVertex3f(xmax
, ymax
, zmin
-d
);
882 glVertex3f(xmin
, ymax
, zmin
-d
);
886 glEnable(GL_LIGHTING
);
888 glEnable(GL_TEXTURE_2D
);
893 PrintString(const char *s
)
896 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
905 static double t0
= -1.0;
906 static int frames
= 0;
907 double t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
916 else if (t
- t0
>= 1.0) {
917 fps
= (int) (frames
/ (t
- t0
) + 0.5);
932 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
936 glTranslatef(0.0, 0.0, -View
.Distance
);
937 build_rotmatrix(rot
, View
.CurQuat
);
938 glMultMatrixf(&rot
[0][0]);
941 glTranslatef(0, -0.75, 0);
944 DrawEngine(Engines
+ CurEngine
, Theta
);
950 if (Render
.ShowInfo
) {
951 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
952 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
954 sprintf(s
, "%s %d FPS %s", Engines
[CurEngine
].Name
, fps
,
955 Render
.UseLists
? "Display Lists" : "Immediate mode");
956 glDisable(GL_LIGHTING
);
957 glDisable(GL_TEXTURE_2D
);
959 glWindowPos2iARB(10, 10);
962 glEnable(GL_LIGHTING
);
964 glEnable(GL_TEXTURE_2D
);
967 /* also print out a periodic fps to stdout. useful for trying to
968 * figure out the performance impact of rendering the string above
973 static GLint Frames
= 0;
974 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
978 if (t
- T0
>= 5000) {
979 GLfloat seconds
= (t
- T0
) / 1000.0;
980 GLfloat fps
= Frames
/ seconds
;
981 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
994 * Handle window resize.
997 Reshape(int width
, int height
)
999 float ar
= (float) width
/ height
;
1001 glViewport(0, 0, width
, height
);
1002 glMatrixMode(GL_PROJECTION
);
1004 glFrustum(-ar
* s
, ar
* s
, -s
, s
, 2.0, 50.0);
1005 glMatrixMode(GL_MODELVIEW
);
1013 * Handle mouse button.
1016 Mouse(int button
, int state
, int x
, int y
)
1018 if (button
== GLUT_LEFT_BUTTON
) {
1019 if (state
== GLUT_DOWN
) {
1022 View
.Rotating
= GL_TRUE
;
1024 else if (state
== GLUT_UP
) {
1025 View
.Rotating
= GL_FALSE
;
1028 else if (button
== GLUT_MIDDLE_BUTTON
) {
1029 if (state
== GLUT_DOWN
) {
1032 View
.StartDistance
= View
.Distance
;
1033 View
.Translating
= GL_TRUE
;
1035 else if (state
== GLUT_UP
) {
1036 View
.Translating
= GL_FALSE
;
1043 * Handle mouse motion
1046 Motion(int x
, int y
)
1049 if (View
.Rotating
) {
1050 float x0
= (2.0 * View
.StartX
- WinWidth
) / WinWidth
;
1051 float y0
= (WinHeight
- 2.0 * View
.StartY
) / WinHeight
;
1052 float x1
= (2.0 * x
- WinWidth
) / WinWidth
;
1053 float y1
= (WinHeight
- 2.0 * y
) / WinHeight
;
1056 trackball(q
, x0
, y0
, x1
, y1
);
1059 for (i
= 0; i
< 1; i
++)
1060 add_quats(q
, View
.CurQuat
, View
.CurQuat
);
1062 glutPostRedisplay();
1064 else if (View
.Translating
) {
1065 float dz
= 0.01 * (y
- View
.StartY
);
1066 View
.Distance
= View
.StartDistance
+ dz
;
1067 glutPostRedisplay();
1079 Render
.Anim
= !Render
.Anim
;
1087 OptChangeEngine(void)
1089 CurEngine
= (CurEngine
+ 1) % NUM_ENGINES
;
1096 if (Render
.Mode
> TEXTURED
)
1098 SetRenderState(Render
.Mode
);
1102 OptDisplayLists(void)
1105 Render
.UseLists
= !Render
.UseLists
;
1106 if (Render
.UseLists
) {
1107 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1108 GenerateDisplayLists(Engines
+ i
);
1112 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1113 FreeDisplayLists(Engines
+ i
);
1121 Render
.ShowBlock
= !Render
.ShowBlock
;
1127 Render
.ShowInfo
= !Render
.ShowInfo
;
1133 Render
.DrawBox
= !Render
.DrawBox
;
1150 * Define menu entries (w/ keyboard shortcuts)
1157 void (*Function
)(void);
1160 static const MenuInfo MenuItems
[] = {
1161 { "Animation", 'a', OptAnimation
},
1162 { "Change Engine", 'e', OptChangeEngine
},
1163 { "Rendering Style", 'm', OptRenderMode
},
1164 { "Display Lists", 'd', OptDisplayLists
},
1165 { "Show Block", 'b', OptShowBlock
},
1166 { "Show Info", 'i', OptShowInfo
},
1167 { "Show Box", 'x', OptShowBox
},
1168 { "Exit", 27, OptExit
},
1169 { NULL
, 'r', OptRotate
},
1175 * Handle menu selection.
1178 MenuHandler(int entry
)
1180 MenuItems
[entry
].Function();
1181 glutPostRedisplay();
1192 glutCreateMenu(MenuHandler
);
1193 for (i
= 0; MenuItems
[i
].Text
; i
++) {
1194 glutAddMenuEntry(MenuItems
[i
].Text
, i
);
1196 glutAttachMenu(GLUT_RIGHT_BUTTON
);
1201 * Handle keyboard event.
1204 Key(unsigned char key
, int x
, int y
)
1208 for (i
= 0; MenuItems
[i
].Key
; i
++) {
1209 if (MenuItems
[i
].Key
== key
) {
1210 MenuItems
[i
].Function();
1211 glutPostRedisplay();
1219 void LoadTexture(void)
1221 GLboolean convolve
= GL_FALSE
;
1223 glGenTextures(1, &TextureObj
);
1224 glBindTexture(GL_TEXTURE_2D
, TextureObj
);
1225 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1226 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1227 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1228 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1231 #define FILTER_SIZE 7
1232 /* use convolution to blur the texture to simulate a dull finish
1238 GLfloat filter
[FILTER_SIZE
][FILTER_SIZE
][4];
1240 for (h
= 0; h
< FILTER_SIZE
; h
++) {
1241 for (w
= 0; w
< FILTER_SIZE
; w
++) {
1242 const GLfloat k
= 1.0 / (FILTER_SIZE
* FILTER_SIZE
);
1243 filter
[h
][w
][0] = k
;
1244 filter
[h
][w
][1] = k
;
1245 filter
[h
][w
][2] = k
;
1246 filter
[h
][w
][3] = k
;
1250 glEnable(GL_CONVOLUTION_2D
);
1251 glConvolutionParameteri(GL_CONVOLUTION_2D
,
1252 GL_CONVOLUTION_BORDER_MODE
, GL_CONSTANT_BORDER
);
1253 glConvolutionFilter2D(GL_CONVOLUTION_2D
, GL_RGBA
,
1254 FILTER_SIZE
, FILTER_SIZE
,
1255 GL_RGBA
, GL_FLOAT
, filter
);
1257 img
= LoadRGBImage(TEXTURE_FILE
, &w
, &h
, &format
);
1259 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1263 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, w
, h
, 0,
1264 format
, GL_UNSIGNED_BYTE
, img
);
1268 if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
1269 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1279 const GLfloat lightColor
[4] = { 0.7, 0.7, 0.7, 1.0 };
1280 const GLfloat specular
[4] = { 0.8, 0.8, 0.8, 1.0 };
1281 const GLfloat backColor
[4] = { 1, 1, 0, 0 };
1283 Q
= gluNewQuadric();
1284 gluQuadricNormals(Q
, GLU_SMOOTH
);
1288 glClearColor(0.3, 0.3, 0.3, 0.0);
1289 glEnable(GL_DEPTH_TEST
);
1290 glEnable(GL_LIGHTING
);
1291 glEnable(GL_LIGHT0
);
1292 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, lightColor
);
1293 glMaterialf(GL_FRONT
, GL_SHININESS
, 40);
1294 glMaterialfv(GL_FRONT
, GL_SPECULAR
, specular
);
1295 glEnable(GL_NORMALIZE
);
1297 glMaterialfv(GL_BACK
, GL_DIFFUSE
, backColor
);
1299 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE
, 1);
1301 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1303 InitViewInfo(&View
);
1304 InitRenderInfo(&Render
);
1309 main(int argc
, char *argv
[])
1311 glutInit(&argc
, argv
);
1312 glutInitWindowSize(WinWidth
, WinHeight
);
1313 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
1314 glutCreateWindow("OpenGL Engine Demo");
1316 glutReshapeFunc(Reshape
);
1317 glutMouseFunc(Mouse
);
1318 glutMotionFunc(Motion
);
1319 glutKeyboardFunc(Key
);
1320 glutDisplayFunc(Draw
);