coeng

view src/test.cc @ 8:8cce82794f90

seems to work nicely
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Feb 2015 05:14:20 +0200
parents af24cfbdf9b6
children 305c8f6abef1
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 CoPRS);
102 obj->add_component(new CoRigid);
103 obj->add_component(new CoCollider);
104 gobj_co_collider(obj)->shape = new Plane;
105 gobj_co_rigid(obj)->set_fixed(true);
106 objects.push_back(obj);
107 simworld.add_object(obj);
109 return true;
110 }
112 static void cleanup()
113 {
114 for(size_t i=0; i<objects.size(); i++) {
115 delete objects[i];
116 }
117 objects.clear();
118 }
120 static void update()
121 {
122 static unsigned int prev_upd;
123 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
124 float dt = (float)(msec - prev_upd) / 1000.0f;
125 prev_upd = msec;
127 for(size_t i=0; i<objects.size(); i++) {
128 objects[i]->update(dt);
129 }
130 }
132 static void display()
133 {
134 update();
136 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
138 glMatrixMode(GL_MODELVIEW);
139 glLoadIdentity();
140 glTranslatef(0, 0, -cam_dist);
141 glRotatef(cam_phi, 1, 0, 0);
142 glRotatef(cam_theta, 0, 1, 0);
143 glTranslatef(0, -0.5, 0);
145 float floor_col[] = {0.2, 0.8, 0.2, 1};
146 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col);
148 glBegin(GL_QUADS);
149 glNormal3f(0, 1, 0);
150 glVertex3f(-10, 0, 10);
151 glVertex3f(10, 0, 10);
152 glVertex3f(10, 0, -10);
153 glVertex3f(-10, 0, -10);
154 glEnd();
156 float obj_col[] = {0.9, 0.9, 0.9, 1};
157 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, obj_col);
159 for(size_t i=0; i<objects.size(); i++) {
160 objects[i]->draw();
161 }
163 glutSwapBuffers();
164 assert(glGetError() == GL_NO_ERROR);
165 }
167 static void idle()
168 {
169 glutPostRedisplay();
170 }
172 static void reshape(int x, int y)
173 {
174 glViewport(0, 0, x, y);
176 glMatrixMode(GL_PROJECTION);
177 glLoadIdentity();
178 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
179 }
181 static void keyb(unsigned char key, int x, int y)
182 {
183 switch(key) {
184 case 27:
185 exit(0);
186 }
187 }
189 static bool bnstate[16];
190 static int prev_x, prev_y;
192 static void mouse(int bn, int st, int x, int y)
193 {
194 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
195 prev_x = x;
196 prev_y = y;
197 }
199 static void motion(int x, int y)
200 {
201 int dx = x - prev_x;
202 int dy = y - prev_y;
203 prev_x = x;
204 prev_y = y;
206 if(bnstate[0]) {
207 cam_theta += dx * 0.5;
208 cam_phi += dy * 0.5;
210 if(cam_phi < -90) cam_phi = -90;
211 if(cam_phi > 90) cam_phi = 90;
213 glutPostRedisplay();
214 }
215 if(bnstate[2]) {
216 cam_dist += dy * 0.1;
218 if(cam_dist < 0.0) cam_dist = 0.0;
219 }
220 }
222 static Mesh *load_mesh(const char *fname)
223 {
224 struct objfile *objf = objf_load(fname);
225 if(!objf) {
226 return 0;
227 }
228 int nverts = objf_vertex_count(objf);
230 Mesh *m = new Mesh;
231 m->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, objf_vertices(objf));
232 m->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, objf_normals(objf));
233 m->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, objf_texcoords(objf));
234 objf_free(objf);
235 return m;
236 }