windows glutFullScreen implementation
[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         static int fullscr;
105
106         switch(key) {
107         case 27:
108         case 'q':
109                 glutExit();
110                 break;
111
112         case ' ':
113                 anim ^= 1;
114                 glutIdleFunc(anim ? idle : 0);
115                 glutPostRedisplay();
116                 break;
117
118         case 'f':
119                 fullscr ^= 1;
120                 if(fullscr) {
121                         glutFullScreen();
122                 } else {
123                         glutPositionWindow(10, 10);
124                 }
125                 break;
126         }
127 }
128
129 void mouse(int bn, int st, int x, int y)
130 {
131         int bidx = bn - GLUT_LEFT_BUTTON;
132         bnstate[bidx] = st == GLUT_DOWN;
133         mouse_x = x;
134         mouse_y = y;
135 }
136
137 void motion(int x, int y)
138 {
139         int dx = x - mouse_x;
140         int dy = y - mouse_y;
141         mouse_x = x;
142         mouse_y = y;
143
144         if(!(dx | dy)) return;
145
146         if(bnstate[0]) {
147                 cam_theta += dx * 0.5;
148                 cam_phi += dy * 0.5;
149                 if(cam_phi < -90) cam_phi = -90;
150                 if(cam_phi > 90) cam_phi = 90;
151                 glutPostRedisplay();
152         }
153         if(bnstate[2]) {
154                 cam_dist += dy * 0.1;
155                 if(cam_dist < 0) cam_dist = 0;
156                 glutPostRedisplay();
157         }
158 }