aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api/phys/bphysmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/lua_api/phys/bphysmodel.cpp')
-rw-r--r--src/shared/lua_api/phys/bphysmodel.cpp161
1 files changed, 108 insertions, 53 deletions
diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp
index e9fecbf..55039ba 100644
--- a/src/shared/lua_api/phys/bphysmodel.cpp
+++ b/src/shared/lua_api/phys/bphysmodel.cpp
@@ -19,39 +19,27 @@ extern "C" {
extern btDiscreteDynamicsWorld* World;
extern std::list<btRigidBody*> Objects;
-//newbphysmodel("graphicfile","physicfile",mass,[,{position}][,{lookat}])
-static int newbphysmodel(lua_State* L){
- printf("Creating bphysmodel\n");
+//"physicfile",mass[,position][,lookat] :: ud_rigidbody
+void makebphysmodel(lua_State *L){
+ printf("making bphysmodel\n");
int nargs = lua_gettop(L);
double lx,ly,lz;
double x,y,z;
- if(nargs > 4){
- //"graphicsfile","physicsfile",{position},{lookat}
+ if(nargs > 3){
+ //"physicsfile",{position},{lookat}
popvector3d(L,&lx,&ly,&lz);
}
- if(nargs > 3){
- //"graphicsfile","physicsfile",{position}
+ if(nargs > 2){
+ //"physicsfile",{position}
popvector3d(L,&x,&y,&z);
}
- //"graphicsfile","physicsfile"
+ printf("got arguments for bphysmodel\n");
+ //"physicsfile"
double mass = lua_tonumber(L,-1);
const char *ppath = lua_tostring(L,-2);
- //const char *gpath = lua_tostring(L,-3);
- lua_pop(L,3);
-
- //ISceneManager *smgr = device->getSceneManager();
+ lua_pop(L,2);
- //printf("bphysnode, creating the scene node\n");
-
- ////Create the scene node
- //IMesh *gmesh = smgr->getMesh(gpath);
- //ISceneNode *node = smgr->addMeshSceneNode(gmesh,0,-1,vector3df(x,y,z));
-
- printf("bphysnode, createing the physics body\n");
- //Create the physics body
- //IMesh *pmesh = smgr->getMesh(ppath);
- //printf("We have %d mesh buffers\n",pmesh->getMeshBufferCount());
tinyobj_attrib_t attrib;
tinyobj_shape_t *shapes = NULL;
size_t meshcount;
@@ -62,43 +50,59 @@ static int newbphysmodel(lua_State* L){
FILE *objfile = fopen(ppath,"rb");
fseek(objfile,0,SEEK_END);
data_len = ftell(objfile);
+ printf("model data is %d long\n",(int)data_len);
fseek(objfile,0,SEEK_SET);
char *objdata = (char*)malloc(sizeof(char)*data_len);
fread(objdata, sizeof(char), data_len, objfile);
fclose(objfile);
- int err = tinyobj_parse_obj(&attrib, &shapes, &meshcount, &materials, &num_materials, objdata, data_len, 0);
+ printf("About to tinyobj_parse_obj\n");
+ int err = tinyobj_parse_obj(&attrib, &shapes, &meshcount, &materials, &num_materials, objdata, data_len, TINYOBJ_FLAG_TRIANGULATE);
if(err != TINYOBJ_SUCCESS){
printf("Tinyobj failed to load model:%s\n",ppath);
}
//u32 meshcount = pmesh->getMeshBufferCount();
btTriangleMesh* trimesh = new btTriangleMesh();
- size_t numverts = attrib.num_face_num_verts;
- //size_t stride = 9; //9 = 3 position floats + 3 normal floats + 3 color floats
- size_t face_offset = 0;
- for(size_t i = 0; i < numverts; i++){
- for(size_t j = 0; j < (size_t)attrib.face_num_verts[i] / 3; j++){
- float v[3][3]; //this tri
- tinyobj_vertex_index_t idx0, idx1, idx2;
- idx0 = attrib.faces[face_offset + 3 * j + 0];
- idx1 = attrib.faces[face_offset + 3 * j + 1];
- idx2 = attrib.faces[face_offset + 3 * j + 2];
- for(short k = 0; k < 3; k++){
- int f0, f1, f2;
- f0 = idx0.v_idx;
- f1 = idx1.v_idx;
- f2 = idx2.v_idx;
- v[0][k] = attrib.vertices[3 * (size_t)f0 + k];
- v[1][k] = attrib.vertices[3 * (size_t)f1 + k];
- v[2][k] = attrib.vertices[3 * (size_t)f2 + k];
- }
- btVector3 b1,b2,b3;
- b1 = btVector3(v[0][0],v[0][1],v[0][2]);
- b2 = btVector3(v[1][0],v[1][1],v[1][2]);
- b3 = btVector3(v[2][0],v[2][1],v[2][2]);
- trimesh->addTriangle(b1,b2,b3);
- }
- face_offset += (size_t)attrib.face_num_verts[i];
+ for(size_t i = 0; i < attrib.num_vertices; i++){
+ float *vs = attrib.vertices + (sizeof(float)*3*i);//3 floats per vertex
+ float v1 = vs[0];
+ float v2 = vs[1];
+ float v3 = vs[2];
+ trimesh->findOrAddVertex(btVector3(v1,v2,v3),true);
}
+ for(size_t i = 0; i < attrib.num_faces; i+= 3){
+ tinyobj_vertex_index_t i1,i2,i3;
+ i1 = attrib.faces[i];
+ i2 = attrib.faces[i+1];
+ i3 = attrib.faces[i+2];
+ trimesh->addTriangleIndices(i1.v_idx,i2.v_idx,i3.v_idx);
+ }
+ //size_t numverts = attrib.num_face_num_verts;
+ ////size_t stride = 9; //9 = 3 position floats + 3 normal floats + 3 color floats
+ //size_t face_offset = 0;
+ //for(size_t i = 0; i < numverts; i++){
+ //for(size_t j = 0; j < (size_t)attrib.face_num_verts[i] / 3; j++){
+ //float v[3][3]; //this tri
+ //tinyobj_vertex_index_t idx0, idx1, idx2;
+ //idx0 = attrib.faces[face_offset + 3 * j + 0];
+ //idx1 = attrib.faces[face_offset + 3 * j + 1];
+ //idx2 = attrib.faces[face_offset + 3 * j + 2];
+ //for(short k = 0; k < 3; k++){
+ //int f0, f1, f2;
+ //f0 = idx0.v_idx;
+ //f1 = idx1.v_idx;
+ //f2 = idx2.v_idx;
+ //v[0][k] = attrib.vertices[3 * (size_t)f0 + k];
+ //v[1][k] = attrib.vertices[3 * (size_t)f1 + k];
+ //v[2][k] = attrib.vertices[3 * (size_t)f2 + k];
+ //}
+ //btVector3 b1,b2,b3;
+ //b1 = btVector3(v[0][0],v[0][1],v[0][2]);
+ //b2 = btVector3(v[1][0],v[1][1],v[1][2]);
+ //b3 = btVector3(v[2][0],v[2][1],v[2][2]);
+ //trimesh->addTriangle(b1,b2,b3);
+ //}
+ //face_offset += (size_t)attrib.face_num_verts[i];
+ //}
printf("Done building trimesh\n");
btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true);
btTransform tr;
@@ -109,15 +113,66 @@ static int newbphysmodel(lua_State* L){
btVector3 li;
shape->calculateLocalInertia(mass, li);
btRigidBody *rb = new btRigidBody(mass,ms,shape,li);
- //rb->setUserPointer((void*) node);
World->addRigidBody(rb);
Objects.push_back(rb);
printf("Rigid body finished\n");
+ lua_pushlightuserdata(L,rb);//ud_rigidbody
+}
+
+//newbphysmodel("graphicfile","physicfile",mass[,position][,lookat]) :: ud_rigidbody
+static int newbphysmodel(lua_State* L){
+ printf("Creating bphysmodel\n");
+ int nargs = lua_gettop(L);
+ double lx,ly,lz;
+ double x,y,z;
+ if(nargs > 4){
+ //"graphicsfile","physicsfile",{position},{lookat}
+ popvector3d(L,&lx,&ly,&lz);
+ }else{
+ lx = 1; ly = 1; lz = 1;
+ }
+ if(nargs > 3){
+ //"graphicsfile","physicsfile",{position}
+ popvector3d(L,&x,&y,&z);
+ }else{
+ x = 0; y = 0; z = 0;
+ }
+ //"graphicsfile","physicsfile"
+
+ double mass = lua_tonumber(L,-1);
+ const char *ppath = lua_tostring(L,-2);
+ //const char *gpath = lua_tostring(L,-3);
+ lua_pop(L,3);//
+
+ lua_pushstring(L,ppath);//"phys_path"
+ lua_pushnumber(L,mass);//"phys_path",double_mass
+ pushvector3d(L,x,y,z);//"phys_path",double_mass,{position}
+ pushvector3d(L,lx,ly,lz);//"phys_path",double_mass,{position},{lookat}
+ printf("Starting makeing bphysmodel\n");
+ makebphysmodel(L);//ud_rigidbody
+ printf("Done making bphysmodel\n");
+ btRigidBody *rb = (btRigidBody*)lua_touserdata(L,-1);
+ printf("bphysnode, createing the physics body\n");
//Create the lua representation
- lua_newtable(L);
- lua_pushlightuserdata(L,rb);
- lua_setfield(L,-2,"rigidbody");
+ lua_newtable(L);//{}
+ lua_pushlightuserdata(L,rb);//{},ud_rigidbody
+ lua_setfield(L,-2,"collider");//{collider=ud_rigidbody}
+ lua_pushstring(L,"rigidbody");//{collider=ud_rigidbody},"rigidbody"
+ lua_setfield(L,-2,"type");//{rb}
+
+ printf("Added collider to lua rep.\n");
+
+ //Add it to the global list of colliders
+ lua_getglobal(L,"phys");//{rb},{phys}
+ lua_getfield(L,-1,"colliders");//{rb},{phys},{phys.colliders}
+ lua_pushlightuserdata(L,rb);//{rb},{phys},{phys.colliders},ud_collider
+ lua_pushvalue(L,-4);//{rb},{phys},{phys.colliders},ud_collider,{rb}
+ lua_settable(L,-3);//{rb},{phys},{phys.colliders}
+ lua_pop(L,2);//{rb}
+
+ printf("Added collider to phys.colliders\n");
+
//lua_pushlightuserdata(L,node);
//lua_setfield(L,-2,"node");
luaL_getmetatable(L,"phys.physmodel");