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);
30 static std::vector<Mesh*> meshes;
31 static Mesh *mesh_head;
34 static unsigned int grad_tex;
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 */
42 int main(int argc, char **argv)
44 glutInit(&argc, argv);
45 glutInitWindowSize(800, 600);
46 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
47 glutCreateWindow("hair test");
49 /* for the keydown, keyup functions to work */
50 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
52 glutDisplayFunc(display);
53 glutReshapeFunc(reshape);
54 glutKeyboardFunc(keydown);
55 glutKeyboardUpFunc(keyup);
57 glutMotionFunc(motion);
73 grad_tex = gen_grad_tex(32, Vec3(0, 0, 1), Vec3(0, 1, 0));
75 glEnable(GL_DEPTH_TEST);
76 glEnable(GL_CULL_FACE);
77 // glEnable(GL_COLOR_MATERIAL);
79 glEnable(GL_LIGHTING);
82 glClearColor(0.5, 0.5, 0.5, 1);
83 meshes = load_meshes("data/head.fbx");
85 fprintf(stderr, "Failed to load mesh.\n");
89 for(size_t i=0; i<meshes.size(); i++) {
90 meshes[i]->calc_bbox();
92 Vec3 v0 = meshes[i]->bbox.v0;
93 Vec3 v1 = meshes[i]->bbox.v1;
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);
99 meshes[i]->update_vbo(MESH_ALL);
101 printf("num vertices: %d num triangles: %d\n",
102 (int)meshes[i]->vertices.size(),
103 (int)meshes[i]->indices.size() / 3);
105 if(meshes[i]->name == "head") {
106 mesh_head = meshes[i];
110 fprintf(stderr, "Failed to find the head mesh.\n");
114 // coll_sphere.radius = 1.0;
115 // coll_sphere.center = Vec3(0, 0.6, 0.53);
117 if(!hair.init(mesh_head, MAX_NUM_SPAWNS, THRESH)) {
118 fprintf(stderr, "Failed to initialize hair\n");
122 // hair.add_collider(&coll_sphere);
127 static void cleanup()
129 for(size_t i=0; i<meshes.size(); i++) {
134 static void display()
136 static unsigned long prev_time;
137 unsigned long msec = glutGet(GLUT_ELAPSED_TIME);
138 float dt = (float)(msec - prev_time) / 1000.0;
141 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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));
147 glMatrixMode(GL_MODELVIEW);
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 */
154 glMultMatrixf(head_xform[0]);
156 glPushAttrib(GL_LINE_BIT);
158 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
160 for(size_t i=0; i<meshes.size(); i++) {
164 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
170 hair.set_transform(head_xform);
175 glPushAttrib(GL_ENABLE_BIT);
176 glDisable(GL_DEPTH_TEST);
177 glDisable(GL_LIGHTING);
179 for (int i=0; i<500; i++) {
181 p.x = (float)rand() / RAND_MAX * 8 - 4;
182 p.y = (float)rand() / RAND_MAX * 4;
185 Vec3 tmp = inverse(head_xform) * p;
186 if(coll_sphere.contains(tmp)) {
189 else glColor3f(0, 1, 0);
191 glVertex3f(p.x, p.y, p.z);
201 glRotatef(90, 1, 0, 0);
203 glPushAttrib(GL_ENABLE_BIT);
204 glDisable(GL_LIGHTING);
205 glEnable(GL_TEXTURE_1D);
206 glBindTexture(GL_TEXTURE_1D, grad_tex);
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);
215 glutSolidSphere(350, 16, 8);
216 glDisable(GL_TEXTURE_1D);
218 glColor3f(0.2, 0.2, 0.2);
219 glutWireSphere(350, 32, 16);
228 assert(glGetError() == GL_NO_ERROR);
231 static void reshape(int x, int y)
233 glViewport(0, 0, x, y);
237 glMatrixMode(GL_PROJECTION);
239 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
242 static bool hpressed;
243 static void keydown(unsigned char key, int /*x*/, int /*y*/)
257 static void keyup(unsigned char key, int /*x*/, int /*y*/)
272 static void mouse(int bn, int st, int x, int y)
274 bnstate[bn] = st == GLUT_DOWN;
279 static void motion(int x, int y)
286 if(!dx && !dy) return;
293 if(head_rx < -45) head_rx = -45;
294 if(head_rx > 45) head_rx = 45;
296 if(head_rz < -90) head_rz = -90;
297 if(head_rz > 90) head_rz = 30;
302 cam_theta += dx * 0.5;
305 if(cam_phi < -90) cam_phi = -90;
306 if(cam_phi > 90) cam_phi = 90;
309 cam_dist += dy * 0.1;
310 if(cam_dist < 0) cam_dist = 0;
320 static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1)
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;
332 glGenTextures(1, &tex);
333 glBindTexture(GL_TEXTURE_1D, tex);
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);
339 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, sz, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);