5 #include "cgmath/cgmath.h"
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);
20 float cam_theta, cam_phi, cam_dist = 10;
28 struct cmesh *mesh_gout, *mesh_gin, *mesh_suz;
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];
38 struct rsim_world rsim;
40 int main(int argc, char **argv)
42 glutInit(&argc, argv);
43 glutInitWindowSize(1280, 800);
44 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
45 glutCreateWindow("ropesim");
47 glutDisplayFunc(display);
49 glutReshapeFunc(reshape);
50 glutKeyboardFunc(keyb);
52 glutMotionFunc(motion);
53 glutSpaceballMotionFunc(sball_motion);
54 glutSpaceballRotateFunc(sball_rotate);
55 glutSpaceballButtonFunc(sball_button);
62 start_msec = glutGet(GLUT_ELAPSED_TIME);
68 #define ROPE_SPRINGS (ROPE_MASSES - 1)
70 #define ROPE_MASSES_MASS 0.1f
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};
79 struct rsim_rope *rope, *ropes_tail;
81 glEnable(GL_CULL_FACE);
82 glEnable(GL_DEPTH_TEST);
83 glEnable(GL_LIGHTING);
88 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
90 if(!(scn = cmesh_alloc()) || cmesh_load(scn, "gimbal.obj") == -1) {
91 fprintf(stderr, "failed to load scene file\n");
95 for(i=0; i<sizeof meshes / sizeof *meshes; i++) {
97 if((idx = cmesh_find_submesh(scn, meshnames[i])) == -1) {
98 fprintf(stderr, "failed to locate required submesh (%s)\n", meshnames[i]);
101 if(!(*meshes[i] = cmesh_alloc()) || cmesh_clone_submesh(*meshes[i], scn, idx) == -1) {
102 fprintf(stderr, "failed to clone submesh\n");
105 cmesh_remove_submesh(scn, idx);
111 /* anchor points on the inner gimbal */
113 ganchor[i].x = (float)(((i & 1) << 1) - 1) * 1.5f;
114 ganchor[i].y = (float)((i & 2) - 1) * 1.5f;
117 manchor[i] = ganchor[i];
118 cgm_vscale(manchor + i, 0.32);
122 manchor[i].y += 0.15;
124 /* create a rope hanging from the anchor point */
125 if(!(rope = rsim_alloc_rope(ROPE_MASSES, ROPE_SPRINGS))) {
126 fprintf(stderr, "failed to allocate rope\n");
129 for(j=0; j<ROPE_MASSES; j++) {
130 float t = (float)j / (float)(ROPE_MASSES - 1.0f);
131 cgm_vlerp(&rope->masses[j].p, ganchor + i, manchor + i, t);
132 rope->masses[j].m = 0.1f;
134 if(j < ROPE_SPRINGS) {
135 rope->springs[j].rest_len = ROPE_LEN / ROPE_SPRINGS;
136 rope->springs[j].k = ROPE_K;
137 rope->springs[j].mass[0] = rope->masses + j;
138 rope->springs[j].mass[1] = rope->masses + j + 1;
141 rsim_freeze_rope_mass(rope, rope->masses); /* freeze first mass */
142 rsim_freeze_rope_mass(rope, rope->masses + j - 1); /* freeze last mass */
145 rsim.ropes = ropes_tail = rope;
147 ropes_tail->next = rope;
158 cmesh_free(mesh_suz);
159 cmesh_free(mesh_gout);
160 cmesh_free(mesh_gin);
166 void update(long tmsec, float dt)
170 float theta, phi, brot;
171 struct rsim_rope *rope;
174 cgm_mrotation_quat(ginner_xform, &grot);
175 cgm_mtranslate(ginner_xform, gmove.x, gmove.y, gmove.z);
178 theta = cgm_deg_to_rad(grot_theta);
179 phi = cgm_deg_to_rad(grot_phi);
181 cgm_mrotation_euler(ginner_xform, phi, theta, 0, CGM_EULER_XYZ);
182 cgm_mrotation_euler(gouter_xform, phi, 0, 0, CGM_EULER_XYZ);
187 cgm_vmul_m4v3(&apt0, ginner_xform);
190 rope->masses[0].p = apt0;
195 rsim_step(&rsim, dt);
200 static const float lpos[][4] = {
205 static const float lcol[][4] = {
211 long tmsec = glutGet(GLUT_ELAPSED_TIME) - start_msec;
212 static long prev_tmsec;
213 struct rsim_rope *rope;
215 update(tmsec, (float)(tmsec - prev_tmsec) / 1000.0f);
218 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
220 glMatrixMode(GL_MODELVIEW);
222 glTranslatef(0, 0, -cam_dist);
223 glRotatef(cam_phi, 1, 0, 0);
224 glRotatef(cam_theta, 0, 1, 0);
227 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
228 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
231 count = cmesh_submesh_count(scn);
232 for(i=0; i<count; i++) {
233 cmesh_draw_submesh(scn, i);
237 glMultMatrixf(gouter_xform);
238 cmesh_draw(mesh_gout);
242 glMultMatrixf(ginner_xform);
243 cmesh_draw(mesh_gin);
246 cmesh_draw(mesh_suz);
251 glVertex3f(dbgvec[i].x, dbgvec[i].y, dbgvec[i].z);
255 glPushAttrib(GL_ENABLE_BIT);
256 glDisable(GL_LIGHTING);
262 glBegin(GL_LINE_STRIP);
263 glColor3f(0.2, 1, 0.2);
264 for(i=0; i<rope->num_masses; i++) {
265 glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z);
270 glColor3f(1, 0.2, 0.2);
271 for(i=0; i<rope->num_masses; i++) {
272 glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z);
286 void reshape(int x, int y)
288 glViewport(0, 0, x, y);
289 glMatrixMode(GL_PROJECTION);
291 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
294 void keyb(unsigned char key, int x, int y)
302 void mouse(int bn, int st, int x, int y)
306 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
307 modkeys = glutGetModifiers();
310 void motion(int x, int y)
312 int dx = x - prev_mx;
313 int dy = y - prev_my;
317 if(!(dx | dy)) return;
321 grot_theta += dx * 0.5;
322 grot_phi += dy * 0.5;
326 cam_theta += dx * 0.5;
328 if(cam_phi < -90) cam_phi = -90;
329 if(cam_phi > 90) cam_phi = 90;
333 cam_dist += dy * 0.1;
334 if(cam_dist < 0.0f) cam_dist = 0.0f;
339 void sball_motion(int x, int y, int z)
341 gmove.x += x * 0.001f;
342 gmove.y += y * 0.001f;
343 gmove.z -= z * 0.001f;
346 void sball_rotate(int x, int y, int z)
350 axis_len = (float)sqrt(x * x + y * y + z * z);
351 s = axis_len == 0.0f ? 1.0f : 1.0f / axis_len;
352 cgm_qrotate(&grot, axis_len * 0.001f, -x * s, -y * s, z * s);
355 grot_theta += y * 0.03f;
356 grot_phi += x * 0.03f;
359 void sball_button(int bn, int st)
361 if(st == GLUT_DOWN) {
362 /*cgm_qcons(&grot, 0, 0, 0, 1);*/
363 grot_theta = grot_phi = 0.0f;
364 cgm_vcons(&gmove, 0, 0, 0);