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 */
134 0.5, /* PistonRadius */
135 0.6, /* PistonHeight */
136 0.1, /* WristPinRadius */
138 0.2, /* CrankPlateThickness */
139 0.25, /* CrankPinRadius */
140 0.3, /* CrankJournalRadius */
141 0.4, /* CrankJournalLength */
142 1.5, /* ConnectingRodLength */
143 0.1, /* ConnectingRodThickness */
154 0.5, /* PistonRadius */
155 0.6, /* PistonHeight */
156 0.1, /* WristPinRadius */
158 0.2, /* CrankPlateThickness */
159 0.25, /* CrankPinRadius */
160 0.3, /* CrankJournalRadius */
161 0.4, /* CrankJournalLength */
162 1.5, /* ConnectingRodLength */
163 0.1, /* ConnectingRodThickness */
171 static int CurEngine
= 0;
176 InitViewInfo(ViewInfo
*view
)
178 view
->Rotating
= GL_FALSE
;
179 view
->Translating
= GL_FALSE
;
180 view
->StartX
= view
->StartY
= 0;
181 view
->Distance
= 12.0;
182 view
->StartDistance
= 0.0;
183 view
->CurQuat
[0] = -0.194143;
184 view
->CurQuat
[1] = 0.507848;
185 view
->CurQuat
[2] = 0.115245;
186 view
->CurQuat
[3] = 0.831335;
191 InitRenderInfo(RenderInfo
*render
)
194 render
->Anim
= GL_TRUE
;
195 render
->Wireframe
= GL_FALSE
;
196 render
->Blend
= GL_FALSE
;
197 render
->Antialias
= GL_FALSE
;
198 render
->Texture
= GL_FALSE
;
199 render
->DrawBox
= GL_FALSE
;
200 render
->ShowInfo
= GL_TRUE
;
201 render
->ShowBlock
= GL_FALSE
;
202 render
->UseLists
= GL_FALSE
;
207 * Set GL for given rendering mode.
210 SetRenderState(RenderMode mode
)
212 static const GLfloat gray2
[4] = { 0.2, 0.2, 0.2, 1.0 };
213 static const GLfloat gray4
[4] = { 0.4, 0.4, 0.4, 1.0 };
216 glDisable(GL_LIGHTING
);
217 glDisable(GL_TEXTURE_2D
);
219 glDisable(GL_LINE_SMOOTH
);
220 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
221 glDisable(GL_TEXTURE_GEN_S
);
222 glDisable(GL_TEXTURE_GEN_T
);
223 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, gray2
);
227 glEnable(GL_LIGHTING
);
230 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
231 glEnable(GL_LINE_SMOOTH
);
236 glEnable(GL_LIGHTING
);
237 glEnable(GL_TEXTURE_2D
);
238 glEnable(GL_TEXTURE_GEN_S
);
239 glEnable(GL_TEXTURE_GEN_T
);
240 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, gray4
);
249 * Animate the engine parts.
254 /* convert degrees per millisecond to RPM: */
255 const float m
= 360.0 / 1000.0 / 60.0;
256 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
257 Theta
= ((int) (t
* RPM
* m
)) % 360;
263 * Compute piston's position along its stroke.
266 PistonStrokePosition(float throwDist
, float crankAngle
, float connRodLength
)
268 float x
= throwDist
* cos(DEG_TO_RAD(crankAngle
));
269 float y
= throwDist
* sin(DEG_TO_RAD(crankAngle
));
270 float pos
= y
+ sqrt(connRodLength
* connRodLength
- x
* x
);
276 * Compute position of nth piston along the crankshaft.
279 PistonShaftPosition(const Engine
*eng
, int piston
)
281 const int i
= piston
/ (eng
->Pistons
/ eng
->Cranks
);
283 assert(piston
< eng
->Pistons
);
284 z
= 1.5 * eng
->CrankJournalLength
+ eng
->CrankPlateThickness
285 + i
* (2.0 * (eng
->CrankJournalLength
+ eng
->CrankPlateThickness
));
286 if (eng
->Pistons
> eng
->Cranks
) {
288 z
+= eng
->ConnectingRodThickness
;
290 z
-= eng
->ConnectingRodThickness
;
297 * Compute distance between two adjacent pistons
300 PistonSpacing(const Engine
*eng
)
302 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
303 const float z0
= PistonShaftPosition(eng
, 0);
304 const float z1
= PistonShaftPosition(eng
, pistonsPerCrank
);
310 * (x0, y0) = position of big end on crankshaft
311 * (x1, y1) = position of small end on piston
314 ComputeConnectingRodPosition(float throwDist
, float crankAngle
,
316 float *x0
, float *y0
, float *x1
, float *y1
)
318 *x0
= throwDist
* cos(DEG_TO_RAD(crankAngle
));
319 *y0
= throwDist
* sin(DEG_TO_RAD(crankAngle
));
321 *y1
= PistonStrokePosition(throwDist
, crankAngle
, connRodLength
);
326 * Compute total length of the crankshaft.
329 CrankshaftLength(const Engine
*eng
)
331 float len
= (eng
->Cranks
* 2 + 1) * eng
->CrankJournalLength
332 + 2 * eng
->Cranks
* eng
->CrankPlateThickness
;
339 * Axis of piston = Z axis. Wrist pin is centered on (0, 0, 0).
342 DrawPiston(const Engine
*eng
)
344 const int slices
= 30, stacks
= 4, loops
= 4;
345 const float innerRadius
= 0.9 * eng
->PistonRadius
;
346 const float innerHeight
= eng
->PistonHeight
- 0.15;
347 const float wristPinLength
= 1.8 * eng
->PistonRadius
;
352 glTranslatef(0, 0, -1.1 * eng
->WristPinRadius
);
354 gluQuadricOrientation(Q
, GLU_INSIDE
);
357 gluDisk(Q
, innerRadius
, eng
->PistonRadius
, slices
, 1/*loops*/);
360 gluCylinder(Q
, innerRadius
, innerRadius
, innerHeight
, slices
, stacks
);
364 glTranslatef(0, 0, innerHeight
);
365 gluDisk(Q
, 0, innerRadius
, slices
, loops
);
368 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
371 gluCylinder(Q
, eng
->PistonRadius
, eng
->PistonRadius
, eng
->PistonHeight
,
375 glTranslatef(0, 0, eng
->PistonHeight
);
376 gluDisk(Q
, 0, eng
->PistonRadius
, slices
, loops
);
382 glTranslatef(0, 0.5 * wristPinLength
, 0.0);
383 glRotatef(90, 1, 0, 0);
384 gluCylinder(Q
, eng
->WristPinRadius
, eng
->WristPinRadius
, wristPinLength
,
391 * Draw piston at particular position.
394 DrawPositionedPiston(const Engine
*eng
, float crankAngle
)
396 const float pos
= PistonStrokePosition(eng
->Throw
, crankAngle
,
397 eng
->ConnectingRodLength
);
399 glRotatef(-90, 1, 0, 0);
400 glTranslatef(0, 0, pos
);
402 glCallList(eng
->PistonList
);
410 * Draw connector plate. Used for crankshaft and connecting rods.
413 DrawConnector(float length
, float thickness
,
414 float bigEndRadius
, float smallEndRadius
)
416 const float bigRadius
= 1.2 * bigEndRadius
;
417 const float smallRadius
= 1.2 * smallEndRadius
;
418 const float z0
= -0.5 * thickness
, z1
= -z0
;
419 GLfloat points
[36][2], normals
[36][2];
422 /* compute vertex locations, normals */
423 for (i
= 0; i
< 36; i
++) {
424 const int angle
= i
* 10;
425 float x
= cos(DEG_TO_RAD(angle
));
426 float y
= sin(DEG_TO_RAD(angle
));
429 if (angle
>= 0 && angle
<= 180) {
431 y
= y
* smallRadius
+ length
;
444 for (i
= 0; i
< 36; i
++) {
445 glVertex3f(points
[i
][0], points
[i
][1], z1
);
450 glNormal3f(0, 0, -1);
452 for (i
= 0; i
< 36; i
++) {
453 glVertex3f(points
[35-i
][0], points
[35-i
][1], z0
);
458 glBegin(GL_QUAD_STRIP
);
459 for (i
= 0; i
<= 36; i
++) {
460 const int j
= i
% 36;
461 glNormal3f(normals
[j
][0], normals
[j
][1], 0);
462 glVertex3f(points
[j
][0], points
[j
][1], z1
);
463 glVertex3f(points
[j
][0], points
[j
][1], z0
);
470 * Draw a crankshaft. Shaft lies along +Z axis, starting at zero.
473 DrawCrankshaft(const Engine
*eng
)
475 const int slices
= 20, stacks
= 2;
476 const int n
= eng
->Cranks
* 4 + 1;
477 const float phiStep
= 360 / eng
->Cranks
;
482 for (i
= 0; i
< n
; i
++) {
484 glTranslatef(0, 0, z
);
486 /* draw a crank plate */
487 glRotatef(phi
, 0, 0, 1);
488 glTranslatef(0, 0, 0.5 * eng
->CrankPlateThickness
);
489 DrawConnector(eng
->Throw
, eng
->CrankPlateThickness
,
490 eng
->CrankJournalRadius
, eng
->CrankPinRadius
);
495 else if (i
% 4 == 0) {
496 /* draw crank journal segment */
497 gluCylinder(Q
, eng
->CrankJournalRadius
, eng
->CrankJournalRadius
,
498 eng
->CrankJournalLength
, slices
, stacks
);
499 z
+= eng
->CrankJournalLength
;
501 else if (i
% 4 == 2) {
502 /* draw crank pin segment */
503 glRotatef(phi
, 0, 0, 1);
504 glTranslatef(0, eng
->Throw
, 0);
505 gluCylinder(Q
, eng
->CrankPinRadius
, eng
->CrankPinRadius
,
506 eng
->CrankJournalLength
, slices
, stacks
);
507 z
+= eng
->CrankJournalLength
;
515 * Draw crankshaft at a particular rotation.
516 * \param crankAngle current crankshaft rotation, in radians
519 DrawPositionedCrankshaft(const Engine
*eng
, float crankAngle
)
522 glRotatef(crankAngle
, 0, 0, 1);
524 glCallList(eng
->CrankList
);
532 * Draw a connecting rod at particular position.
533 * \param eng description of connecting rod to draw
534 * \param crankAngle current crankshaft rotation, in radians
537 DrawPositionedConnectingRod(const Engine
*eng
, float crankAngle
)
539 float x0
, y0
, x1
, y1
;
542 ComputeConnectingRodPosition(eng
->Throw
, crankAngle
,
543 eng
->ConnectingRodLength
,
545 d
= sqrt(eng
->ConnectingRodLength
* eng
->ConnectingRodLength
- x0
* x0
);
546 phi
= atan(x0
/ d
) * 180.0 / M_PI
;
549 glTranslatef(x0
, y0
, 0);
550 glRotatef(phi
, 0, 0, 1);
551 if (eng
->ConnRodList
)
552 glCallList(eng
->ConnRodList
);
554 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
555 eng
->CrankPinRadius
, eng
->WristPinRadius
);
561 * Draw a square with a hole in middle.
564 SquareWithHole(float squareSize
, float holeRadius
)
567 glBegin(GL_QUAD_STRIP
);
569 for (i
= 0; i
<= 360; i
+= 5) {
570 const float x1
= holeRadius
* cos(DEG_TO_RAD(i
));
571 const float y1
= holeRadius
* sin(DEG_TO_RAD(i
));
572 float x2
= 0.0F
, y2
= 0.0F
;
573 if (i
> 315 || i
<= 45) {
575 y2
= squareSize
* tan(DEG_TO_RAD(i
));
577 else if (i
> 45 && i
<= 135) {
578 x2
= -squareSize
* tan(DEG_TO_RAD(i
- 90));
581 else if (i
> 135 && i
<= 225) {
583 y2
= -squareSize
* tan(DEG_TO_RAD(i
-180));
585 else if (i
> 225 && i
<= 315) {
586 x2
= squareSize
* tan(DEG_TO_RAD(i
- 270));
589 glVertex2f(x1
, y1
); /* inner circle */
590 glVertex2f(x2
, y2
); /* outer square */
597 * Draw block with hole through middle.
598 * Hole is centered on Z axis.
599 * Bottom of block is at z=0, top of block is at z = blockHeight.
600 * index is in [0, count - 1] to determine which block faces are drawn.
603 DrawBlockWithHole(float blockSize
, float blockHeight
, float holeRadius
,
604 int index
, int count
)
606 const int slices
= 30, stacks
= 4;
607 const float x
= blockSize
;
608 const float y
= blockSize
;
610 const float z1
= blockHeight
;
612 assert(index
< count
);
614 gluQuadricOrientation(Q
, GLU_INSIDE
);
619 glVertex3f( x
, -y
, z0
);
620 glVertex3f( x
, y
, z0
);
621 glVertex3f( x
, y
, z1
);
622 glVertex3f( x
, -y
, z1
);
624 glNormal3f(-1, 0, 0);
625 glVertex3f(-x
, -y
, z1
);
626 glVertex3f(-x
, y
, z1
);
627 glVertex3f(-x
, y
, z0
);
628 glVertex3f(-x
, -y
, z0
);
632 glVertex3f(-x
, y
, z1
);
633 glVertex3f( x
, y
, z1
);
634 glVertex3f( x
, y
, z0
);
635 glVertex3f(-x
, y
, z0
);
637 if (index
== count
- 1) {
639 glNormal3f(0, -1, 0);
640 glVertex3f(-x
, -y
, z0
);
641 glVertex3f( x
, -y
, z0
);
642 glVertex3f( x
, -y
, z1
);
643 glVertex3f(-x
, -y
, z1
);
647 /* cylinder / hole */
648 gluCylinder(Q
, holeRadius
, holeRadius
, blockHeight
, slices
, stacks
);
652 glRotatef(180, 1, 0, 0);
653 SquareWithHole(blockSize
, holeRadius
);
657 glTranslatef(0, 0, z1
);
658 SquareWithHole(blockSize
, holeRadius
);
660 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
665 * Draw the engine block.
668 DrawEngineBlock(const Engine
*eng
)
670 const float blockHeight
= eng
->Throw
+ 1.5 * eng
->PistonHeight
;
671 const float cylRadius
= 1.01 * eng
->PistonRadius
;
672 const float blockSize
= 0.5 * PistonSpacing(eng
);
673 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
676 for (i
= 0; i
< eng
->Pistons
; i
++) {
677 const float z
= PistonShaftPosition(eng
, i
);
678 const int crank
= i
/ pistonsPerCrank
;
682 glTranslatef(0, 0, z
);
684 /* additional rotation for kth piston per crank */
685 k
= i
% pistonsPerCrank
;
686 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
689 glRotatef(-90, 1, 0, 0);
690 glTranslatef(0, 0, eng
->Throw
* 2);
691 DrawBlockWithHole(blockSize
, blockHeight
, cylRadius
,
699 * Generate display lists for engine parts.
702 GenerateDisplayLists(Engine
*eng
)
704 eng
->CrankList
= glGenLists(1);
705 glNewList(eng
->CrankList
, GL_COMPILE
);
709 eng
->ConnRodList
= glGenLists(1);
710 glNewList(eng
->ConnRodList
, GL_COMPILE
);
711 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
712 eng
->CrankPinRadius
, eng
->WristPinRadius
);
715 eng
->PistonList
= glGenLists(1);
716 glNewList(eng
->PistonList
, GL_COMPILE
);
720 eng
->BlockList
= glGenLists(1);
721 glNewList(eng
->BlockList
, GL_COMPILE
);
722 DrawEngineBlock(eng
);
728 * Free engine display lists (render with immediate mode).
731 FreeDisplayLists(Engine
*eng
)
733 glDeleteLists(eng
->CrankList
, 1);
735 glDeleteLists(eng
->ConnRodList
, 1);
736 eng
->ConnRodList
= 0;
737 glDeleteLists(eng
->PistonList
, 1);
739 glDeleteLists(eng
->BlockList
, 1);
745 * Draw complete engine.
746 * \param eng description of engine to draw
747 * \param crankAngle current crankshaft angle, in radians
750 DrawEngine(const Engine
*eng
, float crankAngle
)
752 const float crankDelta
= 360.0 / eng
->Cranks
;
753 const float crankLen
= CrankshaftLength(eng
);
754 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
758 glRotatef(eng
->V_Angle
* 0.5, 0, 0, 1);
759 glTranslatef(0, 0, -0.5 * crankLen
);
762 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, CrankshaftColor
);
763 glColor4fv(CrankshaftColor
);
764 DrawPositionedCrankshaft(eng
, crankAngle
);
766 for (i
= 0; i
< eng
->Pistons
; i
++) {
767 const float z
= PistonShaftPosition(eng
, i
);
768 const int crank
= i
/ pistonsPerCrank
;
769 float rot
= crankAngle
+ crank
* crankDelta
;
773 glTranslatef(0, 0, z
);
775 /* additional rotation for kth piston per crank */
776 k
= i
% pistonsPerCrank
;
777 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
778 rot
+= k
* eng
->V_Angle
;
781 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, PistonColor
);
782 glColor4fv(PistonColor
);
783 DrawPositionedPiston(eng
, rot
);
786 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, ConnRodColor
);
787 glColor4fv(ConnRodColor
);
788 DrawPositionedConnectingRod(eng
, rot
);
792 if (Render
.ShowBlock
) {
793 const GLboolean blend
= glIsEnabled(GL_BLEND
);
795 glDepthMask(GL_FALSE
);
799 glEnable(GL_CULL_FACE
);
801 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, BlockColor
);
802 glColor4fv(BlockColor
);
804 glCallList(eng
->BlockList
);
806 DrawEngineBlock(eng
);
808 glDisable(GL_CULL_FACE
);
809 glDepthMask(GL_TRUE
);
822 const float xmin
= -3.0, xmax
= 3.0;
823 const float ymin
= -1.0, ymax
= 3.0;
824 const float zmin
= -4.0, zmax
= 4.0;
825 const float step
= 0.5;
826 const float d
= 0.01;
828 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
829 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
831 glDisable(GL_LIGHTING
);
832 glDisable(GL_TEXTURE_2D
);
838 for (x
= xmin
; x
<= xmax
; x
+= step
) {
839 glVertex3f(x
, ymin
, zmin
);
840 glVertex3f(x
, ymax
, zmin
);
844 for (y
= ymin
; y
<= ymax
; y
+= step
) {
845 glVertex3f(xmin
, y
, zmin
);
846 glVertex3f(xmax
, y
, zmin
);
852 for (x
= xmin
; x
<= xmax
; x
+= step
) {
853 glVertex3f(x
, ymin
, zmin
);
854 glVertex3f(x
, ymin
, zmax
);
858 for (z
= zmin
; z
<= zmax
; z
+= step
) {
859 glVertex3f(xmin
, ymin
, z
);
860 glVertex3f(xmax
, ymin
, z
);
866 for (y
= ymin
; y
<= ymax
; y
+= step
) {
867 glVertex3f(xmin
, y
, zmin
);
868 glVertex3f(xmin
, y
, zmax
);
872 for (z
= zmin
; z
<= zmax
; z
+= step
) {
873 glVertex3f(xmin
, ymin
, z
);
874 glVertex3f(xmin
, ymax
, z
);
878 glColor3f(0.4, 0.4, 0.6);
881 glVertex3f(xmin
-d
, ymin
, zmin
);
882 glVertex3f(xmin
-d
, ymax
, zmin
);
883 glVertex3f(xmin
-d
, ymax
, zmax
);
884 glVertex3f(xmin
-d
, ymin
, zmax
);
886 glVertex3f(xmin
, ymin
-d
, zmin
);
887 glVertex3f(xmax
, ymin
-d
, zmin
);
888 glVertex3f(xmax
, ymin
-d
, zmax
);
889 glVertex3f(xmin
, ymin
-d
, zmax
);
891 glVertex3f(xmin
, ymin
, zmin
-d
);
892 glVertex3f(xmax
, ymin
, zmin
-d
);
893 glVertex3f(xmax
, ymax
, zmin
-d
);
894 glVertex3f(xmin
, ymax
, zmin
-d
);
898 glEnable(GL_LIGHTING
);
900 glEnable(GL_TEXTURE_2D
);
905 PrintString(const char *s
)
908 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
917 static double t0
= -1.0;
918 static int frames
= 0;
919 double t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
928 else if (t
- t0
>= 1.0) {
929 fps
= (int) (frames
/ (t
- t0
) + 0.5);
944 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
948 glTranslatef(0.0, 0.0, -View
.Distance
);
949 build_rotmatrix(rot
, View
.CurQuat
);
950 glMultMatrixf(&rot
[0][0]);
953 glTranslatef(0, -0.75, 0);
956 DrawEngine(Engines
+ CurEngine
, Theta
);
962 if (Render
.ShowInfo
) {
963 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
964 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
966 sprintf(s
, "%s %d FPS %s", Engines
[CurEngine
].Name
, fps
,
967 Render
.UseLists
? "Display Lists" : "Immediate mode");
968 glDisable(GL_LIGHTING
);
969 glDisable(GL_TEXTURE_2D
);
971 glWindowPos2iARB(10, 10);
974 glEnable(GL_LIGHTING
);
976 glEnable(GL_TEXTURE_2D
);
979 /* also print out a periodic fps to stdout. useful for trying to
980 * figure out the performance impact of rendering the string above
985 static GLint Frames
= 0;
986 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
990 if (t
- T0
>= 5000) {
991 GLfloat seconds
= (t
- T0
) / 1000.0;
992 GLfloat fps
= Frames
/ seconds
;
993 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
1006 * Handle window resize.
1009 Reshape(int width
, int height
)
1011 float ar
= (float) width
/ height
;
1013 glViewport(0, 0, width
, height
);
1014 glMatrixMode(GL_PROJECTION
);
1016 glFrustum(-ar
* s
, ar
* s
, -s
, s
, 2.0, 50.0);
1017 glMatrixMode(GL_MODELVIEW
);
1025 * Handle mouse button.
1028 Mouse(int button
, int state
, int x
, int y
)
1030 if (button
== GLUT_LEFT_BUTTON
) {
1031 if (state
== GLUT_DOWN
) {
1034 View
.Rotating
= GL_TRUE
;
1036 else if (state
== GLUT_UP
) {
1037 View
.Rotating
= GL_FALSE
;
1040 else if (button
== GLUT_MIDDLE_BUTTON
) {
1041 if (state
== GLUT_DOWN
) {
1044 View
.StartDistance
= View
.Distance
;
1045 View
.Translating
= GL_TRUE
;
1047 else if (state
== GLUT_UP
) {
1048 View
.Translating
= GL_FALSE
;
1055 * Handle mouse motion
1058 Motion(int x
, int y
)
1061 if (View
.Rotating
) {
1062 float x0
= (2.0 * View
.StartX
- WinWidth
) / WinWidth
;
1063 float y0
= (WinHeight
- 2.0 * View
.StartY
) / WinHeight
;
1064 float x1
= (2.0 * x
- WinWidth
) / WinWidth
;
1065 float y1
= (WinHeight
- 2.0 * y
) / WinHeight
;
1068 trackball(q
, x0
, y0
, x1
, y1
);
1071 for (i
= 0; i
< 1; i
++)
1072 add_quats(q
, View
.CurQuat
, View
.CurQuat
);
1074 glutPostRedisplay();
1076 else if (View
.Translating
) {
1077 float dz
= 0.01 * (y
- View
.StartY
);
1078 View
.Distance
= View
.StartDistance
+ dz
;
1079 glutPostRedisplay();
1091 Render
.Anim
= !Render
.Anim
;
1099 OptChangeEngine(void)
1101 CurEngine
= (CurEngine
+ 1) % NUM_ENGINES
;
1108 if (Render
.Mode
> TEXTURED
)
1110 SetRenderState(Render
.Mode
);
1114 OptDisplayLists(void)
1117 Render
.UseLists
= !Render
.UseLists
;
1118 if (Render
.UseLists
) {
1119 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1120 GenerateDisplayLists(Engines
+ i
);
1124 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1125 FreeDisplayLists(Engines
+ i
);
1133 Render
.ShowBlock
= !Render
.ShowBlock
;
1139 Render
.ShowInfo
= !Render
.ShowInfo
;
1145 Render
.DrawBox
= !Render
.DrawBox
;
1162 * Define menu entries (w/ keyboard shortcuts)
1169 void (*Function
)(void);
1172 static const MenuInfo MenuItems
[] = {
1173 { "Animation", 'a', OptAnimation
},
1174 { "Change Engine", 'e', OptChangeEngine
},
1175 { "Rendering Style", 'm', OptRenderMode
},
1176 { "Display Lists", 'd', OptDisplayLists
},
1177 { "Show Block", 'b', OptShowBlock
},
1178 { "Show Info", 'i', OptShowInfo
},
1179 { "Show Box", 'x', OptShowBox
},
1180 { "Exit", 27, OptExit
},
1181 { NULL
, 'r', OptRotate
},
1187 * Handle menu selection.
1190 MenuHandler(int entry
)
1192 MenuItems
[entry
].Function();
1193 glutPostRedisplay();
1204 glutCreateMenu(MenuHandler
);
1205 for (i
= 0; MenuItems
[i
].Text
; i
++) {
1206 glutAddMenuEntry(MenuItems
[i
].Text
, i
);
1208 glutAttachMenu(GLUT_RIGHT_BUTTON
);
1213 * Handle keyboard event.
1216 Key(unsigned char key
, int x
, int y
)
1220 for (i
= 0; MenuItems
[i
].Key
; i
++) {
1221 if (MenuItems
[i
].Key
== key
) {
1222 MenuItems
[i
].Function();
1223 glutPostRedisplay();
1231 void LoadTexture(void)
1233 GLboolean convolve
= GL_FALSE
;
1235 glGenTextures(1, &TextureObj
);
1236 glBindTexture(GL_TEXTURE_2D
, TextureObj
);
1237 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1238 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1239 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1240 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1243 #define FILTER_SIZE 7
1244 /* use convolution to blur the texture to simulate a dull finish
1250 GLfloat filter
[FILTER_SIZE
][FILTER_SIZE
][4];
1252 for (h
= 0; h
< FILTER_SIZE
; h
++) {
1253 for (w
= 0; w
< FILTER_SIZE
; w
++) {
1254 const GLfloat k
= 1.0 / (FILTER_SIZE
* FILTER_SIZE
);
1255 filter
[h
][w
][0] = k
;
1256 filter
[h
][w
][1] = k
;
1257 filter
[h
][w
][2] = k
;
1258 filter
[h
][w
][3] = k
;
1262 glEnable(GL_CONVOLUTION_2D
);
1263 glConvolutionParameteri(GL_CONVOLUTION_2D
,
1264 GL_CONVOLUTION_BORDER_MODE
, GL_CONSTANT_BORDER
);
1265 glConvolutionFilter2D(GL_CONVOLUTION_2D
, GL_RGBA
,
1266 FILTER_SIZE
, FILTER_SIZE
,
1267 GL_RGBA
, GL_FLOAT
, filter
);
1269 img
= LoadRGBImage(TEXTURE_FILE
, &w
, &h
, &format
);
1271 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1275 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, w
, h
, 0,
1276 format
, GL_UNSIGNED_BYTE
, img
);
1280 if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
1281 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1291 const GLfloat lightColor
[4] = { 0.7, 0.7, 0.7, 1.0 };
1292 const GLfloat specular
[4] = { 0.8, 0.8, 0.8, 1.0 };
1293 const GLfloat backColor
[4] = { 1, 1, 0, 0 };
1295 Q
= gluNewQuadric();
1296 gluQuadricNormals(Q
, GLU_SMOOTH
);
1300 glClearColor(0.3, 0.3, 0.3, 0.0);
1301 glEnable(GL_DEPTH_TEST
);
1302 glEnable(GL_LIGHTING
);
1303 glEnable(GL_LIGHT0
);
1304 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, lightColor
);
1305 glMaterialf(GL_FRONT
, GL_SHININESS
, 40);
1306 glMaterialfv(GL_FRONT
, GL_SPECULAR
, specular
);
1307 glEnable(GL_NORMALIZE
);
1309 glMaterialfv(GL_BACK
, GL_DIFFUSE
, backColor
);
1311 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE
, 1);
1313 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1315 InitViewInfo(&View
);
1316 InitRenderInfo(&Render
);
1321 main(int argc
, char *argv
[])
1323 glutInitWindowSize(WinWidth
, WinHeight
);
1324 glutInit(&argc
, argv
);
1325 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
1326 glutCreateWindow("OpenGL Engine Demo");
1328 glutReshapeFunc(Reshape
);
1329 glutMouseFunc(Mouse
);
1330 glutMotionFunc(Motion
);
1331 glutKeyboardFunc(Key
);
1332 glutDisplayFunc(Draw
);