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
);
395 * Draw connector plate. Used for crankshaft and connecting rods.
398 DrawConnector(float length
, float thickness
,
399 float bigEndRadius
, float smallEndRadius
)
401 const float bigRadius
= 1.2 * bigEndRadius
;
402 const float smallRadius
= 1.2 * smallEndRadius
;
403 const float z0
= -0.5 * thickness
, z1
= -z0
;
404 GLfloat points
[36][2], normals
[36][2];
407 /* compute vertex locations, normals */
408 for (i
= 0; i
< 36; i
++) {
409 const int angle
= i
* 10;
410 float x
= cos(DEG_TO_RAD(angle
));
411 float y
= sin(DEG_TO_RAD(angle
));
414 if (angle
>= 0 && angle
<= 180) {
416 y
= y
* smallRadius
+ length
;
429 for (i
= 0; i
< 36; i
++) {
430 glVertex3f(points
[i
][0], points
[i
][1], z1
);
435 glNormal3f(0, 0, -1);
437 for (i
= 0; i
< 36; i
++) {
438 glVertex3f(points
[35-i
][0], points
[35-i
][1], z0
);
443 glBegin(GL_QUAD_STRIP
);
444 for (i
= 0; i
<= 36; i
++) {
445 const int j
= i
% 36;
446 glNormal3f(normals
[j
][0], normals
[j
][1], 0);
447 glVertex3f(points
[j
][0], points
[j
][1], z1
);
448 glVertex3f(points
[j
][0], points
[j
][1], z0
);
455 * Draw a crankshaft. Shaft lies along +Z axis, starting at zero.
458 DrawCrankshaft(const Engine
*eng
)
460 const int slices
= 20, stacks
= 2;
461 const int n
= eng
->Cranks
* 4 + 1;
462 const float phiStep
= 360 / eng
->Cranks
;
467 for (i
= 0; i
< n
; i
++) {
469 glTranslatef(0, 0, z
);
471 /* draw a crank plate */
472 glRotatef(phi
, 0, 0, 1);
473 glTranslatef(0, 0, 0.5 * eng
->CrankPlateThickness
);
474 DrawConnector(eng
->Throw
, eng
->CrankPlateThickness
,
475 eng
->CrankJournalRadius
, eng
->CrankPinRadius
);
480 else if (i
% 4 == 0) {
481 /* draw crank journal segment */
482 gluCylinder(Q
, eng
->CrankJournalRadius
, eng
->CrankJournalRadius
,
483 eng
->CrankJournalLength
, slices
, stacks
);
484 z
+= eng
->CrankJournalLength
;
486 else if (i
% 4 == 2) {
487 /* draw crank pin segment */
488 glRotatef(phi
, 0, 0, 1);
489 glTranslatef(0, eng
->Throw
, 0);
490 gluCylinder(Q
, eng
->CrankPinRadius
, eng
->CrankPinRadius
,
491 eng
->CrankJournalLength
, slices
, stacks
);
492 z
+= eng
->CrankJournalLength
;
500 * Draw crankshaft at a particular rotation.
501 * \param crankAngle current crankshaft rotation, in radians
504 DrawPositionedCrankshaft(const Engine
*eng
, float crankAngle
)
507 glRotatef(crankAngle
, 0, 0, 1);
509 glCallList(eng
->CrankList
);
517 * Draw a connecting rod at particular position.
518 * \param eng description of connecting rod to draw
519 * \param crankAngle current crankshaft rotation, in radians
522 DrawPositionedConnectingRod(const Engine
*eng
, float crankAngle
)
524 float x0
, y0
, x1
, y1
;
527 ComputeConnectingRodPosition(eng
->Throw
, crankAngle
,
528 eng
->ConnectingRodLength
,
530 d
= sqrt(eng
->ConnectingRodLength
* eng
->ConnectingRodLength
- x0
* x0
);
531 phi
= atan(x0
/ d
) * 180.0 / M_PI
;
534 glTranslatef(x0
, y0
, 0);
535 glRotatef(phi
, 0, 0, 1);
536 if (eng
->ConnRodList
)
537 glCallList(eng
->ConnRodList
);
539 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
540 eng
->CrankPinRadius
, eng
->WristPinRadius
);
546 * Draw a square with a hole in middle.
549 SquareWithHole(float squareSize
, float holeRadius
)
552 glBegin(GL_QUAD_STRIP
);
554 for (i
= 0; i
<= 360; i
+= 5) {
555 const float x1
= holeRadius
* cos(DEG_TO_RAD(i
));
556 const float y1
= holeRadius
* sin(DEG_TO_RAD(i
));
557 float x2
= 0.0F
, y2
= 0.0F
;
558 if (i
> 315 || i
<= 45) {
560 y2
= squareSize
* tan(DEG_TO_RAD(i
));
562 else if (i
> 45 && i
<= 135) {
563 x2
= -squareSize
* tan(DEG_TO_RAD(i
- 90));
566 else if (i
> 135 && i
<= 225) {
568 y2
= -squareSize
* tan(DEG_TO_RAD(i
-180));
570 else if (i
> 225 && i
<= 315) {
571 x2
= squareSize
* tan(DEG_TO_RAD(i
- 270));
574 glVertex2f(x1
, y1
); /* inner circle */
575 glVertex2f(x2
, y2
); /* outer square */
582 * Draw block with hole through middle.
583 * Hole is centered on Z axis.
584 * Bottom of block is at z=0, top of block is at z = blockHeight.
585 * index is in [0, count - 1] to determine which block faces are drawn.
588 DrawBlockWithHole(float blockSize
, float blockHeight
, float holeRadius
,
589 int index
, int count
)
591 const int slices
= 30, stacks
= 4;
592 const float x
= blockSize
;
593 const float y
= blockSize
;
595 const float z1
= blockHeight
;
597 assert(index
< count
);
599 gluQuadricOrientation(Q
, GLU_INSIDE
);
604 glVertex3f( x
, -y
, z0
);
605 glVertex3f( x
, y
, z0
);
606 glVertex3f( x
, y
, z1
);
607 glVertex3f( x
, -y
, z1
);
609 glNormal3f(-1, 0, 0);
610 glVertex3f(-x
, -y
, z1
);
611 glVertex3f(-x
, y
, z1
);
612 glVertex3f(-x
, y
, z0
);
613 glVertex3f(-x
, -y
, z0
);
617 glVertex3f(-x
, y
, z1
);
618 glVertex3f( x
, y
, z1
);
619 glVertex3f( x
, y
, z0
);
620 glVertex3f(-x
, y
, z0
);
622 if (index
== count
- 1) {
624 glNormal3f(0, -1, 0);
625 glVertex3f(-x
, -y
, z0
);
626 glVertex3f( x
, -y
, z0
);
627 glVertex3f( x
, -y
, z1
);
628 glVertex3f(-x
, -y
, z1
);
632 /* cylinder / hole */
633 gluCylinder(Q
, holeRadius
, holeRadius
, blockHeight
, slices
, stacks
);
637 glRotatef(180, 1, 0, 0);
638 SquareWithHole(blockSize
, holeRadius
);
642 glTranslatef(0, 0, z1
);
643 SquareWithHole(blockSize
, holeRadius
);
645 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
650 * Draw the engine block.
653 DrawEngineBlock(const Engine
*eng
)
655 const float blockHeight
= eng
->Throw
+ 1.5 * eng
->PistonHeight
;
656 const float cylRadius
= 1.01 * eng
->PistonRadius
;
657 const float blockSize
= 0.5 * PistonSpacing(eng
);
658 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
661 for (i
= 0; i
< eng
->Pistons
; i
++) {
662 const float z
= PistonShaftPosition(eng
, i
);
663 const int crank
= i
/ pistonsPerCrank
;
667 glTranslatef(0, 0, z
);
669 /* additional rotation for kth piston per crank */
670 k
= i
% pistonsPerCrank
;
671 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
674 glRotatef(-90, 1, 0, 0);
675 glTranslatef(0, 0, eng
->Throw
* 2);
676 DrawBlockWithHole(blockSize
, blockHeight
, cylRadius
,
684 * Generate display lists for engine parts.
687 GenerateDisplayLists(Engine
*eng
)
689 eng
->CrankList
= glGenLists(1);
690 glNewList(eng
->CrankList
, GL_COMPILE
);
694 eng
->ConnRodList
= glGenLists(1);
695 glNewList(eng
->ConnRodList
, GL_COMPILE
);
696 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
697 eng
->CrankPinRadius
, eng
->WristPinRadius
);
700 eng
->PistonList
= glGenLists(1);
701 glNewList(eng
->PistonList
, GL_COMPILE
);
705 eng
->BlockList
= glGenLists(1);
706 glNewList(eng
->BlockList
, GL_COMPILE
);
707 DrawEngineBlock(eng
);
713 * Free engine display lists (render with immediate mode).
716 FreeDisplayLists(Engine
*eng
)
718 glDeleteLists(eng
->CrankList
, 1);
720 glDeleteLists(eng
->ConnRodList
, 1);
721 eng
->ConnRodList
= 0;
722 glDeleteLists(eng
->PistonList
, 1);
724 glDeleteLists(eng
->BlockList
, 1);
730 * Draw complete engine.
731 * \param eng description of engine to draw
732 * \param crankAngle current crankshaft angle, in radians
735 DrawEngine(const Engine
*eng
, float crankAngle
)
737 const float crankDelta
= 360.0 / eng
->Cranks
;
738 const float crankLen
= CrankshaftLength(eng
);
739 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
743 glRotatef(eng
->V_Angle
* 0.5, 0, 0, 1);
744 glTranslatef(0, 0, -0.5 * crankLen
);
747 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, CrankshaftColor
);
748 glColor4fv(CrankshaftColor
);
749 DrawPositionedCrankshaft(eng
, crankAngle
);
751 for (i
= 0; i
< eng
->Pistons
; i
++) {
752 const float z
= PistonShaftPosition(eng
, i
);
753 const int crank
= i
/ pistonsPerCrank
;
754 float rot
= crankAngle
+ crank
* crankDelta
;
758 glTranslatef(0, 0, z
);
760 /* additional rotation for kth piston per crank */
761 k
= i
% pistonsPerCrank
;
762 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
763 rot
+= k
* eng
->V_Angle
;
766 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, PistonColor
);
767 glColor4fv(PistonColor
);
768 DrawPositionedPiston(eng
, rot
);
771 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, ConnRodColor
);
772 glColor4fv(ConnRodColor
);
773 DrawPositionedConnectingRod(eng
, rot
);
777 if (Render
.ShowBlock
) {
778 const GLboolean blend
= glIsEnabled(GL_BLEND
);
780 glDepthMask(GL_FALSE
);
784 glEnable(GL_CULL_FACE
);
786 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, BlockColor
);
787 glColor4fv(BlockColor
);
789 glCallList(eng
->BlockList
);
791 DrawEngineBlock(eng
);
793 glDisable(GL_CULL_FACE
);
794 glDepthMask(GL_TRUE
);
807 const float xmin
= -3.0, xmax
= 3.0;
808 const float ymin
= -1.0, ymax
= 3.0;
809 const float zmin
= -4.0, zmax
= 4.0;
810 const float step
= 0.5;
811 const float d
= 0.01;
813 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
814 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
816 glDisable(GL_LIGHTING
);
817 glDisable(GL_TEXTURE_2D
);
823 for (x
= xmin
; x
<= xmax
; x
+= step
) {
824 glVertex3f(x
, ymin
, zmin
);
825 glVertex3f(x
, ymax
, zmin
);
829 for (y
= ymin
; y
<= ymax
; y
+= step
) {
830 glVertex3f(xmin
, y
, zmin
);
831 glVertex3f(xmax
, y
, zmin
);
837 for (x
= xmin
; x
<= xmax
; x
+= step
) {
838 glVertex3f(x
, ymin
, zmin
);
839 glVertex3f(x
, ymin
, zmax
);
843 for (z
= zmin
; z
<= zmax
; z
+= step
) {
844 glVertex3f(xmin
, ymin
, z
);
845 glVertex3f(xmax
, ymin
, z
);
851 for (y
= ymin
; y
<= ymax
; y
+= step
) {
852 glVertex3f(xmin
, y
, zmin
);
853 glVertex3f(xmin
, y
, zmax
);
857 for (z
= zmin
; z
<= zmax
; z
+= step
) {
858 glVertex3f(xmin
, ymin
, z
);
859 glVertex3f(xmin
, ymax
, z
);
863 glColor3f(0.4, 0.4, 0.6);
866 glVertex3f(xmin
-d
, ymin
, zmin
);
867 glVertex3f(xmin
-d
, ymax
, zmin
);
868 glVertex3f(xmin
-d
, ymax
, zmax
);
869 glVertex3f(xmin
-d
, ymin
, zmax
);
871 glVertex3f(xmin
, ymin
-d
, zmin
);
872 glVertex3f(xmax
, ymin
-d
, zmin
);
873 glVertex3f(xmax
, ymin
-d
, zmax
);
874 glVertex3f(xmin
, ymin
-d
, zmax
);
876 glVertex3f(xmin
, ymin
, zmin
-d
);
877 glVertex3f(xmax
, ymin
, zmin
-d
);
878 glVertex3f(xmax
, ymax
, zmin
-d
);
879 glVertex3f(xmin
, ymax
, zmin
-d
);
883 glEnable(GL_LIGHTING
);
885 glEnable(GL_TEXTURE_2D
);
890 PrintString(const char *s
)
893 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
902 static double t0
= -1.0;
903 static int frames
= 0;
904 double t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
913 else if (t
- t0
>= 1.0) {
914 fps
= (int) (frames
/ (t
- t0
) + 0.5);
929 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
933 glTranslatef(0.0, 0.0, -View
.Distance
);
934 build_rotmatrix(rot
, View
.CurQuat
);
935 glMultMatrixf(&rot
[0][0]);
938 glTranslatef(0, -0.75, 0);
941 DrawEngine(Engines
+ CurEngine
, Theta
);
947 if (Render
.ShowInfo
) {
948 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
949 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
951 sprintf(s
, "%s %d FPS %s", Engines
[CurEngine
].Name
, fps
,
952 Render
.UseLists
? "Display Lists" : "Immediate mode");
953 glDisable(GL_LIGHTING
);
954 glDisable(GL_TEXTURE_2D
);
956 glWindowPos2iARB(10, 10);
959 glEnable(GL_LIGHTING
);
961 glEnable(GL_TEXTURE_2D
);
969 * Handle window resize.
972 Reshape(int width
, int height
)
974 float ar
= (float) width
/ height
;
976 glViewport(0, 0, width
, height
);
977 glMatrixMode(GL_PROJECTION
);
979 glFrustum(-ar
* s
, ar
* s
, -s
, s
, 2.0, 50.0);
980 glMatrixMode(GL_MODELVIEW
);
988 * Handle mouse button.
991 Mouse(int button
, int state
, int x
, int y
)
993 if (button
== GLUT_LEFT_BUTTON
) {
994 if (state
== GLUT_DOWN
) {
997 View
.Rotating
= GL_TRUE
;
999 else if (state
== GLUT_UP
) {
1000 View
.Rotating
= GL_FALSE
;
1003 else if (button
== GLUT_MIDDLE_BUTTON
) {
1004 if (state
== GLUT_DOWN
) {
1007 View
.StartDistance
= View
.Distance
;
1008 View
.Translating
= GL_TRUE
;
1010 else if (state
== GLUT_UP
) {
1011 View
.Translating
= GL_FALSE
;
1018 * Handle mouse motion
1021 Motion(int x
, int y
)
1024 if (View
.Rotating
) {
1025 float x0
= (2.0 * View
.StartX
- WinWidth
) / WinWidth
;
1026 float y0
= (WinHeight
- 2.0 * View
.StartY
) / WinHeight
;
1027 float x1
= (2.0 * x
- WinWidth
) / WinWidth
;
1028 float y1
= (WinHeight
- 2.0 * y
) / WinHeight
;
1031 trackball(q
, x0
, y0
, x1
, y1
);
1034 for (i
= 0; i
< 1; i
++)
1035 add_quats(q
, View
.CurQuat
, View
.CurQuat
);
1037 glutPostRedisplay();
1039 else if (View
.Translating
) {
1040 float dz
= 0.01 * (y
- View
.StartY
);
1041 View
.Distance
= View
.StartDistance
+ dz
;
1042 glutPostRedisplay();
1054 Render
.Anim
= !Render
.Anim
;
1062 OptChangeEngine(void)
1064 CurEngine
= (CurEngine
+ 1) % NUM_ENGINES
;
1071 if (Render
.Mode
> TEXTURED
)
1073 SetRenderState(Render
.Mode
);
1077 OptDisplayLists(void)
1080 Render
.UseLists
= !Render
.UseLists
;
1081 if (Render
.UseLists
) {
1082 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1083 GenerateDisplayLists(Engines
+ i
);
1087 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1088 FreeDisplayLists(Engines
+ i
);
1096 Render
.ShowBlock
= !Render
.ShowBlock
;
1102 Render
.ShowInfo
= !Render
.ShowInfo
;
1108 Render
.DrawBox
= !Render
.DrawBox
;
1125 * Define menu entries (w/ keyboard shortcuts)
1132 void (*Function
)(void);
1135 static const MenuInfo MenuItems
[] = {
1136 { "Animation", 'a', OptAnimation
},
1137 { "Change Engine", 'e', OptChangeEngine
},
1138 { "Rendering Style", 'm', OptRenderMode
},
1139 { "Display Lists", 'd', OptDisplayLists
},
1140 { "Show Block", 'b', OptShowBlock
},
1141 { "Show Info", 'i', OptShowInfo
},
1142 { "Show Box", 'x', OptShowBox
},
1143 { "Exit", 27, OptExit
},
1144 { NULL
, 'r', OptRotate
},
1150 * Handle menu selection.
1153 MenuHandler(int entry
)
1155 MenuItems
[entry
].Function();
1156 glutPostRedisplay();
1167 glutCreateMenu(MenuHandler
);
1168 for (i
= 0; MenuItems
[i
].Text
; i
++) {
1169 glutAddMenuEntry(MenuItems
[i
].Text
, i
);
1171 glutAttachMenu(GLUT_RIGHT_BUTTON
);
1176 * Handle keyboard event.
1179 Key(unsigned char key
, int x
, int y
)
1183 for (i
= 0; MenuItems
[i
].Key
; i
++) {
1184 if (MenuItems
[i
].Key
== key
) {
1185 MenuItems
[i
].Function();
1186 glutPostRedisplay();
1194 void LoadTexture(void)
1196 GLboolean convolve
= GL_FALSE
;
1198 glGenTextures(1, &TextureObj
);
1199 glBindTexture(GL_TEXTURE_2D
, TextureObj
);
1200 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1201 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1202 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1203 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1206 #define FILTER_SIZE 7
1207 /* use convolution to blur the texture to simulate a dull finish
1213 GLfloat filter
[FILTER_SIZE
][FILTER_SIZE
][4];
1215 for (h
= 0; h
< FILTER_SIZE
; h
++) {
1216 for (w
= 0; w
< FILTER_SIZE
; w
++) {
1217 const GLfloat k
= 1.0 / (FILTER_SIZE
* FILTER_SIZE
);
1218 filter
[h
][w
][0] = k
;
1219 filter
[h
][w
][1] = k
;
1220 filter
[h
][w
][2] = k
;
1221 filter
[h
][w
][3] = k
;
1225 glEnable(GL_CONVOLUTION_2D
);
1226 glConvolutionParameteri(GL_CONVOLUTION_2D
,
1227 GL_CONVOLUTION_BORDER_MODE
, GL_CONSTANT_BORDER
);
1228 glConvolutionFilter2D(GL_CONVOLUTION_2D
, GL_RGBA
,
1229 FILTER_SIZE
, FILTER_SIZE
,
1230 GL_RGBA
, GL_FLOAT
, filter
);
1232 img
= LoadRGBImage(TEXTURE_FILE
, &w
, &h
, &format
);
1234 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1238 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, w
, h
, 0,
1239 format
, GL_UNSIGNED_BYTE
, img
);
1243 if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
1244 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1254 const GLfloat lightColor
[4] = { 0.7, 0.7, 0.7, 1.0 };
1255 const GLfloat specular
[4] = { 0.8, 0.8, 0.8, 1.0 };
1256 const GLfloat backColor
[4] = { 1, 1, 0, 0 };
1258 Q
= gluNewQuadric();
1259 gluQuadricNormals(Q
, GLU_SMOOTH
);
1263 glClearColor(0.3, 0.3, 0.3, 0.0);
1264 glEnable(GL_DEPTH_TEST
);
1265 glEnable(GL_LIGHTING
);
1266 glEnable(GL_LIGHT0
);
1267 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, lightColor
);
1268 glMaterialf(GL_FRONT
, GL_SHININESS
, 40);
1269 glMaterialfv(GL_FRONT
, GL_SPECULAR
, specular
);
1270 glEnable(GL_NORMALIZE
);
1272 glMaterialfv(GL_BACK
, GL_DIFFUSE
, backColor
);
1274 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE
, 1);
1276 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1278 InitViewInfo(&View
);
1279 InitRenderInfo(&Render
);
1284 main(int argc
, char *argv
[])
1286 glutInit(&argc
, argv
);
1287 glutInitWindowSize(WinWidth
, WinHeight
);
1288 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
1289 glutCreateWindow("OpenGL Engine Demo");
1291 glutReshapeFunc(Reshape
);
1292 glutMouseFunc(Mouse
);
1293 glutMotionFunc(Motion
);
1294 glutKeyboardFunc(Key
);
1295 glutDisplayFunc(Draw
);