c4696f6c6eabd1f25445e2ae972ba4915d083a70
[antikythera] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <GL/glew.h>
5 #ifdef __APPLE__
6 #include <GLUT/glut.h>
7 #else
8 #include <GL/glut.h>
9 #endif
10 #include "gear.h"
11
12 bool init();
13 void cleanup();
14 void display();
15 void draw_gears();
16 void reshape(int x, int y);
17 void keyb(unsigned char key, int x, int y);
18 void mouse(int bn, int st, int x, int y);
19 void motion(int x, int y);
20
21 static float cam_dist = 2;
22 static float cam_theta, cam_phi;
23 static int prev_mx, prev_my;
24 static bool bnstate[8];
25
26 static Gear *test_gear;
27
28 int main(int argc, char **argv)
29 {
30         glutInit(&argc, argv);
31         glutInitWindowSize(1024, 768);
32         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
33         glutCreateWindow("Antikythera");
34
35         glutDisplayFunc(display);
36         glutReshapeFunc(reshape);
37         glutKeyboardFunc(keyb);
38         glutMouseFunc(mouse);
39         glutMotionFunc(motion);
40
41         if(!init()) {
42                 return 1;
43         }
44         atexit(cleanup);
45
46         glutMainLoop();
47         return 0;
48 }
49
50 bool init()
51 {
52         glewInit();
53
54         glEnable(GL_DEPTH_TEST);
55         glEnable(GL_CULL_FACE);
56         glEnable(GL_LIGHTING);
57         glEnable(GL_LIGHT0);
58         glEnable(GL_NORMALIZE);
59
60         Mesh::use_custom_sdr_attr = false;
61
62         test_gear = new Gear;
63         test_gear->gen_mesh();
64
65         return true;
66 }
67
68 void cleanup()
69 {
70         delete test_gear;
71 }
72
73 void display()
74 {
75         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
76
77         glMatrixMode(GL_MODELVIEW);
78         glLoadIdentity();
79         glTranslatef(0, 0, -cam_dist);
80         glRotatef(cam_phi, 1, 0, 0);
81         glRotatef(cam_theta, 0, 1, 0);
82
83         draw_gears();
84
85         glutSwapBuffers();
86         assert(glGetError() == GL_NO_ERROR);
87 }
88
89 void draw_gears()
90 {
91         /* world scale is in meters, gears are in millimeters, sclae by 1/1000 */
92         glPushMatrix();
93         glScalef(0.001, 0.001, 0.001);
94
95         test_gear->draw();
96
97         glPopMatrix();
98 }
99
100 void reshape(int x, int y)
101 {
102         glViewport(0, 0, x, y);
103
104         glMatrixMode(GL_PROJECTION);
105         glLoadIdentity();
106         gluPerspective(50.0, (float)x / (float)y, 0.05, 100.0);
107 }
108
109 void keyb(unsigned char key, int x, int y)
110 {
111         switch(key) {
112         case 27:
113                 exit(0);
114         }
115 }
116
117 void mouse(int bn, int st, int x, int y)
118 {
119         prev_mx = x;
120         prev_my = y;
121         bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
122 }
123
124 void motion(int x, int y)
125 {
126         int dx = x - prev_mx;
127         int dy = y - prev_my;
128         prev_mx = x;
129         prev_my = y;
130
131         if(!dx && !dy) return;
132
133         if(bnstate[0]) {
134                 cam_theta += dx * 0.5;
135                 cam_phi += dy * 0.5;
136
137                 if(cam_phi < -90) cam_phi = -90;
138                 if(cam_phi > 90) cam_phi = 90;
139                 glutPostRedisplay();
140         }
141         if(bnstate[2]) {
142                 cam_dist += dy * 0.05;
143                 if(cam_dist < 0.0) cam_dist = 0.0;
144                 glutPostRedisplay();
145         }
146 }