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;
24 struct cmesh *mesh_gout, *mesh_gin, *mesh_suz;
27 int main(int argc, char **argv)
29 glutInit(&argc, argv);
30 glutInitWindowSize(1280, 800);
31 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
32 glutCreateWindow("ropesim");
34 glutDisplayFunc(display);
36 glutReshapeFunc(reshape);
37 glutKeyboardFunc(keyb);
39 glutMotionFunc(motion);
40 glutSpaceballMotionFunc(sball_motion);
41 glutSpaceballRotateFunc(sball_rotate);
42 glutSpaceballButtonFunc(sball_button);
56 static const char *meshnames[] = {"suzanne", "gimbal_outer", "gimbal_inner"};
57 static struct cmesh **meshes[] = {&mesh_suz, &mesh_gout, &mesh_gin};
58 static const float amb[] = {0.05, 0.05, 0.08, 1};
61 glEnable(GL_CULL_FACE);
62 glEnable(GL_DEPTH_TEST);
63 glEnable(GL_LIGHTING);
68 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
70 if(!(scn = cmesh_alloc()) || cmesh_load(scn, "gimbal.obj") == -1) {
71 fprintf(stderr, "failed to load scene file\n");
75 for(i=0; i<sizeof meshes / sizeof *meshes; i++) {
77 if((idx = cmesh_find_submesh(scn, meshnames[i])) == -1) {
78 fprintf(stderr, "failed to locate required submesh (%s)\n", meshnames[i]);
81 if(!(*meshes[i] = cmesh_alloc()) || cmesh_clone_submesh(*meshes[i], scn, idx) == -1) {
82 fprintf(stderr, "failed to clone submesh\n");
85 cmesh_remove_submesh(scn, idx);
94 cmesh_free(mesh_gout);
101 static const float lpos[][4] = {
106 static const float lcol[][4] = {
113 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
115 glMatrixMode(GL_MODELVIEW);
117 glTranslatef(0, 0, -cam_dist);
118 glRotatef(cam_phi, 1, 0, 0);
119 glRotatef(cam_theta, 0, 1, 0);
122 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
123 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
126 count = cmesh_submesh_count(scn);
127 for(i=0; i<count; i++) {
128 cmesh_draw_submesh(scn, i);
130 cmesh_draw(mesh_gout);
131 cmesh_draw(mesh_gin);
132 cmesh_draw(mesh_suz);
141 void reshape(int x, int y)
143 glViewport(0, 0, x, y);
144 glMatrixMode(GL_PROJECTION);
146 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
149 void keyb(unsigned char key, int x, int y)
157 void mouse(int bn, int st, int x, int y)
161 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
164 void motion(int x, int y)
166 int dx = x - prev_mx;
167 int dy = y - prev_my;
171 if(!(dx | dy)) return;
174 cam_theta += dx * 0.5;
176 if(cam_phi < -90) cam_phi = -90;
177 if(cam_phi > 90) cam_phi = 90;
181 cam_dist += dy * 0.1;
182 if(cam_dist < 0.0f) cam_dist = 0.0f;
186 void sball_motion(int x, int y, int z)
190 void sball_rotate(int rx, int ry, int rz)
194 void sball_button(int bn, int st)