fixed warnings
[dosdemo] / tools / gltest / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/glut.h>
4 #include "bsptree.h"
5 #include "mesh.h"
6
7 int init(void);
8 void cleanup(void);
9 void display(void);
10 void reshape(int x, int y);
11 void keydown(unsigned char key, int x, int y);
12 void mouse(int bn, int st, int x, int y);
13 void motion(int x, int y);
14
15 static float cam_theta, cam_phi, cam_dist = 5;
16 static int prev_x, prev_y;
17 static int bnstate[8];
18
19 /* ----------------------------------- */
20 static struct g3d_mesh torus;
21 static struct bsptree torus_bsp;
22
23 static int rebuild_bsp;
24 int debug_max_clip_level = 0;
25 /* ----------------------------------- */
26
27 int main(int argc, char **argv)
28 {
29         glutInit(&argc, argv);
30         glutInitWindowSize(800, 600);
31         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
32         glutCreateWindow("OpenGL test");
33
34         glutDisplayFunc(display);
35         glutReshapeFunc(reshape);
36         glutKeyboardFunc(keydown);
37         glutMouseFunc(mouse);
38         glutMotionFunc(motion);
39
40         if(init() == -1) {
41                 return 1;
42         }
43         atexit(cleanup);
44
45         glutMainLoop();
46         return 0;
47 }
48
49 int init(void)
50 {
51         glEnable(GL_DEPTH_TEST);
52         glEnable(GL_LIGHTING);
53         glEnable(GL_LIGHT0);
54
55         gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
56
57         init_bsp(&torus_bsp);
58         if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
59                 fprintf(stderr, "failed to construct torus BSP tree\n");
60                 return -1;
61         }
62
63         return 0;
64 }
65
66 void cleanup(void)
67 {
68         free(torus.varr);
69         free(torus.iarr);
70         destroy_bsp(&torus_bsp);
71 }
72
73 static void draw_plane(struct bspnode *n)
74 {
75         int i;
76         float cx = 0, cy = 0, cz = 0;
77
78         for(i=0; i<n->vcount; i++) {
79                 cx += n->verts[i].x;
80                 cy += n->verts[i].y;
81                 cz += n->verts[i].z;
82         }
83         cx /= n->vcount;
84         cy /= n->vcount;
85         cz /= n->vcount;
86
87         glPushAttrib(GL_ENABLE_BIT);
88         glDisable(GL_CULL_FACE);
89         glDisable(GL_LIGHTING);
90         glEnable(GL_BLEND);
91         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
92
93         glPushMatrix();
94         glTranslatef(cx, cy, cz);
95         glScalef(10, 10, 10);
96         glTranslatef(-cx, -cy, -cz);
97
98         glBegin(GL_POLYGON);
99         glColor4f(0.2, 0.3, 1.0, 0.5);
100         for(i=0; i<n->vcount; i++) {
101                 glVertex3f(n->verts[i].x, n->verts[i].y, n->verts[i].z);
102         }
103         glEnd();
104
105         glBegin(GL_LINE_LOOP);
106         glColor4f(0.2, 0.3, 1.0, 0.85);
107         for(i=0; i<n->vcount; i++) {
108                 glVertex3f(n->verts[i].x, n->verts[i].y, n->verts[i].z);
109         }
110         glEnd();
111
112         glPopMatrix();
113         glPopAttrib();
114 }
115
116 void display(void)
117 {
118         float vdir[3];
119         float mat[16];
120
121         if(rebuild_bsp) {
122                 destroy_bsp(&torus_bsp);
123                 init_bsp(&torus_bsp);
124                 if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
125                         fprintf(stderr, "failed to construct torus BSP tree\n");
126                         abort();
127                 }
128                 rebuild_bsp = 0;
129         }
130
131         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132
133         glMatrixMode(GL_MODELVIEW);
134         glLoadIdentity();
135         glTranslatef(0, 0, -cam_dist);
136         glRotatef(cam_phi, 1, 0, 0);
137         glRotatef(cam_theta, 0, 1, 0);
138
139         glGetFloatv(GL_MODELVIEW_MATRIX, mat);
140         vdir[0] = -mat[2];
141         vdir[1] = -mat[6];
142         vdir[2] = -mat[10];
143
144         //g3d_draw_indexed(torus.prim, torus.varr, torus.vcount, torus.iarr, torus.icount);
145         draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
146
147         draw_plane(torus_bsp.root);
148
149         glutSwapBuffers();
150 }
151
152 void reshape(int x, int y)
153 {
154         glViewport(0, 0, x, y);
155         glMatrixMode(GL_PROJECTION);
156         glLoadIdentity();
157         gluPerspective(50, (float)x / (float)y, 0.5, 500.0);
158 }
159
160 void keydown(unsigned char key, int x, int y)
161 {
162         switch(key) {
163         case 27:
164                 exit(0);
165
166         case '=':
167                 debug_max_clip_level++;
168                 printf("max_clip_level: %d\n", debug_max_clip_level);
169                 rebuild_bsp = 1;
170                 glutPostRedisplay();
171                 break;
172
173         case '-':
174                 if(--debug_max_clip_level < 0) {
175                         debug_max_clip_level = 0;
176                 } else {
177                         printf("max_clip_level: %d\n", debug_max_clip_level);
178                         rebuild_bsp = 1;
179                         glutPostRedisplay();
180                 }
181                 break;
182         }
183 }
184
185 void mouse(int bn, int st, int x, int y)
186 {
187         prev_x = x;
188         prev_y = y;
189         bnstate[bn - GLUT_LEFT] = st == GLUT_DOWN ? 1 : 0;
190 }
191
192 void motion(int x, int y)
193 {
194         int dx = x - prev_x;
195         int dy = y - prev_y;
196         prev_x = x;
197         prev_y = y;
198
199         if(!dx && !dy) return;
200
201         if(bnstate[0]) {
202                 cam_theta += dx * 0.5;
203                 cam_phi += dy * 0.5;
204
205                 if(cam_phi < -90) cam_phi = -90;
206                 if(cam_phi > 90) cam_phi = 90;
207                 glutPostRedisplay();
208         }
209         if(bnstate[2]) {
210                 cam_dist += dy * 0.1;
211
212                 if(cam_dist < 0.0f) cam_dist = 0.0f;
213                 glutPostRedisplay();
214         }
215 }
216
217 /* dummy functions to allow linking with all the demo code */
218 void swap_buffers(void *buf)
219 {
220 }
221
222 unsigned int get_msec(void)
223 {
224         return glutGet(GLUT_ELAPSED_TIME);
225 }
226
227 void wait_vsync(void)
228 {
229 }
230
231 void demo_quit(void)
232 {
233 }