coeng

view src/test.cc @ 10:305c8f6abef1

the plane doesn't need a PRS really
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Feb 2015 05:25:04 +0200
parents 8cce82794f90
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <vector>
6 #include "opengl.h"
7 #ifndef __APPLE__
8 #include <GL/glut.h>
9 #else
10 #include <GLUT/glut.h>
11 #endif
13 #include "objfile.h"
14 #include "mesh.h"
15 #include "gobj.h"
16 #include "co_xform.h"
17 #include "co_dbgvis.h"
18 #include "co_mesh.h"
19 #include "co_phys.h"
20 #include "sim.h"
22 static bool init();
23 static void cleanup();
24 static void display();
25 static void idle();
26 static void reshape(int x, int y);
27 static void keyb(unsigned char key, int x, int y);
28 static void mouse(int bn, int st, int x, int y);
29 static void motion(int x, int y);
30 static Mesh *load_mesh(const char *fname);
32 float cam_theta, cam_phi = 25, cam_dist = 10;
34 std::vector<GObject*> objects;
35 SimWorld simworld;
38 int main(int argc, char **argv)
39 {
40 glutInit(&argc, argv);
41 glutInitWindowSize(800, 600);
42 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
43 glutCreateWindow("component system test");
45 glutDisplayFunc(display);
46 glutIdleFunc(idle);
47 glutReshapeFunc(reshape);
48 glutKeyboardFunc(keyb);
49 glutMouseFunc(mouse);
50 glutMotionFunc(motion);
52 if(!init()) {
53 return 1;
54 }
55 atexit(cleanup);
57 glutMainLoop();
58 return 0;
59 }
61 static bool init()
62 {
63 init_opengl();
65 glEnable(GL_DEPTH_TEST);
66 glEnable(GL_CULL_FACE);
68 glEnable(GL_LIGHTING);
69 glEnable(GL_LIGHT0);
70 float ldir[] = {-1, 1, 2, 0};
71 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
73 GObject *obj;
75 Mesh *mesh = load_mesh("lucy.obj");
76 if(!mesh) {
77 return false;
78 }
80 obj = new GObject;
81 obj->add_component(new CoXForm);
82 obj->add_component(new CoPRS);
83 obj->add_component(new CoMesh);
84 gobj_co_prs(obj)->pos = Vector3(-3, 0, 0);
85 gobj_co_mesh(obj)->mesh = mesh;
86 objects.push_back(obj);
88 obj = new GObject;
89 obj->add_component(new CoSphereVis);
90 obj->add_component(new CoXForm);
91 obj->add_component(new CoPRS);
92 gobj_co_prs(obj)->pos = Vector3(3, 3, 0);
93 obj->add_component(new CoRigid);
94 obj->add_component(new CoCollider);
95 gobj_co_collider(obj)->shape = new Sphere;
96 objects.push_back(obj);
97 simworld.add_object(obj);
99 obj = new GObject;
100 obj->add_component(new CoXForm);
101 obj->add_component(new CoRigid);
102 obj->add_component(new CoCollider);
103 gobj_co_collider(obj)->shape = new Plane;
104 gobj_co_rigid(obj)->set_fixed(true);
105 objects.push_back(obj);
106 simworld.add_object(obj);
108 return true;
109 }
111 static void cleanup()
112 {
113 for(size_t i=0; i<objects.size(); i++) {
114 delete objects[i];
115 }
116 objects.clear();
117 }
119 static void update()
120 {
121 static unsigned int prev_upd;
122 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
123 float dt = (float)(msec - prev_upd) / 1000.0f;
124 prev_upd = msec;
126 for(size_t i=0; i<objects.size(); i++) {
127 objects[i]->update(dt);
128 }
129 }
131 static void display()
132 {
133 update();
135 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
137 glMatrixMode(GL_MODELVIEW);
138 glLoadIdentity();
139 glTranslatef(0, 0, -cam_dist);
140 glRotatef(cam_phi, 1, 0, 0);
141 glRotatef(cam_theta, 0, 1, 0);
142 glTranslatef(0, -0.5, 0);
144 float floor_col[] = {0.2, 0.8, 0.2, 1};
145 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col);
147 glBegin(GL_QUADS);
148 glNormal3f(0, 1, 0);
149 glVertex3f(-10, 0, 10);
150 glVertex3f(10, 0, 10);
151 glVertex3f(10, 0, -10);
152 glVertex3f(-10, 0, -10);
153 glEnd();
155 float obj_col[] = {0.9, 0.9, 0.9, 1};
156 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, obj_col);
158 for(size_t i=0; i<objects.size(); i++) {
159 objects[i]->draw();
160 }
162 glutSwapBuffers();
163 assert(glGetError() == GL_NO_ERROR);
164 }
166 static void idle()
167 {
168 glutPostRedisplay();
169 }
171 static void reshape(int x, int y)
172 {
173 glViewport(0, 0, x, y);
175 glMatrixMode(GL_PROJECTION);
176 glLoadIdentity();
177 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
178 }
180 static void keyb(unsigned char key, int x, int y)
181 {
182 switch(key) {
183 case 27:
184 exit(0);
185 }
186 }
188 static bool bnstate[16];
189 static int prev_x, prev_y;
191 static void mouse(int bn, int st, int x, int y)
192 {
193 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
194 prev_x = x;
195 prev_y = y;
196 }
198 static void motion(int x, int y)
199 {
200 int dx = x - prev_x;
201 int dy = y - prev_y;
202 prev_x = x;
203 prev_y = y;
205 if(bnstate[0]) {
206 cam_theta += dx * 0.5;
207 cam_phi += dy * 0.5;
209 if(cam_phi < -90) cam_phi = -90;
210 if(cam_phi > 90) cam_phi = 90;
212 glutPostRedisplay();
213 }
214 if(bnstate[2]) {
215 cam_dist += dy * 0.1;
217 if(cam_dist < 0.0) cam_dist = 0.0;
218 }
219 }
221 static Mesh *load_mesh(const char *fname)
222 {
223 struct objfile *objf = objf_load(fname);
224 if(!objf) {
225 return 0;
226 }
227 int nverts = objf_vertex_count(objf);
229 Mesh *m = new Mesh;
230 m->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, objf_vertices(objf));
231 m->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, objf_normals(objf));
232 m->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, objf_texcoords(objf));
233 objf_free(objf);
234 return m;
235 }