Up: OpenGL Wagon examples. Next: None.
1 #include2 #include 3 #include "wheel.h" 4 #include "wagon.h" 5 // Initializes data necessary for drawing a wagon. 6 void wagonInit() { 7 wheelInit(); 8 } 9 void drawAlignedRectangle(double x0, double x1, double y0, double y1, 10 double z0, double z1) { 11 glPushMatrix(); // Draw wagon bed. 12 glTranslatef((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2); 13 glScalef(x1 - x0, y1 - y0, z1 - z0); 14 glutSolidCube(1.0); 15 glPopMatrix(); 16 } 17 // Draws wagon with wheels' bottom on the x-axis, with front at 18 // origin (0,0) and rear at (1,0). The parameter, expressed in 19 // radians, indicates the rotation of the wheels. 20 void wagonDraw(double wheel_theta) { 21 int i; 22 double x, z; 23 glColor3f(1.0, 0.0, 0.0); // Draw red wagon bed. 24 drawAlignedRectangle(0.0, 1.0, 0.12, 0.13, -.24, 0.24); 25 drawAlignedRectangle(0.0, 1.0, 0.13, 0.25, -.24, -.23); 26 drawAlignedRectangle(0.0, 1.0, 0.13, 0.25, 0.23, 0.24); 27 drawAlignedRectangle(0.0, .01, 0.13, 0.25, -.23, 0.23); 28 glColor3f(0.7, 0.6, 0.0); // Draw brown wagon bench. 29 drawAlignedRectangle(0.13, 0.25, 0.27, 0.28, -.23, 0.23); // bench 30 drawAlignedRectangle(0.24, 0.25, 0.28, 0.45, -.23, 0.23); // back 31 glColor3f(0.0, 0.0, 0.0); // Switch to black. 32 glBegin(GL_LINES); 33 glVertex3f(0.15, 0.1, -.25); // Draw front and rear axles 34 glVertex3f(0.15, 0.1, 0.25); 35 glVertex3f(0.80, 0.1, -.25); 36 glVertex3f(0.80, 0.1, 0.25); 37 glVertex3f(0.20, 0.13, -.15); // Draw bench legs 38 glVertex3f(0.20, 0.27, -.15); 39 glVertex3f(0.20, 0.13, 0.15); 40 glVertex3f(0.20, 0.27, 0.15); 41 glEnd(); 42 for(i = 0; i < 4; i++) { // Draw the four wheels. 43 x = (i % 2 == 0 ? 0.15 : 0.80); // (front or rear?) 44 z = (i / 2 == 0 ? 0.25 : -.25); // (left or right?) 45 glPushMatrix(); 46 glTranslatef(x, 0.1, z); 47 glScalef(0.1, 0.1, 0.1); 48 glRotatef(wheel_theta * 180 / M_PI, 0.0, 0.0, 1.0); 49 wheelDraw(); 50 glPopMatrix(); 51 } 52 }
1 #include2 #include 3 #include 4 #include "wagon.h" 5 #define MOVEMENT_RATE 0.0002 6 // Initializes information for drawing within OpenGL. 7 void init() { 8 GLfloat sun_direction[] = { 0.0, 2.0, -1.0, 0.0 }; 9 GLfloat half_intensity[] = { 0.5, 0.5, 0.5, 1.0 }; 10 double sinth = sin(0.15); // we're 0.15 radians from horizontal 11 double costh = cos(0.15); 12 glClearColor(0.5, 1.0, 0.5, 0.0); // Set window color to green. 13 glMatrixMode(GL_PROJECTION); // Set projection parameters. 14 glLoadIdentity(); 15 // glOrtho(-1.5, 1.5, -0.5, 0.5, 1, 3); 16 glFrustum(-0.75, 0.75, -.25, 0.25, 1.0, 3.0); 17 gluLookAt(1.5, 2 * sinth + 0.25, -2 * costh, // (my location) 18 1.5, 0.25, 0, // (where I face) 19 0, costh, sinth); // (up direction) 20 glEnable(GL_DEPTH_TEST); // Draw only closest surfaces 21 glEnable(GL_LIGHTING); // Set up ambient light. 22 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, half_intensity); 23 glEnable(GL_LIGHT0); // Set up sunlight. 24 glLightfv(GL_LIGHT0, GL_POSITION, sun_direction); 25 glLightfv(GL_LIGHT0, GL_DIFFUSE, half_intensity); 26 glEnable(GL_COLOR_MATERIAL); // Configure glColor(). 27 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); 28 wagonInit(); 29 } 30 // Draw a wagon that has traveled across the window the given distance. 31 void drawWagonAt(double dist) { 32 glPushMatrix(); 33 glTranslatef(3.0 - dist, 0.0, 0.0); 34 wagonDraw(dist / 0.1); 35 glPopMatrix(); 36 } 37 // Draws the current image. 38 void draw() { 39 // Compute how far the youngest wagon on-screen has traveled. 40 double offs = fmod(MOVEMENT_RATE * glutGet(GLUT_ELAPSED_TIME), 1.5); 41 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear window. 42 drawWagonAt(offs - 0.3); // Draw the wagons. (Up to three 43 drawWagonAt(offs + 1.2); // may be on-screen at once.) 44 drawWagonAt(offs + 2.7); 45 glFlush(); // Process all OpenGL routines as quickly as possible. 46 } 47 // Arranges that the window will be redrawn roughly every 40 ms. 48 void idle() { 49 static int lastTime = 0; // time of last redraw 50 int time = glutGet(GLUT_ELAPSED_TIME); // current time 51 if(lastTime == 0 || time >= lastTime + 40) { 52 lastTime = time; 53 glutPostRedisplay(); 54 } 55 } 56 // When window becomes visible, we want the window to 57 // continuously repaint itself. 58 void visible(int vis) { 59 glutIdleFunc(vis == GLUT_VISIBLE ? idle : NULL); 60 } 61 int main(int argc, char **argv) { 62 glutInit(&argc, argv); 63 glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGB); 64 glutInitWindowPosition(50, 100); // Set up display window. 65 glutInitWindowSize(450, 150); 66 glutCreateWindow("3D Wagon Train"); 67 init(); 68 glutDisplayFunc(draw); 69 glutVisibilityFunc(visible); 70 glutMainLoop(); 71 return 0; 72 } 73 // To compile: gcc wagon3d_main.c wagon3d.c wheel.c -lglut -lGLU -lGL -lm