aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-07-15 11:35:44 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2017-07-15 11:35:44 -0400
commitfa49161fe2d7e0a025c9fd8559815d56dfd1f427 (patch)
treecb3a64e2d45ff9f675c256a94f5c1ccb01ec5a09 /src/client
parentb98dbac4ed2f755ce71bd9be17f26a3f86c1e3cc (diff)
downloadbrokengine-fa49161fe2d7e0a025c9fd8559815d56dfd1f427.tar.gz
brokengine-fa49161fe2d7e0a025c9fd8559815d56dfd1f427.tar.bz2
brokengine-fa49161fe2d7e0a025c9fd8559815d56dfd1f427.zip
Added static physics things
Added some phyics stuff! woo! * Added physics models from file * Added physics boxes * Added a maya camera * Added lights * Various refactoring
Diffstat (limited to 'src/client')
-rw-r--r--src/client/lua_api/gameparts.hpp15
-rw-r--r--src/client/lua_api/load_core.cpp4
-rw-r--r--src/client/lua_api/phys/bphysbox.cpp49
-rw-r--r--src/client/lua_api/phys/bphysbox.hpp2
-rw-r--r--src/client/lua_api/phys/bphysmodel.cpp319
-rw-r--r--src/client/lua_api/phys/bphysmodel.hpp11
-rw-r--r--src/client/lua_api/phys/butil.cpp26
-rw-r--r--src/client/lua_api/phys/butil.hpp9
-rw-r--r--src/client/lua_api/scene/icamera.cpp55
-rw-r--r--src/client/lua_api/scene/igeneric.cpp17
-rw-r--r--src/client/lua_api/scene/igeneric.hpp2
-rw-r--r--src/client/lua_api/scene/ilight.cpp130
-rw-r--r--src/client/lua_api/scene/ilight.hpp11
-rw-r--r--src/client/lua_api/scene/imesh.cpp90
-rw-r--r--src/client/main.cpp178
15 files changed, 747 insertions, 171 deletions
diff --git a/src/client/lua_api/gameparts.hpp b/src/client/lua_api/gameparts.hpp
index 2a14f4a..8a54950 100644
--- a/src/client/lua_api/gameparts.hpp
+++ b/src/client/lua_api/gameparts.hpp
@@ -7,17 +7,14 @@ extern "C" {
}
typedef struct LISceneNode {
- irr::scene::ISceneNode* n;
- map_t funcmap;
- const char* type;
+ irr::scene::ISceneNode* n;
+ map_t funcmap;
+ const char* type;
} LISceneNode;
-typedef struct LBPhysNode {
- irr::scene::ISceneNode* n;
- btRigidBody* r;
- map_t funcmap;
- const char* type;
-} LIPhysNode;
+typedef struct LBPhysNode : LISceneNode {
+ btCollisionObject* r;
+} LBPhysNode;
extern lua_State* tL;
extern irr::IrrlichtDevice* gamedevice;
diff --git a/src/client/lua_api/load_core.cpp b/src/client/lua_api/load_core.cpp
index 152ab76..4a74d39 100644
--- a/src/client/lua_api/load_core.cpp
+++ b/src/client/lua_api/load_core.cpp
@@ -10,7 +10,9 @@ extern "C" {
#include <irrlicht.h>
#include "scene/icamera.hpp"
#include "scene/imesh.hpp"
+#include "scene/ilight.hpp"
#include "phys/bphysbox.hpp"
+#include "phys/bphysmodel.hpp"
using namespace irr;
@@ -19,7 +21,9 @@ extern IrrlichtDevice* device;
void load_corefuncs(lua_State* L){
icamera_register(L,device);
imesh_register(L,device);
+ ilight_register(L,device);
bphysbox_register(L,device);
+ bphysmodel_register(L,device);
lua_pop(L, 1);
lua_newtable(L);
diff --git a/src/client/lua_api/phys/bphysbox.cpp b/src/client/lua_api/phys/bphysbox.cpp
index 9a87045..80cd2eb 100644
--- a/src/client/lua_api/phys/bphysbox.cpp
+++ b/src/client/lua_api/phys/bphysbox.cpp
@@ -10,9 +10,11 @@ extern "C" {
#include <lauxlib.h>
#include <lualib.h>
}
+#include <btBulletDynamicsCommon.h>
#include <irrlicht.h>
#include "../gameparts.hpp"
#include "bphysbox.hpp"
+#include "../scene/igeneric.hpp"
using namespace irr;
using namespace scene;
@@ -99,7 +101,8 @@ static int newbphysbox(lua_State* L){
//Node->setMaterialFlag(video::EMF_WIREFRAME,true)
//Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
- Node->setMaterialTexture(0, device->getVideoDriver()->getTexture("sydney.bmp"));
+ Node->setMaterialFlag(video::EMF_LIGHTING,true);
+ //Node->setMaterialTexture(0, device->getVideoDriver()->getTexture("../data/wall.jpg"));
printf("Set node's lighting stuff...\n");
@@ -148,7 +151,7 @@ static int newbphysbox(lua_State* L){
lua_rawgeti(L,LUA_REGISTRYINDEX,tref);//Put it back on the stack since luaL_ref pops the object.
//Set it's metatable
- luaL_getmetatable(L, "physbox");
+ luaL_getmetatable(L, "phys.physbox");
lua_setmetatable(L, -2);
//Create the struct
@@ -169,7 +172,7 @@ static int newbphysbox(lua_State* L){
static int delbphysbox(lua_State* L){
LBPhysNode* pnode = checkisbphysbox(L,-1);
- delete pnode->r->getMotionState();
+ //delete pnode->r->getMotionState();
delete pnode->r->getCollisionShape();
delete pnode->r;
@@ -213,7 +216,7 @@ static int bphyssetpos(lua_State *L){
bt.setOrigin(to);
printf("managed to set phys transform\n");
i->r->setWorldTransform(bt);
-
+ i->r->activate();
printf("sucess! returning from call\n");
return 0;
}
@@ -248,7 +251,7 @@ static const luaL_reg bphysbox_f[] = {
};
static const luaL_reg bphysbox_m[] = {
-// {"setMaterial", setmaterial},
+ {"setmaterial", iscenesetmaterial},
{"getpos", bphysgetpos},
{"setpos", bphyssetpos},
// {"settext", setiguitext},
@@ -258,29 +261,29 @@ static const luaL_reg bphysbox_m[] = {
int bphysbox_register(lua_State* L, IrrlichtDevice* d){
- device = d;
+ device = d;
- printf("bphysbox registered\n");
+ printf("bphysbox registered\n");
- luaL_newmetatable(L, "phys.physbox");
+ luaL_newmetatable(L, "phys.physbox");
- luaL_register(L,"physbox",bphysbox_f);
+ luaL_register(L,"physbox",bphysbox_f);
- lua_pushstring(L,"__index");
- lua_pushstring(L,"gethandeler");
- lua_gettable(L,-3);
- lua_settable(L,-4);
+ lua_pushstring(L,"__index");
+ lua_pushstring(L,"gethandeler");
+ lua_gettable(L,-3);
+ lua_settable(L,-4);
- lua_pushstring(L,"__newindex");
- lua_pushstring(L,"sethandeler");
- lua_gettable(L,-3);
- lua_settable(L,-4);
-
- lua_pushstring(L,"__gc");
- lua_pushcfunction(L,delbphysbox);
- lua_settable(L,-4);
+ lua_pushstring(L,"__newindex");
+ lua_pushstring(L,"sethandeler");
+ lua_gettable(L,-3);
+ lua_settable(L,-4);
- luaL_register(L, NULL, bphysbox_m);
+ lua_pushstring(L,"__gc");
+ lua_pushcfunction(L,delbphysbox);
+ lua_settable(L,-4);
- return 1;
+ luaL_register(L, NULL, bphysbox_m);
+
+ return 1;
}
diff --git a/src/client/lua_api/phys/bphysbox.hpp b/src/client/lua_api/phys/bphysbox.hpp
index cad3d02..43285f2 100644
--- a/src/client/lua_api/phys/bphysbox.hpp
+++ b/src/client/lua_api/phys/bphysbox.hpp
@@ -9,3 +9,5 @@ extern "C" {
#include <irrlicht.h>
int bphysbox_register(lua_State* L, irr::IrrlichtDevice* d);
+static int bphyssetpos(lua_State *L);
+static int bphysgetpos(lua_State *L);
diff --git a/src/client/lua_api/phys/bphysmodel.cpp b/src/client/lua_api/phys/bphysmodel.cpp
new file mode 100644
index 0000000..08ec599
--- /dev/null
+++ b/src/client/lua_api/phys/bphysmodel.cpp
@@ -0,0 +1,319 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+#include <memory>
+#include <map>
+#include <functional>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include <irrlicht.h>
+#include "../gameparts.hpp"
+#include "bphysbox.hpp"
+#include "bphysmodel.hpp"
+#include "../scene/igeneric.hpp"
+
+using namespace irr;
+using namespace scene;
+using namespace core;
+using namespace video;
+
+extern IrrlichtDevice* device;
+
+extern btDiscreteDynamicsWorld* World;
+extern core::list<btRigidBody*> Objects;
+
+static LBPhysNode* checkisbphysmodel(lua_State* L, int index){
+ void* ud = luaL_checkudata(L,index,"phys.physmodel");
+ luaL_argcheck(L,ud != NULL, index, "'phys.physmodel' expected");
+ return (LBPhysNode*) ud;
+}
+
+/*
+static LISceneNode* checkismesh(lua_State* L){
+ return checkismesh(L,1);
+}
+*/
+
+//iscenecamera.new(Vector position, Vector lookat, parrent)
+// {} {} 0 1
+static int newbphysmodel(lua_State* L){
+ printf("Createing bphysbox!\n");
+ int nargs = lua_gettop(L);
+ if(nargs != 3){
+ printf("Incorrect # of args to create a physmodel!");
+ }
+ //The model for the mesh
+ //const char* modelpath = luaL_optstring(L,1,"error");
+
+ //Find the vector position
+ lua_pushnumber(L,1);
+ lua_gettable(L,-4);
+ float x = lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pushnumber(L,2);
+ lua_gettable(L,-4);
+ float y = lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pushnumber(L,3);
+ lua_gettable(L,-4);
+ float z = lua_tonumber(L,-1);
+ lua_pop(L,1);
+ printf("Found position for phys model: %f %f %f\n",x,y,z);
+
+ //Find the vector scale
+ lua_pushnumber(L,1);
+ lua_gettable(L,-3);
+ float sx = lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pushnumber(L,2);
+ lua_gettable(L,-3);
+ float sy = lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pushnumber(L,3);
+ lua_gettable(L,-3);
+ float sz = lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ printf("Found scale for phys model: %f %f %f\n",sx,sy,sz);
+
+ //find the model path
+ const char* mpath = luaL_optstring(L,3,"error.obj");
+
+ printf("I want to use model %s\n", mpath);
+
+ ISceneManager* smgr = device->getSceneManager();
+ IMesh* amesh = smgr->getMesh(mpath);
+ IMeshBuffer* bf = amesh->getMeshBuffer(0);
+ u32 ni = bf->getIndexCount();
+
+ btTriangleMesh* trimesh = new btTriangleMesh();
+ for(u32 i = 0; i < ni; i+=3){
+ vector3df p1 = bf->getPosition(i + 0);
+ vector3df p2 = bf->getPosition(i + 1);
+ vector3df p3 = bf->getPosition(i + 2);
+ btVector3 b1 = btVector3(p1.X,p1.Y,p1.Z);
+ btVector3 b2 = btVector3(p2.X,p2.Y,p2.Z);
+ btVector3 b3 = btVector3(p3.X,p3.Y,p3.Z);
+ trimesh->addTriangle(b1,b2,b3);
+ }
+ btCollisionShape* shape = new btConvexTriangleMeshShape(trimesh,true);
+
+ core::vector3df scale = core::vector3df(sx,sy,sz);
+ btVector3 pos = btVector3(x,y,z);
+ core::vector3df ipos = core::vector3df(x,y,z);
+ //Find the mass
+ float mass = luaL_optint(L,4,0);
+ printf("Found mass for physbox:%f\n",mass);
+
+
+
+ // Create an Irrlicht cube
+ scene::ISceneNode* Node = smgr->addMeshSceneNode(
+ amesh,
+ (ISceneNode*)0,
+ (s32)-1,
+ ipos,
+ vector3df(0,0,0),
+ scale
+ );
+ //Node->setScale(scale);
+
+ printf("Added cube scene node and set it's scale\n");
+
+ //Node->setMaterialFlag(video::EMF_WIREFRAME,true)
+ //Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
+ Node->setMaterialFlag(video::EMF_LIGHTING,true);
+ //Node->setMaterialTexture(0, device->getVideoDriver()->getTexture("../data/wall.jpg"));
+
+ printf("Set node's lighting stuff...\n");
+
+ // Set the initial position of the object
+ btTransform Transform;
+ Transform.setIdentity();
+ Transform.setOrigin(pos);
+
+ printf("Created transform at pos...\n");
+
+ // Give it a default MotionState
+ btDefaultMotionState *MotionState = new btDefaultMotionState(Transform);
+
+ // Create the shape
+ // btVector3 HalfExtents(sx * 0.5f, sy * 0.5f, sz * 0.5f);
+ // btCollisionShape *Shape = new btBoxShape(HalfExtents);
+
+ printf("Created collision shape...");
+
+ // Add mass
+ btVector3 LocalInertia;
+ shape->calculateLocalInertia(mass, LocalInertia);
+
+ // Create the rigid body object
+ btRigidBody *RigidBody = new btRigidBody(mass, MotionState, shape, LocalInertia);
+
+ printf("Created rigidboxy...");
+
+ // Store a pointer to the irrlicht node so we can update it later
+ RigidBody->setUserPointer((void *)(Node));
+
+ printf("Set user pointer");
+
+ // Add it to the world
+ World->addRigidBody(RigidBody);
+ printf("Added to world");
+ Objects.push_back(RigidBody);
+
+ //Register it's callback
+ printf("Everything created, makeing the lua representation\n");
+
+ //Create it's lua representation
+ LBPhysNode* pnode = (LBPhysNode*)lua_newuserdata(L, sizeof(LBPhysNode));
+ int tref = luaL_ref(L,LUA_REGISTRYINDEX);
+ //iguielements[lcam] = tref;
+ lua_rawgeti(L,LUA_REGISTRYINDEX,tref);//Put it back on the stack since luaL_ref pops the object.
+
+ //Set it's metatable
+ luaL_getmetatable(L, "phys.physmodel");
+ lua_setmetatable(L, -2);
+
+ //Create the struct
+ pnode->n = Node;
+ pnode->r = RigidBody;
+ pnode->funcmap = hashmap_new();
+ pnode->type = "bphysbox";
+
+ printf("Done createing lua representation!\n");
+ //Free up anything made in this function
+ //free(label);
+
+ //Put it on top and return it
+ lua_rawgeti(L,LUA_REGISTRYINDEX,tref);
+ return 1;
+}
+
+static int delbphysmodel(lua_State* L){
+ LBPhysNode* pnode = checkisbphysmodel(L,-1);
+
+ //delete pnode->r->getMotionState();
+ delete pnode->r->getCollisionShape();
+ delete pnode->r;
+
+ return 0;
+}
+
+static int bphyssetpos(lua_State *L){
+ LBPhysNode* i = checkisbphysmodel(L,1);
+ btTransform bt = i->r->getWorldTransform();
+
+ lua_pushnumber(L,1);
+ lua_gettable(L,-2);
+ f32 x = (f32)lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pushnumber(L,2);
+ lua_gettable(L,-2);
+ f32 y = (f32)lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pushnumber(L,3);
+ lua_gettable(L,-2);
+ f32 z = (f32)lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ btVector3 to = btVector3(x,y,z);
+ printf("managed to set phys origin\n");
+ bt.setOrigin(to);
+ printf("managed to set phys transform\n");
+ i->r->setWorldTransform(bt);
+ i->r->activate();
+ printf("sucess! returning from call\n");
+ return 0;
+}
+
+static int bphysgetpos(lua_State *L){
+ LBPhysNode* i = checkisbphysmodel(L,1);
+ btTransform bt = i->r->getWorldTransform();
+ btVector3 bv = bt.getOrigin();
+
+ lua_createtable(L,3,0);
+
+ lua_pushnumber(L,1);
+ lua_pushnumber(L,bv.x());
+ lua_settable(L,-3);
+
+ lua_pushnumber(L,2);
+ lua_pushnumber(L,bv.y());
+ lua_settable(L,-3);
+
+ lua_pushnumber(L,3);
+ lua_pushnumber(L,bv.z());
+ lua_settable(L,-3);
+
+ return 1;
+}
+
+/*mesh:setmaterial("string",layernum=0)*/
+/*
+static int setmaterial(lua_State* L){
+ LISceneNode* n = checkismesh(L,1);
+ u32 layernum = luaL_optint(L,3,0);
+ const char* matfile = luaL_optstring(L,2,"error.png");
+ printf("Setting material on a %s",n->type);
+ IVideoDriver* driver = device->getVideoDriver();
+ n->n->setMaterialTexture( 0, driver->getTexture(matfile) );
+ return 0;
+}
+*/
+
+static const luaL_reg bphysmodel_f[] = {
+ {"new", newbphysmodel},
+// {"gethandeler", guigethandeler},
+// {"sethandeler", guisethandeler},
+ {0,0},
+};
+
+static const luaL_reg bphysmodel_m[] = {
+ {"setmaterial", iscenesetmaterial},
+ {"getpos", bphysgetpos},
+ {"setpos", bphyssetpos},
+// {"settext", setiguitext},
+// {"remove", removeiguielement},
+ {0, 0},
+};
+
+int bphysmodel_register(lua_State* L, IrrlichtDevice* d){
+
+ device = d;
+
+ printf("bphysmodel registered\n");
+
+ luaL_newmetatable(L, "phys.physmodel");
+
+ luaL_register(L,"physmodel",bphysmodel_f);
+
+ lua_pushstring(L,"__index");
+ lua_pushstring(L,"gethandeler");
+ lua_gettable(L,-3);
+ lua_settable(L,-4);
+
+ lua_pushstring(L,"__newindex");
+ lua_pushstring(L,"sethandeler");
+ lua_gettable(L,-3);
+ lua_settable(L,-4);
+
+ lua_pushstring(L,"__gc");
+ lua_pushcfunction(L,delbphysmodel);
+ lua_settable(L,-4);
+
+ luaL_register(L, NULL, bphysmodel_m);
+
+ return 1;
+}
diff --git a/src/client/lua_api/phys/bphysmodel.hpp b/src/client/lua_api/phys/bphysmodel.hpp
new file mode 100644
index 0000000..a59c451
--- /dev/null
+++ b/src/client/lua_api/phys/bphysmodel.hpp
@@ -0,0 +1,11 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+
+int bphysmodel_register(lua_State* L, irr::IrrlichtDevice* d);
diff --git a/src/client/lua_api/phys/butil.cpp b/src/client/lua_api/phys/butil.cpp
new file mode 100644
index 0000000..6319688
--- /dev/null
+++ b/src/client/lua_api/phys/butil.cpp
@@ -0,0 +1,26 @@
+#include <stdio.h>
+
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+
+#include <btBulletDynamicsCommon.h>
+
+#include "butil.hpp"
+
+btVector3 lua_popbtvector(lua_State* L,int pos){
+ f32 p[3];
+
+ for(int i = 0; i < 3; i++){
+ lua_pushnumber(L,i+1);
+ lua_gettable(L,pos);
+ p[i] = (f32) lua_tonumber(L,-1);
+ lua_pop(L,1);
+ }
+
+ printf("Found vector (%f,%f,%f)\n",p[0],p[1],p[2]);
+
+ return btVector3(p[0],p[1],p[2]);
+}
diff --git a/src/client/lua_api/phys/butil.hpp b/src/client/lua_api/phys/butil.hpp
new file mode 100644
index 0000000..c288b97
--- /dev/null
+++ b/src/client/lua_api/phys/butil.hpp
@@ -0,0 +1,9 @@
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+
+#include <btBulletDynamicsCommon.h>
+
+btVector3 lua_popbtvector(lua_State* L,int pos);
diff --git a/src/client/lua_api/scene/icamera.cpp b/src/client/lua_api/scene/icamera.cpp
index ce54ef2..5a4e3a2 100644
--- a/src/client/lua_api/scene/icamera.cpp
+++ b/src/client/lua_api/scene/icamera.cpp
@@ -22,8 +22,8 @@ using namespace core;
extern IrrlichtDevice* device;
static LISceneNode* checkiscenecamera(lua_State* L, int index){
- void* ud = luaL_checkudata(L,index,"scene.icamera");
- luaL_argcheck(L,ud != NULL, index, "'scene.icamera' expected");
+ void* ud = luaL_checkudata(L,index,"scene.iscenecamera");
+ luaL_argcheck(L,ud != NULL, index, "'scene.iscenecamera' expected");
return (LISceneNode*) ud;
}
@@ -31,6 +31,42 @@ static LISceneNode* checkiscenecamera(lua_State* L){
return checkiscenecamera(L,1);
}
+static LISceneNode* checkismayacamera(lua_State* L, int index){
+ void* ud = luaL_checkudata(L,index,"scene.iscenemayacamera");
+ luaL_argcheck(L,ud != NULL, index, "'scene.iscenemayacamera' expected");
+ return (LISceneNode*) ud;
+}
+
+static LISceneNode* checkismayacamera(lua_State* L){
+ return checkismayacamera(L,1);
+}
+
+static int newiscenemayacamera(lua_State* L){
+ printf("createing maya camera!\n");
+ int nargs = lua_gettop(L);
+ ISceneManager* smgr = device->getSceneManager();
+ ICameraSceneNode* cam = smgr->addCameraSceneNodeMaya();
+ printf("cam is %p",cam);
+ LISceneNode* lcam = (LISceneNode*)lua_newuserdata(L, sizeof(LISceneNode));
+ int tref = luaL_ref(L,LUA_REGISTRYINDEX);
+
+ //Set it's metatable
+ luaL_getmetatable(L, "scene.iscenemayacamera");
+ lua_setmetatable(L, -2);
+
+ //Create the struct
+ lcam->n = cam;
+ lcam->funcmap = hashmap_new();
+ lcam->type = "iscenemayacamera";
+
+ //Free up anything made in this function
+ //free(label);
+
+ //Put it on top and return it
+ lua_rawgeti(L,LUA_REGISTRYINDEX,tref);
+ return 1;
+}
+
//iscenecamera.new(Vector position, Vector lookat, parrent)
static int newiscenecamera(lua_State* L){
printf("Createing camera!\n");
@@ -110,6 +146,15 @@ static const luaL_reg icamera_m[] = {
{0, 0},
};
+static const luaL_reg imayacamera_f[] = {
+ {"new", newiscenemayacamera},
+ {0,0},
+};
+
+static const luaL_reg imayacamera_m[] = {
+ {0,0},
+};
+
int icamera_register(lua_State* L, IrrlichtDevice* d){
device = d;
@@ -117,7 +162,6 @@ int icamera_register(lua_State* L, IrrlichtDevice* d){
printf("icamera registered\n");
luaL_newmetatable(L, "scene.icamera");
-
luaL_register(L,"icamera",icamera_f);
lua_pushstring(L,"__index");
@@ -131,6 +175,11 @@ int icamera_register(lua_State* L, IrrlichtDevice* d){
lua_settable(L,-4);
luaL_register(L, NULL, icamera_m);
+ //End of camera
+
+ //Start of maya camera
+ luaL_newmetatable(L,"scene.imayacamera");
+ luaL_register(L,"imayacamera",imayacamera_f);
return 1;
}
diff --git a/src/client/lua_api/scene/igeneric.cpp b/src/client/lua_api/scene/igeneric.cpp
index 51aaf66..fc32e83 100644
--- a/src/client/lua_api/scene/igeneric.cpp
+++ b/src/client/lua_api/scene/igeneric.cpp
@@ -11,6 +11,9 @@ extern "C" {
using namespace irr;
using namespace core;
using namespace scene;
+using namespace video;
+
+extern IrrlichtDevice* device;
static LISceneNode* toiscenenode(lua_State* L, int index){
LISceneNode* ret = (LISceneNode*)lua_touserdata(L,index);
@@ -39,6 +42,7 @@ int iscenegetpos(lua_State* L){
return 1;
}
+
int iscenesetpos(lua_State* L){
ISceneNode* i = toiscenenode(L,1)->n;
@@ -65,6 +69,15 @@ int iscenesetpos(lua_State* L){
vector3df pos = i->getAbsolutePosition();
printf("After setting pos, new pos is %f %f %f",pos.X,pos.Y,pos.Z);
-
- return 0;
+ return 0;
+}
+
+int iscenesetmaterial(lua_State* L){
+ ISceneNode* i = toiscenenode(L,1)->n;
+ const char* s = luaL_optstring(L,2,"error.png");
+ printf("Setting material to %s",s);
+ IVideoDriver* driver = device->getVideoDriver();
+ i->setMaterialTexture(0, driver->getTexture(s));
+
+ return 0;
}
diff --git a/src/client/lua_api/scene/igeneric.hpp b/src/client/lua_api/scene/igeneric.hpp
index 848709b..85a9d3e 100644
--- a/src/client/lua_api/scene/igeneric.hpp
+++ b/src/client/lua_api/scene/igeneric.hpp
@@ -12,4 +12,6 @@ extern "C" {
int iscenegetpos(lua_State* L);
int iscenesetpos(lua_State* L);
+int iscenesetmaterial(lua_State* L);
+
#endif
diff --git a/src/client/lua_api/scene/ilight.cpp b/src/client/lua_api/scene/ilight.cpp
new file mode 100644
index 0000000..df01dd3
--- /dev/null
+++ b/src/client/lua_api/scene/ilight.cpp
@@ -0,0 +1,130 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+#include <memory>
+#include <map>
+#include <functional>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+#include "../gameparts.hpp"
+#include "ilight.hpp"
+#include "igeneric.hpp"
+
+using namespace irr;
+using namespace scene;
+using namespace core;
+
+extern IrrlichtDevice* device;
+
+static LISceneNode* checkiscenelight(lua_State* L, int index){
+ void* ud = luaL_checkudata(L,index,"scene.ilight");
+ luaL_argcheck(L,ud != NULL, index, "'scene.ilight' expected");
+ return (LISceneNode*) ud;
+}
+
+static LISceneNode* checkilight(lua_State* L){
+ return checkiscenelight(L,1);
+}
+
+//iscenelight.new(Vector position, parrent = nil)
+static int newiscenelight(lua_State* L){
+ printf("Createing light!\n");
+ int nargs = lua_gettop(L);
+ //lua_pop(L,1);
+ // float radius = 100;
+ // if(nargs == 2){
+ // radius = luaL_optnumber(L,2,100);
+ // }
+ float radius = lua_tonumber(L,-1);
+ printf("radius was %f\n" ,radius);
+ lua_pop(L,1);
+
+ //The position of the light
+ lua_pushnumber(L,1);
+ lua_gettable(L,-2);
+ float x = lua_tonumber(L,-1);
+ printf("got x: %f\n",x);
+ lua_pop(L,1);
+ lua_pushnumber(L,2);
+ lua_gettable(L,-2);
+ float y = lua_tonumber(L,-1);
+ lua_pop(L,1);
+ lua_pushnumber(L,3);
+ lua_gettable(L,-2);
+ float z = lua_tonumber(L,-1);
+ lua_pop(L,1);
+ printf("Found position for light: %f %f %f\n",x,y,z);
+
+ //Create the mesh
+ ISceneManager* smgr = device->getSceneManager();
+ ILightSceneNode* light = smgr->addLightSceneNode(0,vector3df(x,y,z),video::SColor(1.0f,1.0f,1.0f,1.0f),(f32)radius,(s32)-1);
+ printf("Registered the light!\n");
+
+ //Register it's callback
+ //registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent);
+
+ //Create it's lua representation
+ LISceneNode* lnode = (LISceneNode*)lua_newuserdata(L, sizeof(LISceneNode));
+ int tref = luaL_ref(L,LUA_REGISTRYINDEX);
+ //iguielements[lcam] = tref;
+ lua_rawgeti(L,LUA_REGISTRYINDEX,tref);//Put it back on the stack since luaL_ref pops the object.
+
+ //Set it's metatable
+ luaL_getmetatable(L, "scene.imesh");
+ lua_setmetatable(L, -2);
+
+ lnode->n = light;
+ lnode->funcmap = hashmap_new();
+ lnode->type = "ilight";
+
+ //Put it on top and return it
+ lua_rawgeti(L,LUA_REGISTRYINDEX,tref);
+ return 1;
+
+ }
+
+static const luaL_reg icamera_f[] = {
+ {"new", newiscenelight},
+// {"gethandeler", guigethandeler},
+// {"sethandeler", guisethandeler},
+ {0,0},
+};
+
+static const luaL_reg icamera_m[] = {
+ {"getpos", iscenegetpos},
+ {"setpos", iscenesetpos},
+// {"move", moveiguielement},
+// {"settext", setiguitext},
+// {"remove", removeiguielement},
+ {0, 0},
+};
+
+int ilight_register(lua_State* L, IrrlichtDevice* d){
+
+ device = d;
+
+ printf("icamera registered\n");
+
+ luaL_newmetatable(L, "scene.ilight");
+
+ luaL_register(L,"ilight",icamera_f);
+
+ lua_pushstring(L,"__index");
+ lua_pushstring(L,"gethandeler");
+ lua_gettable(L,-3);
+ lua_settable(L,-4);
+
+ lua_pushstring(L,"__newindex");
+ lua_pushstring(L,"sethandeler");
+ lua_gettable(L,-3);
+ lua_settable(L,-4);
+
+ luaL_register(L, NULL, icamera_m);
+
+ return 1;
+}
diff --git a/src/client/lua_api/scene/ilight.hpp b/src/client/lua_api/scene/ilight.hpp
new file mode 100644
index 0000000..87d2841
--- /dev/null
+++ b/src/client/lua_api/scene/ilight.hpp
@@ -0,0 +1,11 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+
+int ilight_register(lua_State* L, irr::IrrlichtDevice* d);
diff --git a/src/client/lua_api/scene/imesh.cpp b/src/client/lua_api/scene/imesh.cpp
index c95366f..bbb0017 100644
--- a/src/client/lua_api/scene/imesh.cpp
+++ b/src/client/lua_api/scene/imesh.cpp
@@ -35,58 +35,46 @@ static LISceneNode* checkismesh(lua_State* L){
}
*/
-//iscenecamera.new(Vector position, Vector lookat, parrent)
static int newiscenemesh(lua_State* L){
- printf("Createing mesh!\n");
- int nargs = lua_gettop(L);
- if(nargs != 1){
+ printf("Createing mesh!\n");
+ int nargs = lua_gettop(L);
+ if(nargs != 1){
printf("Incorrect # of args to create a mesh!");
- }
- //The model for the mesh
- const char* modelpath = luaL_optstring(L,1,"error");
-
-
- //Create the mesh
- ISceneManager* smgr = device->getSceneManager();
- IAnimatedMesh* amesh = smgr->getMesh(modelpath);
- IAnimatedMeshSceneNode* mesh = smgr->addAnimatedMeshSceneNode( amesh );
- printf("Registered the mesh!\n");
-
- //Register it's callback
- //registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent);
-
- //Create it's lua representation
- LISceneNode* lmesh = (LISceneNode*)lua_newuserdata(L, sizeof(LISceneNode));
- int tref = luaL_ref(L,LUA_REGISTRYINDEX);
- //iguielements[lcam] = tref;
- lua_rawgeti(L,LUA_REGISTRYINDEX,tref);//Put it back on the stack since luaL_ref pops the object.
-
- //Set it's metatable
- luaL_getmetatable(L, "scene.imesh");
- lua_setmetatable(L, -2);
-
- //Create the struct
- lmesh->n = mesh;
- lmesh->funcmap = hashmap_new();
- lmesh->type = "imesh";
-
- //Free up anything made in this function
- //free(label);
-
- //Put it on top and return it
- lua_rawgeti(L,LUA_REGISTRYINDEX,tref);
- return 1;
-}
-
-/*mesh:setmaterial("string",layernum=0)*/
-static int setmaterial(lua_State* L){
- LISceneNode* n = checkismesh(L,1);
- //u32 layernum = luaL_optint(L,3,0);
- const char* matfile = luaL_optstring(L,2,"error.png");
- printf("Setting material on a %s",n->type);
- IVideoDriver* driver = device->getVideoDriver();
- n->n->setMaterialTexture( 0, driver->getTexture(matfile) );
- return 0;
+ }
+ //The model for the mesh
+ const char* modelpath = luaL_optstring(L,1,"error");
+
+ //Create the mesh
+ ISceneManager* smgr = device->getSceneManager();
+ IAnimatedMesh* amesh = smgr->getMesh(modelpath);
+ IAnimatedMeshSceneNode* mesh = smgr->addAnimatedMeshSceneNode( amesh );
+ mesh->setMaterialFlag(EMF_GOURAUD_SHADING,true);
+ printf("Registered the mesh!\n");
+
+ //Register it's callback
+ //registerguicallback(llabel,EGET_ELEMENT_HOVERED,iguilabelevent);
+
+ //Create it's lua representation
+ LISceneNode* lmesh = (LISceneNode*)lua_newuserdata(L, sizeof(LISceneNode));
+ int tref = luaL_ref(L,LUA_REGISTRYINDEX);
+ //iguielements[lcam] = tref;
+ lua_rawgeti(L,LUA_REGISTRYINDEX,tref);//Put it back on the stack since luaL_ref pops the object.
+
+ //Set it's metatable
+ luaL_getmetatable(L, "scene.imesh");
+ lua_setmetatable(L, -2);
+
+ //Create the struct
+ lmesh->n = mesh;
+ lmesh->funcmap = hashmap_new();
+ lmesh->type = "imesh";
+
+ //Free up anything made in this function
+ //free(label);
+
+ //Put it on top and return it
+ lua_rawgeti(L,LUA_REGISTRYINDEX,tref);
+ return 1;
}
static const luaL_reg imesh_f[] = {
@@ -97,7 +85,7 @@ static const luaL_reg imesh_f[] = {
};
static const luaL_reg imesh_m[] = {
- {"setMaterial", setmaterial},
+ {"setMaterial", iscenesetmaterial},
{"getpos", iscenegetpos},
{"setpos", iscenesetpos},
// {"settext", setiguitext},
diff --git a/src/client/main.cpp b/src/client/main.cpp
index 65dd99c..52de76f 100644
--- a/src/client/main.cpp
+++ b/src/client/main.cpp
@@ -55,93 +55,98 @@ void ClearObjects() {
for(list<btRigidBody *>::Iterator Iterator = Objects.begin(); Iterator != Objects.end(); ++Iterator) {
btRigidBody *Object = *Iterator;
+ printf("Found an object to clean:%p\n",Object);
+
// Delete irrlicht node
ISceneNode *Node = static_cast<ISceneNode *>(Object->getUserPointer());
+ printf("got node\n");
Node->remove();
-
+ printf("removed node\n");
// Remove the object from the world
World->removeRigidBody(Object);
-
+ printf("removed rigidbody\n");
+ printf("right before delete, object is %p\n",Object);
delete Object;
+ printf("deleted object\n");
}
Objects.clear();
}
// Create a box rigid body
-void CreateBox(ISceneManager* irrScene, const btVector3 &TPosition, const core::vector3df &TScale, btScalar TMass) {
-
- // Create an Irrlicht cube
- scene::ISceneNode *Node = irrScene->addCubeSceneNode(1.0f);
- Node->setScale(TScale);
-
- Node->setMaterialFlag(video::EMF_LIGHTING, 1);
- Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
- //Node->setMaterialTexture(0, irrDriver->getTexture("rust0.jpg"));
-
- // Set the initial position of the object
- btTransform Transform;
- Transform.setIdentity();
- Transform.setOrigin(TPosition);
-
- // Give it a default MotionState
- btDefaultMotionState *MotionState = new btDefaultMotionState(Transform);
-
- // Create the shape
- btVector3 HalfExtents(TScale.X * 0.5f, TScale.Y * 0.5f, TScale.Z * 0.5f);
- btCollisionShape *Shape = new btBoxShape(HalfExtents);
-
- // Add mass
- btVector3 LocalInertia;
- Shape->calculateLocalInertia(TMass, LocalInertia);
-
- // Create the rigid body object
- btRigidBody *RigidBody = new btRigidBody(TMass, MotionState, Shape, LocalInertia);
-
- // Store a pointer to the irrlicht node so we can update it later
- RigidBody->setUserPointer((void *)(Node));
-
- // Add it to the world
- World->addRigidBody(RigidBody);
- Objects.push_back(RigidBody);
-}
-
-// Create a sphere rigid body
-void CreateSphere(ISceneManager* irrScene, const btVector3 &TPosition, btScalar TRadius, btScalar TMass) {
-
- // Create an Irrlicht sphere
- scene::ISceneNode *Node = irrScene->addSphereSceneNode(TRadius, 32);
- Node->setMaterialFlag(video::EMF_LIGHTING, 1);
- Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
- //Node->setMaterialTexture(0, irrDriver->getTexture("ice0.jpg"));
-
- // Set the initial position of the object
- btTransform Transform;
- Transform.setIdentity();
- Transform.setOrigin(TPosition);
-
- // Give it a default MotionState
- btDefaultMotionState *MotionState = new btDefaultMotionState(Transform);
-
- // Create the shape
- btCollisionShape *Shape = new btSphereShape(TRadius);
-
- // Add mass
- btVector3 LocalInertia;
- Shape->calculateLocalInertia(TMass, LocalInertia);
-
- // Create the rigid body object
- btRigidBody *RigidBody = new btRigidBody(TMass, MotionState, Shape, LocalInertia);
-
- // Store a pointer to the irrlicht node so we can update it later
- RigidBody->setUserPointer((void *)(Node));
-
- // Add it to the world
- World->addRigidBody(RigidBody);
- Objects.push_back(RigidBody);
-}
-
-// Converts a quaternion to an euler angle
+// void CreateBox(ISceneManager* irrScene, const btVector3 &TPosition, const core::vector3df &TScale, btScalar TMass) {
+//
+// // Create an Irrlicht cube
+// scene::ISceneNode *Node = irrScene->addCubeSceneNode(1.0f);
+// Node->setScale(TScale);
+//
+// Node->setMaterialFlag(video::EMF_LIGHTING, 1);
+// Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
+// //Node->setMaterialTexture(0, irrDriver->getTexture("rust0.jpg"));
+//
+// // Set the initial position of the object
+// btTransform Transform;
+// Transform.setIdentity();
+// Transform.setOrigin(TPosition);
+//
+// // Give it a default MotionState
+// btDefaultMotionState *MotionState = new btDefaultMotionState(Transform);
+//
+// // Create the shape
+// btVector3 HalfExtents(TScale.X * 0.5f, TScale.Y * 0.5f, TScale.Z * 0.5f);
+// btCollisionShape *Shape = new btBoxShape(HalfExtents);
+//
+// // Add mass
+// btVector3 LocalInertia;
+// Shape->calculateLocalInertia(TMass, LocalInertia);
+//
+// // Create the rigid body object
+// btRigidBody *RigidBody = new btRigidBody(TMass, MotionState, Shape, LocalInertia);
+//
+// // Store a pointer to the irrlicht node so we can update it later
+// RigidBody->setUserPointer((void *)(Node));
+//
+// // Add it to the world
+// World->addRigidBody(RigidBody);
+// Objects.push_back(RigidBody);
+// }
+//
+// // Create a sphere rigid body
+// void CreateSphere(ISceneManager* irrScene, const btVector3 &TPosition, btScalar TRadius, btScalar TMass) {
+//
+// // Create an Irrlicht sphere
+// scene::ISceneNode *Node = irrScene->addSphereSceneNode(TRadius, 32);
+// Node->setMaterialFlag(video::EMF_LIGHTING, 1);
+// Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
+// //Node->setMaterialTexture(0, irrDriver->getTexture("ice0.jpg"));
+//
+// // Set the initial position of the object
+// btTransform Transform;
+// Transform.setIdentity();
+// Transform.setOrigin(TPosition);
+//
+// // Give it a default MotionState
+// btDefaultMotionState *MotionState = new btDefaultMotionState(Transform);
+//
+// // Create the shape
+// btCollisionShape *Shape = new btSphereShape(TRadius);
+//
+// // Add mass
+// btVector3 LocalInertia;
+// Shape->calculateLocalInertia(TMass, LocalInertia);
+//
+// // Create the rigid body object
+// btRigidBody *RigidBody = new btRigidBody(TMass, MotionState, Shape, LocalInertia);
+//
+// // Store a pointer to the irrlicht node so we can update it later
+// RigidBody->setUserPointer((void *)(Node));
+//
+// // Add it to the world
+// World->addRigidBody(RigidBody);
+// Objects.push_back(RigidBody);
+// }
+//
+// // Converts a quaternion to an euler angle
void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) {
btScalar W = TQuat.getW();
btScalar X = TQuat.getX();
@@ -162,7 +167,7 @@ void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) {
// - TDeltaTime tells the simulation how much time has passed since the last frame so the simulation can run independently of the frame rate.
void UpdatePhysics(u32 TDeltaTime) {
- World->stepSimulation(TDeltaTime * 0.001f, 60);
+ World->stepSimulation(TDeltaTime * 0.002f, 60);
btRigidBody *TObject;
// Relay the object's orientation to irrlicht
@@ -188,17 +193,22 @@ void UpdatePhysics(u32 TDeltaTime) {
void CreateStartScene(ISceneManager* smgr) {
ClearObjects();
- CreateBox(smgr,btVector3(0.0f, 0.0f, 0.0f), core::vector3df(10.0f, 0.5f, 10.0f), 0.0f);
+ //CreateBox(smgr,btVector3(0.0f, 0.0f, 0.0f), core::vector3df(10.0f, 0.5f, 10.0f), 0.0f);
}
int main(int argc, char *argv[]){
+ printf("Brok[en]gine");
// Initialize bullet
btBroadphaseInterface *BroadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000));
+ printf("Broadphase\n");
btDefaultCollisionConfiguration *CollisionConfiguration = new btDefaultCollisionConfiguration();
+ printf("Collision config\n");
btCollisionDispatcher *Dispatcher = new btCollisionDispatcher(CollisionConfiguration);
+ printf("Dispatcher\n");
btSequentialImpulseConstraintSolver *Solver = new btSequentialImpulseConstraintSolver();
+ printf("Solver\n");
World = new btDiscreteDynamicsWorld(Dispatcher, BroadPhase, Solver, CollisionConfiguration);
-
+ printf("Physics world init ok.\n");
//Create a new lua state, this gets shared everywhere
lua_State *state = luaL_newstate();
L = state;
@@ -253,10 +263,11 @@ int main(int argc, char *argv[]){
GetRandInt(5) / 5.0f + 0.2f, 1.0f);
*/
- guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
- rect<s32>(10,10,260,22), true);
-
- smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
+ //guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
+ // rect<s32>(10,10,260,22), true);
+ printf("Abbout to add camera\n");
+ //smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
+ //smgr->addCameraSceneNodeMaya();
printf("Everything registered, about to start running device!\n");
@@ -281,11 +292,12 @@ int main(int argc, char *argv[]){
lua_getfield(state,-1,"tick");
if(!lua_isnil(state,-1))
lua_call(state,0,0);
+ lua_pop(state,2);
}
printf("Claoseing lua state...\n");
//lua_close(state);
printf("clearing objects...\n");
- ClearObjects();
+ ClearObjects(); //Clearing objects must be done after we droped the device.
printf("cleared objects\n");
delete BroadPhase;
printf("deleted broadphase\n");