5 #include "cgmath/cgmath.h"
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);
19 float cam_theta, cam_phi, cam_dist = 10;
26 struct cmesh *mesh_gout, *mesh_gin, *mesh_suz;
29 /*cgm_quat grot = {0, 0, 0, 1};*/
30 float grot_theta, grot_phi;
31 float ginner_xform[16], gouter_xform[16];
36 int main(int argc, char **argv)
38 glutInit(&argc, argv);
39 glutInitWindowSize(1280, 800);
40 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
41 glutCreateWindow("ropesim");
43 glutDisplayFunc(display);
45 glutReshapeFunc(reshape);
46 glutKeyboardFunc(keyb);
48 glutMotionFunc(motion);
49 glutSpaceballMotionFunc(sball_motion);
50 glutSpaceballRotateFunc(sball_rotate);
51 glutSpaceballButtonFunc(sball_button);
58 start_msec = glutGet(GLUT_ELAPSED_TIME);
66 static const char *meshnames[] = {"suzanne", "gimbal_outer", "gimbal_inner"};
67 static struct cmesh **meshes[] = {&mesh_suz, &mesh_gout, &mesh_gin};
68 static const float amb[] = {0.05, 0.05, 0.08, 1};
71 glEnable(GL_CULL_FACE);
72 glEnable(GL_DEPTH_TEST);
73 glEnable(GL_LIGHTING);
78 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
80 if(!(scn = cmesh_alloc()) || cmesh_load(scn, "gimbal.obj") == -1) {
81 fprintf(stderr, "failed to load scene file\n");
85 for(i=0; i<sizeof meshes / sizeof *meshes; i++) {
87 if((idx = cmesh_find_submesh(scn, meshnames[i])) == -1) {
88 fprintf(stderr, "failed to locate required submesh (%s)\n", meshnames[i]);
91 if(!(*meshes[i] = cmesh_alloc()) || cmesh_clone_submesh(*meshes[i], scn, idx) == -1) {
92 fprintf(stderr, "failed to clone submesh\n");
95 cmesh_remove_submesh(scn, idx);
98 /* anchor points on the inner gimbal */
100 ganchor[i].x = (float)(((i & 1) << 1) - 1) * 1.5f;
101 ganchor[i].y = (float)((i & 2) - 1) * 1.5f;
110 cmesh_free(mesh_suz);
111 cmesh_free(mesh_gout);
112 cmesh_free(mesh_gin);
116 void update(long tmsec, float dt)
120 float theta, phi, brot;
123 cgm_mrotation_quat(ginner_xform, &grot);
124 cgm_mtranslate(ginner_xform, gmove.x, gmove.y, gmove.z);
127 theta = cgm_deg_to_rad(grot_theta);
128 phi = cgm_deg_to_rad(grot_phi);
130 cgm_mrotation_euler(ginner_xform, phi, theta, 0, CGM_EULER_XYZ);
131 cgm_mrotation_euler(gouter_xform, phi, 0, 0, CGM_EULER_XYZ);
135 cgm_vmul_m4v3(&apt0, ginner_xform);
143 static const float lpos[][4] = {
148 static const float lcol[][4] = {
154 long tmsec = glutGet(GLUT_ELAPSED_TIME) - start_msec;
155 static long prev_tmsec;
157 update(tmsec, (float)(tmsec - prev_tmsec) / 1000.0f);
160 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
162 glMatrixMode(GL_MODELVIEW);
164 glTranslatef(0, 0, -cam_dist);
165 glRotatef(cam_phi, 1, 0, 0);
166 glRotatef(cam_theta, 0, 1, 0);
169 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
170 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
173 count = cmesh_submesh_count(scn);
174 for(i=0; i<count; i++) {
175 cmesh_draw_submesh(scn, i);
179 glMultMatrixf(gouter_xform);
180 cmesh_draw(mesh_gout);
184 glMultMatrixf(ginner_xform);
185 cmesh_draw(mesh_gin);
188 cmesh_draw(mesh_suz);
193 glVertex3f(dbgvec[i].x, dbgvec[i].y, dbgvec[i].z);
204 void reshape(int x, int y)
206 glViewport(0, 0, x, y);
207 glMatrixMode(GL_PROJECTION);
209 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
212 void keyb(unsigned char key, int x, int y)
220 void mouse(int bn, int st, int x, int y)
224 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
227 void motion(int x, int y)
229 int dx = x - prev_mx;
230 int dy = y - prev_my;
234 if(!(dx | dy)) return;
237 cam_theta += dx * 0.5;
239 if(cam_phi < -90) cam_phi = -90;
240 if(cam_phi > 90) cam_phi = 90;
244 grot_theta += dx * 0.5;
245 grot_phi += dy * 0.5;
249 cam_dist += dy * 0.1;
250 if(cam_dist < 0.0f) cam_dist = 0.0f;
254 void sball_motion(int x, int y, int z)
256 gmove.x += x * 0.001f;
257 gmove.y += y * 0.001f;
258 gmove.z -= z * 0.001f;
261 void sball_rotate(int x, int y, int z)
265 axis_len = (float)sqrt(x * x + y * y + z * z);
266 s = axis_len == 0.0f ? 1.0f : 1.0f / axis_len;
267 cgm_qrotate(&grot, axis_len * 0.001f, -x * s, -y * s, z * s);
270 grot_theta += y * 0.03f;
271 grot_phi += x * 0.03f;
274 void sball_button(int bn, int st)
276 if(st == GLUT_DOWN) {
277 /*cgm_qcons(&grot, 0, 0, 0, 1);*/
278 grot_theta = grot_phi = 0.0f;
279 cgm_vcons(&gmove, 0, 0, 0);