X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fmain.cc;h=852b7cb337318dae928e6053de52be19c2034f06;hb=c595e4a5c4d54f24af1357816a1baecfdeb7d25e;hp=8b0fe13393ba2e031bbcb461f614169472f1ac39;hpb=da5cbacf755273da510c37c819a59c7fe9894c4e;p=hair diff --git a/src/main.cc b/src/main.cc index 8b0fe13..852b7cb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,21 +6,34 @@ #include #include +#include + #include "mesh.h" +#include "hair.h" +#include "object.h" + +#define MAX_NUM_SPAWNS 400 +#define THRESH 0.5 static bool init(); static void cleanup(); static void display(); static void reshape(int x, int y); static void keydown(unsigned char key, int x, int y); +static void keyup(unsigned char key, int x, int y); static void mouse(int bn, int st, int x, int y); static void motion(int x, int y); +static void idle(); static std::vector meshes; static Mesh *mesh_head; +static Hair hair; -int win_width, win_height; -float cam_theta, cam_phi = 25, cam_dist = 8; +static int win_width, win_height; +static float cam_theta, cam_phi = 25, cam_dist = 8; +static float head_rz, head_rx; /* rot angles x, z axis */ +static Mat4 head_xform; +static CollSphere coll_sphere; /* sphere used for collision detection */ int main(int argc, char **argv) { @@ -29,11 +42,16 @@ int main(int argc, char **argv) glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("hair test"); + /* for the keydown, keyup functions to work */ + glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF); + glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keydown); + glutKeyboardUpFunc(keyup); glutMouseFunc(mouse); glutMotionFunc(motion); + glutIdleFunc(idle); if(!init()) { return 1; @@ -55,7 +73,7 @@ static bool init() glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); - glClearColor(1, 0.5, 0.5, 1); + glClearColor(0.5, 0.5, 0.5, 1); meshes = load_meshes("data/head.fbx"); if (meshes.empty()) { fprintf(stderr, "Failed to load mesh.\n"); @@ -64,46 +82,111 @@ static bool init() for(size_t i=0; icalc_bbox(); - +/* Vec3 v0 = meshes[i]->bbox.v0; Vec3 v1 = meshes[i]->bbox.v1; printf("mesh: %s\n", meshes[i]->name.c_str()); printf("AABB mesh %d: v0: (%f, %f, %f) v1: (%f, %f, %f)\n", (int)i, v0.x, v0.y, v0.z, v1.x, v1.y, v1.z); - +*/ meshes[i]->update_vbo(MESH_ALL); +/* printf("num vertices: %d num triangles: %d\n", (int)meshes[i]->vertices.size(), (int)meshes[i]->indices.size() / 3); - +*/ if(meshes[i]->name == "head") { mesh_head = meshes[i]; } } + if(!mesh_head) { + fprintf(stderr, "Failed to find the head mesh.\n"); + return false; + } + + coll_sphere.radius = 1.0; + coll_sphere.center = Vec3(0, 0.6, 0.53); + + if(!hair.init(mesh_head, MAX_NUM_SPAWNS, THRESH)) { + fprintf(stderr, "Failed to initialize hair\n"); + return false; + } + + hair.add_collider(&coll_sphere); return true; } static void cleanup() { + for(size_t i=0; idraw(); } +/* + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glPopAttrib(); +*/ + + glPopMatrix(); + + hair.set_transform(head_xform); + hair.update(dt); + hair.draw(); + +/* + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glBegin(GL_POINTS); + for (int i=0; i<500; i++) { + Vec3 p; + p.x = (float)rand() / RAND_MAX * 8 - 4; + p.y = (float)rand() / RAND_MAX * 4; + p.z = 0; + + Vec3 tmp = inverse(head_xform) * p; + if(coll_sphere.contains(tmp)) { + glColor3f(1, 0, 0); + } + else glColor3f(0, 1, 0); + + glVertex3f(p.x, p.y, p.z); + } + glEnd(); + glPopAttrib(); +*/ glutSwapBuffers(); assert(glGetError() == GL_NO_ERROR); @@ -120,11 +203,30 @@ static void reshape(int x, int y) gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); } +static bool hpressed; static void keydown(unsigned char key, int /*x*/, int /*y*/) { switch(key) { + case 'h': + case 'H': + hpressed = true; + break; case 27: exit(0); + default: + break; + } +} + +static void keyup(unsigned char key, int /*x*/, int /*y*/) +{ + switch(key) { + case 'h': + case 'H': + hpressed = false; + break; + default: + break; } } @@ -147,17 +249,34 @@ static void motion(int x, int y) if(!dx && !dy) return; - if(bnstate[0]) { - cam_theta += dx * 0.5; - cam_phi += dy * 0.5; + if(hpressed) { + if(bnstate[0]) { + head_rz += dx * 0.5; + head_rx += dy * 0.5; - if(cam_phi < -90) cam_phi = -90; - if(cam_phi > 90) cam_phi = 90; - glutPostRedisplay(); + if(head_rx < -45) head_rx = -45; + if(head_rx > 45) head_rx = 45; + + if(head_rz < -90) head_rz = -90; + if(head_rz > 90) head_rz = 30; + } } - if(bnstate[2]) { - cam_dist += dy * 0.1; - if(cam_dist < 0) cam_dist = 0; - glutPostRedisplay(); + else { + if(bnstate[0]) { + cam_theta += dx * 0.5; + cam_phi += dy * 0.5; + + if(cam_phi < -90) cam_phi = -90; + if(cam_phi > 90) cam_phi = 90; + } + if(bnstate[2]) { + cam_dist += dy * 0.1; + if(cam_dist < 0) cam_dist = 0; + } } } + +static void idle() +{ + glutPostRedisplay(); +}