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