nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@6: #include nuclear@0: nuclear@0: #ifndef __APPLE__ nuclear@0: #include nuclear@0: #else nuclear@0: #include nuclear@0: #endif nuclear@0: nuclear@6: #include "gobj.h" nuclear@6: #include "co_xform.h" nuclear@6: #include "co_dbgvis.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@0: nuclear@6: float cam_theta, cam_phi = 25, cam_dist = 8; 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@6: //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@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@6: GObject *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@6: obj->add_component(new CoRigid); nuclear@6: COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(5, 1, 0); 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@6: COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(-5, 1, 0); nuclear@6: objects.push_back(obj); nuclear@6: nuclear@0: return true; nuclear@0: } nuclear@0: nuclear@0: static void cleanup() nuclear@0: { nuclear@0: } nuclear@0: nuclear@6: static void update() nuclear@6: { nuclear@6: static unsigned int prev_upd; nuclear@6: unsigned int msec = glutGet(GLUT_ELAPSED_TIME); nuclear@6: float dt = (float)(msec - prev_upd) / 1000.0f; nuclear@6: prev_upd = msec; nuclear@6: nuclear@6: 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@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@6: for(size_t i=0; iget_component("spherevis")); nuclear@6: if(co) { nuclear@6: co->draw(); nuclear@6: } 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: }