added OpenGL debug/test program
[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
24 int main(int argc, char **argv)
25 {
26         glutInit(&argc, argv);
27         glutInitWindowSize(800, 600);
28         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
29         glutCreateWindow("OpenGL test");
30
31         glutDisplayFunc(display);
32         glutReshapeFunc(reshape);
33         glutKeyboardFunc(keydown);
34         glutMouseFunc(mouse);
35         glutMotionFunc(motion);
36
37         if(init() == -1) {
38                 return 1;
39         }
40         atexit(cleanup);
41
42         glutMainLoop();
43         return 0;
44 }
45
46 int init(void)
47 {
48         glEnable(GL_DEPTH_TEST);
49         glEnable(GL_CULL_FACE);
50         glEnable(GL_LIGHTING);
51         glEnable(GL_LIGHT0);
52
53         gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
54
55         init_bsp(&torus_bsp);
56         if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
57                 fprintf(stderr, "failed to construct torus BSP tree\n");
58                 return -1;
59         }
60
61         return 0;
62 }
63
64 void cleanup(void)
65 {
66         free(torus.varr);
67         free(torus.iarr);
68         destroy_bsp(&torus_bsp);
69 }
70
71 void display(void)
72 {
73         float vdir[3];
74         float mat[16];
75
76         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77
78         glMatrixMode(GL_MODELVIEW);
79         glLoadIdentity();
80         glTranslatef(0, 0, -cam_dist);
81         glRotatef(cam_phi, 1, 0, 0);
82         glRotatef(cam_theta, 0, 1, 0);
83
84         glGetFloatv(GL_MODELVIEW_MATRIX, mat);
85         vdir[0] = -mat[2];
86         vdir[1] = -mat[6];
87         vdir[2] = -mat[10];
88
89         //g3d_draw_indexed(torus.prim, torus.varr, torus.vcount, torus.iarr, torus.icount);
90         draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
91
92         glutSwapBuffers();
93 }
94
95 void reshape(int x, int y)
96 {
97         glViewport(0, 0, x, y);
98         glMatrixMode(GL_PROJECTION);
99         glLoadIdentity();
100         gluPerspective(50, (float)x / (float)y, 0.5, 500.0);
101 }
102
103 void keydown(unsigned char key, int x, int y)
104 {
105         switch(key) {
106         case 27:
107                 exit(0);
108         }
109 }
110
111 void mouse(int bn, int st, int x, int y)
112 {
113         prev_x = x;
114         prev_y = y;
115         bnstate[bn - GLUT_LEFT] = st == GLUT_DOWN ? 1 : 0;
116 }
117
118 void motion(int x, int y)
119 {
120         int dx = x - prev_x;
121         int dy = y - prev_y;
122         prev_x = x;
123         prev_y = y;
124
125         if(!dx && !dy) return;
126
127         if(bnstate[0]) {
128                 cam_theta += dx * 0.5;
129                 cam_phi += dy * 0.5;
130
131                 if(cam_phi < -90) cam_phi = -90;
132                 if(cam_phi > 90) cam_phi = 90;
133                 glutPostRedisplay();
134         }
135         if(bnstate[2]) {
136                 cam_dist += dy * 0.1;
137
138                 if(cam_dist < 0.0f) cam_dist = 0.0f;
139                 glutPostRedisplay();
140         }
141 }
142
143 /* dummy functions to allow linking with all the demo code */
144 void swap_buffers(void *buf)
145 {
146 }
147
148 unsigned int get_msec(void)
149 {
150         return glutGet(GLUT_ELAPSED_TIME);
151 }
152
153 void wait_vsync(void)
154 {
155 }
156
157 void demo_quit(void)
158 {
159 }