nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@6: #include nuclear@0: nuclear@8: #include "opengl.h" nuclear@0: #ifndef __APPLE__ nuclear@0: #include nuclear@0: #else nuclear@0: #include nuclear@0: #endif nuclear@0: nuclear@8: #include "objfile.h" nuclear@8: #include "mesh.h" nuclear@6: #include "gobj.h" nuclear@6: #include "co_xform.h" nuclear@6: #include "co_dbgvis.h" nuclear@8: #include "co_mesh.h" nuclear@8: #include "co_phys.h" nuclear@6: #include "sim.h" nuclear@6: nuclear@0: static bool init(); nuclear@0: static void cleanup(); nuclear@0: static void display(); nuclear@0: static void idle(); nuclear@0: static void reshape(int x, int y); nuclear@0: static void keyb(unsigned char key, int x, int y); nuclear@0: static void mouse(int bn, int st, int x, int y); nuclear@0: static void motion(int x, int y); nuclear@8: static Mesh *load_mesh(const char *fname); nuclear@0: nuclear@8: float cam_theta, cam_phi = 25, cam_dist = 10; nuclear@6: nuclear@6: std::vector objects; nuclear@6: SimWorld simworld; nuclear@6: nuclear@0: nuclear@0: int main(int argc, char **argv) nuclear@0: { nuclear@0: glutInit(&argc, argv); nuclear@0: glutInitWindowSize(800, 600); nuclear@0: glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); nuclear@0: glutCreateWindow("component system test"); nuclear@0: nuclear@0: glutDisplayFunc(display); nuclear@7: glutIdleFunc(idle); nuclear@0: glutReshapeFunc(reshape); nuclear@0: glutKeyboardFunc(keyb); nuclear@0: glutMouseFunc(mouse); nuclear@0: glutMotionFunc(motion); nuclear@0: nuclear@0: if(!init()) { nuclear@0: return 1; nuclear@0: } nuclear@0: atexit(cleanup); nuclear@0: nuclear@0: glutMainLoop(); nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: static bool init() nuclear@0: { nuclear@8: init_opengl(); nuclear@8: nuclear@0: glEnable(GL_DEPTH_TEST); nuclear@0: glEnable(GL_CULL_FACE); nuclear@0: nuclear@6: glEnable(GL_LIGHTING); nuclear@6: glEnable(GL_LIGHT0); nuclear@6: float ldir[] = {-1, 1, 2, 0}; nuclear@6: glLightfv(GL_LIGHT0, GL_POSITION, ldir); nuclear@6: nuclear@8: GObject *obj; nuclear@8: nuclear@8: Mesh *mesh = load_mesh("lucy.obj"); nuclear@8: if(!mesh) { nuclear@8: return false; nuclear@8: } nuclear@8: nuclear@8: obj = new GObject; nuclear@6: obj->add_component(new CoXForm); nuclear@6: obj->add_component(new CoPRS); nuclear@8: obj->add_component(new CoMesh); nuclear@8: gobj_co_prs(obj)->pos = Vector3(-3, 0, 0); nuclear@8: gobj_co_mesh(obj)->mesh = mesh; nuclear@6: objects.push_back(obj); nuclear@6: nuclear@6: obj = new GObject; nuclear@6: obj->add_component(new CoSphereVis); nuclear@6: obj->add_component(new CoXForm); nuclear@6: obj->add_component(new CoPRS); nuclear@8: gobj_co_prs(obj)->pos = Vector3(3, 3, 0); nuclear@8: obj->add_component(new CoRigid); nuclear@8: obj->add_component(new CoCollider); nuclear@8: gobj_co_collider(obj)->shape = new Sphere; nuclear@6: objects.push_back(obj); nuclear@8: simworld.add_object(obj); nuclear@8: nuclear@8: obj = new GObject; nuclear@8: obj->add_component(new CoXForm); nuclear@8: obj->add_component(new CoRigid); nuclear@8: obj->add_component(new CoCollider); nuclear@8: gobj_co_collider(obj)->shape = new Plane; nuclear@8: gobj_co_rigid(obj)->set_fixed(true); nuclear@8: objects.push_back(obj); nuclear@8: simworld.add_object(obj); nuclear@6: nuclear@0: return true; nuclear@0: } nuclear@0: nuclear@0: static void cleanup() nuclear@0: { nuclear@8: for(size_t i=0; iupdate(dt); nuclear@6: } nuclear@6: } nuclear@6: nuclear@0: static void display() nuclear@0: { nuclear@6: update(); nuclear@6: nuclear@0: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); nuclear@0: nuclear@6: glMatrixMode(GL_MODELVIEW); nuclear@6: glLoadIdentity(); nuclear@6: glTranslatef(0, 0, -cam_dist); nuclear@6: glRotatef(cam_phi, 1, 0, 0); nuclear@6: glRotatef(cam_theta, 0, 1, 0); nuclear@8: glTranslatef(0, -0.5, 0); nuclear@6: nuclear@6: float floor_col[] = {0.2, 0.8, 0.2, 1}; nuclear@6: glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col); nuclear@6: nuclear@6: glBegin(GL_QUADS); nuclear@6: glNormal3f(0, 1, 0); nuclear@6: glVertex3f(-10, 0, 10); nuclear@6: glVertex3f(10, 0, 10); nuclear@6: glVertex3f(10, 0, -10); nuclear@6: glVertex3f(-10, 0, -10); nuclear@6: glEnd(); nuclear@6: nuclear@8: float obj_col[] = {0.9, 0.9, 0.9, 1}; nuclear@8: glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, obj_col); nuclear@8: nuclear@6: for(size_t i=0; idraw(); nuclear@6: } nuclear@6: nuclear@0: glutSwapBuffers(); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: } nuclear@0: nuclear@0: static void idle() nuclear@0: { nuclear@0: glutPostRedisplay(); nuclear@0: } nuclear@0: nuclear@0: static void reshape(int x, int y) nuclear@0: { nuclear@0: glViewport(0, 0, x, y); nuclear@0: nuclear@2: glMatrixMode(GL_PROJECTION); nuclear@0: glLoadIdentity(); nuclear@0: gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); nuclear@0: } nuclear@0: nuclear@0: static void keyb(unsigned char key, int x, int y) nuclear@0: { nuclear@0: switch(key) { nuclear@0: case 27: nuclear@0: exit(0); nuclear@0: } nuclear@0: } nuclear@0: nuclear@6: static bool bnstate[16]; nuclear@0: static int prev_x, prev_y; nuclear@0: nuclear@0: static void mouse(int bn, int st, int x, int y) nuclear@0: { nuclear@6: bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; nuclear@6: prev_x = x; nuclear@6: prev_y = y; nuclear@0: } nuclear@0: nuclear@0: static void motion(int x, int y) nuclear@0: { nuclear@6: int dx = x - prev_x; nuclear@6: int dy = y - prev_y; nuclear@6: prev_x = x; nuclear@6: prev_y = y; nuclear@6: nuclear@6: if(bnstate[0]) { nuclear@6: cam_theta += dx * 0.5; nuclear@6: cam_phi += dy * 0.5; nuclear@6: nuclear@6: if(cam_phi < -90) cam_phi = -90; nuclear@6: if(cam_phi > 90) cam_phi = 90; nuclear@6: nuclear@6: glutPostRedisplay(); nuclear@6: } nuclear@6: if(bnstate[2]) { nuclear@6: cam_dist += dy * 0.1; nuclear@6: nuclear@6: if(cam_dist < 0.0) cam_dist = 0.0; nuclear@6: } nuclear@0: } nuclear@8: nuclear@8: static Mesh *load_mesh(const char *fname) nuclear@8: { nuclear@8: struct objfile *objf = objf_load(fname); nuclear@8: if(!objf) { nuclear@8: return 0; nuclear@8: } nuclear@8: int nverts = objf_vertex_count(objf); nuclear@8: nuclear@8: Mesh *m = new Mesh; nuclear@8: m->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, objf_vertices(objf)); nuclear@8: m->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, objf_normals(objf)); nuclear@8: m->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, objf_texcoords(objf)); nuclear@8: objf_free(objf); nuclear@8: return m; nuclear@8: }