X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fmain.cc;h=45b56325282bbdb89a74e3947ccd14971e81aa18;hb=c64bd959ffb4034cb288780f13a351b00fb22ca0;hp=5dd28570746a2611803b82e10e067e8ad657e968;hpb=080d7a779d43f549fc16c44e709cbf5989180fdf;p=laserbrain_demo diff --git a/src/main.cc b/src/main.cc index 5dd2857..45b5632 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,309 +2,156 @@ #include #include #include -#ifdef __APPLE__ -#include -#else -#include -#endif +#include #include "app.h" -#include "machine.h" static bool init(); -static void cleanup(); -static void display(); -static void idle(); -static void draw_gears(); -static void reshape(int x, int y); -static void keyb(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 void process_event(SDL_Event *ev); +static void proc_modkeys(); -static int win_width, win_height; +static SDL_Window *win; +static SDL_GLContext ctx; +static bool fullscreen, mouse_grabbed; +static bool quit; -static float cam_dist = 0.5; -static float cam_theta, cam_phi; -static int prev_mx, prev_my; -static bool bnstate[8]; +static unsigned int start_time; +static unsigned int modkeys; -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 int scale_factor = 1; int main(int argc, char **argv) { - glutInitWindowSize(1024, 768); - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); - glutCreateWindow("Antikythera"); - - glutDisplayFunc(display); - glutIdleFunc(idle); - glutReshapeFunc(reshape); - glutKeyboardFunc(keyb); - glutMouseFunc(mouse); - glutMotionFunc(motion); - glutPassiveMotionFunc(passive_motion); - - if(!init()) { + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) { + fprintf(stderr, "failed to initialize SDL\n"); return 1; } - atexit(cleanup); - - glutMainLoop(); - return 0; -} - -static bool init() -{ - glewInit(); - glEnable(GL_MULTISAMPLE); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glEnable(GL_LIGHTING); - glEnable(GL_NORMALIZE); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); + SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1); - Mesh::use_custom_sdr_attr = false; + int defpos = SDL_WINDOWPOS_UNDEFINED; + unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; - 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(); - 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); - - 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); + if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) { + // try again without sRGB capability + SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0); + if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) { + fprintf(stderr, "failed to create window\n"); + SDL_Quit(); + return 1; + } + } - machine->add_motor(0, 1.0); + if(!(ctx = SDL_GL_CreateContext(win))) { + fprintf(stderr, "failed to create OpenGL context\n"); + SDL_Quit(); + return 1; + } + SDL_GL_GetDrawableSize(win, &win_width, &win_height); + win_aspect = (float)win_width / (float)win_height; - start_time = glutGet(GLUT_ELAPSED_TIME); - return true; -} + if(!init()) { + SDL_Quit(); + return 1; + } + app_reshape(win_width, win_height); -static void cleanup() -{ - delete machine; -} + while(!quit) { + SDL_Event ev; -static void update(float dt) -{ - machine->update(dt); + time_msec = SDL_GetTicks() - start_time; + while(SDL_PollEvent(&ev)) { + process_event(&ev); + if(quit) goto break_evloop; + } - if(sel_gear) { + app_display(); } +break_evloop: - hover_gear = pick_gear(prev_mx, prev_my); + app_cleanup(); + SDL_Quit(); + return 0; } -static void set_light(int idx, const Vec3 &pos, const Vec3 &color) +void app_swap_buffers() { - unsigned int lt = GL_LIGHT0 + idx; - float posv[] = { pos.x, pos.y, pos.z, 1 }; - float colv[] = { color.x, color.y, color.z, 1 }; - - glEnable(lt); - glLightfv(lt, GL_POSITION, posv); - glLightfv(lt, GL_DIFFUSE, colv); - glLightfv(lt, GL_SPECULAR, colv); + SDL_GL_SwapWindow(win); } -static void display() +void app_quit() { - 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); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glTranslatef(0, 0, -cam_dist); - glRotatef(cam_phi, 1, 0, 0); - glRotatef(cam_theta, 0, 1, 0); - - set_light(0, Vec3(-50, 75, 100), Vec3(1.0, 0.8, 0.7) * 0.8); - set_light(1, Vec3(100, 0, 30), Vec3(0.6, 0.7, 1.0) * 0.6); - set_light(2, Vec3(-10, -10, 60), Vec3(0.8, 1.0, 0.8) * 0.3); - - update(dt); - - draw_gears(); - - glutSwapBuffers(); - assert(glGetError() == GL_NO_ERROR); + quit = true; } -static void idle() +unsigned int app_get_modifiers() { - glutPostRedisplay(); + return modkeys; } -static void draw_gears() +static bool init() { - /* world scale is in meters, gears are in millimeters, scale by 1/1000 */ - glPushMatrix(); - glScalef(0.001, 0.001, 0.001); - - if(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(); - } + glewInit(); - glPopAttrib(); + if(!app_init()) { + return false; } - machine->draw(); - - 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, 100.0); + start_time = SDL_GetTicks(); + return true; } -static void keyb(unsigned char key, int x, int y) +static void process_event(SDL_Event *ev) { - switch(key) { - case 27: - exit(0); - - case 'w': - opt_gear_wireframe = !opt_gear_wireframe; - glutPostRedisplay(); + switch(ev->type) { + case SDL_QUIT: + quit = true; break; - } -} -static void mouse(int bn, int st, int x, int y) -{ - int bidx = bn - GLUT_LEFT_BUTTON; - bool down = st == GLUT_DOWN; + case SDL_KEYDOWN: + case SDL_KEYUP: + proc_modkeys(); + app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); + break; - prev_mx = x; - prev_my = y; - bnstate[bidx] = down; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + proc_modkeys(); + app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED, + ev->button.x * scale_factor, ev->button.y * scale_factor); + break; - if(bidx == 0) { - if(down) { - sel_gear = pick_gear(x, y); - sel_hit_pos = pick_hit.pos; + case SDL_MOUSEMOTION: + if(mouse_grabbed) { + app_mouse_delta(ev->motion.xrel, ev->motion.yrel); } else { - sel_gear = 0; + app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor); } - } + break; - 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(); + case SDL_WINDOWEVENT: + if(ev->window.event == SDL_WINDOWEVENT_RESIZED) { + SDL_GL_GetDrawableSize(win, &win_width, &win_height); + win_aspect = (float)win_width / (float)win_height; + scale_factor = win_width / ev->window.data1; + app_reshape(win_width, win_height); } + break; } } -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; - - 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; + SDL_Keymod sdlmod = SDL_GetModState(); + if(sdlmod & KMOD_SHIFT) { + modkeys |= MOD_SHIFT; + } + if(sdlmod & KMOD_ALT) { + modkeys |= MOD_ALT; + } + if(sdlmod & KMOD_CTRL) { + modkeys |= MOD_CTRL; } -} - -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); }