diff --git a/CMakeLists.txt b/CMakeLists.txt index 1cc55ac..81a744f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ set_property(SOURCE scripting/couch.i PROPERTY CPLUSPLUS ON) swig_add_library(couchlua TYPE STATIC LANGUAGE lua - SOURCES scripting/couch.i) + SOURCES scripting/couch.i scripting/lua/helpers.i) target_link_libraries(couchlua ${LUA_LIBRARIES}) target_link_libraries(couch couchlua) diff --git a/core/Ball.cpp b/core/Ball.cpp deleted file mode 100644 index abf78c0..0000000 --- a/core/Ball.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "Ball.h" - -Ball::Ball() { - IndexList indices; - VertexList vertices; - // It's a cube really - // Front - vertices.push_back(Vertex(1.0f, 1.0f, 1.0f, 0.0f, 1.0f)); - vertices.push_back(Vertex(1.0f, -1.0f, 1.0f, 0.0f, 0.0f)); - vertices.push_back(Vertex(-1.0f, 1.0f, 1.0f, 1.0f, 1.0f)); - vertices.push_back(Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f)); - // Back - vertices.push_back(Vertex(1.0f, 1.0f, -1.0f, 0.0f, 0.0f)); - vertices.push_back(Vertex(1.0f, -1.0f, -1.0f, 0.0f, 1.0f)); - vertices.push_back(Vertex(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f)); - vertices.push_back(Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f)); - - //Front - indices.push_back(Index(0, 1, 2)); - indices.push_back(Index(1, 2, 3)); - //Back - indices.push_back(Index(4, 5, 6)); - indices.push_back(Index(5, 6, 7)); - // Top - indices.push_back(Index(0, 4, 6)); - indices.push_back(Index(0, 2, 6)); - // Bottom - indices.push_back(Index(1, 3, 7)); - indices.push_back(Index(1, 5, 7)); - // Left side - indices.push_back(Index(0, 1, 5)); - indices.push_back(Index(0, 4, 5)); - // Right side - indices.push_back(Index(2, 3, 7)); - indices.push_back(Index(2, 6, 7)); - - submeshes.push_back(new SubMesh(vertices, indices)); -} - diff --git a/core/Ball.h b/core/Ball.h deleted file mode 100644 index 84aa577..0000000 --- a/core/Ball.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef BALL_H -#define BALL_H - -#include "Mesh.h" - -class Ball : public Mesh { -public: - Ball(); -}; - -#endif /* BALL_H */ diff --git a/core/Mesh.cpp b/core/Mesh.cpp index b6b1206..136d001 100644 --- a/core/Mesh.cpp +++ b/core/Mesh.cpp @@ -80,6 +80,8 @@ Mesh* Mesh::FromFile(const char *filename) { my_mesh->submeshes.push_back(aiMesh2SubMesh(mesh_to_import)); } + my_mesh->SetupMesh(); + return my_mesh; } diff --git a/core/Mesh.h b/core/Mesh.h index cde76ef..3e4267f 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -25,10 +25,11 @@ public: VertexList vertices; IndexList indices; Material material; - void SetupSubMesh(); void Draw(Shader *shader); private: Id VAO, VBO, EBO; + void SetupSubMesh(); + friend class Mesh; }; typedef std::vector SubMeshList; @@ -41,9 +42,9 @@ public: static Mesh *FromFile(const char *filename); virtual bool IsDrawable() const {return true;} virtual void Draw(Shader *shader); - virtual void SetupMesh(); protected: SubMeshList submeshes; + virtual void SetupMesh(); private: static SubMesh *aiMesh2SubMesh(aiMesh *mesh); }; diff --git a/core/couch.cpp b/core/couch.cpp index 990610c..fc80afd 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -13,10 +13,10 @@ #include "Screen.h" -#include "Ball.h" #include "Camera.h" #include "Input.h" #include "Node.h" +#include "Mesh.h" #include "Scripting/Lua.h" // Thirdparty Includes diff --git a/demo/cube.glb b/demo/cube.glb new file mode 100644 index 0000000..0f1a5cd Binary files /dev/null and b/demo/cube.glb differ diff --git a/demo/main.lua b/demo/main.lua index f78fd02..e2eefb9 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -29,15 +29,15 @@ function init() camera = couch.Camera() camera:MakeCurrent() camera.transform:Translate(0.0, 0.0, 10.0) - ball = couch.Ball() - ball:SetupMesh() + + ball = couch.Mesh.FromFile("cube.glb") material = couch.Material() material.color = RED material.usesColor = true ball:SetMaterial(0, material) couch.Node.GetRoot().children:Append(ball) - ball1 = couch.Ball() - ball1:SetupMesh() + + ball1 = couch.Mesh.FromFile("cube.glb") material = couch.Material() material.tex = couch.Texture.FromFile("container.png") material.usesTex = true @@ -46,25 +46,11 @@ function init() ball1.transform:Translate(0.0, 3.0, 0.0) - trough = couch.Mesh.FromFile("trough.glb") - trough:SetupMesh() - material = couch.Material() - material.tex = couch.Texture.FromFile("wood_lowres.png") - material.usesTex = true - trough:SetMaterial(0, material) + trough = couch.TexturedMesh("trough.glb", "wood_lowres.png") couch.Node.GetRoot().children:Append(trough) trough.transform:Translate(10.0, 0.0, 0.0) - scaffold = couch.Mesh.FromFile("scaffold.glb") - scaffold:SetupMesh() - material = couch.Material() - material.tex = couch.Texture.FromFile("grate_floor_lowres.png") - material.usesTex = true - scaffold:SetMaterial(0, material) - material = couch.Material() - material.tex = couch.Texture.FromFile("railing.png") - material.usesTex = true - scaffold:SetMaterial(1, material) + scaffold = couch.TexturedMesh("scaffold.glb", "grate_floor_lowres.png", "railing.png") couch.Node.GetRoot().children:Append(scaffold) scaffold.transform:Translate(-10.0, 0.0, 0.0) end diff --git a/scripting/couch.i b/scripting/couch.i index 92b2fed..0bc3f16 100644 --- a/scripting/couch.i +++ b/scripting/couch.i @@ -1,6 +1,9 @@ %module couch %include "typemaps.i" +#ifdef SWIGLUA +%include "lua/helpers.i" +#endif // SWIGLUA %{ #include "types.h" @@ -8,7 +11,6 @@ #include "Transform.h" #include "Spatial.h" #include "Mesh.h" -#include "Ball.h" #include "Material.h" #include "Camera.h" %} @@ -38,7 +40,6 @@ public: %include "Node.h" %include "Spatial.h" %include "Mesh.h" -%include "Ball.h" %include "Transform.h" %include "Material.h" %include "Camera.h" diff --git a/scripting/lua/helpers.i b/scripting/lua/helpers.i new file mode 100644 index 0000000..e2cdc6f --- /dev/null +++ b/scripting/lua/helpers.i @@ -0,0 +1,19 @@ +%module helpers + +%luacode { +function couch.TexturedMesh(meshfile, ...) + local mesh = couch.Mesh.FromFile(meshfile) + for i, texturefile in ipairs({...}) do + local material = couch.Material() + material.usesTex = true + material.tex = couch.Texture.FromFile(texturefile) + mesh:SetMaterial(i - 1, material) + end + return mesh +end + +} // luacode + +// Local Variables: +// mode: poly-swig +// End: