3ac82704f3278225525288e7c6ba7b3229d9c091
[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
51         glBegin(GL_QUADS);
52         glNormal3f(0, 1, 0);
53         glVertex3f(-5, -1.3, 5);
54         glVertex3f(5, -1.3, 5);
55         glVertex3f(5, -1.3, -5);
56         glVertex3f(-5, -1.3, -5);
57         glEnd();
58
59         glutSwapBuffers();
60 }
61
62 #define ZNEAR   0.5f
63 void reshape(int x, int y)
64 {
65         float vsz, aspect = (float)x / (float)y;
66         glViewport(0, 0, x, y);
67         glMatrixMode(GL_PROJECTION);
68         glLoadIdentity();
69         vsz = 0.4663f * ZNEAR;
70         glFrustum(-aspect * vsz, aspect * vsz, -vsz, vsz, 0.5, 500.0);
71 }
72
73 void keypress(unsigned char key, int x, int y)
74 {
75         if(key == 27 || key == 'q') {
76                 glutExit();
77         }
78 }
79
80 void mouse(int bn, int st, int x, int y)
81 {
82         int bidx = bn - GLUT_LEFT_BUTTON;
83         bnstate[bidx] = st == GLUT_DOWN;
84         mouse_x = x;
85         mouse_y = y;
86 }
87
88 void motion(int x, int y)
89 {
90         int dx = x - mouse_x;
91         int dy = y - mouse_y;
92         mouse_x = x;
93         mouse_y = y;
94
95         if(!(dx | dy)) return;
96
97         if(bnstate[0]) {
98                 cam_theta += dx * 0.5;
99                 cam_phi += dy * 0.5;
100                 if(cam_phi < -90) cam_phi = -90;
101                 if(cam_phi > 90) cam_phi = 90;
102                 glutPostRedisplay();
103         }
104         if(bnstate[2]) {
105                 cam_dist += dy * 0.1;
106                 if(cam_dist < 0) cam_dist = 0;
107                 glutPostRedisplay();
108         }
109 }