coeng

view src/test.cc @ 7:af24cfbdf9b6

adding collision detection
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Feb 2015 10:08:00 +0200
parents 2f872a179914
children 8cce82794f90
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, 2, 0);
71 objects.push_back(obj);
72 simworld.add_object(obj);
74 obj = new GObject;
75 obj->add_component(new CoSphereVis);
76 obj->add_component(new CoXForm);
77 obj->add_component(new CoPRS);
78 COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(-5, 1, 0);
79 objects.push_back(obj);
81 return true;
82 }
84 static void cleanup()
85 {
86 }
88 static void update()
89 {
90 static unsigned int prev_upd;
91 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
92 float dt = (float)(msec - prev_upd) / 1000.0f;
93 prev_upd = msec;
95 for(size_t i=0; i<objects.size(); i++) {
96 objects[i]->update(dt);
97 }
98 }
100 static void display()
101 {
102 update();
104 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106 glMatrixMode(GL_MODELVIEW);
107 glLoadIdentity();
108 glTranslatef(0, 0, -cam_dist);
109 glRotatef(cam_phi, 1, 0, 0);
110 glRotatef(cam_theta, 0, 1, 0);
112 float floor_col[] = {0.2, 0.8, 0.2, 1};
113 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col);
115 glBegin(GL_QUADS);
116 glNormal3f(0, 1, 0);
117 glVertex3f(-10, 0, 10);
118 glVertex3f(10, 0, 10);
119 glVertex3f(10, 0, -10);
120 glVertex3f(-10, 0, -10);
121 glEnd();
123 for(size_t i=0; i<objects.size(); i++) {
124 CoSphereVis *co = COCAST(CoSphereVis, objects[i]->get_component("spherevis"));
125 if(co) {
126 co->draw();
127 }
128 }
130 glutSwapBuffers();
131 assert(glGetError() == GL_NO_ERROR);
132 }
134 static void idle()
135 {
136 glutPostRedisplay();
137 }
139 static void reshape(int x, int y)
140 {
141 glViewport(0, 0, x, y);
143 glMatrixMode(GL_PROJECTION);
144 glLoadIdentity();
145 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
146 }
148 static void keyb(unsigned char key, int x, int y)
149 {
150 switch(key) {
151 case 27:
152 exit(0);
153 }
154 }
156 static bool bnstate[16];
157 static int prev_x, prev_y;
159 static void mouse(int bn, int st, int x, int y)
160 {
161 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
162 prev_x = x;
163 prev_y = y;
164 }
166 static void motion(int x, int y)
167 {
168 int dx = x - prev_x;
169 int dy = y - prev_y;
170 prev_x = x;
171 prev_y = y;
173 if(bnstate[0]) {
174 cam_theta += dx * 0.5;
175 cam_phi += dy * 0.5;
177 if(cam_phi < -90) cam_phi = -90;
178 if(cam_phi > 90) cam_phi = 90;
180 glutPostRedisplay();
181 }
182 if(bnstate[2]) {
183 cam_dist += dy * 0.1;
185 if(cam_dist < 0.0) cam_dist = 0.0;
186 }
187 }