X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fmain.cc;h=f9c9f3a0d88dd0e87aab3f946af2403dd7f5a523;hb=dbcb9345c23c5c027d808915962843e7db2d14aa;hp=772b63c71f06f6a7d0862ee41fd1e0641080e070;hpb=ccc1a688b59e25bb934a0d3e2bbf477960068d4f;p=laserbrain_demo diff --git a/src/main.cc b/src/main.cc index 772b63c..f9c9f3a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -8,270 +8,75 @@ #include #endif #include "app.h" -#include "sdr.h" -#include "shadow.h" -#include "texture.h" -#include "machine.h" -#include "meshgen.h" static bool init(); -static void cleanup(); static void display(); static void idle(); -static void draw_scene(); -static void draw_gears(); static void reshape(int x, int y); -static void keyb(unsigned char key, int x, int y); +static void key_press(unsigned char key, int x, int y); +static void key_release(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 passive_motion(int x, int y); -static Gear *pick_gear(int x, int y); - -static int win_width, win_height; - -static float cam_dist = 0.25; -static float cam_theta, cam_phi = 20; -static int prev_mx, prev_my; -static bool bnstate[8]; - -static Mat4 view_matrix; - -static unsigned int start_time, prev_msec; -static Machine *machine; -static Gear *hover_gear, *sel_gear; -static HitPoint pick_hit; -static Vec3 sel_hit_pos; - -static unsigned int sdr_shadow_notex; -static int dbg_show_shadowmap; - -static TextureSet texman; -static Texture *envmap; -static Mesh *skydome; -static unsigned int sdr_skydome; +static void proc_modkeys(); +static unsigned int start_time; +static unsigned int modkeys; int main(int argc, char **argv) { glutInitWindowSize(1024, 768); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); - glutCreateWindow("Antikythera"); + glutCreateWindow("demo"); glutDisplayFunc(display); glutIdleFunc(idle); glutReshapeFunc(reshape); - glutKeyboardFunc(keyb); + glutKeyboardFunc(key_press); + glutKeyboardUpFunc(key_release); glutMouseFunc(mouse); - glutMotionFunc(motion); - glutPassiveMotionFunc(passive_motion); + glutMotionFunc(app_mouse_motion); + glutPassiveMotionFunc(app_mouse_motion); if(!init()) { return 1; } - atexit(cleanup); + atexit(app_cleanup); glutMainLoop(); return 0; } -static bool init() +void app_swap_buffers() { - glewInit(); - - glEnable(GL_MULTISAMPLE); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glEnable(GL_LIGHTING); - glEnable(GL_NORMALIZE); - - Mesh::use_custom_sdr_attr = false; - - machine = new Machine; - - const float pitch = 10.0f; - - Gear *gear1 = new Gear; - gear1->pos = Vec3(-50, 0, 0); - gear1->set_teeth(16, pitch); - gear1->gen_mesh(); - gear1->color = Vec3(0.35, 0.6, 0.8); - machine->add_gear(gear1); - - Gear *gear2 = new Gear; - gear2->set_teeth(32, pitch); - gear2->pos = gear1->pos + Vec3(gear1->radius + gear2->radius - gear1->teeth_length * 0.75, 0, 0); - gear2->thickness = 5; - gear2->gen_mesh(); - machine->add_gear(gear2); - - Gear *gear3 = new Gear; - gear3->set_teeth(8, pitch); - gear3->pos = gear2->pos + Vec3(0, gear2->radius + gear3->radius - gear2->teeth_length * 0.75, 0); - gear3->gen_mesh(); - machine->add_gear(gear3); - gear3->color = Vec3(0.5, 0.8, 0.6); - - Gear *subgear = new Gear; - subgear->set_teeth(10, pitch); - subgear->pos = Vec3(0, 0, (gear2->thickness + subgear->thickness) / 2 + 1); - subgear->gen_mesh(); - gear2->attach(subgear); - machine->add_gear(subgear); - subgear->color = Vec3(0.8, 0.7, 0.5); - - machine->add_motor(0, 1.0); - - // shadows - init_shadow(2048); - - if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) { - return false; - } - set_uniform_int(sdr_shadow_notex, "shadowmap", 1); - set_uniform_int(sdr_shadow_notex, "envmap", 2); - - float ambient[] = {0.1, 0.1, 0.1, 0.0}; - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); - - // env - envmap = texman.get_texture("data/stpeters_cross.jpg", TEX_CUBE); - - skydome = new Mesh; - gen_sphere(skydome, 1.0, 16, 8); - skydome->flip_faces(); - - if(!(sdr_skydome = create_program_load("sdr/skydome.v.glsl", "sdr/skydome.p.glsl"))) { - return false; - } - glUseProgram(0); - - start_time = glutGet(GLUT_ELAPSED_TIME); - return true; + glutSwapBuffers(); } -static void cleanup() +void app_quit() { - texman.clear(); - delete machine; + exit(0); } -static void update(float dt) +unsigned int app_get_modifiers() { - texman.update(); - - machine->update(dt); - - if(sel_gear) { - } - - hover_gear = pick_gear(prev_mx, prev_my); + return modkeys; } -static void set_light(int idx, const Vec3 &pos, const Vec3 &color) +static bool init() { - unsigned int lt = GL_LIGHT0 + idx; - float posv[] = { pos.x, pos.y, pos.z, 1 }; - float colv[] = { color.x, color.y, color.z, 1 }; + glewInit(); - glEnable(lt); - glLightfv(lt, GL_POSITION, posv); - glLightfv(lt, GL_DIFFUSE, colv); - glLightfv(lt, GL_SPECULAR, colv); + if(!app_init()) { + return false; + } + + start_time = glutGet(GLUT_ELAPSED_TIME); + return true; } static void display() { - unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time; - float dt = (float)(msec - prev_msec) / 1000.0f; - prev_msec = msec; - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - view_matrix = Mat4::identity; - view_matrix.pre_translate(0, 0, -cam_dist); - view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0); - view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0); - - glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(view_matrix[0]); - - static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) }; - set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8); - set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6); - set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3); - - update(dt); - - // shadowmap pass - begin_shadow_pass(lpos[0], Vec3(0, 0, 0), 0.2, 100, 150); - draw_scene(); - end_shadow_pass(); - - // regular pass - const Mat4 &shadow_matrix = get_shadow_matrix(); - Mat4 env_matrix = transpose(view_matrix.upper3x3()); - set_uniform_matrix4(sdr_shadow_notex, "envmap_matrix", env_matrix[0]); - - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, get_shadow_tex()); - glMatrixMode(GL_TEXTURE); - glLoadMatrixf(shadow_matrix[0]); - - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_CUBE_MAP, envmap->get_id()); - - glActiveTexture(GL_TEXTURE0); - glMatrixMode(GL_MODELVIEW); - - draw_scene(); - - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - - - if(dbg_show_shadowmap) { - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - - glPushAttrib(GL_ENABLE_BIT); - glUseProgram(0); - glBindTexture(GL_TEXTURE_2D, get_shadow_tex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); - glEnable(GL_TEXTURE_2D); - glDisable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); - glDisable(GL_BLEND); - - glBegin(GL_QUADS); - glColor4f(1, 1, 1, 1); - glTexCoord2f(0, 0); glVertex2f(-0.95, -0.95); - glTexCoord2f(1, 0); glVertex2f(-0.5, -0.95); - glTexCoord2f(1, 1); glVertex2f(-0.5, -0.5); - glTexCoord2f(0, 1); glVertex2f(-0.95, -0.5); - glEnd(); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); - glBindTexture(GL_TEXTURE_2D, 0); - - glPopAttrib(); - - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); - } - - - glutSwapBuffers(); - assert(glGetError() == GL_NO_ERROR); + time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time; + app_display(); } static void idle() @@ -279,91 +84,24 @@ static void idle() glutPostRedisplay(); } -static void draw_scene() -{ - // draw skydome - glDepthMask(0); - - Mat4 rot_view = view_matrix.upper3x3(); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadMatrixf(rot_view[0]); - - bind_texture(envmap, 0); - - glUseProgram(sdr_skydome); - skydome->draw(); - glUseProgram(0); - - bind_texture(0, 0); - - glPopMatrix(); - glDepthMask(1); - - // draw mechanism - draw_gears(); -} - -static void draw_gears() -{ - /* world scale is in meters, gears are in millimeters, scale by 1/1000 */ - glPushMatrix(); - glScalef(0.001, 0.001, 0.001); - - if(!shadow_pass && (sel_gear || hover_gear)) { - glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POLYGON_BIT); - - glDisable(GL_LIGHTING); - glFrontFace(GL_CW); - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - glLineWidth(3.0); - - if(sel_gear) { - glColor3f(0.2, 1.0, 0.3); - sel_gear->draw(); - } else { - glColor3f(1.0, 0.75, 0.2); - hover_gear->draw(); - } - - glPopAttrib(); - } - - glUseProgram(shadow_pass ? 0 : sdr_shadow_notex); - machine->draw(); - glUseProgram(0); - - glPopMatrix(); -} - static void reshape(int x, int y) { win_width = x; win_height = y; - glViewport(0, 0, x, y); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(50.0, (float)x / (float)y, 0.01, 50.0); + app_reshape(x, y); } -static void keyb(unsigned char key, int x, int y) +static void key_press(unsigned char key, int x, int y) { - switch(key) { - case 27: - exit(0); - - case 'w': - opt_gear_wireframe = !opt_gear_wireframe; - glutPostRedisplay(); - break; + proc_modkeys(); + app_keyboard(key, true); +} - case 's': - dbg_show_shadowmap = !dbg_show_shadowmap; - glutPostRedisplay(); - break; - } +static void key_release(unsigned char key, int x, int y) +{ + proc_modkeys(); + app_keyboard(key, false); } static void mouse(int bn, int st, int x, int y) @@ -371,86 +109,22 @@ static void mouse(int bn, int st, int x, int y) int bidx = bn - GLUT_LEFT_BUTTON; bool down = st == GLUT_DOWN; - prev_mx = x; - prev_my = y; - bnstate[bidx] = down; - - if(bidx == 0) { - if(down) { - sel_gear = pick_gear(x, y); - sel_hit_pos = pick_hit.pos; - } else { - sel_gear = 0; - } - } - - if(bidx == 3 || bidx == 4) { /* wheel */ - if(hover_gear) { - float dz = bidx == 4 ? 1 : -1; - hover_gear->set_position(hover_gear->get_position() + hover_gear->get_axis() * dz); - machine->invalidate_meshing(); - } - } + proc_modkeys(); + app_mouse_button(bidx, down, x, y); } -static void motion(int x, int y) +static void proc_modkeys() { - int dx = x - prev_mx; - int dy = y - prev_my; - prev_mx = x; - prev_my = y; - - if(!dx && !dy) return; - - if(sel_gear) { - float speed = 0.5; - Vec3 offs = Vec3(dx * speed, -dy * speed, 0.0); - offs = sel_gear->get_dir_matrix() * offs; - - sel_gear->set_position(sel_gear->get_position() + offs); - machine->invalidate_meshing(); - - } else { - if(bnstate[0]) { - cam_theta += dx * 0.5; - cam_phi += dy * 0.5; + int glutmod = glutGetModifiers(); - if(cam_phi < -90) cam_phi = -90; - if(cam_phi > 90) cam_phi = 90; - glutPostRedisplay(); - } - if(bnstate[2]) { - cam_dist += dy * 0.01; - if(cam_dist < 0.0) cam_dist = 0.0; - glutPostRedisplay(); - } + modkeys = 0; + if(glutmod & GLUT_ACTIVE_SHIFT) { + modkeys |= MOD_SHIFT; + } + if(glutmod & GLUT_ACTIVE_CTRL) { + modkeys |= MOD_CTRL; + } + if(glutmod & GLUT_ACTIVE_ALT) { + modkeys |= MOD_ALT; } -} - -static void passive_motion(int x, int y) -{ - prev_mx = x; - prev_my = y; -} - -static Gear *pick_gear(int x, int y) -{ - double pt[3]; - double viewmat[16], projmat[16]; - int vp[4]; - Ray ray; - - y = win_height - y; - - glGetDoublev(GL_MODELVIEW_MATRIX, viewmat); - glGetDoublev(GL_PROJECTION_MATRIX, projmat); - glGetIntegerv(GL_VIEWPORT, vp); - - gluUnProject(x, y, 0, viewmat, projmat, vp, pt, pt + 1, pt + 2); - ray.origin = Vec3(pt[0], pt[1], pt[2]) * 1000.0f; - - gluUnProject(x, y, 1, viewmat, projmat, vp, pt, pt + 1, pt + 2); - ray.dir = Vec3(pt[0], pt[1], pt[2]) * 1000.0f - ray.origin; - - return machine->intersect_gear(ray, &pick_hit); }