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