Many fixes (credits Nuclear)
[hair] / src / main.cc
1 #include <GL/glew.h>
2 #include <GL/glut.h>
3
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string>
8
9 #include <gmath/gmath.h>
10
11 #include "mesh.h"
12 #include "hair.h"
13 #include "object.h"
14
15 #define MAX_NUM_SPAWNS 800
16 #define THRESH 0.5
17
18 static bool init();
19 static void cleanup();
20 static void display();
21 static void reshape(int x, int y);
22 static void keydown(unsigned char key, int x, int y);
23 static void keyup(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 idle();
27
28 static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1);
29
30 static std::vector<Mesh*> meshes;
31 static Mesh *mesh_head;
32 static Hair hair;
33
34 static unsigned int grad_tex;
35
36 static int win_width, win_height;
37 static float cam_theta, cam_phi = 25, cam_dist = 8;
38 static float head_rz, head_rx; /* rot angles x, z axis */
39 static Mat4 head_xform;
40 //static CollSphere coll_sphere; /* sphere used for collision detection */
41
42 int main(int argc, char **argv)
43 {
44         glutInit(&argc, argv);
45         glutInitWindowSize(800, 600);
46         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
47         glutCreateWindow("hair test");
48
49         /* for the keydown, keyup functions to work */
50         glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
51
52         glutDisplayFunc(display);
53         glutReshapeFunc(reshape);
54         glutKeyboardFunc(keydown);
55         glutKeyboardUpFunc(keyup);
56         glutMouseFunc(mouse);
57         glutMotionFunc(motion);
58         glutIdleFunc(idle);
59
60         if(!init()) {
61                 return 1;
62         }
63         atexit(cleanup);
64
65         glutMainLoop();
66         return 0;
67 }
68
69 static bool init()
70 {
71         glewInit();
72
73         grad_tex = gen_grad_tex(32, Vec3(0, 0, 1), Vec3(0, 1, 0));
74
75         glEnable(GL_DEPTH_TEST);
76         glEnable(GL_CULL_FACE);
77 //      glEnable(GL_COLOR_MATERIAL);
78
79         glEnable(GL_LIGHTING);
80         glEnable(GL_LIGHT0);
81
82         glClearColor(0.5, 0.5, 0.5, 1);
83         meshes = load_meshes("data/head.fbx");
84         if (meshes.empty()) {
85                 fprintf(stderr, "Failed to load mesh.\n");
86                 return false;
87         }
88
89         for(size_t i=0; i<meshes.size(); i++) {
90                 meshes[i]->calc_bbox();
91 /*
92                 Vec3 v0 = meshes[i]->bbox.v0;
93                 Vec3 v1 = meshes[i]->bbox.v1;
94
95                 printf("mesh: %s\n", meshes[i]->name.c_str());
96                 printf("AABB mesh %d: v0: (%f, %f, %f) v1: (%f, %f, %f)\n",
97                                 (int)i, v0.x, v0.y, v0.z, v1.x, v1.y, v1.z);
98 */
99                 meshes[i]->update_vbo(MESH_ALL);
100 /*
101                 printf("num vertices: %d num triangles: %d\n",
102                                 (int)meshes[i]->vertices.size(),
103                                 (int)meshes[i]->indices.size() / 3);
104 */
105                 if(meshes[i]->name == "head") {
106                         mesh_head = meshes[i];
107                 }
108         }
109         if(!mesh_head) {
110                 fprintf(stderr, "Failed to find the head mesh.\n");
111                 return false;
112         }
113
114 //      coll_sphere.radius = 1.0;
115 //      coll_sphere.center = Vec3(0, 0.6, 0.53);
116
117         if(!hair.init(mesh_head, MAX_NUM_SPAWNS, THRESH)) {
118                 fprintf(stderr, "Failed to initialize hair\n");
119                 return false;
120         }
121
122 //      hair.add_collider(&coll_sphere);
123
124         return true;
125 }
126
127 static void cleanup()
128 {
129         for(size_t i=0; i<meshes.size(); i++) {
130                 delete meshes[i];
131         }
132         glDeleteTextures(1, &grad_tex);
133 }
134
135 static void display()
136 {
137         static unsigned long prev_time;
138         unsigned long msec = glutGet(GLUT_ELAPSED_TIME);
139         float dt = (float)(msec - prev_time) / 1000.0;
140         prev_time = msec;
141
142         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
143
144         head_xform = Mat4::identity;
145         head_xform.rotate_x(gph::deg_to_rad(head_rx));
146         head_xform.rotate_z(-gph::deg_to_rad(head_rz));
147
148         glMatrixMode(GL_MODELVIEW);
149         glLoadIdentity();
150         glTranslatef(0, 0, -cam_dist);
151         glRotatef(cam_phi, 1, 0, 0);
152         glRotatef(cam_theta, 0, 1, 0);
153         /* multiplying with the head rot matrix */
154         glPushMatrix();
155         glMultMatrixf(head_xform[0]);
156 /*
157         glPushAttrib(GL_LINE_BIT);
158         glLineWidth(1);
159         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
160 */
161         for(size_t i=0; i<meshes.size(); i++) {
162                 meshes[i]->draw();
163         }
164 /*
165         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
166         glPopAttrib();
167 */
168
169         glPopMatrix();
170
171         hair.set_transform(head_xform);
172         hair.update(dt);
173         hair.draw();
174
175 /*
176         glPushAttrib(GL_ENABLE_BIT);
177         glDisable(GL_DEPTH_TEST);
178         glDisable(GL_LIGHTING);
179         glBegin(GL_POINTS);
180         for (int i=0; i<500; i++) {
181                 Vec3 p;
182                 p.x = (float)rand() / RAND_MAX * 8 - 4;
183                 p.y = (float)rand() / RAND_MAX * 4;
184                 p.z = 0;
185
186                 Vec3 tmp = inverse(head_xform) * p;
187                 if(coll_sphere.contains(tmp)) {
188                         glColor3f(1, 0, 0);
189                 }
190                 else glColor3f(0, 1, 0);
191
192                 glVertex3f(p.x, p.y, p.z);
193         }
194         glEnd();
195         glPopAttrib();
196 */
197         float plane[4] = {
198                 0, 0, 0.5 / 350, 0.5
199         };
200
201         glPushMatrix();
202         glRotatef(90, 1, 0, 0);
203
204         glPushAttrib(GL_ENABLE_BIT);
205         glDisable(GL_LIGHTING);
206         glEnable(GL_TEXTURE_1D);
207         glBindTexture(GL_TEXTURE_1D, grad_tex);
208         glFrontFace(GL_CW);
209         glEnable(GL_TEXTURE_GEN_S);
210         glTexGenfv(GL_S, GL_OBJECT_PLANE, plane);
211         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
212         glColor3f(1, 1, 1);
213
214         glDepthMask(0);
215
216         glutSolidSphere(350, 16, 8);
217         glDisable(GL_TEXTURE_1D);
218
219         glColor3f(0.2, 0.2, 0.2);
220         glutWireSphere(350, 32, 16);
221
222         glDepthMask(1);
223         glFrontFace(GL_CCW);
224         glPopAttrib();
225
226         glPopMatrix();
227
228         glutSwapBuffers();
229         assert(glGetError() == GL_NO_ERROR);
230 }
231
232 static void reshape(int x, int y)
233 {
234         glViewport(0, 0, x, y);
235         win_width = x;
236         win_height = y;
237
238         glMatrixMode(GL_PROJECTION);
239         glLoadIdentity();
240         gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
241 }
242
243 static bool hpressed;
244 static void keydown(unsigned char key, int /*x*/, int /*y*/)
245 {
246         switch(key) {
247         case 'h':
248         case 'H':
249                 hpressed = true;
250                 break;
251         case 27:
252                 exit(0);
253         default:
254                 break;
255         }
256 }
257
258 static void keyup(unsigned char key, int /*x*/, int /*y*/)
259 {
260         switch(key) {
261         case 'h':
262         case 'H':
263                 hpressed = false;
264                 break;
265         default:
266                 break;
267         }
268 }
269
270 bool bnstate[8];
271 int prev_x, prev_y;
272
273 static void mouse(int bn, int st, int x, int y)
274 {
275         bnstate[bn] = st == GLUT_DOWN;
276         prev_x = x;
277         prev_y = y;
278 }
279
280 static void motion(int x, int y)
281 {
282         int dx = x - prev_x;
283         int dy = y - prev_y;
284         prev_x = x;
285         prev_y = y;
286
287         if(!dx && !dy) return;
288
289         if(hpressed) {
290                 if(bnstate[0]) {
291                         head_rz += dx * 0.5;
292                         head_rx += dy * 0.5;
293
294                         if(head_rx < -45) head_rx = -45;
295                         if(head_rx > 45) head_rx = 45;
296
297                         if(head_rz < -90) head_rz = -90;
298                         if(head_rz > 90) head_rz = 30;
299                 }
300         }
301         else {
302                 if(bnstate[0]) {
303                         cam_theta += dx * 0.5;
304                         cam_phi += dy * 0.5;
305
306                         if(cam_phi < -90) cam_phi = -90;
307                         if(cam_phi > 90) cam_phi = 90;
308                 }
309                 if(bnstate[2]) {
310                         cam_dist += dy * 0.1;
311                         if(cam_dist < 0) cam_dist = 0;
312                 }
313         }
314 }
315
316 static void idle()
317 {
318         glutPostRedisplay();
319 }
320
321 static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1)
322 {
323         unsigned char *pixels = new unsigned char[sz * 3];
324         for(int i=0; i<sz; i++) {
325                 float t = (float)i / (float)(sz - 1);
326                 Vec3 color = c0 + (c1 - c0) * t;
327                 pixels[i * 3] = color.x * 255;
328                 pixels[i * 3 + 1] = color.y * 255;
329                 pixels[i * 3 + 2] = color.z * 255;
330         }
331
332         unsigned int tex;
333         glGenTextures(1, &tex);
334         glBindTexture(GL_TEXTURE_1D, tex);
335
336         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
337         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
338         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
339
340         glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, sz, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
341
342         delete [] pixels;
343
344         return tex;
345 }