bfd6424cfc6b6ca99a1e0708e50a2a6c10ce01fd
[dosdemo] / tools / ropesim / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/glut.h>
4 #include "cmesh.h"
5 #include "cgmath/cgmath.h"
6
7 int init(void);
8 void cleanup(void);
9 void display(void);
10 void idle(void);
11 void reshape(int x, int y);
12 void keyb(unsigned char key, int x, int y);
13 void mouse(int bn, int st, int x, int y);
14 void motion(int x, int y);
15 void sball_motion(int x, int y, int z);
16 void sball_rotate(int rx, int ry, int rz);
17 void sball_button(int bn, int st);
18
19 float cam_theta, cam_phi, cam_dist = 10;
20 int prev_mx, prev_my;
21 int bnstate[8];
22
23 struct cmesh *scn;
24 struct cmesh *mesh_gout, *mesh_gin, *mesh_suz;
25 cgm_vec3 ganchor[4];
26
27 int main(int argc, char **argv)
28 {
29         glutInit(&argc, argv);
30         glutInitWindowSize(1280, 800);
31         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
32         glutCreateWindow("ropesim");
33
34         glutDisplayFunc(display);
35         glutIdleFunc(idle);
36         glutReshapeFunc(reshape);
37         glutKeyboardFunc(keyb);
38         glutMouseFunc(mouse);
39         glutMotionFunc(motion);
40         glutSpaceballMotionFunc(sball_motion);
41         glutSpaceballRotateFunc(sball_rotate);
42         glutSpaceballButtonFunc(sball_button);
43
44         if(init() == -1) {
45                 return 1;
46         }
47         atexit(cleanup);
48
49         glutMainLoop();
50         return 0;
51 }
52
53
54 int init(void)
55 {
56         static const char *meshnames[] = {"suzanne", "gimbal_outer", "gimbal_inner"};
57         static struct cmesh **meshes[] = {&mesh_suz, &mesh_gout, &mesh_gin};
58         static const float amb[] = {0.05, 0.05, 0.08, 1};
59         int i;
60
61         glEnable(GL_CULL_FACE);
62         glEnable(GL_DEPTH_TEST);
63         glEnable(GL_LIGHTING);
64         glEnable(GL_LIGHT0);
65         glEnable(GL_LIGHT1);
66         glEnable(GL_LIGHT2);
67
68         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
69
70         if(!(scn = cmesh_alloc()) || cmesh_load(scn, "gimbal.obj") == -1) {
71                 fprintf(stderr, "failed to load scene file\n");
72                 return -1;
73         }
74
75         for(i=0; i<sizeof meshes / sizeof *meshes; i++) {
76                 int idx;
77                 if((idx = cmesh_find_submesh(scn, meshnames[i])) == -1) {
78                         fprintf(stderr, "failed to locate required submesh (%s)\n", meshnames[i]);
79                         return -1;
80                 }
81                 if(!(*meshes[i] = cmesh_alloc()) || cmesh_clone_submesh(*meshes[i], scn, idx) == -1) {
82                         fprintf(stderr, "failed to clone submesh\n");
83                         return -1;
84                 }
85                 cmesh_remove_submesh(scn, idx);
86         }
87
88         return 0;
89 }
90
91 void cleanup(void)
92 {
93         cmesh_free(mesh_suz);
94         cmesh_free(mesh_gout);
95         cmesh_free(mesh_gin);
96         cmesh_free(scn);
97 }
98
99 void display(void)
100 {
101         static const float lpos[][4] = {
102                 {-100, 100, 200, 1},
103                 {100, 80, 50, 1},
104                 {20, -50, -150, 1}
105         };
106         static const float lcol[][4] = {
107                 {0.9, 0.9, 0.9, 1},
108                 {0.5, 0.3, 0.2, 1},
109                 {0.2, 0.3, 0.2, 1}
110         };
111         int i, count;
112
113         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
114
115         glMatrixMode(GL_MODELVIEW);
116         glLoadIdentity();
117         glTranslatef(0, 0, -cam_dist);
118         glRotatef(cam_phi, 1, 0, 0);
119         glRotatef(cam_theta, 0, 1, 0);
120
121         for(i=0; i<3; i++) {
122                 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
123                 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
124         }
125
126         count = cmesh_submesh_count(scn);
127         for(i=0; i<count; i++) {
128                 cmesh_draw_submesh(scn, i);
129         }
130         cmesh_draw(mesh_gout);
131         cmesh_draw(mesh_gin);
132         cmesh_draw(mesh_suz);
133
134         glutSwapBuffers();
135 }
136 void idle(void)
137 {
138         glutPostRedisplay();
139 }
140
141 void reshape(int x, int y)
142 {
143         glViewport(0, 0, x, y);
144         glMatrixMode(GL_PROJECTION);
145         glLoadIdentity();
146         gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
147 }
148
149 void keyb(unsigned char key, int x, int y)
150 {
151         switch(key) {
152         case 27:
153                 exit(0);
154         }
155 }
156
157 void mouse(int bn, int st, int x, int y)
158 {
159         prev_mx = x;
160         prev_my = y;
161         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
162 }
163
164 void motion(int x, int y)
165 {
166         int dx = x - prev_mx;
167         int dy = y - prev_my;
168         prev_mx = x;
169         prev_my = y;
170
171         if(!(dx | dy)) return;
172
173         if(bnstate[0]) {
174                 cam_theta += dx * 0.5;
175                 cam_phi += dy * 0.5;
176                 if(cam_phi < -90) cam_phi = -90;
177                 if(cam_phi > 90) cam_phi = 90;
178         }
179
180         if(bnstate[2]) {
181                 cam_dist += dy * 0.1;
182                 if(cam_dist < 0.0f) cam_dist = 0.0f;
183         }
184 }
185
186 void sball_motion(int x, int y, int z)
187 {
188 }
189
190 void sball_rotate(int rx, int ry, int rz)
191 {
192 }
193
194 void sball_button(int bn, int st)
195 {
196 }