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);
15 static float cam_theta, cam_phi, cam_dist = 5;
16 static int prev_x, prev_y;
17 static int bnstate[8];
19 /* ----------------------------------- */
20 static struct g3d_mesh torus;
21 static struct bsptree torus_bsp;
23 static int rebuild_bsp;
24 int debug_max_clip_level = 0;
25 /* ----------------------------------- */
27 int main(int argc, char **argv)
29 glutInit(&argc, argv);
30 glutInitWindowSize(800, 600);
31 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
32 glutCreateWindow("OpenGL test");
34 glutDisplayFunc(display);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keydown);
38 glutMotionFunc(motion);
51 glEnable(GL_DEPTH_TEST);
52 glEnable(GL_LIGHTING);
55 gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
58 if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
59 fprintf(stderr, "failed to construct torus BSP tree\n");
70 destroy_bsp(&torus_bsp);
73 static void draw_plane(struct bspnode *n)
76 float cx = 0, cy = 0, cz = 0;
78 for(i=0; i<n->vcount; i++) {
87 glPushAttrib(GL_ENABLE_BIT);
88 glDisable(GL_CULL_FACE);
89 glDisable(GL_LIGHTING);
91 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
94 glTranslatef(cx, cy, cz);
96 glTranslatef(-cx, -cy, -cz);
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);
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);
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");
131 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133 glMatrixMode(GL_MODELVIEW);
135 glTranslatef(0, 0, -cam_dist);
136 glRotatef(cam_phi, 1, 0, 0);
137 glRotatef(cam_theta, 0, 1, 0);
139 glGetFloatv(GL_MODELVIEW_MATRIX, mat);
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]);
147 draw_plane(torus_bsp.root);
152 void reshape(int x, int y)
154 glViewport(0, 0, x, y);
155 glMatrixMode(GL_PROJECTION);
157 gluPerspective(50, (float)x / (float)y, 0.5, 500.0);
160 void keydown(unsigned char key, int x, int y)
167 debug_max_clip_level++;
168 printf("max_clip_level: %d\n", debug_max_clip_level);
174 if(--debug_max_clip_level < 0) {
175 debug_max_clip_level = 0;
177 printf("max_clip_level: %d\n", debug_max_clip_level);
185 void mouse(int bn, int st, int x, int y)
189 bnstate[bn - GLUT_LEFT] = st == GLUT_DOWN ? 1 : 0;
192 void motion(int x, int y)
199 if(!dx && !dy) return;
202 cam_theta += dx * 0.5;
205 if(cam_phi < -90) cam_phi = -90;
206 if(cam_phi > 90) cam_phi = 90;
210 cam_dist += dy * 0.1;
212 if(cam_dist < 0.0f) cam_dist = 0.0f;
217 /* dummy functions to allow linking with all the demo code */
218 void swap_buffers(void *buf)
222 unsigned int get_msec(void)
224 return glutGet(GLUT_ELAPSED_TIME);
227 void wait_vsync(void)