added sphere around the head, removed debugging stuff, 90s look
[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 }
133
134 static void display()
135 {
136         static unsigned long prev_time;
137         unsigned long msec = glutGet(GLUT_ELAPSED_TIME);
138         float dt = (float)(msec - prev_time) / 1000.0;
139         prev_time = msec;
140
141         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
142
143         head_xform = Mat4::identity;
144         head_xform.rotate_x(gph::deg_to_rad(head_rx));
145         head_xform.rotate_z(-gph::deg_to_rad(head_rz));
146
147         glMatrixMode(GL_MODELVIEW);
148         glLoadIdentity();
149         glTranslatef(0, 0, -cam_dist);
150         glRotatef(cam_phi, 1, 0, 0);
151         glRotatef(cam_theta, 0, 1, 0);
152         /* multiplying with the head rot matrix */
153         glPushMatrix();
154         glMultMatrixf(head_xform[0]);
155 /*
156         glPushAttrib(GL_LINE_BIT);
157         glLineWidth(1);
158         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
159 */
160         for(size_t i=0; i<meshes.size(); i++) {
161                 meshes[i]->draw();
162         }
163 /*
164         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
165         glPopAttrib();
166 */
167
168         glPopMatrix();
169
170         hair.set_transform(head_xform);
171         hair.update(dt);
172         hair.draw();
173
174 /*
175         glPushAttrib(GL_ENABLE_BIT);
176         glDisable(GL_DEPTH_TEST);
177         glDisable(GL_LIGHTING);
178         glBegin(GL_POINTS);
179         for (int i=0; i<500; i++) {
180                 Vec3 p;
181                 p.x = (float)rand() / RAND_MAX * 8 - 4;
182                 p.y = (float)rand() / RAND_MAX * 4;
183                 p.z = 0;
184
185                 Vec3 tmp = inverse(head_xform) * p;
186                 if(coll_sphere.contains(tmp)) {
187                         glColor3f(1, 0, 0);
188                 }
189                 else glColor3f(0, 1, 0);
190
191                 glVertex3f(p.x, p.y, p.z);
192         }
193         glEnd();
194         glPopAttrib();
195 */
196         float plane[4] = {
197                 0, 0, 0.5 / 350, 0.5 
198         };
199
200         glPushMatrix();
201         glRotatef(90, 1, 0, 0);
202
203         glPushAttrib(GL_ENABLE_BIT);
204         glDisable(GL_LIGHTING);
205         glEnable(GL_TEXTURE_1D);
206         glBindTexture(GL_TEXTURE_1D, grad_tex);
207         glFrontFace(GL_CW);
208         glEnable(GL_TEXTURE_GEN_S);
209         glTexGenfv(GL_S, GL_OBJECT_PLANE, plane);
210         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
211         glColor3f(1, 1, 1);
212
213         glDepthMask(0);
214
215         glutSolidSphere(350, 16, 8);
216         glDisable(GL_TEXTURE_1D);
217
218         glColor3f(0.2, 0.2, 0.2);
219         glutWireSphere(350, 32, 16);
220
221         glDepthMask(1);
222         glFrontFace(GL_CCW);
223         glPopAttrib();
224
225         glPopMatrix();
226
227         glutSwapBuffers();
228         assert(glGetError() == GL_NO_ERROR);
229 }
230
231 static void reshape(int x, int y)
232 {
233         glViewport(0, 0, x, y);
234         win_width = x;
235         win_height = y;
236
237         glMatrixMode(GL_PROJECTION);
238         glLoadIdentity();
239         gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
240 }
241
242 static bool hpressed;
243 static void keydown(unsigned char key, int /*x*/, int /*y*/)
244 {
245         switch(key) {
246         case 'h':
247         case 'H':
248                 hpressed = true;
249                 break;
250         case 27:
251                 exit(0);
252         default:
253                 break;
254         }
255 }
256
257 static void keyup(unsigned char key, int /*x*/, int /*y*/)
258 {
259         switch(key) {
260         case 'h':
261         case 'H':
262                 hpressed = false;
263                 break;
264         default:
265                 break;
266         }
267 }
268
269 bool bnstate[8];
270 int prev_x, prev_y;
271
272 static void mouse(int bn, int st, int x, int y)
273 {
274         bnstate[bn] = st == GLUT_DOWN;
275         prev_x = x;
276         prev_y = y;
277 }
278
279 static void motion(int x, int y)
280 {
281         int dx = x - prev_x;
282         int dy = y - prev_y;
283         prev_x = x;
284         prev_y = y;
285
286         if(!dx && !dy) return;
287
288         if(hpressed) {
289                 if(bnstate[0]) {
290                         head_rz += dx * 0.5;
291                         head_rx += dy * 0.5;
292
293                         if(head_rx < -45) head_rx = -45;
294                         if(head_rx > 45) head_rx = 45;
295
296                         if(head_rz < -90) head_rz = -90;
297                         if(head_rz > 90) head_rz = 30;
298                 }
299         }
300         else {
301                 if(bnstate[0]) {
302                         cam_theta += dx * 0.5;
303                         cam_phi += dy * 0.5;
304
305                         if(cam_phi < -90) cam_phi = -90;
306                         if(cam_phi > 90) cam_phi = 90;
307                 }
308                 if(bnstate[2]) {
309                         cam_dist += dy * 0.1;
310                         if(cam_dist < 0) cam_dist = 0;
311                 }
312         }
313 }
314
315 static void idle()
316 {
317         glutPostRedisplay();
318 }
319
320 static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1)
321 {
322         unsigned char *pixels = new unsigned char[sz * 3];
323         for(int i=0; i<sz; i++) {
324                 float t = (float)i / (float)(sz - 1);
325                 Vec3 color = c0 + (c1 - c0) * t;
326                 pixels[i * 3] = color.x * 255;
327                 pixels[i * 3 + 1] = color.y * 255;
328                 pixels[i * 3 + 2] = color.z * 255;
329         }
330
331         unsigned int tex;
332         glGenTextures(1, &tex);
333         glBindTexture(GL_TEXTURE_1D, tex);
334
335         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
336         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
337         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
338
339         glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, sz, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
340
341         delete [] pixels;
342
343         return tex;
344 }