X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fmain.cc;h=36fdeb8eb64df9e287a40e05a70eb821f983f6a7;hb=f6d33178841d379dc9fe111970aff926bde0793c;hp=9720723ffbbc40dbfd9eb314af4dc349201d6b07;hpb=a8a7d557cf22c2bf16eb333d33a6c7c68f532482;p=hair diff --git a/src/main.cc b/src/main.cc index 9720723..36fdeb8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -24,8 +24,13 @@ 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 void sball_motion(int x, int y, int z); +static void sball_rotate(int x, int y, int z); +static void sball_button(int bn, int st); static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1); +static void draw_text(const char *text, int x, int y, float sz, const Vec3 &color); +static void update_sball_matrix(); static std::vector meshes; static Mesh *mesh_head; @@ -39,6 +44,13 @@ static float head_rz, head_rx; /* rot angles x, z axis */ static Mat4 head_xform; //static CollSphere coll_sphere; /* sphere used for collision detection */ +// spaceball (6dof control) state +static Vec3 sball_pos; +static Quat sball_rot; +static Mat4 sball_xform; +static bool sball_update_pending; + + int main(int argc, char **argv) { glutInit(&argc, argv); @@ -56,6 +68,9 @@ int main(int argc, char **argv) glutMouseFunc(mouse); glutMotionFunc(motion); glutIdleFunc(idle); + glutSpaceballMotionFunc(sball_motion); + glutSpaceballRotateFunc(sball_rotate); + glutSpaceballButtonFunc(sball_button); if(!init()) { return 1; @@ -129,6 +144,7 @@ static void cleanup() for(size_t i=0; idraw(); + if(!meshes[i]->mtl.tex || meshes[i]->mtl.tex_opaque) + meshes[i]->draw(); + } + for(size_t i=0; imtl.tex && !meshes[i]->mtl.tex_opaque) + meshes[i]->draw(); } /* glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); @@ -194,7 +221,7 @@ static void display() glPopAttrib(); */ float plane[4] = { - 0, 0, 0.5 / 350, 0.5 + 0, 0, 0.5 / 350, 0.5 }; glPushMatrix(); @@ -224,6 +251,9 @@ static void display() glPopMatrix(); + draw_text("Hold h to move the head with the mouse!", 15, 15, 0.0015 * win_width, Vec3(0, 0, 0)); + draw_text("Hold h to move the head with the mouse!", 12, 17, 0.0015 * win_width, Vec3(0.8, 0.5, 0.7)); + glutSwapBuffers(); assert(glGetError() == GL_NO_ERROR); } @@ -317,6 +347,35 @@ static void idle() glutPostRedisplay(); } +static void sball_motion(int x, int y, int z) +{ + sball_pos.x += (float)x * 0.001; + sball_pos.y += (float)y * 0.001; + sball_pos.z -= (float)z * 0.001; + sball_update_pending = true; +} + +static void sball_rotate(int x, int y, int z) +{ + Vec3 axis = Vec3(x, y, -z); + float axis_len = length(axis); + if(axis_len > 0.0f) { + Quat q; + q.set_rotation(axis / axis_len, axis_len * 0.001); + sball_rot = q * sball_rot; + sball_update_pending = true; + } +} + +static void sball_button(int bn, int st) +{ + if(st != GLUT_DOWN) return; + + sball_pos = Vec3(0, 0, 0); + sball_rot = Quat::identity; + sball_xform = Mat4::identity; +} + static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1) { unsigned char *pixels = new unsigned char[sz * 3]; @@ -342,3 +401,49 @@ static unsigned int gen_grad_tex(int sz, const Vec3 &c0, const Vec3 &c1) return tex; } + +static void draw_text(const char *text, int x, int y, float sz, const Vec3 &color) +{ + glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT); + + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glLineWidth(3); + glPointSize(3); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0, win_width, 0, win_height, -1, 1); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glTranslatef(x, y, 0); + glScalef(0.1 * sz, 0.1 * sz, 1); + + glColor3f(color.x, color.y, color.z); + while(*text != '\0') { + glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *text); + text++; + } + + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + + glPopAttrib(); +} + +static void update_sball_matrix() +{ + Mat4 rot = sball_rot.calc_matrix(); + //rot.transpose(); + + Mat4 trans; + trans.translation(sball_pos); + + sball_xform = rot * trans; +}