coeng
diff 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 diff
1.1 --- a/src/test.cc Sat Feb 14 10:08:00 2015 +0200 1.2 +++ b/src/test.cc Sun Feb 15 05:14:20 2015 +0200 1.3 @@ -3,15 +3,20 @@ 1.4 #include <assert.h> 1.5 #include <vector> 1.6 1.7 +#include "opengl.h" 1.8 #ifndef __APPLE__ 1.9 #include <GL/glut.h> 1.10 #else 1.11 #include <GLUT/glut.h> 1.12 #endif 1.13 1.14 +#include "objfile.h" 1.15 +#include "mesh.h" 1.16 #include "gobj.h" 1.17 #include "co_xform.h" 1.18 #include "co_dbgvis.h" 1.19 +#include "co_mesh.h" 1.20 +#include "co_phys.h" 1.21 #include "sim.h" 1.22 1.23 static bool init(); 1.24 @@ -22,8 +27,9 @@ 1.25 static void keyb(unsigned char key, int x, int y); 1.26 static void mouse(int bn, int st, int x, int y); 1.27 static void motion(int x, int y); 1.28 +static Mesh *load_mesh(const char *fname); 1.29 1.30 -float cam_theta, cam_phi = 25, cam_dist = 8; 1.31 +float cam_theta, cam_phi = 25, cam_dist = 10; 1.32 1.33 std::vector<GObject*> objects; 1.34 SimWorld simworld; 1.35 @@ -54,6 +60,8 @@ 1.36 1.37 static bool init() 1.38 { 1.39 + init_opengl(); 1.40 + 1.41 glEnable(GL_DEPTH_TEST); 1.42 glEnable(GL_CULL_FACE); 1.43 1.44 @@ -62,27 +70,51 @@ 1.45 float ldir[] = {-1, 1, 2, 0}; 1.46 glLightfv(GL_LIGHT0, GL_POSITION, ldir); 1.47 1.48 - GObject *obj = new GObject; 1.49 - obj->add_component(new CoSphereVis); 1.50 + GObject *obj; 1.51 + 1.52 + Mesh *mesh = load_mesh("lucy.obj"); 1.53 + if(!mesh) { 1.54 + return false; 1.55 + } 1.56 + 1.57 + obj = new GObject; 1.58 obj->add_component(new CoXForm); 1.59 obj->add_component(new CoPRS); 1.60 - obj->add_component(new CoRigid); 1.61 - COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(5, 2, 0); 1.62 + obj->add_component(new CoMesh); 1.63 + gobj_co_prs(obj)->pos = Vector3(-3, 0, 0); 1.64 + gobj_co_mesh(obj)->mesh = mesh; 1.65 objects.push_back(obj); 1.66 - simworld.add_object(obj); 1.67 1.68 obj = new GObject; 1.69 obj->add_component(new CoSphereVis); 1.70 obj->add_component(new CoXForm); 1.71 obj->add_component(new CoPRS); 1.72 - COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(-5, 1, 0); 1.73 + gobj_co_prs(obj)->pos = Vector3(3, 3, 0); 1.74 + obj->add_component(new CoRigid); 1.75 + obj->add_component(new CoCollider); 1.76 + gobj_co_collider(obj)->shape = new Sphere; 1.77 objects.push_back(obj); 1.78 + simworld.add_object(obj); 1.79 + 1.80 + obj = new GObject; 1.81 + obj->add_component(new CoXForm); 1.82 + obj->add_component(new CoPRS); 1.83 + obj->add_component(new CoRigid); 1.84 + obj->add_component(new CoCollider); 1.85 + gobj_co_collider(obj)->shape = new Plane; 1.86 + gobj_co_rigid(obj)->set_fixed(true); 1.87 + objects.push_back(obj); 1.88 + simworld.add_object(obj); 1.89 1.90 return true; 1.91 } 1.92 1.93 static void cleanup() 1.94 { 1.95 + for(size_t i=0; i<objects.size(); i++) { 1.96 + delete objects[i]; 1.97 + } 1.98 + objects.clear(); 1.99 } 1.100 1.101 static void update() 1.102 @@ -108,6 +140,7 @@ 1.103 glTranslatef(0, 0, -cam_dist); 1.104 glRotatef(cam_phi, 1, 0, 0); 1.105 glRotatef(cam_theta, 0, 1, 0); 1.106 + glTranslatef(0, -0.5, 0); 1.107 1.108 float floor_col[] = {0.2, 0.8, 0.2, 1}; 1.109 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col); 1.110 @@ -120,11 +153,11 @@ 1.111 glVertex3f(-10, 0, -10); 1.112 glEnd(); 1.113 1.114 + float obj_col[] = {0.9, 0.9, 0.9, 1}; 1.115 + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, obj_col); 1.116 + 1.117 for(size_t i=0; i<objects.size(); i++) { 1.118 - CoSphereVis *co = COCAST(CoSphereVis, objects[i]->get_component("spherevis")); 1.119 - if(co) { 1.120 - co->draw(); 1.121 - } 1.122 + objects[i]->draw(); 1.123 } 1.124 1.125 glutSwapBuffers(); 1.126 @@ -185,3 +218,19 @@ 1.127 if(cam_dist < 0.0) cam_dist = 0.0; 1.128 } 1.129 } 1.130 + 1.131 +static Mesh *load_mesh(const char *fname) 1.132 +{ 1.133 + struct objfile *objf = objf_load(fname); 1.134 + if(!objf) { 1.135 + return 0; 1.136 + } 1.137 + int nverts = objf_vertex_count(objf); 1.138 + 1.139 + Mesh *m = new Mesh; 1.140 + m->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, objf_vertices(objf)); 1.141 + m->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, objf_normals(objf)); 1.142 + m->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, objf_texcoords(objf)); 1.143 + objf_free(objf); 1.144 + return m; 1.145 +}