coeng

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <vector>
6 #ifndef __APPLE__
7 #include <GL/glut.h>
8 #else
9 #include <GLUT/glut.h>
10 #endif
12 #include "gobj.h"
13 #include "co_xform.h"
14 #include "co_dbgvis.h"
15 #include "sim.h"
17 static bool init();
18 static void cleanup();
19 static void display();
20 static void idle();
21 static void reshape(int x, int y);
22 static void keyb(unsigned char key, int x, int y);
23 static void mouse(int bn, int st, int x, int y);
24 static void motion(int x, int y);
26 float cam_theta, cam_phi = 25, cam_dist = 8;
28 std::vector<GObject*> objects;
29 SimWorld simworld;
32 int main(int argc, char **argv)
33 {
34 glutInit(&argc, argv);
35 glutInitWindowSize(800, 600);
36 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
37 glutCreateWindow("component system test");
39 glutDisplayFunc(display);
40 //glutIdleFunc(idle);
41 glutReshapeFunc(reshape);
42 glutKeyboardFunc(keyb);
43 glutMouseFunc(mouse);
44 glutMotionFunc(motion);
46 if(!init()) {
47 return 1;
48 }
49 atexit(cleanup);
51 glutMainLoop();
52 return 0;
53 }
55 static bool init()
56 {
57 glEnable(GL_DEPTH_TEST);
58 glEnable(GL_CULL_FACE);
60 glEnable(GL_LIGHTING);
61 glEnable(GL_LIGHT0);
62 float ldir[] = {-1, 1, 2, 0};
63 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
65 GObject *obj = new GObject;
66 obj->add_component(new CoSphereVis);
67 obj->add_component(new CoXForm);
68 obj->add_component(new CoPRS);
69 obj->add_component(new CoRigid);
70 COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(5, 1, 0);
71 objects.push_back(obj);
73 obj = new GObject;
74 obj->add_component(new CoSphereVis);
75 obj->add_component(new CoXForm);
76 obj->add_component(new CoPRS);
77 COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(-5, 1, 0);
78 objects.push_back(obj);
80 return true;
81 }
83 static void cleanup()
84 {
85 }
87 static void update()
88 {
89 static unsigned int prev_upd;
90 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
91 float dt = (float)(msec - prev_upd) / 1000.0f;
92 prev_upd = msec;
94 for(size_t i=0; i<objects.size(); i++) {
95 objects[i]->update(dt);
96 }
97 }
99 static void display()
100 {
101 update();
103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105 glMatrixMode(GL_MODELVIEW);
106 glLoadIdentity();
107 glTranslatef(0, 0, -cam_dist);
108 glRotatef(cam_phi, 1, 0, 0);
109 glRotatef(cam_theta, 0, 1, 0);
111 float floor_col[] = {0.2, 0.8, 0.2, 1};
112 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col);
114 glBegin(GL_QUADS);
115 glNormal3f(0, 1, 0);
116 glVertex3f(-10, 0, 10);
117 glVertex3f(10, 0, 10);
118 glVertex3f(10, 0, -10);
119 glVertex3f(-10, 0, -10);
120 glEnd();
122 for(size_t i=0; i<objects.size(); i++) {
123 CoSphereVis *co = COCAST(CoSphereVis, objects[i]->get_component("spherevis"));
124 if(co) {
125 co->draw();
126 }
127 }
129 glutSwapBuffers();
130 assert(glGetError() == GL_NO_ERROR);
131 }
133 static void idle()
134 {
135 glutPostRedisplay();
136 }
138 static void reshape(int x, int y)
139 {
140 glViewport(0, 0, x, y);
142 glMatrixMode(GL_PROJECTION);
143 glLoadIdentity();
144 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
145 }
147 static void keyb(unsigned char key, int x, int y)
148 {
149 switch(key) {
150 case 27:
151 exit(0);
152 }
153 }
155 static bool bnstate[16];
156 static int prev_x, prev_y;
158 static void mouse(int bn, int st, int x, int y)
159 {
160 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
161 prev_x = x;
162 prev_y = y;
163 }
165 static void motion(int x, int y)
166 {
167 int dx = x - prev_x;
168 int dy = y - prev_y;
169 prev_x = x;
170 prev_y = y;
172 if(bnstate[0]) {
173 cam_theta += dx * 0.5;
174 cam_phi += dy * 0.5;
176 if(cam_phi < -90) cam_phi = -90;
177 if(cam_phi > 90) cam_phi = 90;
179 glutPostRedisplay();
180 }
181 if(bnstate[2]) {
182 cam_dist += dy * 0.1;
184 if(cam_dist < 0.0) cam_dist = 0.0;
185 }
186 }