reorganizing the rope sim
[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;
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
111         if(!(rope = rsim_alloc_rope(ROPE_MASSES * 4))) {
112                 fprintf(stderr, "failed to allocate rope\n");
113                 return -1;
114         }
115         rsim_add_rope(&rsim, rope);
116
117         /* anchor points on the inner gimbal */
118         for(i=0; i<4; i++) {
119                 ganchor[i].x = (float)(((i & 1) << 1) - 1) * 1.5f;
120                 ganchor[i].y = (float)((i & 2) - 1) * 1.5f;
121                 ganchor[i].z = 0;
122
123                 manchor[i] = ganchor[i];
124                 cgm_vscale(manchor + i, 0.32);
125
126
127
128                 manchor[i].y += 0.15;
129
130                 for(j=0; j<ROPE_MASSES; j++) {
131                         int midx = i * ROPE_MASSES + j;
132                         struct rsim_mass *mass = rope->masses + midx;
133
134                         float t = (float)j / (float)(ROPE_MASSES - 1.0f);
135                         cgm_vlerp(&mass->p, ganchor + i, manchor + i, t);
136                         mass->m = ROPE_MASSES_MASS;
137
138                         if(j == 0) {
139                                 rsim_freeze_rope_mass(rope, rope->masses + i * ROPE_MASSES);    /* freeze first mass */
140                         } else {
141                                 rsim_set_rope_spring(rope, midx, midx - 1, ROPE_K, RSIM_RLEN_DEFAULT);
142                         }
143                 }
144         }
145
146         return 0;
147 }
148
149 void cleanup(void)
150 {
151         cmesh_free(mesh_suz);
152         cmesh_free(mesh_gout);
153         cmesh_free(mesh_gin);
154         cmesh_free(scn);
155
156         rsim_destroy(&rsim);
157 }
158
159 void update(long tmsec, float dt)
160 {
161         int i;
162         cgm_vec3 apt0, apt1;
163         float theta, phi, brot;
164         struct rsim_rope *rope;
165
166         /*
167         cgm_mrotation_quat(ginner_xform, &grot);
168         cgm_mtranslate(ginner_xform, gmove.x, gmove.y, gmove.z);
169         */
170
171         theta = cgm_deg_to_rad(grot_theta);
172         phi = cgm_deg_to_rad(grot_phi);
173
174         cgm_mrotation_euler(ginner_xform, phi, theta, 0, CGM_EULER_XYZ);
175         cgm_mrotation_euler(gouter_xform, phi, 0, 0, CGM_EULER_XYZ);
176
177         rope = rsim.ropes;
178         for(i=0; i<4; i++) {
179                 apt0 = ganchor[i];
180                 cgm_vmul_m4v3(&apt0, ginner_xform);
181
182                 dbgvec[i] = apt0;
183                 rope->masses[i * ROPE_MASSES].p = apt0;
184         }
185
186         rsim_step(&rsim, dt);
187 }
188
189 void display(void)
190 {
191         static const float lpos[][4] = {
192                 {-100, 100, 200, 1},
193                 {100, 80, 50, 1},
194                 {20, -50, -150, 1}
195         };
196         static const float lcol[][4] = {
197                 {0.9, 0.9, 0.9, 1},
198                 {0.5, 0.3, 0.2, 1},
199                 {0.2, 0.3, 0.2, 1}
200         };
201         int i, j, count;
202         long tmsec = glutGet(GLUT_ELAPSED_TIME) - start_msec;
203         static long prev_tmsec;
204         struct rsim_rope *rope;
205
206         update(tmsec, (float)(tmsec - prev_tmsec) / 1000.0f);
207         prev_tmsec = tmsec;
208
209         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
210
211         glMatrixMode(GL_MODELVIEW);
212         glLoadIdentity();
213         glTranslatef(0, 0, -cam_dist);
214         glRotatef(cam_phi, 1, 0, 0);
215         glRotatef(cam_theta, 0, 1, 0);
216
217         for(i=0; i<3; i++) {
218                 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
219                 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
220         }
221
222         count = cmesh_submesh_count(scn);
223         for(i=0; i<count; i++) {
224                 cmesh_draw_submesh(scn, i);
225         }
226
227         glPushMatrix();
228         glMultMatrixf(gouter_xform);
229         cmesh_draw(mesh_gout);
230         glPopMatrix();
231
232         glPushMatrix();
233         glMultMatrixf(ginner_xform);
234         cmesh_draw(mesh_gin);
235         glPopMatrix();
236
237         /*cmesh_draw(mesh_suz);*/
238
239         glPointSize(7);
240         glBegin(GL_POINTS);
241         for(i=0; i<4; i++) {
242                 glVertex3f(dbgvec[i].x, dbgvec[i].y, dbgvec[i].z);
243         }
244         glEnd();
245
246         glPushAttrib(GL_ENABLE_BIT);
247         glDisable(GL_LIGHTING);
248         glLineWidth(2);
249         glPointSize(5);
250
251         rope = rsim.ropes;
252         while(rope) {
253                 glBegin(GL_LINES);
254                 glColor3f(0.2, 1, 0.2);
255                 for(i=0; i<rope->num_masses; i++) {
256                         for(j=i+1; j<rope->num_masses; j++) {
257                                 if(rsim_have_spring(rope, i, j)) {
258                                         glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z);
259                                         glVertex3f(rope->masses[j].p.x, rope->masses[j].p.y, rope->masses[j].p.z);
260                                 }
261                         }
262                 }
263                 glEnd();
264
265                 glBegin(GL_POINTS);
266                 glColor3f(1, 0.2, 0.2);
267                 for(i=0; i<rope->num_masses; i++) {
268                         glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z);
269                 }
270                 glEnd();
271                 rope = rope->next;
272         }
273         glPopAttrib();
274
275         glutSwapBuffers();
276 }
277 void idle(void)
278 {
279         glutPostRedisplay();
280 }
281
282 void reshape(int x, int y)
283 {
284         glViewport(0, 0, x, y);
285         glMatrixMode(GL_PROJECTION);
286         glLoadIdentity();
287         gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
288 }
289
290 void keyb(unsigned char key, int x, int y)
291 {
292         switch(key) {
293         case 27:
294                 exit(0);
295         }
296 }
297
298 void mouse(int bn, int st, int x, int y)
299 {
300         prev_mx = x;
301         prev_my = y;
302         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
303         modkeys = glutGetModifiers();
304 }
305
306 void motion(int x, int y)
307 {
308         int dx = x - prev_mx;
309         int dy = y - prev_my;
310         prev_mx = x;
311         prev_my = y;
312
313         if(!(dx | dy)) return;
314
315         if(modkeys) {
316                 if(bnstate[0]) {
317                         grot_theta += dx * 0.5;
318                         grot_phi += dy * 0.5;
319                 }
320         } else {
321                 if(bnstate[0]) {
322                         cam_theta += dx * 0.5;
323                         cam_phi += dy * 0.5;
324                         if(cam_phi < -90) cam_phi = -90;
325                         if(cam_phi > 90) cam_phi = 90;
326                 }
327
328                 if(bnstate[2]) {
329                         cam_dist += dy * 0.1;
330                         if(cam_dist < 0.0f) cam_dist = 0.0f;
331                 }
332         }
333 }
334
335 void sball_motion(int x, int y, int z)
336 {
337         gmove.x += x * 0.001f;
338         gmove.y += y * 0.001f;
339         gmove.z -= z * 0.001f;
340 }
341
342 void sball_rotate(int x, int y, int z)
343 {
344         /*
345         float axis_len, s;
346         axis_len = (float)sqrt(x * x + y * y + z * z);
347         s = axis_len == 0.0f ? 1.0f : 1.0f / axis_len;
348         cgm_qrotate(&grot, axis_len * 0.001f, -x * s, -y * s, z * s);
349         */
350
351         grot_theta += y * 0.03f;
352         grot_phi += x * 0.03f;
353 }
354
355 void sball_button(int bn, int st)
356 {
357         if(st == GLUT_DOWN) {
358                 /*cgm_qcons(&grot, 0, 0, 0, 1);*/
359                 grot_theta = grot_phi = 0.0f;
360                 cgm_vcons(&gmove, 0, 0, 0);
361         }
362 }