aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlexander Pickering <alex@cogarr.net>2018-11-01 13:53:16 -0400
committerAlexander Pickering <alex@cogarr.net>2018-11-01 13:53:16 -0400
commit112517494847f0c86f58544cbf4c35c9b7712ab1 (patch)
tree115d9ae3da8028e67e498ed8c587d14972b7699b /src
parent77b26fba6e78ac4af2b28b3bbb4646dea5682ed1 (diff)
downloadbrokengine-112517494847f0c86f58544cbf4c35c9b7712ab1.tar.gz
brokengine-112517494847f0c86f58544cbf4c35c9b7712ab1.tar.bz2
brokengine-112517494847f0c86f58544cbf4c35c9b7712ab1.zip
Refactored code and added library
Physics code for models now lives in the shared directory. To get file loading without irrlicht, a single-file header library (lib/tinyobjloader-c) was added. Metatables for generics and physics generics have also been seperated out.
Diffstat (limited to 'src')
-rw-r--r--src/client/lua_api/load_cphys.cpp (renamed from src/client/lua_api/load_phys.cpp)4
-rw-r--r--src/client/lua_api/load_cphys.hpp (renamed from src/client/lua_api/load_phys.hpp)0
-rw-r--r--src/client/lua_api/phys/bphysgeneric.hpp28
-rw-r--r--src/client/lua_api/phys/cbphysbox.cpp13
-rw-r--r--src/client/lua_api/phys/cbphysmodel.cpp (renamed from src/client/lua_api/phys/bphysmodel.cpp)7
-rw-r--r--src/client/lua_api/phys/cbphysmodel.hpp (renamed from src/client/lua_api/phys/bphysmodel.hpp)2
-rw-r--r--src/client/lua_api/scene/igeneric.cpp9
-rw-r--r--src/client/lua_api/scene/igeneric.hpp9
-rw-r--r--src/client/main.cpp2
-rw-r--r--src/shared/lua_api/load_phys.cpp6
-rw-r--r--src/shared/lua_api/phys/bhingeconstraint.cpp50
-rw-r--r--src/shared/lua_api/phys/bhingeconstraint.hpp2
-rw-r--r--src/shared/lua_api/phys/bphysbuffer.cpp (renamed from src/client/lua_api/phys/bphysbuffer.cpp)0
-rw-r--r--src/shared/lua_api/phys/bphysbuffer.hpp (renamed from src/client/lua_api/phys/bphysbuffer.hpp)0
-rw-r--r--src/shared/lua_api/phys/bphysgeneric.cpp (renamed from src/client/lua_api/phys/bphysgeneric.cpp)12
-rw-r--r--src/shared/lua_api/phys/bphysgeneric.hpp18
-rw-r--r--src/shared/lua_api/phys/bphysmodel.cpp153
-rw-r--r--src/shared/lua_api/phys/bphysmodel.hpp11
-rw-r--r--src/shared/util/tinyobj.cpp3
-rw-r--r--src/shared/util/tinyobj.hpp2
20 files changed, 278 insertions, 53 deletions
diff --git a/src/client/lua_api/load_phys.cpp b/src/client/lua_api/load_cphys.cpp
index 0a4889b..b38b84d 100644
--- a/src/client/lua_api/load_phys.cpp
+++ b/src/client/lua_api/load_cphys.cpp
@@ -10,7 +10,7 @@ extern "C" {
#include <irrlicht.h>
#include "../callbackhandeler.hpp"
#include "phys/cbphysbox.hpp"
-#include "phys/bphysmodel.hpp"
+#include "phys/cbphysmodel.hpp"
#include <btBulletDynamicsCommon.h>
#include <btBulletCollisionCommon.h>
//#include <shared/phys/physcommon.hpp>
@@ -46,7 +46,7 @@ void load_physfuncs(lua_State* L){
//phys things
cbphysbox_register(L);
- bphysmodel_register(L,device);
+ cbphysmodel_register(L);
lua_getglobal(L,"phys");//{}
lua_pushcfunction(L,raytest);//{},raytest()
diff --git a/src/client/lua_api/load_phys.hpp b/src/client/lua_api/load_cphys.hpp
index 7ee07fe..7ee07fe 100644
--- a/src/client/lua_api/load_phys.hpp
+++ b/src/client/lua_api/load_cphys.hpp
diff --git a/src/client/lua_api/phys/bphysgeneric.hpp b/src/client/lua_api/phys/bphysgeneric.hpp
deleted file mode 100644
index d035084..0000000
--- a/src/client/lua_api/phys/bphysgeneric.hpp
+++ /dev/null
@@ -1,28 +0,0 @@
-
-extern "C" {
- #include <lua.h>
- #include <lauxlib.h>
- #include <lualib.h>
-}
-
-
-int setgravity(lua_State *L);
-int applyforce(lua_State *L);
-int getlineardamping(lua_State *L);
-int getangulardamping(lua_State *L);
-int setdamping(lua_State *L);
-int activate(lua_State *L);
-int getvelocity(lua_State *L);
-int setvelocity(lua_State *L);
-
-static const luaL_reg brigidbody_m[] = {
- {"setgravity", setgravity},
- {"applyforce", applyforce},
- {"getldamping", getlineardamping},
- {"getadamping", getangulardamping},
- {"setdamping", setdamping},
- {"activate", activate},
- {"getvelocity", getvelocity},
- {"setvelocity", setvelocity},
- {NULL, NULL}
-};
diff --git a/src/client/lua_api/phys/cbphysbox.cpp b/src/client/lua_api/phys/cbphysbox.cpp
index 578fc8d..86deacf 100644
--- a/src/client/lua_api/phys/cbphysbox.cpp
+++ b/src/client/lua_api/phys/cbphysbox.cpp
@@ -16,7 +16,7 @@ extern "C" {
#include "../scene/igeneric.hpp"
#include <shared/lua_api/common.hpp>
-#include "bphysgeneric.hpp"
+#include <shared/lua_api/phys/bphysgeneric.hpp>
using namespace irr;
using namespace scene;
@@ -146,11 +146,11 @@ int cbsetmaterial(lua_State* L){
}
static const luaL_reg cbphysbox_m[] = {
- {"setcpos", cbphyssetpos},//overload
- {"getcpos", cbphysgetpos},
- {"getgravity", cbphysgetgravity},
- {"applygravity",cbphysapplygravity},
- {"setMaterial", cbsetmaterial},
+ {"setpos", cbphyssetpos},//overload
+ {"getpos", cbphysgetpos},
+ //{"getgravity", cbphysgetgravity},
+ //{"applygravity",cbphysapplygravity},
+ //{"setMaterial", cbsetmaterial},
// {"delete", delbphysbox},//client side delete needs to delete the visual representation
{0, 0},
};
@@ -166,6 +166,7 @@ void cbphysbox_register(lua_State* L){
luaL_getmetatable(L,"phys.physbox");//phys.physbox
lua_newtable(L);//phys.physbox,{}
luaL_register(L,NULL,brigidbody_m);
+ luaL_register(L,NULL,igeneric_m);
luaL_register(L,NULL,cbphysbox_m);//phys.physbox,{}
lua_setfield(L,-2,"__index");//phys.physbox
diff --git a/src/client/lua_api/phys/bphysmodel.cpp b/src/client/lua_api/phys/cbphysmodel.cpp
index 2bf65fb..51ff2d8 100644
--- a/src/client/lua_api/phys/bphysmodel.cpp
+++ b/src/client/lua_api/phys/cbphysmodel.cpp
@@ -15,7 +15,7 @@ extern "C" {
#include <irrlicht.h>
#include "../gameparts.hpp"
#include "cbphysbox.hpp"
-#include "bphysmodel.hpp"
+#include "cbphysmodel.hpp"
#include <client/lua_api/scene/igeneric.hpp>
#include <shared/lua_api/common.hpp>
@@ -121,10 +121,7 @@ static const luaL_reg bphysmodel_m[] = {
{0, 0},
};
-int bphysmodel_register(lua_State* L, IrrlichtDevice* d){
-
- device = d;
-
+int cbphysmodel_register(lua_State* L){
//printf("bphysmodel registered\n");
luaL_newmetatable(L, "phys.physmodel");//{}
diff --git a/src/client/lua_api/phys/bphysmodel.hpp b/src/client/lua_api/phys/cbphysmodel.hpp
index 1c0fbac..5ea42a8 100644
--- a/src/client/lua_api/phys/bphysmodel.hpp
+++ b/src/client/lua_api/phys/cbphysmodel.hpp
@@ -8,5 +8,5 @@ extern "C" {
}
#include <irrlicht.h>
-int bphysmodel_register(lua_State* L, irr::IrrlichtDevice* d);
+int cbphysmodel_register(lua_State* L);
#endif
diff --git a/src/client/lua_api/scene/igeneric.cpp b/src/client/lua_api/scene/igeneric.cpp
index 0383619..38479d4 100644
--- a/src/client/lua_api/scene/igeneric.cpp
+++ b/src/client/lua_api/scene/igeneric.cpp
@@ -67,3 +67,12 @@ int iscenesetmaterial(lua_State* L){
return 0;
}
+
+extern const luaL_reg igeneric_m[] = {
+ {"getpos", iscenegetpos},
+ {"setpos", iscenesetpos},
+ {"getang", iscenegetangle},
+ {"setang", iscenesetangle},
+ {"setmaterial", iscenesetmaterial},
+ {0, 0},
+};
diff --git a/src/client/lua_api/scene/igeneric.hpp b/src/client/lua_api/scene/igeneric.hpp
index 6d41028..9e3d6f6 100644
--- a/src/client/lua_api/scene/igeneric.hpp
+++ b/src/client/lua_api/scene/igeneric.hpp
@@ -15,12 +15,5 @@ int iscenegetangle(lua_State* L);
int iscenesetangle(lua_State* L);
int iscenesetmaterial(lua_State* L);
-static const luaL_reg igeneric_m[] = {
- {"getpos", iscenegetpos},
- {"setpos", iscenesetpos},
- {"getang", iscenegetangle},
- {"setang", iscenesetangle},
- {"setmaterial", iscenesetmaterial},
- {0, 0},
-};
+extern const luaL_reg igeneric_m[];
#endif
diff --git a/src/client/main.cpp b/src/client/main.cpp
index 6336dc1..bc509c7 100644
--- a/src/client/main.cpp
+++ b/src/client/main.cpp
@@ -17,7 +17,7 @@ extern "C" {
#include "lua_api/load_gui.hpp"
#include "lua_api/load_game.hpp"
#include "lua_api/load_scene.hpp"
-#include "lua_api/load_phys.hpp"
+#include "lua_api/load_cphys.hpp"
#include "lua_api/load_video.hpp"
#include "lua_api/load_io.hpp"
#include "callbackhandeler.hpp"
diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp
index 14a27ea..a846945 100644
--- a/src/shared/lua_api/load_phys.cpp
+++ b/src/shared/lua_api/load_phys.cpp
@@ -1,8 +1,10 @@
-#include "./load_phys.hpp"
-#include "./phys/bphysbox.hpp"
+#include "load_phys.hpp"
+#include "phys/bphysbox.hpp"
+#include "phys/bhingeconstraint.hpp"
void loadPhysLibs(lua_State* L){
lua_newtable(L);//{}
lua_setglobal(L,"phys");
bphysbox_register(L);
+ bhingeconstraint_register(L);
}
diff --git a/src/shared/lua_api/phys/bhingeconstraint.cpp b/src/shared/lua_api/phys/bhingeconstraint.cpp
new file mode 100644
index 0000000..168d1b6
--- /dev/null
+++ b/src/shared/lua_api/phys/bhingeconstraint.cpp
@@ -0,0 +1,50 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <list>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <Irrlicht.h>
+#include <btBulletDynamicsCommon.h>
+#include <shared/lua_api/phys/bhingeconstraint.hpp>
+
+using namespace irr;
+using namespace scene;
+using namespace core;
+using namespace video;
+
+extern btDiscreteDynamicsWorld* World;
+extern std::list<btRigidBody*> Objects;
+
+int newbhingeconstraint(lua_State *L){
+ return 0;
+}
+
+static const luaL_reg hingeconstraint_m[] = {
+// {"delete", delbphysbox},//client side delete needs to delete the visual representation
+ {0, 0},
+};
+
+void bhingeconstraint_register(lua_State* L){
+ lua_getglobal(L,"phys");//{}
+ lua_pushcfunction(L,newbhingeconstraint);//{},newhingeconstraint()
+ lua_setfield(L,-2,"newhingeconstraint");//{}
+
+ lua_pop(L,1);//
+
+ luaL_newmetatable(L,"phys.hingeconstraint");
+ lua_newtable(L);//phys.hingeconstraint,{}
+ luaL_register(L,NULL,hingeconstraint_m);
+ //luaL_register(L,NULL,cbphysbox_m);//phys.hingeconstraint,{}
+ lua_setfield(L,-2,"__index");//phys.physbox
+
+ lua_pop(L,1);
+
+ //printf("When registering physbox, new() is %p\n",newcbphysbox);
+ //printf("setpos is %p\n",cbphyssetpos);
+
+ lua_pop(L,1);
+}
diff --git a/src/shared/lua_api/phys/bhingeconstraint.hpp b/src/shared/lua_api/phys/bhingeconstraint.hpp
new file mode 100644
index 0000000..4e8d72c
--- /dev/null
+++ b/src/shared/lua_api/phys/bhingeconstraint.hpp
@@ -0,0 +1,2 @@
+
+void bhingeconstraint_register(lua_State* L);
diff --git a/src/client/lua_api/phys/bphysbuffer.cpp b/src/shared/lua_api/phys/bphysbuffer.cpp
index f1f20c0..f1f20c0 100644
--- a/src/client/lua_api/phys/bphysbuffer.cpp
+++ b/src/shared/lua_api/phys/bphysbuffer.cpp
diff --git a/src/client/lua_api/phys/bphysbuffer.hpp b/src/shared/lua_api/phys/bphysbuffer.hpp
index cfbf549..cfbf549 100644
--- a/src/client/lua_api/phys/bphysbuffer.hpp
+++ b/src/shared/lua_api/phys/bphysbuffer.hpp
diff --git a/src/client/lua_api/phys/bphysgeneric.cpp b/src/shared/lua_api/phys/bphysgeneric.cpp
index 91b4c21..e8ef2b5 100644
--- a/src/client/lua_api/phys/bphysgeneric.cpp
+++ b/src/shared/lua_api/phys/bphysgeneric.cpp
@@ -198,3 +198,15 @@ int setdamping(lua_State *L){
return 0;
}
+
+extern const luaL_reg brigidbody_m[] = {
+ {"setgravity", setgravity},
+ {"applyforce", applyforce},
+ {"getldamping", getlineardamping},
+ {"getadamping", getangulardamping},
+ {"setdamping", setdamping},
+ {"activate", activate},
+ {"getvelocity", getvelocity},
+ {"setvelocity", setvelocity},
+ {NULL, NULL}
+};
diff --git a/src/shared/lua_api/phys/bphysgeneric.hpp b/src/shared/lua_api/phys/bphysgeneric.hpp
new file mode 100644
index 0000000..6d3ef70
--- /dev/null
+++ b/src/shared/lua_api/phys/bphysgeneric.hpp
@@ -0,0 +1,18 @@
+
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+
+
+int setgravity(lua_State *L);
+int applyforce(lua_State *L);
+int getlineardamping(lua_State *L);
+int getangulardamping(lua_State *L);
+int setdamping(lua_State *L);
+int activate(lua_State *L);
+int getvelocity(lua_State *L);
+int setvelocity(lua_State *L);
+
+extern const luaL_reg brigidbody_m[];
diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp
new file mode 100644
index 0000000..e9fecbf
--- /dev/null
+++ b/src/shared/lua_api/phys/bphysmodel.cpp
@@ -0,0 +1,153 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+#include <memory>
+#include <map>
+#include <functional>
+#include <list>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include "bphysmodel.hpp"
+#include <shared/lua_api/common.hpp>
+#include <shared/util/tinyobj.hpp>
+
+extern btDiscreteDynamicsWorld* World;
+extern std::list<btRigidBody*> Objects;
+
+//newbphysmodel("graphicfile","physicfile",mass,[,{position}][,{lookat}])
+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);
+ }
+ if(nargs > 3){
+ //"graphicsfile","physicsfile",{position}
+ popvector3d(L,&x,&y,&z);
+ }
+ //"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);
+
+ //ISceneManager *smgr = device->getSceneManager();
+
+ //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;
+ tinyobj_material_t *materials = NULL;
+ size_t num_materials;
+
+ size_t data_len = 0;
+ FILE *objfile = fopen(ppath,"rb");
+ fseek(objfile,0,SEEK_END);
+ data_len = ftell(objfile);
+ 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);
+ 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];
+ }
+ printf("Done building trimesh\n");
+ btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true);
+ btTransform tr;
+ tr.setIdentity();
+ tr.setOrigin(btVector3(x,y,z));
+ printf("Created default motion shape\n");
+ btDefaultMotionState *ms = new btDefaultMotionState(btTransform(tr));
+ 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");
+
+ //Create the lua representation
+ lua_newtable(L);
+ lua_pushlightuserdata(L,rb);
+ lua_setfield(L,-2,"rigidbody");
+ //lua_pushlightuserdata(L,node);
+ //lua_setfield(L,-2,"node");
+ luaL_getmetatable(L,"phys.physmodel");
+ lua_setmetatable(L,-2);
+ printf("finished creating the lua representation\n");
+
+ return 1;
+}
+
+static const luaL_reg bphysmodel_f[] = {
+ {"newphysmodel", newbphysmodel},
+ {0,0},
+};
+
+static const luaL_reg bphysmodel_m[] = {
+ {0, 0},
+};
+
+int bphysmodel_register(lua_State* L){
+
+ //device = d;
+
+ //printf("bphysmodel registered\n");
+
+ luaL_newmetatable(L, "phys.physmodel");//{}
+ luaL_register(L,NULL,bphysmodel_m);
+ //luaL_register(L,NULL,igeneric_m); //Inherit all the things to do with scene nodes
+
+ lua_getglobal(L,"phys");
+ luaL_register(L,NULL,bphysmodel_f);
+
+ return 1;
+}
diff --git a/src/shared/lua_api/phys/bphysmodel.hpp b/src/shared/lua_api/phys/bphysmodel.hpp
new file mode 100644
index 0000000..50645cc
--- /dev/null
+++ b/src/shared/lua_api/phys/bphysmodel.hpp
@@ -0,0 +1,11 @@
+#ifndef _BPHYSMODEL_HPP_
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+
+int bphysmodel_register(lua_State* L);
+#endif
diff --git a/src/shared/util/tinyobj.cpp b/src/shared/util/tinyobj.cpp
new file mode 100644
index 0000000..a5dfe73
--- /dev/null
+++ b/src/shared/util/tinyobj.cpp
@@ -0,0 +1,3 @@
+/* Includes the tinyobjloader library */
+#define TINYOBJ_LOADER_C_IMPLEMENTATION
+#include "tinyobj.hpp"
diff --git a/src/shared/util/tinyobj.hpp b/src/shared/util/tinyobj.hpp
new file mode 100644
index 0000000..6e4cc74
--- /dev/null
+++ b/src/shared/util/tinyobj.hpp
@@ -0,0 +1,2 @@
+/* Includes the tinyobjloader library */
+#include <tinyobj_loader_c.h>