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;
27 struct cmesh *mesh_gout, *mesh_gin, *mesh_suz;
30 /*cgm_quat grot = {0, 0, 0, 1};*/
31 float grot_theta, grot_phi;
32 float ginner_xform[16], gouter_xform[16];
37 int main(int argc, char **argv)
39 glutInit(&argc, argv);
40 glutInitWindowSize(1280, 800);
41 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
42 glutCreateWindow("ropesim");
44 glutDisplayFunc(display);
46 glutReshapeFunc(reshape);
47 glutKeyboardFunc(keyb);
49 glutMotionFunc(motion);
50 glutSpaceballMotionFunc(sball_motion);
51 glutSpaceballRotateFunc(sball_rotate);
52 glutSpaceballButtonFunc(sball_button);
59 start_msec = glutGet(GLUT_ELAPSED_TIME);
67 static const char *meshnames[] = {"suzanne", "gimbal_outer", "gimbal_inner"};
68 static struct cmesh **meshes[] = {&mesh_suz, &mesh_gout, &mesh_gin};
69 static const float amb[] = {0.05, 0.05, 0.08, 1};
72 glEnable(GL_CULL_FACE);
73 glEnable(GL_DEPTH_TEST);
74 glEnable(GL_LIGHTING);
79 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
81 if(!(scn = cmesh_alloc()) || cmesh_load(scn, "gimbal.obj") == -1) {
82 fprintf(stderr, "failed to load scene file\n");
86 for(i=0; i<sizeof meshes / sizeof *meshes; i++) {
88 if((idx = cmesh_find_submesh(scn, meshnames[i])) == -1) {
89 fprintf(stderr, "failed to locate required submesh (%s)\n", meshnames[i]);
92 if(!(*meshes[i] = cmesh_alloc()) || cmesh_clone_submesh(*meshes[i], scn, idx) == -1) {
93 fprintf(stderr, "failed to clone submesh\n");
96 cmesh_remove_submesh(scn, idx);
99 /* anchor points on the inner gimbal */
101 ganchor[i].x = (float)(((i & 1) << 1) - 1) * 1.5f;
102 ganchor[i].y = (float)((i & 2) - 1) * 1.5f;
111 cmesh_free(mesh_suz);
112 cmesh_free(mesh_gout);
113 cmesh_free(mesh_gin);
117 void update(long tmsec, float dt)
121 float theta, phi, brot;
124 cgm_mrotation_quat(ginner_xform, &grot);
125 cgm_mtranslate(ginner_xform, gmove.x, gmove.y, gmove.z);
128 theta = cgm_deg_to_rad(grot_theta);
129 phi = cgm_deg_to_rad(grot_phi);
131 cgm_mrotation_euler(ginner_xform, phi, theta, 0, CGM_EULER_XYZ);
132 cgm_mrotation_euler(gouter_xform, phi, 0, 0, CGM_EULER_XYZ);
136 cgm_vmul_m4v3(&apt0, ginner_xform);
144 static const float lpos[][4] = {
149 static const float lcol[][4] = {
155 long tmsec = glutGet(GLUT_ELAPSED_TIME) - start_msec;
156 static long prev_tmsec;
158 update(tmsec, (float)(tmsec - prev_tmsec) / 1000.0f);
161 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
163 glMatrixMode(GL_MODELVIEW);
165 glTranslatef(0, 0, -cam_dist);
166 glRotatef(cam_phi, 1, 0, 0);
167 glRotatef(cam_theta, 0, 1, 0);
170 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
171 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
174 count = cmesh_submesh_count(scn);
175 for(i=0; i<count; i++) {
176 cmesh_draw_submesh(scn, i);
180 glMultMatrixf(gouter_xform);
181 cmesh_draw(mesh_gout);
185 glMultMatrixf(ginner_xform);
186 cmesh_draw(mesh_gin);
189 cmesh_draw(mesh_suz);
194 glVertex3f(dbgvec[i].x, dbgvec[i].y, dbgvec[i].z);
205 void reshape(int x, int y)
207 glViewport(0, 0, x, y);
208 glMatrixMode(GL_PROJECTION);
210 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
213 void keyb(unsigned char key, int x, int y)
221 void mouse(int bn, int st, int x, int y)
225 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
226 modkeys = glutGetModifiers();
229 void motion(int x, int y)
231 int dx = x - prev_mx;
232 int dy = y - prev_my;
236 if(!(dx | dy)) return;
240 grot_theta += dx * 0.5;
241 grot_phi += dy * 0.5;
245 cam_theta += dx * 0.5;
247 if(cam_phi < -90) cam_phi = -90;
248 if(cam_phi > 90) cam_phi = 90;
252 cam_dist += dy * 0.1;
253 if(cam_dist < 0.0f) cam_dist = 0.0f;
258 void sball_motion(int x, int y, int z)
260 gmove.x += x * 0.001f;
261 gmove.y += y * 0.001f;
262 gmove.z -= z * 0.001f;
265 void sball_rotate(int x, int y, int z)
269 axis_len = (float)sqrt(x * x + y * y + z * z);
270 s = axis_len == 0.0f ? 1.0f : 1.0f / axis_len;
271 cgm_qrotate(&grot, axis_len * 0.001f, -x * s, -y * s, z * s);
274 grot_theta += y * 0.03f;
275 grot_phi += x * 0.03f;
278 void sball_button(int bn, int st)
280 if(st == GLUT_DOWN) {
281 /*cgm_qcons(&grot, 0, 0, 0, 1);*/
282 grot_theta = grot_phi = 0.0f;
283 cgm_vcons(&gmove, 0, 0, 0);