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