9 #include <gmath/gmath.h>
15 #define MAX_NUM_SPAWNS 800
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);
28 static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1);
29 static void draw_text(const char *text, int x, int y, float sz, const Vec3 &color);
31 static std::vector<Mesh*> meshes;
32 static Mesh *mesh_head;
35 static unsigned int grad_tex;
37 static int win_width, win_height;
38 static float cam_theta, cam_phi = 25, cam_dist = 8;
39 static float head_rz, head_rx; /* rot angles x, z axis */
40 static Mat4 head_xform;
41 //static CollSphere coll_sphere; /* sphere used for collision detection */
43 int main(int argc, char **argv)
45 glutInit(&argc, argv);
46 glutInitWindowSize(800, 600);
47 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
48 glutCreateWindow("hair test");
50 /* for the keydown, keyup functions to work */
51 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
53 glutDisplayFunc(display);
54 glutReshapeFunc(reshape);
55 glutKeyboardFunc(keydown);
56 glutKeyboardUpFunc(keyup);
58 glutMotionFunc(motion);
74 grad_tex = gen_grad_tex(32, Vec3(0, 0, 1), Vec3(0, 1, 0));
76 glEnable(GL_DEPTH_TEST);
77 glEnable(GL_CULL_FACE);
78 // glEnable(GL_COLOR_MATERIAL);
80 glEnable(GL_LIGHTING);
83 glClearColor(0.5, 0.5, 0.5, 1);
84 meshes = load_meshes("data/head.fbx");
86 fprintf(stderr, "Failed to load mesh.\n");
90 for(size_t i=0; i<meshes.size(); i++) {
91 meshes[i]->calc_bbox();
93 Vec3 v0 = meshes[i]->bbox.v0;
94 Vec3 v1 = meshes[i]->bbox.v1;
96 printf("mesh: %s\n", meshes[i]->name.c_str());
97 printf("AABB mesh %d: v0: (%f, %f, %f) v1: (%f, %f, %f)\n",
98 (int)i, v0.x, v0.y, v0.z, v1.x, v1.y, v1.z);
100 meshes[i]->update_vbo(MESH_ALL);
102 printf("num vertices: %d num triangles: %d\n",
103 (int)meshes[i]->vertices.size(),
104 (int)meshes[i]->indices.size() / 3);
106 if(meshes[i]->name == "head") {
107 mesh_head = meshes[i];
111 fprintf(stderr, "Failed to find the head mesh.\n");
115 // coll_sphere.radius = 1.0;
116 // coll_sphere.center = Vec3(0, 0.6, 0.53);
118 if(!hair.init(mesh_head, MAX_NUM_SPAWNS, THRESH)) {
119 fprintf(stderr, "Failed to initialize hair\n");
123 // hair.add_collider(&coll_sphere);
128 static void cleanup()
130 for(size_t i=0; i<meshes.size(); i++) {
133 glDeleteTextures(1, &grad_tex);
136 static void display()
138 static unsigned long prev_time;
139 unsigned long msec = glutGet(GLUT_ELAPSED_TIME);
140 float dt = (float)(msec - prev_time) / 1000.0;
143 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
145 head_xform = Mat4::identity;
146 head_xform.rotate_x(gph::deg_to_rad(head_rx));
147 head_xform.rotate_z(-gph::deg_to_rad(head_rz));
149 glMatrixMode(GL_MODELVIEW);
151 glTranslatef(0, 0, -cam_dist);
152 glRotatef(cam_phi, 1, 0, 0);
153 glRotatef(cam_theta, 0, 1, 0);
154 /* multiplying with the head rot matrix */
156 glMultMatrixf(head_xform[0]);
158 glPushAttrib(GL_LINE_BIT);
160 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
162 for(size_t i=0; i<meshes.size(); i++) {
163 if(!meshes[i]->mtl.tex || meshes[i]->mtl.tex_opaque)
166 for(size_t i=0; i<meshes.size(); i++) {
167 if(meshes[i]->mtl.tex && !meshes[i]->mtl.tex_opaque)
171 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
177 hair.set_transform(head_xform);
182 glPushAttrib(GL_ENABLE_BIT);
183 glDisable(GL_DEPTH_TEST);
184 glDisable(GL_LIGHTING);
186 for (int i=0; i<500; i++) {
188 p.x = (float)rand() / RAND_MAX * 8 - 4;
189 p.y = (float)rand() / RAND_MAX * 4;
192 Vec3 tmp = inverse(head_xform) * p;
193 if(coll_sphere.contains(tmp)) {
196 else glColor3f(0, 1, 0);
198 glVertex3f(p.x, p.y, p.z);
208 glRotatef(90, 1, 0, 0);
210 glPushAttrib(GL_ENABLE_BIT);
211 glDisable(GL_LIGHTING);
212 glEnable(GL_TEXTURE_1D);
213 glBindTexture(GL_TEXTURE_1D, grad_tex);
215 glEnable(GL_TEXTURE_GEN_S);
216 glTexGenfv(GL_S, GL_OBJECT_PLANE, plane);
217 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
222 glutSolidSphere(350, 16, 8);
223 glDisable(GL_TEXTURE_1D);
225 glColor3f(0.2, 0.2, 0.2);
226 glutWireSphere(350, 32, 16);
234 draw_text("Hold h to move the head with the mouse!", 15, 15, 0.0015 * win_width, Vec3(0, 0, 0));
235 draw_text("Hold h to move the head with the mouse!", 12, 17, 0.0015 * win_width, Vec3(0.8, 0.5, 0.7));
238 assert(glGetError() == GL_NO_ERROR);
241 static void reshape(int x, int y)
243 glViewport(0, 0, x, y);
247 glMatrixMode(GL_PROJECTION);
249 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
252 static bool hpressed;
253 static void keydown(unsigned char key, int /*x*/, int /*y*/)
267 static void keyup(unsigned char key, int /*x*/, int /*y*/)
282 static void mouse(int bn, int st, int x, int y)
284 bnstate[bn] = st == GLUT_DOWN;
289 static void motion(int x, int y)
296 if(!dx && !dy) return;
303 if(head_rx < -45) head_rx = -45;
304 if(head_rx > 45) head_rx = 45;
306 if(head_rz < -90) head_rz = -90;
307 if(head_rz > 90) head_rz = 30;
312 cam_theta += dx * 0.5;
315 if(cam_phi < -90) cam_phi = -90;
316 if(cam_phi > 90) cam_phi = 90;
319 cam_dist += dy * 0.1;
320 if(cam_dist < 0) cam_dist = 0;
330 static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1)
332 unsigned char *pixels = new unsigned char[sz * 3];
333 for(int i=0; i<sz; i++) {
334 float t = (float)i / (float)(sz - 1);
335 Vec3 color = c0 + (c1 - c0) * t;
336 pixels[i * 3] = color.x * 255;
337 pixels[i * 3 + 1] = color.y * 255;
338 pixels[i * 3 + 2] = color.z * 255;
342 glGenTextures(1, &tex);
343 glBindTexture(GL_TEXTURE_1D, tex);
345 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
346 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
347 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, sz, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
356 static void draw_text(const char *text, int x, int y, float sz, const Vec3 &color)
358 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT);
360 glDisable(GL_DEPTH_TEST);
361 glDisable(GL_LIGHTING);
365 glMatrixMode(GL_PROJECTION);
368 glOrtho(0, win_width, 0, win_height, -1, 1);
370 glMatrixMode(GL_MODELVIEW);
373 glTranslatef(x, y, 0);
374 glScalef(0.1 * sz, 0.1 * sz, 1);
376 glColor3f(color.x, color.y, color.z);
377 while(*text != '\0') {
378 glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *text);
382 glMatrixMode(GL_PROJECTION);
385 glMatrixMode(GL_MODELVIEW);