#include <stdio.h>
#include <string>
+#include <gmath/gmath.h>
+
#include "mesh.h"
#include "hair.h"
-#define MAX_NUM_SPAWNS 500
+#define MAX_NUM_SPAWNS 4
+#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<Mesh*> 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;
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;
for(size_t i=0; i<meshes.size(); i++) {
meshes[i]->calc_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];
}
return false;
}
- if(!hair.init(mesh_head, MAX_NUM_SPAWNS, 0.1)) {
+ if(!hair.init(mesh_head, MAX_NUM_SPAWNS, THRESH)) {
fprintf(stderr, "Failed to initialize hair\n");
return false;
}
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ head_xform = Mat4::identity;
+ head_xform.rotate_x(gph::deg_to_rad(head_rx));
+ head_xform.rotate_z(-gph::deg_to_rad(head_rz));
+
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -cam_dist);
glRotatef(cam_phi, 1, 0, 0);
glRotatef(cam_theta, 0, 1, 0);
+ glMultMatrixf(head_xform[0]);
+
for(size_t i=0; i<meshes.size(); i++) {
meshes[i]->draw();
}
+ hair.set_transform(head_xform);
hair.draw();
glutSwapBuffers();
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;
}
}
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(head_rx < -45) head_rx = -45;
+ if(head_rx > 45) head_rx = 45;
- if(cam_phi < -90) cam_phi = -90;
- if(cam_phi > 90) cam_phi = 90;
- glutPostRedisplay();
+ if(head_rz < -90) head_rz = -90;
+ if(head_rz > 90) head_rx = 90;
+ }
}
- 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();
+}