Computer Graphics OpenGL
--keyboard control three-dimensional car
Final Implementation effect:
Source:
#include "stdafx.h" #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> #include <gl/
glut.h> #include <math.h> static glfloat Xrot = 0.0f;
static Glfloat Yrot = 0.0f; void Display (void) {Glclear (Gl_color_buffer_bit |
Gl_depth_buffer_bit);
Glpushmatrix (); Glrotatef (Xrot, 1.0f, 0.0f, 0.0f); Rotate around the x-axis Glrotatef (Yrot, 0.0f, 1.0f, 0.0f);
Rotate around Y axis//lower half of Car glbegin (Gl_polygon);
GLCOLOR3F (0.1, 0.1, 0.8);
glvertex3f ( -40.0f,0.0f,15.0f);
glvertex3f (40.0f,0.0f,15.0f);
glcolor3f (1, 0.5, 0.8);
glvertex3f (40.0f,-15.0f,15.0f);
glvertex3f ( -40.0f,-15.0f,15.0f);
Glend ();
Glbegin (Gl_polygon);
GLCOLOR3F (0.3, 0.2, 0.5);
glvertex3f (40.0f,0.0f,-15.0f);
glvertex3f (40.0f,0.0f,15.0f);
glvertex3f (40.0f,-15.0f,15.0f);
glvertex3f (40.0f,-15.0f,-15.0f);
Glend ();
Glbegin (Gl_polygon);
GLCOLOR3F (0.3, 0.1, 0.3);
glvertex3f ( -40.0f,0.0f,-15.0f);
glvertex3f ( -40.0f,0.0f,15.0f);
glvertex3f ( -40.0f,-15.0f,15.0f); glvertex3f ( -40.0f,-15.0f,-15.0f);
Glend ();
Glbegin (Gl_polygon);
GLCOLOR3F (0.1, 0.1, 0.8);
glvertex3f ( -40.0f,0.0f,-15.0f);
glvertex3f (40.0f,0.0f,-15.0f);
glcolor3f (1, 0.5, 0.8);
glvertex3f (40.0f,-15.0f,-15.0f);
glvertex3f ( -40.0f,-15.0f,-15.0f);
Glend ();
Glbegin (Gl_polygon);
glcolor3f (0, 0, 1);
glvertex3f ( -40.0f,0.0f,15.0f);
glvertex3f ( -40.0f,0.0f,-15.0f);
glvertex3f (40.0f,0.0f,-15.0f);
glvertex3f (40.0f,0.0f,15.0f);
Glend ();
Glbegin (Gl_polygon);
GLCOLOR3F (0.8, 0.5, 0.2);
glvertex3f ( -40.0f,-15.0f,15.0f);
glvertex3f ( -40.0f,-15.0f,-15.0f);
glvertex3f (40.0f,-15.0f,-15.0f);
glvertex3f (40.0f,-15.0f,15.0f);
Glend ();
The upper half of the vehicle is Glbegin (Gl_polygon);
glcolor3f (0, 0, 1);
glvertex3f ( -20.0f,0.0f,15.0f);
glvertex3f ( -10.0f,10.0f,15.0f);
glvertex3f (20.0f,10.0f,15.0f);
glvertex3f (25.0f,0.0f,15.0f);
Glend ();
Glbegin (Gl_polygon);
glcolor3f (0, 0, 1);
glvertex3f ( -20.0f,0.0f,-15.0f);
glvertex3f ( -10.0f,10.0f,-15.0f);
glvertex3f (20.0f,10.0f,-15.0f); glvertex3f (25.0f,0.0f,-15.0f);
Glend ();
Glbegin (Gl_polygon);
glcolor3f (0, 1, 1);
glvertex3f ( -10.0f,10.0f,15.0f);
glvertex3f ( -10.0f,10.0f,-15.0f);
glvertex3f (20.0f,10.0f,-15.0f);
glvertex3f (20.0f,10.0f,15.0f);
Glend ();
Glbegin (Gl_polygon);
glcolor3f (0.5, 0.8, 0.8);
glvertex3f ( -10.0f,10.0f,15.0f);
glvertex3f ( -20.0f,0.0f,15.0f);
glvertex3f ( -20.0f,0.0f,-15.0f);
glvertex3f ( -10.0f,10.0f,-15.0f);
Glend ();
Glbegin (Gl_polygon);
glcolor3f (0, 0.5, 0.5);
glvertex3f (20.0f,10.0f,15.0f);
glvertex3f (20.0f,10.0f,-15.0f);
glvertex3f (25.0f,0.0f,-15.0f);
glvertex3f (25.0f,0.0f,15.0f);
Glend ();
Glbegin (Gl_polygon);
glcolor3f (0, 0, 1);
glvertex3f ( -30.0f,-15.0f,15.0f);
glvertex3f ( -30.0f,-15.0f,-15.0f);
glvertex3f (30.0f,-15.0f,-15.0f);
glvertex3f (30.0f,-15.0f,15.0f);
Glend ();
Wheel glcolor3f (1, 0, 0);
Gltranslated ( -20.0f,-15.0f,15.0f);
Glutsolidtorus (2,5,5,100);
Gltranslated (0.0f,0.0f,-30.0f);
Glutsolidtorus (2,5,5,100);
Gltranslated (45.0f,0.0f,0.0f); GlutsolidTorus (2,5,5,100);
Gltranslated (0.0f,0.0f,30.0f);
Glutsolidtorus (2,5,5,100);
Glpopmatrix (); Glutswapbuffers ();
Swap buffering}//define keyboard control function void Specialkeys (int key, int x, int y) {if (key = = glut_key_up) xrot-= 5.0f;
if (key = = Glut_key_down) Xrot + = 5.0f;
if (key = = Glut_key_left) Yrot-= 5.0f;
if (key = = glut_key_right) Yrot + = 5.0f; Glutpostredisplay ();
Refresh window} int main (int argc, char* argv[]) {glutinit (&ARGC, argv); Glutinitdisplaymode (glut_double | Glut_rgb |
Glut_depth);
Glutcreatewindow ("Car");
Glclearcolor (0.0f, 0.0f, 0.0f, 1.0f);
Glenable (gl_depth_test);
Glshademodel (Gl_smooth);
Glmatrixmode (gl_projection);
Glloadidentity ();
Glortho (-100, 100,-100, 100,-100, 100); Glutspecialfunc (Specialkeys);
Register function key callback function Glutdisplayfunc (Display);
Glutmainloop ();
return 0;
}