assimp
[laserbrain_demo] / 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 "app.h"
11 #include "sdr.h"
12 #include "shadow.h"
13 #include "texture.h"
14 #include "mesh.h"
15 #include "meshgen.h"
16
17 static bool init();
18 static void cleanup();
19 static void display();
20 static void idle();
21 static void draw_scene();
22 static void reshape(int x, int y);
23 static void keyb(unsigned char key, int x, int y);
24 static void mouse(int bn, int st, int x, int y);
25 static void motion(int x, int y);
26 static void passive_motion(int x, int y);
27
28 static int win_width, win_height;
29
30 static float cam_dist = 0.25;
31 static float cam_theta, cam_phi = 20;
32 static int prev_mx, prev_my;
33 static bool bnstate[8];
34
35 static Mat4 view_matrix;
36
37 static unsigned int start_time, prev_msec;
38
39 static TextureSet texman;
40
41
42 int main(int argc, char **argv)
43 {
44         glutInitWindowSize(1024, 768);
45         glutInit(&argc, argv);
46         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
47         glutCreateWindow("demo");
48
49         glutDisplayFunc(display);
50         glutIdleFunc(idle);
51         glutReshapeFunc(reshape);
52         glutKeyboardFunc(keyb);
53         glutMouseFunc(mouse);
54         glutMotionFunc(motion);
55         glutPassiveMotionFunc(passive_motion);
56
57         if(!init()) {
58                 return 1;
59         }
60         atexit(cleanup);
61
62         glutMainLoop();
63         return 0;
64 }
65
66 static bool init()
67 {
68         glewInit();
69
70         glEnable(GL_MULTISAMPLE);
71         glEnable(GL_DEPTH_TEST);
72         glEnable(GL_CULL_FACE);
73         glEnable(GL_LIGHTING);
74         glEnable(GL_NORMALIZE);
75
76         Mesh::use_custom_sdr_attr = false;
77
78         float ambient[] = {0.1, 0.1, 0.1, 0.0};
79         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
80
81         glUseProgram(0);
82
83         start_time = glutGet(GLUT_ELAPSED_TIME);
84         return true;
85 }
86
87 static void cleanup()
88 {
89         texman.clear();
90 }
91
92 static void update(float dt)
93 {
94         texman.update();
95 }
96
97 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
98 {
99         unsigned int lt = GL_LIGHT0 + idx;
100         float posv[] = { pos.x, pos.y, pos.z, 1 };
101         float colv[] = { color.x, color.y, color.z, 1 };
102
103         glEnable(lt);
104         glLightfv(lt, GL_POSITION, posv);
105         glLightfv(lt, GL_DIFFUSE, colv);
106         glLightfv(lt, GL_SPECULAR, colv);
107 }
108
109 static void display()
110 {
111         unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
112         float dt = (float)(msec - prev_msec) / 1000.0f;
113         prev_msec = msec;
114
115         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
116
117         view_matrix = Mat4::identity;
118         view_matrix.pre_translate(0, 0, -cam_dist);
119         view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
120         view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
121
122         glMatrixMode(GL_MODELVIEW);
123         glLoadMatrixf(view_matrix[0]);
124
125         static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
126         set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
127         set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
128         set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
129
130         update(dt);
131
132         draw_scene();
133
134         glutSwapBuffers();
135         assert(glGetError() == GL_NO_ERROR);
136 }
137
138 static void idle()
139 {
140         glutPostRedisplay();
141 }
142
143 static void draw_scene()
144 {
145         glBegin(GL_QUADS);
146         glNormal3f(0, 1, 0);
147         glVertex3f(-30, -10, 30);
148         glVertex3f(30, -10, 30);
149         glVertex3f(30, -10, -30);
150         glVertex3f(-30, -10, -30);
151         glEnd();
152 }
153
154 static void reshape(int x, int y)
155 {
156         win_width = x;
157         win_height = y;
158
159         glViewport(0, 0, x, y);
160
161         glMatrixMode(GL_PROJECTION);
162         glLoadIdentity();
163         gluPerspective(50.0, (float)x / (float)y, 0.01, 50.0);
164 }
165
166 static void keyb(unsigned char key, int x, int y)
167 {
168         switch(key) {
169         case 27:
170                 exit(0);
171         }
172 }
173
174 static void mouse(int bn, int st, int x, int y)
175 {
176         int bidx = bn - GLUT_LEFT_BUTTON;
177         bool down = st == GLUT_DOWN;
178
179         prev_mx = x;
180         prev_my = y;
181         bnstate[bidx] = down;
182 }
183
184 static void motion(int x, int y)
185 {
186         int dx = x - prev_mx;
187         int dy = y - prev_my;
188         prev_mx = x;
189         prev_my = y;
190
191         if(!dx && !dy) return;
192
193         if(bnstate[0]) {
194                 cam_theta += dx * 0.5;
195                 cam_phi += dy * 0.5;
196
197                 if(cam_phi < -90) cam_phi = -90;
198                 if(cam_phi > 90) cam_phi = 90;
199                 glutPostRedisplay();
200         }
201         if(bnstate[2]) {
202                 cam_dist += dy * 0.01;
203                 if(cam_dist < 0.0) cam_dist = 0.0;
204                 glutPostRedisplay();
205         }
206 }
207
208 static void passive_motion(int x, int y)
209 {
210         prev_mx = x;
211         prev_my = y;
212 }