coeng
diff src/test.cc @ 6:2f872a179914
first component test:
- prs, xform, physics components with dependencies
- topological sort of components to update them in the correct order
- debug visualization component
todo: remove draw() from components, doesn't make sense
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 14 Feb 2015 07:27:12 +0200 |
parents | 4a1c9597f4d3 |
children | af24cfbdf9b6 |
line diff
1.1 --- a/src/test.cc Sat Feb 14 01:35:42 2015 +0200 1.2 +++ b/src/test.cc Sat Feb 14 07:27:12 2015 +0200 1.3 @@ -1,6 +1,7 @@ 1.4 #include <stdio.h> 1.5 #include <stdlib.h> 1.6 #include <assert.h> 1.7 +#include <vector> 1.8 1.9 #ifndef __APPLE__ 1.10 #include <GL/glut.h> 1.11 @@ -8,6 +9,11 @@ 1.12 #include <GLUT/glut.h> 1.13 #endif 1.14 1.15 +#include "gobj.h" 1.16 +#include "co_xform.h" 1.17 +#include "co_dbgvis.h" 1.18 +#include "sim.h" 1.19 + 1.20 static bool init(); 1.21 static void cleanup(); 1.22 static void display(); 1.23 @@ -17,6 +23,11 @@ 1.24 static void mouse(int bn, int st, int x, int y); 1.25 static void motion(int x, int y); 1.26 1.27 +float cam_theta, cam_phi = 25, cam_dist = 8; 1.28 + 1.29 +std::vector<GObject*> objects; 1.30 +SimWorld simworld; 1.31 + 1.32 1.33 int main(int argc, char **argv) 1.34 { 1.35 @@ -26,7 +37,7 @@ 1.36 glutCreateWindow("component system test"); 1.37 1.38 glutDisplayFunc(display); 1.39 - glutIdleFunc(idle); 1.40 + //glutIdleFunc(idle); 1.41 glutReshapeFunc(reshape); 1.42 glutKeyboardFunc(keyb); 1.43 glutMouseFunc(mouse); 1.44 @@ -46,6 +57,26 @@ 1.45 glEnable(GL_DEPTH_TEST); 1.46 glEnable(GL_CULL_FACE); 1.47 1.48 + glEnable(GL_LIGHTING); 1.49 + glEnable(GL_LIGHT0); 1.50 + float ldir[] = {-1, 1, 2, 0}; 1.51 + glLightfv(GL_LIGHT0, GL_POSITION, ldir); 1.52 + 1.53 + GObject *obj = new GObject; 1.54 + obj->add_component(new CoSphereVis); 1.55 + obj->add_component(new CoXForm); 1.56 + obj->add_component(new CoPRS); 1.57 + obj->add_component(new CoRigid); 1.58 + COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(5, 1, 0); 1.59 + objects.push_back(obj); 1.60 + 1.61 + obj = new GObject; 1.62 + obj->add_component(new CoSphereVis); 1.63 + obj->add_component(new CoXForm); 1.64 + obj->add_component(new CoPRS); 1.65 + COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(-5, 1, 0); 1.66 + objects.push_back(obj); 1.67 + 1.68 return true; 1.69 } 1.70 1.71 @@ -53,10 +84,48 @@ 1.72 { 1.73 } 1.74 1.75 +static void update() 1.76 +{ 1.77 + static unsigned int prev_upd; 1.78 + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); 1.79 + float dt = (float)(msec - prev_upd) / 1000.0f; 1.80 + prev_upd = msec; 1.81 + 1.82 + for(size_t i=0; i<objects.size(); i++) { 1.83 + objects[i]->update(dt); 1.84 + } 1.85 +} 1.86 + 1.87 static void display() 1.88 { 1.89 + update(); 1.90 + 1.91 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.92 1.93 + glMatrixMode(GL_MODELVIEW); 1.94 + glLoadIdentity(); 1.95 + glTranslatef(0, 0, -cam_dist); 1.96 + glRotatef(cam_phi, 1, 0, 0); 1.97 + glRotatef(cam_theta, 0, 1, 0); 1.98 + 1.99 + float floor_col[] = {0.2, 0.8, 0.2, 1}; 1.100 + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col); 1.101 + 1.102 + glBegin(GL_QUADS); 1.103 + glNormal3f(0, 1, 0); 1.104 + glVertex3f(-10, 0, 10); 1.105 + glVertex3f(10, 0, 10); 1.106 + glVertex3f(10, 0, -10); 1.107 + glVertex3f(-10, 0, -10); 1.108 + glEnd(); 1.109 + 1.110 + for(size_t i=0; i<objects.size(); i++) { 1.111 + CoSphereVis *co = COCAST(CoSphereVis, objects[i]->get_component("spherevis")); 1.112 + if(co) { 1.113 + co->draw(); 1.114 + } 1.115 + } 1.116 + 1.117 glutSwapBuffers(); 1.118 assert(glGetError() == GL_NO_ERROR); 1.119 } 1.120 @@ -83,13 +152,35 @@ 1.121 } 1.122 } 1.123 1.124 -static int bnstate[16]; 1.125 +static bool bnstate[16]; 1.126 static int prev_x, prev_y; 1.127 1.128 static void mouse(int bn, int st, int x, int y) 1.129 { 1.130 + bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; 1.131 + prev_x = x; 1.132 + prev_y = y; 1.133 } 1.134 1.135 static void motion(int x, int y) 1.136 { 1.137 + int dx = x - prev_x; 1.138 + int dy = y - prev_y; 1.139 + prev_x = x; 1.140 + prev_y = y; 1.141 + 1.142 + if(bnstate[0]) { 1.143 + cam_theta += dx * 0.5; 1.144 + cam_phi += dy * 0.5; 1.145 + 1.146 + if(cam_phi < -90) cam_phi = -90; 1.147 + if(cam_phi > 90) cam_phi = 90; 1.148 + 1.149 + glutPostRedisplay(); 1.150 + } 1.151 + if(bnstate[2]) { 1.152 + cam_dist += dy * 0.1; 1.153 + 1.154 + if(cam_dist < 0.0) cam_dist = 0.0; 1.155 + } 1.156 }