bb5e5c593373bb7b8e12f166135c2cc303be0a23
[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
7 int init(void);
8 void cleanup(void);
9 void display(void);
10 void idle(void);
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);
18
19 float cam_theta, cam_phi, cam_dist = 10;
20 int prev_mx, prev_my;
21 int bnstate[8];
22
23 struct cmesh *scn;
24 struct cmesh *mesh_gout, *mesh_gin, *mesh_suz;
25 cgm_vec3 ganchor[4];
26
27 int main(int argc, char **argv)
28 {
29         glutInit(&argc, argv);
30         glutInitWindowSize(1280, 800);
31         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
32         glutCreateWindow("ropesim");
33
34         glutDisplayFunc(display);
35         glutIdleFunc(idle);
36         glutReshapeFunc(reshape);
37         glutKeyboardFunc(keyb);
38         glutMouseFunc(mouse);
39         glutMotionFunc(motion);
40         glutSpaceballMotionFunc(sball_motion);
41         glutSpaceballRotateFunc(sball_rotate);
42         glutSpaceballButtonFunc(sball_button);
43
44         if(init() == -1) {
45                 return 1;
46         }
47         atexit(cleanup);
48
49         glutMainLoop();
50         return 0;
51 }
52
53
54 int init(void)
55 {
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};
59         int i;
60
61         glEnable(GL_CULL_FACE);
62         glEnable(GL_DEPTH_TEST);
63         glEnable(GL_LIGHTING);
64         glEnable(GL_LIGHT0);
65         glEnable(GL_LIGHT1);
66         glEnable(GL_LIGHT2);
67
68         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
69
70         if(!(scn = cmesh_alloc()) || cmesh_load(scn, "gimbal.obj") == -1) {
71                 fprintf(stderr, "failed to load scene file\n");
72                 return -1;
73         }
74
75         for(i=0; i<sizeof meshes / sizeof *meshes; i++) {
76                 int idx;
77                 if((idx = cmesh_find_submesh(scn, meshnames[i])) == -1) {
78                         fprintf(stderr, "failed to locate required submesh (%s)\n", meshnames[i]);
79                         return -1;
80                 }
81                 if(!(*meshes[i] = cmesh_alloc()) || cmesh_clone_submesh(*meshes[i], scn, idx) == -1) {
82                         fprintf(stderr, "failed to clone submesh\n");
83                         return -1;
84                 }
85                 cmesh_remove_submesh(scn, idx);
86         }
87
88         return 0;
89 }
90
91 void cleanup(void)
92 {
93         cmesh_free(mesh_suz);
94         cmesh_free(mesh_gout);
95         cmesh_free(mesh_gin);
96         cmesh_free(scn);
97 }
98
99 void display(void)
100 {
101         static const float lpos[][4] = {
102                 {-100, 100, 200, 1},
103                 {100, 80, 50, 1},
104                 {20, -50, -150, 1}
105         };
106         static const float lcol[][4] = {
107                 {0.9, 0.9, 0.9, 1},
108                 {0.5, 0.3, 0.2, 1},
109                 {0.2, 0.3, 0.2, 1}
110         };
111         int i;
112
113         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
114
115         glMatrixMode(GL_MODELVIEW);
116         glLoadIdentity();
117         glTranslatef(0, 0, -cam_dist);
118         glRotatef(cam_phi, 1, 0, 0);
119         glRotatef(cam_theta, 0, 1, 0);
120
121         for(i=0; i<3; i++) {
122                 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
123                 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
124         }
125
126         cmesh_draw(scn);
127
128         glutSwapBuffers();
129 }
130 void idle(void)
131 {
132         glutPostRedisplay();
133 }
134
135 void reshape(int x, int y)
136 {
137         glViewport(0, 0, x, y);
138         glMatrixMode(GL_PROJECTION);
139         glLoadIdentity();
140         gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
141 }
142
143 void keyb(unsigned char key, int x, int y)
144 {
145         switch(key) {
146         case 27:
147                 exit(0);
148         }
149 }
150
151 void mouse(int bn, int st, int x, int y)
152 {
153         prev_mx = x;
154         prev_my = y;
155         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
156 }
157
158 void motion(int x, int y)
159 {
160         int dx = x - prev_mx;
161         int dy = y - prev_my;
162         prev_mx = x;
163         prev_my = y;
164
165         if(!(dx | dy)) return;
166
167         if(bnstate[0]) {
168                 cam_theta += dx * 0.5;
169                 cam_phi += dy * 0.5;
170                 if(cam_phi < -90) cam_phi = -90;
171                 if(cam_phi > 90) cam_phi = 90;
172         }
173
174         if(bnstate[2]) {
175                 cam_dist += dy * 0.1;
176                 if(cam_dist < 0.0f) cam_dist = 0.0f;
177         }
178 }
179
180 void sball_motion(int x, int y, int z)
181 {
182 }
183
184 void sball_rotate(int rx, int ry, int rz)
185 {
186 }
187
188 void sball_button(int bn, int st)
189 {
190 }