780c018c7cf8645755c8338852c4ec50a57a5863
[miniglut] / test.c
1 #include "miniglut.h"
2
3 void idle(void);
4 void display(void);
5 void reshape(int x, int y);
6 void keypress(unsigned char key, int x, int y);
7 void mouse(int bn, int st, int x, int y);
8 void motion(int x, int y);
9
10 float cam_theta, cam_phi = 25, cam_dist = 8;
11 int mouse_x, mouse_y;
12 int bnstate[8];
13 int anim;
14
15 int main(int argc, char **argv)
16 {
17         glutInit(&argc, argv);
18         glutInitWindowSize(1024, 768);
19         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
20         glutCreateWindow("miniglut test");
21
22         glutDisplayFunc(display);
23         glutReshapeFunc(reshape);
24         glutKeyboardFunc(keypress);
25         glutMouseFunc(mouse);
26         glutMotionFunc(motion);
27
28         glEnable(GL_DEPTH_TEST);
29         glEnable(GL_CULL_FACE);
30         glEnable(GL_LIGHTING);
31         glEnable(GL_LIGHT0);
32
33         glutMainLoop();
34         return 0;
35 }
36
37 void idle(void)
38 {
39         glutPostRedisplay();
40 }
41
42 void display(void)
43 {
44         long tm;
45         float lpos[] = {-1, 2, 3, 0};
46
47         tm = glutGet(GLUT_ELAPSED_TIME);
48
49         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
50
51         glMatrixMode(GL_MODELVIEW);
52         glLoadIdentity();
53         glTranslatef(0, 0, -cam_dist);
54         glRotatef(cam_phi, 1, 0, 0);
55         glRotatef(cam_theta, 0, 1, 0);
56
57         glLightfv(GL_LIGHT0, GL_POSITION, lpos);
58
59         glPushMatrix();
60         if(anim) {
61                 glRotatef(tm / 10.0f, 1, 0, 0);
62                 glRotatef(tm / 10.0f, 0, 1, 0);
63         }
64         glutSolidTorus(0.3, 1, 16, 24);
65         glPopMatrix();
66
67         glutSolidSphere(0.4, 16, 8);
68
69         glPushMatrix();
70         glTranslatef(-2.5, 0, 0);
71         glutSolidCube(1.5);
72         glPopMatrix();
73
74         glPushMatrix();
75         glTranslatef(2.5, -1, 0);
76         glRotatef(-90, 1, 0, 0);
77         glutSolidCone(1.1, 2, 16, 2);
78         glPopMatrix();
79
80         glBegin(GL_QUADS);
81         glNormal3f(0, 1, 0);
82         glVertex3f(-5, -1.3, 5);
83         glVertex3f(5, -1.3, 5);
84         glVertex3f(5, -1.3, -5);
85         glVertex3f(-5, -1.3, -5);
86         glEnd();
87
88         glutSwapBuffers();
89 }
90
91 #define ZNEAR   0.5f
92 void reshape(int x, int y)
93 {
94         float vsz, aspect = (float)x / (float)y;
95         glViewport(0, 0, x, y);
96         glMatrixMode(GL_PROJECTION);
97         glLoadIdentity();
98         vsz = 0.4663f * ZNEAR;
99         glFrustum(-aspect * vsz, aspect * vsz, -vsz, vsz, 0.5, 500.0);
100 }
101
102 void keypress(unsigned char key, int x, int y)
103 {
104         switch(key) {
105         case 27:
106         case 'q':
107                 glutExit();
108                 break;
109
110         case ' ':
111                 anim ^= 1;
112                 glutIdleFunc(anim ? idle : 0);
113                 glutPostRedisplay();
114                 break;
115         }
116 }
117
118 void mouse(int bn, int st, int x, int y)
119 {
120         int bidx = bn - GLUT_LEFT_BUTTON;
121         bnstate[bidx] = st == GLUT_DOWN;
122         mouse_x = x;
123         mouse_y = y;
124 }
125
126 void motion(int x, int y)
127 {
128         int dx = x - mouse_x;
129         int dy = y - mouse_y;
130         mouse_x = x;
131         mouse_y = y;
132
133         if(!(dx | dy)) return;
134
135         if(bnstate[0]) {
136                 cam_theta += dx * 0.5;
137                 cam_phi += dy * 0.5;
138                 if(cam_phi < -90) cam_phi = -90;
139                 if(cam_phi > 90) cam_phi = 90;
140                 glutPostRedisplay();
141         }
142         if(bnstate[2]) {
143                 cam_dist += dy * 0.1;
144                 if(cam_dist < 0) cam_dist = 0;
145                 glutPostRedisplay();
146         }
147 }