f300a570122aae67a42303d11a61e43beecfd4d2
[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 #include "ropesim.h"
7
8 int init(void);
9 void cleanup(void);
10 void display(void);
11 void idle(void);
12 void reshape(int x, int y);
13 void keyb(unsigned char key, int x, int y);
14 void mouse(int bn, int st, int x, int y);
15 void motion(int x, int y);
16 void sball_motion(int x, int y, int z);
17 void sball_rotate(int rx, int ry, int rz);
18 void sball_button(int bn, int st);
19
20 float cam_theta, cam_phi, cam_dist = 10;
21 int prev_mx, prev_my;
22 int bnstate[8];
23 int modkeys;
24
25 long start_msec;
26
27 struct cmesh *scn;
28 struct cmesh *mesh_gout, *mesh_gin, *mesh_suz;
29
30 cgm_vec3 gmove;
31 /*cgm_quat grot = {0, 0, 0, 1};*/
32 float grot_theta, grot_phi;
33 float ginner_xform[16], gouter_xform[16];
34 cgm_vec3 ganchor[4], manchor[4];
35
36 cgm_vec3 dbgvec[4];
37
38 struct rsim_world rsim;
39
40 int main(int argc, char **argv)
41 {
42         glutInit(&argc, argv);
43         glutInitWindowSize(1280, 800);
44         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
45         glutCreateWindow("ropesim");
46
47         glutDisplayFunc(display);
48         glutIdleFunc(idle);
49         glutReshapeFunc(reshape);
50         glutKeyboardFunc(keyb);
51         glutMouseFunc(mouse);
52         glutMotionFunc(motion);
53         glutSpaceballMotionFunc(sball_motion);
54         glutSpaceballRotateFunc(sball_rotate);
55         glutSpaceballButtonFunc(sball_button);
56
57         if(init() == -1) {
58                 return 1;
59         }
60         atexit(cleanup);
61
62         start_msec = glutGet(GLUT_ELAPSED_TIME);
63         glutMainLoop();
64         return 0;
65 }
66
67 #define ROPE_MASSES                     10
68 #define ROPE_SPRINGS            (ROPE_MASSES - 1)
69 #define ROPE_LEN                        0.8f
70 #define ROPE_MASSES_MASS        0.01f
71 #define ROPE_K                          180.0f
72
73 int init(void)
74 {
75         static const char *meshnames[] = {"suzanne", "gimbal_outer", "gimbal_inner"};
76         static struct cmesh **meshes[] = {&mesh_suz, &mesh_gout, &mesh_gin};
77         static const float amb[] = {0.05, 0.05, 0.08, 1};
78         int i, j;
79         struct rsim_rope *rope, *ropes_tail;
80
81         glEnable(GL_CULL_FACE);
82         glEnable(GL_DEPTH_TEST);
83         glEnable(GL_LIGHTING);
84         glEnable(GL_LIGHT0);
85         glEnable(GL_LIGHT1);
86         glEnable(GL_LIGHT2);
87
88         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
89
90         if(!(scn = cmesh_alloc()) || cmesh_load(scn, "gimbal.obj") == -1) {
91                 fprintf(stderr, "failed to load scene file\n");
92                 return -1;
93         }
94
95         for(i=0; i<sizeof meshes / sizeof *meshes; i++) {
96                 int idx;
97                 if((idx = cmesh_find_submesh(scn, meshnames[i])) == -1) {
98                         fprintf(stderr, "failed to locate required submesh (%s)\n", meshnames[i]);
99                         return -1;
100                 }
101                 if(!(*meshes[i] = cmesh_alloc()) || cmesh_clone_submesh(*meshes[i], scn, idx) == -1) {
102                         fprintf(stderr, "failed to clone submesh\n");
103                         return -1;
104                 }
105                 cmesh_remove_submesh(scn, idx);
106         }
107
108         rsim_init(&rsim);
109         rsim.damping = 0.3;
110         ropes_tail = 0;
111
112         /* anchor points on the inner gimbal */
113         for(i=0; i<4; i++) {
114                 ganchor[i].x = (float)(((i & 1) << 1) - 1) * 1.5f;
115                 ganchor[i].y = (float)((i & 2) - 1) * 1.5f;
116                 ganchor[i].z = 0;
117
118                 manchor[i] = ganchor[i];
119                 cgm_vscale(manchor + i, 0.32);
120
121
122
123                 manchor[i].y += 0.15;
124
125                 /* create a rope hanging from the anchor point */
126                 if(!(rope = rsim_alloc_rope(ROPE_MASSES, ROPE_SPRINGS))) {
127                         fprintf(stderr, "failed to allocate rope\n");
128                         return -1;
129                 }
130                 for(j=0; j<ROPE_MASSES; j++) {
131                         float t = (float)j / (float)(ROPE_MASSES - 1.0f);
132                         cgm_vlerp(&rope->masses[j].p, ganchor + i, manchor + i, t);
133                         rope->masses[j].m = 0.1f;
134
135                         if(j < ROPE_SPRINGS) {
136                                 rope->springs[j].rest_len = ROPE_LEN / ROPE_SPRINGS;
137                                 rope->springs[j].k = ROPE_K;
138                                 rope->springs[j].mass[0] = rope->masses + j;
139                                 rope->springs[j].mass[1] = rope->masses + j + 1;
140                         }
141                 }
142                 rsim_freeze_rope_mass(rope, rope->masses);      /* freeze first mass */
143                 rsim_freeze_rope_mass(rope, rope->masses + j - 1);      /* freeze last mass */
144
145                 if(!ropes_tail) {
146                         rsim.ropes = ropes_tail = rope;
147                 } else {
148                         ropes_tail->next = rope;
149                         ropes_tail = rope;
150                 }
151                 rope->next = 0;
152         }
153
154         return 0;
155 }
156
157 void cleanup(void)
158 {
159         cmesh_free(mesh_suz);
160         cmesh_free(mesh_gout);
161         cmesh_free(mesh_gin);
162         cmesh_free(scn);
163
164         rsim_destroy(&rsim);
165 }
166
167 void update(long tmsec, float dt)
168 {
169         int i;
170         cgm_vec3 apt0, apt1;
171         float theta, phi, brot;
172         struct rsim_rope *rope;
173
174         /*
175         cgm_mrotation_quat(ginner_xform, &grot);
176         cgm_mtranslate(ginner_xform, gmove.x, gmove.y, gmove.z);
177         */
178
179         theta = cgm_deg_to_rad(grot_theta);
180         phi = cgm_deg_to_rad(grot_phi);
181
182         cgm_mrotation_euler(ginner_xform, phi, theta, 0, CGM_EULER_XYZ);
183         cgm_mrotation_euler(gouter_xform, phi, 0, 0, CGM_EULER_XYZ);
184
185         rope = rsim.ropes;
186         for(i=0; i<4; i++) {
187                 apt0 = ganchor[i];
188                 cgm_vmul_m4v3(&apt0, ginner_xform);
189
190                 dbgvec[i] = apt0;
191                 rope->masses[0].p = apt0;
192
193                 rope = rope->next;
194         }
195
196         rsim_step(&rsim, dt);
197 }
198
199 void display(void)
200 {
201         static const float lpos[][4] = {
202                 {-100, 100, 200, 1},
203                 {100, 80, 50, 1},
204                 {20, -50, -150, 1}
205         };
206         static const float lcol[][4] = {
207                 {0.9, 0.9, 0.9, 1},
208                 {0.5, 0.3, 0.2, 1},
209                 {0.2, 0.3, 0.2, 1}
210         };
211         int i, count;
212         long tmsec = glutGet(GLUT_ELAPSED_TIME) - start_msec;
213         static long prev_tmsec;
214         struct rsim_rope *rope;
215
216         update(tmsec, (float)(tmsec - prev_tmsec) / 1000.0f);
217         prev_tmsec = tmsec;
218
219         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
220
221         glMatrixMode(GL_MODELVIEW);
222         glLoadIdentity();
223         glTranslatef(0, 0, -cam_dist);
224         glRotatef(cam_phi, 1, 0, 0);
225         glRotatef(cam_theta, 0, 1, 0);
226
227         for(i=0; i<3; i++) {
228                 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
229                 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
230         }
231
232         count = cmesh_submesh_count(scn);
233         for(i=0; i<count; i++) {
234                 cmesh_draw_submesh(scn, i);
235         }
236
237         glPushMatrix();
238         glMultMatrixf(gouter_xform);
239         cmesh_draw(mesh_gout);
240         glPopMatrix();
241
242         glPushMatrix();
243         glMultMatrixf(ginner_xform);
244         cmesh_draw(mesh_gin);
245         glPopMatrix();
246
247         /*cmesh_draw(mesh_suz);*/
248
249         glPointSize(7);
250         glBegin(GL_POINTS);
251         for(i=0; i<4; i++) {
252                 glVertex3f(dbgvec[i].x, dbgvec[i].y, dbgvec[i].z);
253         }
254         glEnd();
255
256         glPushAttrib(GL_ENABLE_BIT);
257         glDisable(GL_LIGHTING);
258         glLineWidth(2);
259         glPointSize(5);
260
261         rope = rsim.ropes;
262         while(rope) {
263                 glBegin(GL_LINE_STRIP);
264                 glColor3f(0.2, 1, 0.2);
265                 for(i=0; i<rope->num_masses; i++) {
266                         glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z);
267                 }
268                 glEnd();
269
270                 glBegin(GL_POINTS);
271                 glColor3f(1, 0.2, 0.2);
272                 for(i=0; i<rope->num_masses; i++) {
273                         glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z);
274                 }
275                 glEnd();
276                 rope = rope->next;
277         }
278         glPopAttrib();
279
280         glutSwapBuffers();
281 }
282 void idle(void)
283 {
284         glutPostRedisplay();
285 }
286
287 void reshape(int x, int y)
288 {
289         glViewport(0, 0, x, y);
290         glMatrixMode(GL_PROJECTION);
291         glLoadIdentity();
292         gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
293 }
294
295 void keyb(unsigned char key, int x, int y)
296 {
297         switch(key) {
298         case 27:
299                 exit(0);
300         }
301 }
302
303 void mouse(int bn, int st, int x, int y)
304 {
305         prev_mx = x;
306         prev_my = y;
307         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
308         modkeys = glutGetModifiers();
309 }
310
311 void motion(int x, int y)
312 {
313         int dx = x - prev_mx;
314         int dy = y - prev_my;
315         prev_mx = x;
316         prev_my = y;
317
318         if(!(dx | dy)) return;
319
320         if(modkeys) {
321                 if(bnstate[0]) {
322                         grot_theta += dx * 0.5;
323                         grot_phi += dy * 0.5;
324                 }
325         } else {
326                 if(bnstate[0]) {
327                         cam_theta += dx * 0.5;
328                         cam_phi += dy * 0.5;
329                         if(cam_phi < -90) cam_phi = -90;
330                         if(cam_phi > 90) cam_phi = 90;
331                 }
332
333                 if(bnstate[2]) {
334                         cam_dist += dy * 0.1;
335                         if(cam_dist < 0.0f) cam_dist = 0.0f;
336                 }
337         }
338 }
339
340 void sball_motion(int x, int y, int z)
341 {
342         gmove.x += x * 0.001f;
343         gmove.y += y * 0.001f;
344         gmove.z -= z * 0.001f;
345 }
346
347 void sball_rotate(int x, int y, int z)
348 {
349         /*
350         float axis_len, s;
351         axis_len = (float)sqrt(x * x + y * y + z * z);
352         s = axis_len == 0.0f ? 1.0f : 1.0f / axis_len;
353         cgm_qrotate(&grot, axis_len * 0.001f, -x * s, -y * s, z * s);
354         */
355
356         grot_theta += y * 0.03f;
357         grot_phi += x * 0.03f;
358 }
359
360 void sball_button(int bn, int st)
361 {
362         if(st == GLUT_DOWN) {
363                 /*cgm_qcons(&grot, 0, 0, 0, 1);*/
364                 grot_theta = grot_phi = 0.0f;
365                 cgm_vcons(&gmove, 0, 0, 0);
366         }
367 }