changed gimbal motion from middle mouse button to modkeys + left
[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 int modkeys;
23
24 long start_msec;
25
26 struct cmesh *scn;
27 struct cmesh *mesh_gout, *mesh_gin, *mesh_suz;
28
29 cgm_vec3 gmove;
30 /*cgm_quat grot = {0, 0, 0, 1};*/
31 float grot_theta, grot_phi;
32 float ginner_xform[16], gouter_xform[16];
33 cgm_vec3 ganchor[4];
34
35 cgm_vec3 dbgvec[4];
36
37 int main(int argc, char **argv)
38 {
39         glutInit(&argc, argv);
40         glutInitWindowSize(1280, 800);
41         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
42         glutCreateWindow("ropesim");
43
44         glutDisplayFunc(display);
45         glutIdleFunc(idle);
46         glutReshapeFunc(reshape);
47         glutKeyboardFunc(keyb);
48         glutMouseFunc(mouse);
49         glutMotionFunc(motion);
50         glutSpaceballMotionFunc(sball_motion);
51         glutSpaceballRotateFunc(sball_rotate);
52         glutSpaceballButtonFunc(sball_button);
53
54         if(init() == -1) {
55                 return 1;
56         }
57         atexit(cleanup);
58
59         start_msec = glutGet(GLUT_ELAPSED_TIME);
60         glutMainLoop();
61         return 0;
62 }
63
64
65 int init(void)
66 {
67         static const char *meshnames[] = {"suzanne", "gimbal_outer", "gimbal_inner"};
68         static struct cmesh **meshes[] = {&mesh_suz, &mesh_gout, &mesh_gin};
69         static const float amb[] = {0.05, 0.05, 0.08, 1};
70         int i;
71
72         glEnable(GL_CULL_FACE);
73         glEnable(GL_DEPTH_TEST);
74         glEnable(GL_LIGHTING);
75         glEnable(GL_LIGHT0);
76         glEnable(GL_LIGHT1);
77         glEnable(GL_LIGHT2);
78
79         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
80
81         if(!(scn = cmesh_alloc()) || cmesh_load(scn, "gimbal.obj") == -1) {
82                 fprintf(stderr, "failed to load scene file\n");
83                 return -1;
84         }
85
86         for(i=0; i<sizeof meshes / sizeof *meshes; i++) {
87                 int idx;
88                 if((idx = cmesh_find_submesh(scn, meshnames[i])) == -1) {
89                         fprintf(stderr, "failed to locate required submesh (%s)\n", meshnames[i]);
90                         return -1;
91                 }
92                 if(!(*meshes[i] = cmesh_alloc()) || cmesh_clone_submesh(*meshes[i], scn, idx) == -1) {
93                         fprintf(stderr, "failed to clone submesh\n");
94                         return -1;
95                 }
96                 cmesh_remove_submesh(scn, idx);
97         }
98
99         /* anchor points on the inner gimbal */
100         for(i=0; i<4; i++) {
101                 ganchor[i].x = (float)(((i & 1) << 1) - 1) * 1.5f;
102                 ganchor[i].y = (float)((i & 2) - 1) * 1.5f;
103                 ganchor[i].z = 0;
104         }
105
106         return 0;
107 }
108
109 void cleanup(void)
110 {
111         cmesh_free(mesh_suz);
112         cmesh_free(mesh_gout);
113         cmesh_free(mesh_gin);
114         cmesh_free(scn);
115 }
116
117 void update(long tmsec, float dt)
118 {
119         int i;
120         cgm_vec3 apt0, apt1;
121         float theta, phi, brot;
122
123         /*
124         cgm_mrotation_quat(ginner_xform, &grot);
125         cgm_mtranslate(ginner_xform, gmove.x, gmove.y, gmove.z);
126         */
127
128         theta = cgm_deg_to_rad(grot_theta);
129         phi = cgm_deg_to_rad(grot_phi);
130
131         cgm_mrotation_euler(ginner_xform, phi, theta, 0, CGM_EULER_XYZ);
132         cgm_mrotation_euler(gouter_xform, phi, 0, 0, CGM_EULER_XYZ);
133
134         for(i=0; i<4; i++) {
135                 apt0 = ganchor[i];
136                 cgm_vmul_m4v3(&apt0, ginner_xform);
137
138                 dbgvec[i] = apt0;
139         }
140 }
141
142 void display(void)
143 {
144         static const float lpos[][4] = {
145                 {-100, 100, 200, 1},
146                 {100, 80, 50, 1},
147                 {20, -50, -150, 1}
148         };
149         static const float lcol[][4] = {
150                 {0.9, 0.9, 0.9, 1},
151                 {0.5, 0.3, 0.2, 1},
152                 {0.2, 0.3, 0.2, 1}
153         };
154         int i, count;
155         long tmsec = glutGet(GLUT_ELAPSED_TIME) - start_msec;
156         static long prev_tmsec;
157
158         update(tmsec, (float)(tmsec - prev_tmsec) / 1000.0f);
159         prev_tmsec = tmsec;
160
161         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
162
163         glMatrixMode(GL_MODELVIEW);
164         glLoadIdentity();
165         glTranslatef(0, 0, -cam_dist);
166         glRotatef(cam_phi, 1, 0, 0);
167         glRotatef(cam_theta, 0, 1, 0);
168
169         for(i=0; i<3; i++) {
170                 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
171                 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
172         }
173
174         count = cmesh_submesh_count(scn);
175         for(i=0; i<count; i++) {
176                 cmesh_draw_submesh(scn, i);
177         }
178
179         glPushMatrix();
180         glMultMatrixf(gouter_xform);
181         cmesh_draw(mesh_gout);
182         glPopMatrix();
183
184         glPushMatrix();
185         glMultMatrixf(ginner_xform);
186         cmesh_draw(mesh_gin);
187         glPopMatrix();
188
189         cmesh_draw(mesh_suz);
190
191         glPointSize(7);
192         glBegin(GL_POINTS);
193         for(i=0; i<4; i++) {
194                 glVertex3f(dbgvec[i].x, dbgvec[i].y, dbgvec[i].z);
195         }
196         glEnd();
197
198         glutSwapBuffers();
199 }
200 void idle(void)
201 {
202         glutPostRedisplay();
203 }
204
205 void reshape(int x, int y)
206 {
207         glViewport(0, 0, x, y);
208         glMatrixMode(GL_PROJECTION);
209         glLoadIdentity();
210         gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
211 }
212
213 void keyb(unsigned char key, int x, int y)
214 {
215         switch(key) {
216         case 27:
217                 exit(0);
218         }
219 }
220
221 void mouse(int bn, int st, int x, int y)
222 {
223         prev_mx = x;
224         prev_my = y;
225         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
226         modkeys = glutGetModifiers();
227 }
228
229 void motion(int x, int y)
230 {
231         int dx = x - prev_mx;
232         int dy = y - prev_my;
233         prev_mx = x;
234         prev_my = y;
235
236         if(!(dx | dy)) return;
237
238         if(modkeys) {
239                 if(bnstate[0]) {
240                         grot_theta += dx * 0.5;
241                         grot_phi += dy * 0.5;
242                 }
243         } else {
244                 if(bnstate[0]) {
245                         cam_theta += dx * 0.5;
246                         cam_phi += dy * 0.5;
247                         if(cam_phi < -90) cam_phi = -90;
248                         if(cam_phi > 90) cam_phi = 90;
249                 }
250
251                 if(bnstate[2]) {
252                         cam_dist += dy * 0.1;
253                         if(cam_dist < 0.0f) cam_dist = 0.0f;
254                 }
255         }
256 }
257
258 void sball_motion(int x, int y, int z)
259 {
260         gmove.x += x * 0.001f;
261         gmove.y += y * 0.001f;
262         gmove.z -= z * 0.001f;
263 }
264
265 void sball_rotate(int x, int y, int z)
266 {
267         /*
268         float axis_len, s;
269         axis_len = (float)sqrt(x * x + y * y + z * z);
270         s = axis_len == 0.0f ? 1.0f : 1.0f / axis_len;
271         cgm_qrotate(&grot, axis_len * 0.001f, -x * s, -y * s, z * s);
272         */
273
274         grot_theta += y * 0.03f;
275         grot_phi += x * 0.03f;
276 }
277
278 void sball_button(int bn, int st)
279 {
280         if(st == GLUT_DOWN) {
281                 /*cgm_qcons(&grot, 0, 0, 0, 1);*/
282                 grot_theta = grot_phi = 0.0f;
283                 cgm_vcons(&gmove, 0, 0, 0);
284         }
285 }