2 * Simple engine demo (crankshaft, pistons, connecting rods)
8 #define GL_GLEXT_PROTOTYPES
16 #include "trackball.h"
20 #define M_PI 3.14159265358979323846
23 #define DEG_TO_RAD(DEG) ((DEG) * M_PI / 180.0)
25 #define TEXTURE_FILE "../images/reflect.rgb"
27 /* Target engine speed: */
28 const int RPM
= 100.0;
44 float CrankPlateThickness
;
46 float CrankJournalRadius
;
47 float CrankJournalLength
;
48 float ConnectingRodLength
;
49 float ConnectingRodThickness
;
50 /* display list IDs */
62 /* When mouse is moving: */
63 GLboolean Rotating
, Translating
;
94 static GLfloat Theta
= 0.0;
96 static const GLfloat PistonColor
[4] = { 1.0, 0.5, 0.5, 1.0 };
97 static const GLfloat ConnRodColor
[4] = { 0.7, 1.0, 0.7, 1.0 };
98 static const GLfloat CrankshaftColor
[4] = { 0.7, 0.7, 1.0, 1.0 };
99 static const GLfloat BlockColor
[4] = {0.8, 0.8, 0.8, 0.75 };
101 static GLuint TextureObj
;
102 static GLint WinWidth
= 800, WinHeight
= 500;
104 static ViewInfo View
;
105 static RenderInfo Render
;
107 #define NUM_ENGINES 3
108 static Engine Engines
[NUM_ENGINES
] =
115 0.5, /* PistonRadius */
116 0.6, /* PistonHeight */
117 0.1, /* WristPinRadius */
119 0.2, /* CrankPlateThickness */
120 0.25, /* CrankPinRadius */
121 0.3, /* CrankJournalRadius */
122 0.4, /* CrankJournalLength */
123 1.5, /* ConnectingRodLength */
124 0.1 /* ConnectingRodThickness */
131 0.5, /* PistonRadius */
132 0.6, /* PistonHeight */
133 0.1, /* WristPinRadius */
135 0.2, /* CrankPlateThickness */
136 0.25, /* CrankPinRadius */
137 0.3, /* CrankJournalRadius */
138 0.4, /* CrankJournalLength */
139 1.5, /* ConnectingRodLength */
140 0.1 /* ConnectingRodThickness */
147 0.5, /* PistonRadius */
148 0.6, /* PistonHeight */
149 0.1, /* WristPinRadius */
151 0.2, /* CrankPlateThickness */
152 0.25, /* CrankPinRadius */
153 0.3, /* CrankJournalRadius */
154 0.4, /* CrankJournalLength */
155 1.5, /* ConnectingRodLength */
156 0.1 /* ConnectingRodThickness */
160 static int CurEngine
= 0;
165 InitViewInfo(ViewInfo
*view
)
167 view
->Rotating
= GL_FALSE
;
168 view
->Translating
= GL_FALSE
;
169 view
->StartX
= view
->StartY
= 0;
170 view
->Distance
= 12.0;
171 view
->StartDistance
= 0.0;
172 view
->CurQuat
[0] = -0.194143;
173 view
->CurQuat
[1] = 0.507848;
174 view
->CurQuat
[2] = 0.115245;
175 view
->CurQuat
[3] = 0.831335;
180 InitRenderInfo(RenderInfo
*render
)
183 render
->Anim
= GL_TRUE
;
184 render
->Wireframe
= GL_FALSE
;
185 render
->Blend
= GL_FALSE
;
186 render
->Antialias
= GL_FALSE
;
187 render
->Texture
= GL_FALSE
;
188 render
->DrawBox
= GL_FALSE
;
189 render
->ShowInfo
= GL_TRUE
;
190 render
->ShowBlock
= GL_FALSE
;
191 render
->UseLists
= GL_FALSE
;
196 * Set GL for given rendering mode.
199 SetRenderState(RenderMode mode
)
201 static const GLfloat gray2
[4] = { 0.2, 0.2, 0.2, 1.0 };
202 static const GLfloat gray4
[4] = { 0.4, 0.4, 0.4, 1.0 };
205 glDisable(GL_LIGHTING
);
206 glDisable(GL_TEXTURE_2D
);
208 glDisable(GL_LINE_SMOOTH
);
209 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
210 glDisable(GL_TEXTURE_GEN_S
);
211 glDisable(GL_TEXTURE_GEN_T
);
212 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, gray2
);
216 glEnable(GL_LIGHTING
);
219 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
220 glEnable(GL_LINE_SMOOTH
);
225 glEnable(GL_LIGHTING
);
226 glEnable(GL_TEXTURE_2D
);
227 glEnable(GL_TEXTURE_GEN_S
);
228 glEnable(GL_TEXTURE_GEN_T
);
229 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, gray4
);
238 * Animate the engine parts.
243 /* convert degrees per millisecond to RPM: */
244 const float m
= 360.0 / 1000.0 / 60.0;
245 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
246 Theta
= ((int) (t
* RPM
* m
)) % 360;
252 * Compute piston's position along its stroke.
255 PistonStrokePosition(float throwDist
, float crankAngle
, float connRodLength
)
257 float x
= throwDist
* cos(DEG_TO_RAD(crankAngle
));
258 float y
= throwDist
* sin(DEG_TO_RAD(crankAngle
));
259 float pos
= y
+ sqrt(connRodLength
* connRodLength
- x
* x
);
265 * Compute position of nth piston along the crankshaft.
268 PistonShaftPosition(const Engine
*eng
, int piston
)
270 const int i
= piston
/ (eng
->Pistons
/ eng
->Cranks
);
272 assert(piston
< eng
->Pistons
);
273 z
= 1.5 * eng
->CrankJournalLength
+ eng
->CrankPlateThickness
274 + i
* (2.0 * (eng
->CrankJournalLength
+ eng
->CrankPlateThickness
));
275 if (eng
->Pistons
> eng
->Cranks
) {
277 z
+= eng
->ConnectingRodThickness
;
279 z
-= eng
->ConnectingRodThickness
;
286 * Compute distance between two adjacent pistons
289 PistonSpacing(const Engine
*eng
)
291 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
292 const float z0
= PistonShaftPosition(eng
, 0);
293 const float z1
= PistonShaftPosition(eng
, pistonsPerCrank
);
299 * (x0, y0) = position of big end on crankshaft
300 * (x1, y1) = position of small end on piston
303 ComputeConnectingRodPosition(float throwDist
, float crankAngle
,
305 float *x0
, float *y0
, float *x1
, float *y1
)
307 *x0
= throwDist
* cos(DEG_TO_RAD(crankAngle
));
308 *y0
= throwDist
* sin(DEG_TO_RAD(crankAngle
));
310 *y1
= PistonStrokePosition(throwDist
, crankAngle
, connRodLength
);
315 * Compute total length of the crankshaft.
318 CrankshaftLength(const Engine
*eng
)
320 float len
= (eng
->Cranks
* 2 + 1) * eng
->CrankJournalLength
321 + 2 * eng
->Cranks
* eng
->CrankPlateThickness
;
328 * Axis of piston = Z axis. Wrist pin is centered on (0, 0, 0).
331 DrawPiston(const Engine
*eng
)
333 const int slices
= 30, stacks
= 4, loops
= 4;
334 const float innerRadius
= 0.9 * eng
->PistonRadius
;
335 const float innerHeight
= eng
->PistonHeight
- 0.15;
336 const float wristPinLength
= 1.8 * eng
->PistonRadius
;
341 glTranslatef(0, 0, -1.1 * eng
->WristPinRadius
);
343 gluQuadricOrientation(Q
, GLU_INSIDE
);
346 gluDisk(Q
, innerRadius
, eng
->PistonRadius
, slices
, 1/*loops*/);
349 gluCylinder(Q
, innerRadius
, innerRadius
, innerHeight
, slices
, stacks
);
353 glTranslatef(0, 0, innerHeight
);
354 gluDisk(Q
, 0, innerRadius
, slices
, loops
);
357 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
360 gluCylinder(Q
, eng
->PistonRadius
, eng
->PistonRadius
, eng
->PistonHeight
,
364 glTranslatef(0, 0, eng
->PistonHeight
);
365 gluDisk(Q
, 0, eng
->PistonRadius
, slices
, loops
);
371 glTranslatef(0, 0.5 * wristPinLength
, 0.0);
372 glRotatef(90, 1, 0, 0);
373 gluCylinder(Q
, eng
->WristPinRadius
, eng
->WristPinRadius
, wristPinLength
,
380 * Draw piston at particular position.
383 DrawPositionedPiston(const Engine
*eng
, float crankAngle
)
385 const float pos
= PistonStrokePosition(eng
->Throw
, crankAngle
,
386 eng
->ConnectingRodLength
);
388 glRotatef(-90, 1, 0, 0);
389 glTranslatef(0, 0, pos
);
396 * Draw connector plate. Used for crankshaft and connecting rods.
399 DrawConnector(float length
, float thickness
,
400 float bigEndRadius
, float smallEndRadius
)
402 const float bigRadius
= 1.2 * bigEndRadius
;
403 const float smallRadius
= 1.2 * smallEndRadius
;
404 const float z0
= -0.5 * thickness
, z1
= -z0
;
405 GLfloat points
[36][2], normals
[36][2];
408 /* compute vertex locations, normals */
409 for (i
= 0; i
< 36; i
++) {
410 const int angle
= i
* 10;
411 float x
= cos(DEG_TO_RAD(angle
));
412 float y
= sin(DEG_TO_RAD(angle
));
415 if (angle
>= 0 && angle
<= 180) {
417 y
= y
* smallRadius
+ length
;
430 for (i
= 0; i
< 36; i
++) {
431 glVertex3f(points
[i
][0], points
[i
][1], z1
);
436 glNormal3f(0, 0, -1);
438 for (i
= 0; i
< 36; i
++) {
439 glVertex3f(points
[35-i
][0], points
[35-i
][1], z0
);
444 glBegin(GL_QUAD_STRIP
);
445 for (i
= 0; i
<= 36; i
++) {
446 const int j
= i
% 36;
447 glNormal3f(normals
[j
][0], normals
[j
][1], 0);
448 glVertex3f(points
[j
][0], points
[j
][1], z1
);
449 glVertex3f(points
[j
][0], points
[j
][1], z0
);
456 * Draw a crankshaft. Shaft lies along +Z axis, starting at zero.
459 DrawCrankshaft(const Engine
*eng
)
461 const int slices
= 20, stacks
= 2;
462 const int n
= eng
->Cranks
* 4 + 1;
463 const float phiStep
= 360 / eng
->Cranks
;
468 for (i
= 0; i
< n
; i
++) {
470 glTranslatef(0, 0, z
);
472 /* draw a crank plate */
473 glRotatef(phi
, 0, 0, 1);
474 glTranslatef(0, 0, 0.5 * eng
->CrankPlateThickness
);
475 DrawConnector(eng
->Throw
, eng
->CrankPlateThickness
,
476 eng
->CrankJournalRadius
, eng
->CrankPinRadius
);
481 else if (i
% 4 == 0) {
482 /* draw crank journal segment */
483 gluCylinder(Q
, eng
->CrankJournalRadius
, eng
->CrankJournalRadius
,
484 eng
->CrankJournalLength
, slices
, stacks
);
485 z
+= eng
->CrankJournalLength
;
487 else if (i
% 4 == 2) {
488 /* draw crank pin segment */
489 glRotatef(phi
, 0, 0, 1);
490 glTranslatef(0, eng
->Throw
, 0);
491 gluCylinder(Q
, eng
->CrankPinRadius
, eng
->CrankPinRadius
,
492 eng
->CrankJournalLength
, slices
, stacks
);
493 z
+= eng
->CrankJournalLength
;
501 * Draw crankshaft at a particular rotation.
502 * \param crankAngle current crankshaft rotation, in radians
505 DrawPositionedCrankshaft(const Engine
*eng
, float crankAngle
)
508 glRotatef(crankAngle
, 0, 0, 1);
510 glCallList(eng
->CrankList
);
518 * Draw a connecting rod at particular position.
519 * \param eng description of connecting rod to draw
520 * \param crankAngle current crankshaft rotation, in radians
523 DrawPositionedConnectingRod(const Engine
*eng
, float crankAngle
)
525 float x0
, y0
, x1
, y1
;
528 ComputeConnectingRodPosition(eng
->Throw
, crankAngle
,
529 eng
->ConnectingRodLength
,
531 d
= sqrt(eng
->ConnectingRodLength
* eng
->ConnectingRodLength
- x0
* x0
);
532 phi
= atan(x0
/ d
) * 180.0 / M_PI
;
535 glTranslatef(x0
, y0
, 0);
536 glRotatef(phi
, 0, 0, 1);
537 if (eng
->ConnRodList
)
538 glCallList(eng
->ConnRodList
);
540 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
541 eng
->CrankPinRadius
, eng
->WristPinRadius
);
547 * Draw a square with a hole in middle.
550 SquareWithHole(float squareSize
, float holeRadius
)
553 glBegin(GL_QUAD_STRIP
);
555 for (i
= 0; i
<= 360; i
+= 5) {
556 const float x1
= holeRadius
* cos(DEG_TO_RAD(i
));
557 const float y1
= holeRadius
* sin(DEG_TO_RAD(i
));
559 if (i
> 315 || i
<= 45) {
561 y2
= squareSize
* tan(DEG_TO_RAD(i
));
563 else if (i
> 45 && i
<= 135) {
564 x2
= -squareSize
* tan(DEG_TO_RAD(i
- 90));
567 else if (i
> 135 && i
<= 225) {
569 y2
= -squareSize
* tan(DEG_TO_RAD(i
-180));
571 else if (i
> 225 && i
<= 315) {
572 x2
= squareSize
* tan(DEG_TO_RAD(i
- 270));
575 glVertex2f(x1
, y1
); /* inner circle */
576 glVertex2f(x2
, y2
); /* outer square */
583 * Draw block with hole through middle.
584 * Hole is centered on Z axis.
585 * Bottom of block is at z=0, top of block is at z = blockHeight.
586 * index is in [0, count - 1] to determine which block faces are drawn.
589 DrawBlockWithHole(float blockSize
, float blockHeight
, float holeRadius
,
590 int index
, int count
)
592 const int slices
= 30, stacks
= 4;
593 const float x
= blockSize
;
594 const float y
= blockSize
;
596 const float z1
= blockHeight
;
598 assert(index
< count
);
600 gluQuadricOrientation(Q
, GLU_INSIDE
);
605 glVertex3f( x
, -y
, z0
);
606 glVertex3f( x
, y
, z0
);
607 glVertex3f( x
, y
, z1
);
608 glVertex3f( x
, -y
, z1
);
610 glNormal3f(-1, 0, 0);
611 glVertex3f(-x
, -y
, z1
);
612 glVertex3f(-x
, y
, z1
);
613 glVertex3f(-x
, y
, z0
);
614 glVertex3f(-x
, -y
, z0
);
618 glVertex3f(-x
, y
, z1
);
619 glVertex3f( x
, y
, z1
);
620 glVertex3f( x
, y
, z0
);
621 glVertex3f(-x
, y
, z0
);
623 if (index
== count
- 1) {
625 glNormal3f(0, -1, 0);
626 glVertex3f(-x
, -y
, z0
);
627 glVertex3f( x
, -y
, z0
);
628 glVertex3f( x
, -y
, z1
);
629 glVertex3f(-x
, -y
, z1
);
633 /* cylinder / hole */
634 gluCylinder(Q
, holeRadius
, holeRadius
, blockHeight
, slices
, stacks
);
638 glRotatef(180, 1, 0, 0);
639 SquareWithHole(blockSize
, holeRadius
);
643 glTranslatef(0, 0, z1
);
644 SquareWithHole(blockSize
, holeRadius
);
646 gluQuadricOrientation(Q
, GLU_OUTSIDE
);
651 * Draw the engine block.
654 DrawEngineBlock(const Engine
*eng
)
656 const float blockHeight
= eng
->Throw
+ 1.5 * eng
->PistonHeight
;
657 const float cylRadius
= 1.01 * eng
->PistonRadius
;
658 const float blockSize
= 0.5 * PistonSpacing(eng
);
659 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
662 for (i
= 0; i
< eng
->Pistons
; i
++) {
663 const float z
= PistonShaftPosition(eng
, i
);
664 const int crank
= i
/ pistonsPerCrank
;
668 glTranslatef(0, 0, z
);
670 /* additional rotation for kth piston per crank */
671 k
= i
% pistonsPerCrank
;
672 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
675 glRotatef(-90, 1, 0, 0);
676 glTranslatef(0, 0, eng
->Throw
* 2);
677 DrawBlockWithHole(blockSize
, blockHeight
, cylRadius
,
685 * Generate display lists for engine parts.
688 GenerateDisplayLists(Engine
*eng
)
690 eng
->CrankList
= glGenLists(1);
691 glNewList(eng
->CrankList
, GL_COMPILE
);
695 eng
->ConnRodList
= glGenLists(1);
696 glNewList(eng
->ConnRodList
, GL_COMPILE
);
697 DrawConnector(eng
->ConnectingRodLength
, eng
->ConnectingRodThickness
,
698 eng
->CrankPinRadius
, eng
->WristPinRadius
);
701 eng
->PistonList
= glGenLists(1);
702 glNewList(eng
->PistonList
, GL_COMPILE
);
706 eng
->BlockList
= glGenLists(1);
707 glNewList(eng
->BlockList
, GL_COMPILE
);
708 DrawEngineBlock(eng
);
714 * Free engine display lists (render with immediate mode).
717 FreeDisplayLists(Engine
*eng
)
719 glDeleteLists(eng
->CrankList
, 1);
721 glDeleteLists(eng
->ConnRodList
, 1);
722 eng
->ConnRodList
= 0;
723 glDeleteLists(eng
->PistonList
, 1);
725 glDeleteLists(eng
->BlockList
, 1);
731 * Draw complete engine.
732 * \param eng description of engine to draw
733 * \param crankAngle current crankshaft angle, in radians
736 DrawEngine(const Engine
*eng
, float crankAngle
)
738 const float crankDelta
= 360.0 / eng
->Cranks
;
739 const float crankLen
= CrankshaftLength(eng
);
740 const int pistonsPerCrank
= eng
->Pistons
/ eng
->Cranks
;
744 glRotatef(eng
->V_Angle
* 0.5, 0, 0, 1);
745 glTranslatef(0, 0, -0.5 * crankLen
);
748 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, CrankshaftColor
);
749 glColor4fv(CrankshaftColor
);
750 DrawPositionedCrankshaft(eng
, crankAngle
);
752 for (i
= 0; i
< eng
->Pistons
; i
++) {
753 const float z
= PistonShaftPosition(eng
, i
);
754 const int crank
= i
/ pistonsPerCrank
;
755 float rot
= crankAngle
+ crank
* crankDelta
;
759 glTranslatef(0, 0, z
);
761 /* additional rotation for kth piston per crank */
762 k
= i
% pistonsPerCrank
;
763 glRotatef(k
* -eng
->V_Angle
, 0, 0, 1);
764 rot
+= k
* eng
->V_Angle
;
767 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, PistonColor
);
768 glColor4fv(PistonColor
);
769 DrawPositionedPiston(eng
, rot
);
772 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, ConnRodColor
);
773 glColor4fv(ConnRodColor
);
774 DrawPositionedConnectingRod(eng
, rot
);
778 if (Render
.ShowBlock
) {
779 const GLboolean blend
= glIsEnabled(GL_BLEND
);
781 glDepthMask(GL_FALSE
);
785 glEnable(GL_CULL_FACE
);
787 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, BlockColor
);
788 glColor4fv(BlockColor
);
790 glCallList(eng
->BlockList
);
792 DrawEngineBlock(eng
);
794 glDisable(GL_CULL_FACE
);
795 glDepthMask(GL_TRUE
);
808 const float xmin
= -3.0, xmax
= 3.0;
809 const float ymin
= -1.0, ymax
= 3.0;
810 const float zmin
= -4.0, zmax
= 4.0;
811 const float step
= 0.5;
812 const float d
= 0.01;
814 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
815 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
817 glDisable(GL_LIGHTING
);
818 glDisable(GL_TEXTURE_2D
);
824 for (x
= xmin
; x
<= xmax
; x
+= step
) {
825 glVertex3f(x
, ymin
, zmin
);
826 glVertex3f(x
, ymax
, zmin
);
830 for (y
= ymin
; y
<= ymax
; y
+= step
) {
831 glVertex3f(xmin
, y
, zmin
);
832 glVertex3f(xmax
, y
, zmin
);
838 for (x
= xmin
; x
<= xmax
; x
+= step
) {
839 glVertex3f(x
, ymin
, zmin
);
840 glVertex3f(x
, ymin
, zmax
);
844 for (z
= zmin
; z
<= zmax
; z
+= step
) {
845 glVertex3f(xmin
, ymin
, z
);
846 glVertex3f(xmax
, ymin
, z
);
852 for (y
= ymin
; y
<= ymax
; y
+= step
) {
853 glVertex3f(xmin
, y
, zmin
);
854 glVertex3f(xmin
, y
, zmax
);
858 for (z
= zmin
; z
<= zmax
; z
+= step
) {
859 glVertex3f(xmin
, ymin
, z
);
860 glVertex3f(xmin
, ymax
, z
);
864 glColor3f(0.4, 0.4, 0.6);
867 glVertex3f(xmin
-d
, ymin
, zmin
);
868 glVertex3f(xmin
-d
, ymax
, zmin
);
869 glVertex3f(xmin
-d
, ymax
, zmax
);
870 glVertex3f(xmin
-d
, ymin
, zmax
);
872 glVertex3f(xmin
, ymin
-d
, zmin
);
873 glVertex3f(xmax
, ymin
-d
, zmin
);
874 glVertex3f(xmax
, ymin
-d
, zmax
);
875 glVertex3f(xmin
, ymin
-d
, zmax
);
877 glVertex3f(xmin
, ymin
, zmin
-d
);
878 glVertex3f(xmax
, ymin
, zmin
-d
);
879 glVertex3f(xmax
, ymax
, zmin
-d
);
880 glVertex3f(xmin
, ymax
, zmin
-d
);
884 glEnable(GL_LIGHTING
);
886 glEnable(GL_TEXTURE_2D
);
891 PrintString(const char *s
)
894 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
903 static double t0
= -1.0;
904 static int frames
= 0;
905 double t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
914 else if (t
- t0
>= 1.0) {
915 fps
= (int) (frames
/ (t
- t0
) + 0.5);
930 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
934 glTranslatef(0.0, 0.0, -View
.Distance
);
935 build_rotmatrix(rot
, View
.CurQuat
);
936 glMultMatrixf(&rot
[0][0]);
939 glTranslatef(0, -0.75, 0);
942 DrawEngine(Engines
+ CurEngine
, Theta
);
948 if (Render
.ShowInfo
) {
949 GLboolean lit
= glIsEnabled(GL_LIGHTING
);
950 GLboolean tex
= glIsEnabled(GL_TEXTURE_2D
);
952 sprintf(s
, "%s %d FPS %s", Engines
[CurEngine
].Name
, fps
,
953 Render
.UseLists
? "Display Lists" : "Immediate mode");
954 glDisable(GL_LIGHTING
);
955 glDisable(GL_TEXTURE_2D
);
957 glWindowPos2iARB(10, 10);
960 glEnable(GL_LIGHTING
);
962 glEnable(GL_TEXTURE_2D
);
970 * Handle window resize.
973 Reshape(int width
, int height
)
975 float ar
= (float) width
/ height
;
977 glViewport(0, 0, width
, height
);
978 glMatrixMode(GL_PROJECTION
);
980 glFrustum(-ar
* s
, ar
* s
, -s
, s
, 2.0, 50.0);
981 glMatrixMode(GL_MODELVIEW
);
989 * Handle mouse button.
992 Mouse(int button
, int state
, int x
, int y
)
994 if (button
== GLUT_LEFT_BUTTON
) {
995 if (state
== GLUT_DOWN
) {
998 View
.Rotating
= GL_TRUE
;
1000 else if (state
== GLUT_UP
) {
1001 View
.Rotating
= GL_FALSE
;
1004 else if (button
== GLUT_MIDDLE_BUTTON
) {
1005 if (state
== GLUT_DOWN
) {
1008 View
.StartDistance
= View
.Distance
;
1009 View
.Translating
= GL_TRUE
;
1011 else if (state
== GLUT_UP
) {
1012 View
.Translating
= GL_FALSE
;
1019 * Handle mouse motion
1022 Motion(int x
, int y
)
1025 if (View
.Rotating
) {
1026 float x0
= (2.0 * View
.StartX
- WinWidth
) / WinWidth
;
1027 float y0
= (WinHeight
- 2.0 * View
.StartY
) / WinHeight
;
1028 float x1
= (2.0 * x
- WinWidth
) / WinWidth
;
1029 float y1
= (WinHeight
- 2.0 * y
) / WinHeight
;
1032 trackball(q
, x0
, y0
, x1
, y1
);
1035 for (i
= 0; i
< 1; i
++)
1036 add_quats(q
, View
.CurQuat
, View
.CurQuat
);
1038 glutPostRedisplay();
1040 else if (View
.Translating
) {
1041 float dz
= 0.01 * (y
- View
.StartY
);
1042 View
.Distance
= View
.StartDistance
+ dz
;
1043 glutPostRedisplay();
1055 Render
.Anim
= !Render
.Anim
;
1063 OptChangeEngine(void)
1065 CurEngine
= (CurEngine
+ 1) % NUM_ENGINES
;
1072 if (Render
.Mode
> TEXTURED
)
1074 SetRenderState(Render
.Mode
);
1078 OptDisplayLists(void)
1081 Render
.UseLists
= !Render
.UseLists
;
1082 if (Render
.UseLists
) {
1083 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1084 GenerateDisplayLists(Engines
+ i
);
1088 for (i
= 0; i
< NUM_ENGINES
; i
++) {
1089 FreeDisplayLists(Engines
+ i
);
1097 Render
.ShowBlock
= !Render
.ShowBlock
;
1103 Render
.ShowInfo
= !Render
.ShowInfo
;
1109 Render
.DrawBox
= !Render
.DrawBox
;
1126 * Define menu entries (w/ keyboard shortcuts)
1133 void (*Function
)(void);
1136 static const MenuInfo MenuItems
[] = {
1137 { "Animation", 'a', OptAnimation
},
1138 { "Change Engine", 'e', OptChangeEngine
},
1139 { "Rendering Style", 'm', OptRenderMode
},
1140 { "Display Lists", 'd', OptDisplayLists
},
1141 { "Show Block", 'b', OptShowBlock
},
1142 { "Show Info", 'i', OptShowInfo
},
1143 { "Show Box", 'x', OptShowBox
},
1144 { "Exit", 27, OptExit
},
1145 { NULL
, 'r', OptRotate
},
1151 * Handle menu selection.
1154 MenuHandler(int entry
)
1156 MenuItems
[entry
].Function();
1157 glutPostRedisplay();
1168 glutCreateMenu(MenuHandler
);
1169 for (i
= 0; MenuItems
[i
].Text
; i
++) {
1170 glutAddMenuEntry(MenuItems
[i
].Text
, i
);
1172 glutAttachMenu(GLUT_RIGHT_BUTTON
);
1177 * Handle keyboard event.
1180 Key(unsigned char key
, int x
, int y
)
1184 for (i
= 0; MenuItems
[i
].Key
; i
++) {
1185 if (MenuItems
[i
].Key
== key
) {
1186 MenuItems
[i
].Function();
1187 glutPostRedisplay();
1195 void LoadTexture(void)
1197 GLboolean convolve
= GL_FALSE
;
1199 glGenTextures(1, &TextureObj
);
1200 glBindTexture(GL_TEXTURE_2D
, TextureObj
);
1201 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1202 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1203 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1204 glTexGeni(GL_T
, GL_TEXTURE_GEN_MODE
, GL_SPHERE_MAP
);
1207 #define FILTER_SIZE 7
1208 /* use convolution to blur the texture to simulate a dull finish
1214 GLfloat filter
[FILTER_SIZE
][FILTER_SIZE
][4];
1216 for (h
= 0; h
< FILTER_SIZE
; h
++) {
1217 for (w
= 0; w
< FILTER_SIZE
; w
++) {
1218 const GLfloat k
= 1.0 / (FILTER_SIZE
* FILTER_SIZE
);
1219 filter
[h
][w
][0] = k
;
1220 filter
[h
][w
][1] = k
;
1221 filter
[h
][w
][2] = k
;
1222 filter
[h
][w
][3] = k
;
1226 glEnable(GL_CONVOLUTION_2D
);
1227 glConvolutionParameteri(GL_CONVOLUTION_2D
,
1228 GL_CONVOLUTION_BORDER_MODE
, GL_CONSTANT_BORDER
);
1229 glConvolutionFilter2D(GL_CONVOLUTION_2D
, GL_RGBA
,
1230 FILTER_SIZE
, FILTER_SIZE
,
1231 GL_RGBA
, GL_FLOAT
, filter
);
1233 img
= LoadRGBImage(TEXTURE_FILE
, &w
, &h
, &format
);
1235 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1239 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, w
, h
, 0,
1240 format
, GL_UNSIGNED_BYTE
, img
);
1244 if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
1245 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
1255 const GLfloat lightColor
[4] = { 0.7, 0.7, 0.7, 1.0 };
1256 const GLfloat specular
[4] = { 0.8, 0.8, 0.8, 1.0 };
1257 const GLfloat backColor
[4] = { 1, 1, 0, 0 };
1259 Q
= gluNewQuadric();
1260 gluQuadricNormals(Q
, GLU_SMOOTH
);
1264 glClearColor(0.3, 0.3, 0.3, 0.0);
1265 glEnable(GL_DEPTH_TEST
);
1266 glEnable(GL_LIGHTING
);
1267 glEnable(GL_LIGHT0
);
1268 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, lightColor
);
1269 glMaterialf(GL_FRONT
, GL_SHININESS
, 40);
1270 glMaterialfv(GL_FRONT
, GL_SPECULAR
, specular
);
1271 glEnable(GL_NORMALIZE
);
1273 glMaterialfv(GL_BACK
, GL_DIFFUSE
, backColor
);
1275 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE
, 1);
1277 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1279 InitViewInfo(&View
);
1280 InitRenderInfo(&Render
);
1285 main(int argc
, char *argv
[])
1287 glutInit(&argc
, argv
);
1288 glutInitWindowSize(WinWidth
, WinHeight
);
1289 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
1290 glutCreateWindow("OpenGL Engine Demo");
1291 glutReshapeFunc(Reshape
);
1292 glutMouseFunc(Mouse
);
1293 glutMotionFunc(Motion
);
1294 glutKeyboardFunc(Key
);
1295 glutDisplayFunc(Draw
);