Up: OpenGL Wagon examples. Next: Moving wagon example.

1 #ifndef _WAGON_H_ 2 #define _WAGON_H_ 3 void wagonInit(); 4 void wagonDraw(double wheel_theta); 5 #endif /* _WAGON_H_ */
1 #include <math.h>
2 #include <GL/glut.h>
3 #include "wheel.h"
4 #include "wagon.h"
5 // Initializes data necessary for drawing a wagon.
6 void wagonInit() {
7 wheelInit();
8 }
9 // Draws wagon with wheels' bottom on the x-axis, with front at
10 // origin (0,0) and rear at (1,0). The parameter, expressed in
11 // radians, indicates the rotation of the wheels.
12 void wagonDraw(double wheel_theta) {
13 glBegin(GL_POLYGON); // Draw wagon bed.
14 glVertex2f(1.0, 0.12);
15 glVertex2f(1.0, 0.3);
16 glVertex2f(0.0, 0.3);
17 glVertex2f(0.0, 0.12);
18 glEnd();
19 glBegin(GL_LINES);
20 glVertex2f(0.13, 0.35); // Draw bench.
21 glVertex2f(0.21, 0.35);
22 glVertex2f(0.20, 0.30); // Draw bench back.
23 glVertex2f(0.24, 0.50);
24 glEnd();
25 glPushMatrix(); // Draw front wheel.
26 glTranslatef(0.15, 0.1, 0);
27 glScalef(0.1, 0.1, 0.1);
28 glRotatef(wheel_theta * 180 / M_PI, 0.0, 0.0, 1.0);
29 wheelDraw();
30 glPopMatrix();
31 glPushMatrix(); // Draw rear wheel.
32 glTranslatef(0.8, 0.1, 0);
33 glScalef(0.1, 0.1, 0.1);
34 glRotatef(wheel_theta * 180 / M_PI, 0.0, 0.0, 1.0);
35 wheelDraw();
36 glPopMatrix();
37 }
1 #include <GL/glut.h>
2 #include "wagon.h"
3 // Initializes information for drawing within OpenGL.
4 void init() {
5 glClearColor(1.0, 1.0, 1.0, 0.0); // Set window color to white.
6 glMatrixMode(GL_PROJECTION); // Set projection parameters.
7 gluOrtho2D(-.25, 1.25, -.25, 0.75); // Set viewable coordinates.
8 wagonInit();
9 }
10 // Draws the image.
11 void draw() {
12 glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
13 glColor3f(0.0, 0.0, 0.0); // Set drawing color to black.
14 wagonDraw(0.0);
15 glFlush(); // Process all OpenGL routines as quickly as possible.
16 }
17 int main(int argc, char **argv) {
18 glutInit(&argc, argv);
19 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
20 glutInitWindowPosition(50, 100); // Set up display window.
21 glutInitWindowSize(300, 200);
22 glutCreateWindow("Still Wagon");
23 init();
24 glutDisplayFunc(draw);
25 glutMainLoop();
26 return 0;
27 }
28 // To compile: gcc wagon_main.c wagon.c wheel.c -lglut -lGLU -lGL -lm