diff --git a/doc/tutorial/14_lol_lua.cpp b/doc/tutorial/14_lol_lua.cpp index 3bf6807a..5f67413d 100644 --- a/doc/tutorial/14_lol_lua.cpp +++ b/doc/tutorial/14_lol_lua.cpp @@ -30,13 +30,8 @@ public: UNUSED(arg_nb); return new DemoObject(); } - static const char* GetClassName() { static const char name[] = "LoluaDemo"; return name; } - static const char* GetClassLibName() { static const char name[] = "LoluaDemoLib"; return name; } - static const char* GetClassInstName() { static const char name[] = "LoluaDemoInst"; return name; } - - static const Lolua::ClassMethod* GetStaticMethods(); - static const Lolua::ClassMethod* GetInstanceMethods(); + //------------------------------------------------------------------------- static int AddFive(Lolua::State* l) { Lolua::Var i(l, 1); @@ -54,13 +49,35 @@ public: { return (f + 10); } -}; -//----------------------------------------------------------------------------- -static const Lolua::ClassMethod loluademo_statics[] = { { "AddFive", &DemoObject::AddFive }, { NULL, NULL } }; -static const Lolua::ClassMethod loluademo_methods[] = { { "AddTenInstance", &DemoObject::AddTenInstance }, { NULL, NULL } }; -const Lolua::ClassMethod* DemoObject::GetStaticMethods() { return loluademo_statics; } -const Lolua::ClassMethod* DemoObject::GetInstanceMethods() { return loluademo_methods; } + static int GetX(Lolua::State* l) + { + Lolua::VarPtr obj(l, 1); + Lolua::Var i; + i = obj.V()->m_x; + return i.Return(l); + } + static int SetX(Lolua::State* l) + { + Lolua::VarPtr obj(l, 1); + Lolua::Var i(l, 2); + obj.V()->m_x = i.V(); + return 0; + } + + //------------------------------------------------------------------------- + static const Lolua::ObjectLib* GetLib() + { + static const Lolua::ObjectLib lib = Lolua::ObjectLib( + "LoluaDemo", + { { "AddFive", &DemoObject::AddFive } }, + { { "AddTenInstance", &DemoObject::AddTenInstance } }, + { { "X", &DemoObject::GetX, &DemoObject::SetX } }); + return &lib; + } + + int m_x = 0; +}; //----------------------------------------------------------------------------- static int GlobalAddString(Lolua::State* l) @@ -88,6 +105,29 @@ public: { } + void TestStuff() + { + Lolua::State* l = GetLuaState(); + + /* + //create property + lua_pushnumber(l, 5.0); + lua_setfield(l, -2, "x"); + + lua_getglobal(l, "vector"); + int table = lua_istable(l, -1); + lua_getfield(l, -1, "x"); + Lolua::Var var(l, -1); + lua_pop(l, 2); + vec3 t; + */ + + //table = lua_isuserdata(l, -1); + //Var var(m_lua_state, -1); + //lua_pop(m_lua_state, 1); + //lua_getfield (lua_State *L, int index, const char *k); + //return var.V(); + } }; //----------------------------------------------------------------------------- @@ -106,6 +146,7 @@ public: //Execute script demo_loader->ExecLua("14_lol_lua.lua"); + demo_loader->TestStuff(); //Grab global test values float testvalue_num = demo_loader->GetVar("testvalue_num"); @@ -118,17 +159,16 @@ public: //Grab global values modified with DemoObject int32_t loluademo_return = demo_loader->GetVar("loluademo_return"); + int32_t loluademo_getx = demo_loader->GetVar("loluademo_getx"); float loluademo_inst_return = demo_loader->GetVar("loluademo_inst_return"); DemoObject* loluademo_inst = demo_loader->GetPtr("loluademo_inst"); - String loluademo_inst_name = loluademo_inst->GetClassName(); - Log::Info("Lua Vars: \ testvalue_num: %.2f, testvalue_int: %i, testvalue_uint: %i, testvalue_str: %s.\n", testvalue_num, testvalue_int, testvalue_uint, testvalue_str.C()); Log::Info("Lua Vars: \ - function_return: %s, loluademo_return: %i, loluademo_inst_return: %.f, loluademo_inst_name: %s.\n", - function_return.C(), loluademo_return, loluademo_inst_return, loluademo_inst_name.C()); + function_return: %s, loluademo_return: %i, loluademo_inst_return: %.f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n", + function_return.C(), loluademo_return, loluademo_inst_return, loluademo_getx, loluademo_inst->m_x); delete demo_loader; diff --git a/doc/tutorial/14_lol_lua.lua b/doc/tutorial/14_lol_lua.lua index f55aced9..cea94073 100644 --- a/doc/tutorial/14_lol_lua.lua +++ b/doc/tutorial/14_lol_lua.lua @@ -11,4 +11,6 @@ function_return = GlobalAddString("test"); loluademo_return = LoluaDemo.AddFive(1); loluademo_inst = LoluaDemo.New(); +loluademo_inst:SetX(10); +loluademo_getx = loluademo_inst:GetX(); loluademo_inst_return = loluademo_inst:AddTenInstance(2.0); diff --git a/src/Makefile.am b/src/Makefile.am index 724b52aa..6efc7546 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -75,6 +75,7 @@ liblolcore_sources = \ easymesh/easymeshinternal.cpp easymesh/easymeshcsg.cpp \ easymesh/easymeshprimitive.cpp easymesh/easymeshtransform.cpp \ easymesh/easymeshcursor.cpp easymesh/easymesh.h \ + easymesh/easymeshlua.cpp easymesh/easymeshlua.h \ easymesh/csgbsp.cpp easymesh/csgbsp.h \ easymesh/shiny.lolfx easymesh/shinyflat.lolfx \ easymesh/shinydebugwireframe.lolfx \ diff --git a/src/application/baselua.h b/src/application/baselua.h index 6ec4405d..879abe01 100644 --- a/src/application/baselua.h +++ b/src/application/baselua.h @@ -25,8 +25,75 @@ namespace Lolua //----------------------------------------------------------------------------- typedef luaL_Reg ClassMethod; +typedef struct ClassVar +{ + const char *name; + lua_CFunction get; + lua_CFunction set; +} ClassVar; typedef lua_State State; +//----------------------------------------------------------------------------- +struct ObjectLib +{ + typedef struct ClassVarStr + { + ClassVarStr() { } + ClassVarStr(String var_name, lua_CFunction get, lua_CFunction set) + { + m_get_name = String("Get") + var_name; + m_set_name = String("Set") + var_name; + m_get = get; + m_set = set; + } + String m_get_name = ""; + String m_set_name = ""; + lua_CFunction m_get = nullptr; + lua_CFunction m_set = nullptr; + } ClassVarStr; + + ObjectLib(String class_name, + array statics, + array methods, + array variables) + { + m_class_name = class_name; + m_static_name = class_name + "_lib"; + m_method_name = class_name + "_inst"; + + m_statics = statics; + if (m_statics.Count() == 0 + || m_statics.Last().name != nullptr + || m_statics.Last().func != nullptr) + m_statics.Push({ nullptr, nullptr }); + + m_methods = methods; + if (m_methods.Count() == 0 + || m_methods.Last().name != nullptr + || m_methods.Last().func != nullptr) + m_methods.Push({ nullptr, nullptr }); + + for (ClassVar& cv : variables) + { + if (cv.name && cv.get && cv.set) + { + m_variables.Push({ cv.name, cv.get, cv.set }); + } + } + if (m_variables.Count() == 0 + || variables.Last().name != nullptr + || variables.Last().get != nullptr + || variables.Last().set != nullptr) + m_variables.Push(ClassVarStr()); + } + String m_class_name = ""; + String m_static_name = ""; + String m_method_name = ""; + array m_statics; + array m_methods; + array m_variables; +}; + //----------------------------------------------------------------------------- class ObjectDef { @@ -40,11 +107,7 @@ public: ASSERT(false); return nullptr; } - static const char* GetClassName() { ASSERT(false); return nullptr; } - static const char* GetClassLibName() { ASSERT(false); return nullptr;; } - static const char* GetClassInstName() { ASSERT(false); return nullptr; } - static const ClassMethod* GetStaticMethods() { ASSERT(false); return nullptr; } - static const ClassMethod* GetInstanceMethods() { ASSERT(false); return nullptr; } + static const ObjectLib* GetLib() { ASSERT(false); return nullptr; } }; //----------------------------------------------------------------------------- @@ -64,7 +127,7 @@ public: //Default statics static const luaL_Reg default_statics[] { - { "New", New }, + { "New", New }, { "__gc", Del }, { NULL, NULL } }; @@ -82,26 +145,66 @@ public: //__le : Less than or equal to(<= ) //Create Static metatable - luaL_newmetatable(l, TLuaClass::GetClassLibName()); + luaL_newmetatable(l, GetStaticName()); //Register default statics and template one luaL_setfuncs(l, default_statics, 0); - luaL_setfuncs(l, TLuaClass::GetStaticMethods(), 0); - //Push funcs on metatable + luaL_setfuncs(l, GetStaticMethods(), 0); + //Push metatable on stack lua_pushvalue(l, -1); //Set the "__index" field of the metatable to point to itself, pops the stack lua_setfield(l, -1, "__index"); //Set it global to validate the operation - lua_setglobal(l, TLuaClass::GetClassName()); + lua_setglobal(l, GetObjectName()); //Repeat all the operations for instance metatable - luaL_newmetatable(l, TLuaClass::GetClassInstName()); - luaL_setfuncs(l, TLuaClass::GetInstanceMethods(), 0); + luaL_newmetatable(l, GetMethodName()); + luaL_setfuncs(l, GetInstanceMethods(), 0); lua_pushvalue(l, -1); lua_setfield(l, -1, "__index"); + + //Create variables Get/Set + const array& variables = GetVariables(); + for (const ObjectLib::ClassVarStr& var : variables) + { + if (!var.m_get || !var.m_set) + continue; + + //Add getter + lua_pushcfunction(l, var.m_get); + lua_setfield(l, -2, var.m_get_name.C()); + + //Add setter + lua_pushcfunction(l, var.m_set); + lua_setfield(l, -2, var.m_set_name.C()); + } + //Don't set it to global, but pop the stack to hide the metatable lua_pop(l, 1); } +private: + template + static const ObjectLib* GetLib() + { + const ObjectLib* lib = TLuaClass::GetLib(); + ASSERT(lib); + return lib; + } + +public: + template + static const char* GetObjectName() { return GetLib()->m_class_name.C(); } + template + static const char* GetStaticName() { return GetLib()->m_static_name.C(); } + template + static const char* GetMethodName() { return GetLib()->m_method_name.C(); } + template + static const ClassMethod* GetStaticMethods() { return GetLib()->m_statics.Data(); } + template + static const ClassMethod* GetInstanceMethods() { return GetLib()->m_methods.Data(); } + template + static const array& GetVariables() { return GetLib()->m_variables; } + protected: //------------------------------------------------------------------------- template @@ -115,7 +218,7 @@ protected: *data = TLuaClass::New(l, n_args); //Retrieve instance table - luaL_getmetatable(l, TLuaClass::GetClassInstName()); + luaL_getmetatable(l, GetMethodName()); //Set metatable to instance lua_setmetatable(l, -2); //Return 1 so Lua will get the UserData and clean the stack. @@ -134,10 +237,9 @@ protected: }; //----------------------------------------------------------------------------- -// -//-- -struct Function +class Function { +public: Function(State* l, const char* name, int(*function)(State*)) { lua_pushcfunction(l, function); @@ -147,7 +249,7 @@ struct Function //----------------------------------------------------------------------------- template -struct VarPtr +class VarPtr { protected: T* m_value = nullptr; @@ -155,15 +257,15 @@ protected: public: VarPtr() { } VarPtr(T* value) { m_value = value; } - VarPtr(State* l, int index) { InnerGet(l, index); } + VarPtr(State* l, int index) { InnerGet(l, index); } inline T* V() { return m_value; } inline T* operator=(T* value) { m_value = value; } - inline int Return(State* l) { InnerPush(l); return 1; } + inline int Return(State* l) { InnerPush(l); return 1; } protected: virtual void InnerGet(State* l, int index) { - T** obj = static_cast(luaL_checkudata(l, index, T::GetClassInstName())); + T** obj = static_cast(luaL_checkudata(l, index, Object::GetMethodName())); m_value = obj ? *obj : nullptr; } void InnerPush(State* l) @@ -174,7 +276,7 @@ protected: }; //----------------------------------------------------------------------------- template -struct VarPtrLight +class VarPtrLight { public: VarPtrLight() : VarPtr() { } @@ -183,14 +285,14 @@ public: protected: virtual void InnerGet(State* l, int index) { - T** obj = static_cast(luaL_testudata(l, index, T::GetClassInstName())); + T** obj = static_cast(luaL_testudata(l, index, Object::GetMethodName())); m_value = obj ? *obj : nullptr; } }; //----------------------------------------------------------------------------- template -struct Var +class Var { private: T m_value; @@ -201,20 +303,20 @@ public: Var(State* l, int index) { InnerGet(l, index); } inline T& V() { return m_value; } inline int Return(State* l) { InnerPush(l); return 1; } - inline Var& operator-(const T& value) { m_value - value; return *this; } - inline Var& operator+(const T& value) { m_value + value; return *this; } + inline Var& operator-(const T& value) { m_value - value; return *this; } + inline Var& operator+(const T& value) { m_value + value; return *this; } inline Var& operator*(const T& value) { m_value * value; return *this; } - inline Var& operator/(const T& value) { m_value / value; return *this; } - inline Var& operator=(const T& value) { m_value = value; return *this; } + inline Var& operator/(const T& value) { m_value / value; return *this; } + inline Var& operator=(const T& value) { m_value = value; return *this; } inline Var& operator-=(const T& value) { m_value -= value; return *this; } inline Var& operator+=(const T& value) { m_value += value; return *this; } inline Var& operator*=(const T& value) { m_value *= value; return *this; } inline Var& operator/=(const T& value) { m_value /= value; return *this; } - inline Var& operator-(const Var& o) { m_value - o.m_value; return *this; } - inline Var& operator+(const Var& o) { m_value + o.m_value; return *this; } + inline Var& operator-(const Var& o) { m_value - o.m_value; return *this; } + inline Var& operator+(const Var& o) { m_value + o.m_value; return *this; } inline Var& operator*(const Var& o) { m_value * o.m_value; return *this; } - inline Var& operator/(const Var& o) { m_value / o.m_value; return *this; } - inline Var& operator=(const Var& o) { m_value = o.m_value; return *this; } + inline Var& operator/(const Var& o) { m_value / o.m_value; return *this; } + inline Var& operator=(const Var& o) { m_value = o.m_value; return *this; } inline Var& operator-=(const Var& o) { m_value -= o.m_value; return *this; } inline Var& operator+=(const Var& o) { m_value += o.m_value; return *this; } inline Var& operator*=(const Var& o) { m_value *= o.m_value; return *this; } diff --git a/src/easymesh/csgbsp.cpp b/src/easymesh/csgbsp.cpp index 04ed133d..d8fa56d7 100644 --- a/src/easymesh/csgbsp.cpp +++ b/src/easymesh/csgbsp.cpp @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Csg: The code belonging to CSG operations // // Copyright: (c) 2010-2013 Sam Hocevar // (c) 2010-2013 Benjamin "Touky" Huet @@ -9,11 +9,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include namespace lol diff --git a/src/easymesh/csgbsp.h b/src/easymesh/csgbsp.h index 8a0821a8..aa3e4d59 100644 --- a/src/easymesh/csgbsp.h +++ b/src/easymesh/csgbsp.h @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Csg: The code belonging to CSG operations // // Copyright: (c) 2010-2013 Sam Hocevar // (c) 2010-2013 Benjamin "Touky" Huet @@ -11,11 +11,6 @@ #pragma once -// -// The EasyMesh class -// ------------------ -// - namespace lol { diff --git a/src/easymesh/easymesh.cpp b/src/easymesh/easymesh.cpp index a42bd280..073b3b75 100644 --- a/src/easymesh/easymesh.cpp +++ b/src/easymesh/easymesh.cpp @@ -1,5 +1,6 @@ // -// Lol Engine +// EasyMesh: A class about generating 3D mesh without using the hands +// Mesh can be generated using C++ or lua code // // Copyright: (c) 2010-2015 Sam Hocevar // (c) 2009-2015 Cédric Lecacheur @@ -10,11 +11,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include LOLFX_RESOURCE_DECLARE(shiny); @@ -45,170 +41,6 @@ EasyMesh::EasyMesh(const EasyMesh& em) m_state = MeshRender::NeedData; } -//----------------------------------------------------------------------------- -bool EasyMesh::Compile(char const *command, bool Execute) -{ - bool res = false; - // FIXME: make this work again -#if 0 - EasyMeshCompiler mc(*this); - BD()->Enable(MeshBuildOperation::CommandRecording); - if ((res = mc.ParseString(command))) - { - BD()->Disable(MeshBuildOperation::CommandRecording); - if (Execute) - ExecuteCmdStack(); - } -#endif - return res; -} - -//----------------------------------------------------------------------------- -#define EZSET(M0) BD()->CmdStack().GetValue(M0); -#define EZDEF_1(T0) T0 m0; EZSET(m0) -#define EZDEF_2(T0, T1) EZDEF_1(T0) T1 m1; EZSET(m1) -#define EZDEF_3(T0, T1, T2) EZDEF_2(T0, T1) T2 m2; EZSET(m2) -#define EZDEF_4(T0, T1, T2, T3) EZDEF_3(T0, T1, T2) T3 m3; EZSET(m3) -#define EZDEF_5(T0, T1, T2, T3, T4) EZDEF_4(T0, T1, T2, T3) T4 m4; EZSET(m4) -#define EZDEF_6(T0, T1, T2, T3, T4, T5) EZDEF_5(T0, T1, T2, T3, T4) T5 m5; EZSET(m5) -#define EZDEF_7(T0, T1, T2, T3, T4, T5, T6) EZDEF_6(T0, T1, T2, T3, T4, T5) T6 m6; EZSET(m6) -#define EZDEF_8(T0, T1, T2, T3, T4, T5, T6, T7) EZDEF_7(T0, T1, T2, T3, T4, T5, T6) T7 m7; EZSET(m7) -#define EZDEF_9(T0, T1, T2, T3, T4, T5, T6, T7, T8) EZDEF_8(T0, T1, T2, T3, T4, T5, T6, T7) T8 m8; EZSET(m8) -#define EZDEF_10(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) EZDEF_9(T0, T1, T2, T3, T4, T5, T6, T7, T8) T9 m9; EZSET(m9) - -//---- -#define EZCALL_1(F) F(); -#define EZCALL_2(F, T0) EZDEF_1(T0) F(m0); -#define EZCALL_3(F, T0, T1) EZDEF_2(T0, T1) F(m0, m1); -#define EZCALL_4(F, T0, T1, T2) EZDEF_3(T0, T1, T2) F(m0, m1, m2); -#define EZCALL_5(F, T0, T1, T2, T3) EZDEF_4(T0, T1, T2, T3) F(m0, m1, m2, m3); -#define EZCALL_6(F, T0, T1, T2, T3, T4) EZDEF_5(T0, T1, T2, T3, T4) F(m0, m1, m2, m3, m4); -#define EZCALL_7(F, T0, T1, T2, T3, T4, T5) EZDEF_6(T0, T1, T2, T3, T4, T5) F(m0, m1, m2, m3, m4, m5); -#define EZCALL_8(F, T0, T1, T2, T3, T4, T5, T6) EZDEF_7(T0, T1, T2, T3, T4, T5, T6) F(m0, m1, m2, m3, m4, m5, m6); -#define EZCALL_9(F, T0, T1, T2, T3, T4, T5, T6, T7) EZDEF_8(T0, T1, T2, T3, T4, T5, T6, T7) F(m0, m1, m2, m3, m4, m5, m6, m7); -#define EZCALL_10(F, T0, T1, T2, T3, T4, T5, T6, T7, T8) EZDEF_9(T0, T1, T2, T3, T4, T5, T6, T7, T8) F(m0, m1, m2, m3, m4, m5, m6, m7, m8); -#define EZCALL_11(F, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) EZDEF_10(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) F(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9); - -//---- -#define EZM_CALL_FUNC(...) \ - LOL_CALL(LOL_CAT(EZCALL_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__)) - -//----------------------------------------------------------------------------- -void EasyMesh::ExecuteCmdStack(bool ExecAllStack) -{ -#define DO_EXEC_CMD(MESH_CMD, FUNC_PARAMS) \ - case EasyMeshCmdType::MESH_CMD: \ - { EZM_CALL_FUNC FUNC_PARAMS; break; } - - BD()->Enable(MeshBuildOperation::CommandExecution); - if (ExecAllStack) - BD()->Cmdi() = 0; - - for (; BD()->Cmdi() < BD()->CmdStack().GetCmdNb() && BD()->CmdExecNb() != 0; ++BD()->Cmdi()) - { - if (BD()->CmdExecNb() > 0) - --BD()->CmdExecNb(); - - switch (BD()->CmdStack().GetCmd(BD()->Cmdi())) - { - DO_EXEC_CMD(MeshCsg, (MeshCsg, CSGUsage)) - DO_EXEC_CMD(LoopStart, (LoopStart, int)) - DO_EXEC_CMD(LoopEnd, (LoopEnd)) - DO_EXEC_CMD(OpenBrace, (OpenBrace)) - DO_EXEC_CMD(CloseBrace, (CloseBrace)) - DO_EXEC_CMD(ScaleWinding, (ToggleScaleWinding)) - DO_EXEC_CMD(QuadWeighting, (ToggleQuadWeighting)) - DO_EXEC_CMD(PostBuildNormal, (TogglePostBuildNormal)) - DO_EXEC_CMD(PreventVertCleanup, (ToggleVerticeNoCleanup)) - DO_EXEC_CMD(VerticesMerge, (VerticesMerge)) - DO_EXEC_CMD(VerticesSeparate, (VerticesSeparate)) - DO_EXEC_CMD(SetColorA, (SetCurColorA, vec4)) - DO_EXEC_CMD(SetColorB, (SetCurColorB, vec4)) - DO_EXEC_CMD(SetVertColor, (SetVertColor, vec4)) - DO_EXEC_CMD(Translate, (Translate, vec3)) - DO_EXEC_CMD(Rotate, (Rotate, float, vec3)) - DO_EXEC_CMD(RadialJitter, (RadialJitter, float)) - DO_EXEC_CMD(MeshTranform, (DoMeshTransform, MeshTransform, Axis, Axis, float, float, float, bool)) - DO_EXEC_CMD(Scale, (Scale, vec3)) - DO_EXEC_CMD(DupAndScale, (DupAndScale, vec3, bool)) - DO_EXEC_CMD(Chamfer, (Chamfer, float)) - DO_EXEC_CMD(SplitTriangles, (SplitTriangles, int)) - DO_EXEC_CMD(SmoothMesh, (SmoothMesh, int, int, int)) - DO_EXEC_CMD(AppendCylinder, (AppendCylinder, int, float, float, float, bool, bool, bool)) - DO_EXEC_CMD(AppendCapsule, (AppendCapsule, int, float, float)) - DO_EXEC_CMD(AppendTorus, (AppendTorus, int, float, float)) - DO_EXEC_CMD(AppendBox, (AppendBox, vec3, float, bool)) - DO_EXEC_CMD(AppendStar, (AppendStar, int, float, float, bool, bool)) - DO_EXEC_CMD(AppendExpandedStar, (AppendExpandedStar, int, float, float, float)) - DO_EXEC_CMD(AppendDisc, (AppendDisc, int, float, bool)) - DO_EXEC_CMD(AppendSimpleTriangle, (AppendSimpleTriangle, float, bool)) - DO_EXEC_CMD(AppendSimpleQuad, (AppendSimpleQuad, vec2, vec2, float, bool)) - DO_EXEC_CMD(AppendCog, (AppendCog, int, float, float, float, float, float, float, float, float, bool)) - default: - ASSERT(0, "Unknown command pseudo bytecode"); - } - } - BD()->Disable(MeshBuildOperation::CommandExecution); - - if (!BD()->IsEnabled(MeshBuildOperation::PreventVertCleanup)) - VerticesCleanup(); - - if (BD()->IsEnabled(MeshBuildOperation::PostBuildComputeNormals)) - ComputeNormals(0, (int)m_indices.Count()); - - BD()->Disable(MeshBuildOperation::PostBuildComputeNormals); - BD()->Disable(MeshBuildOperation::PreventVertCleanup); - - if (BD()->CmdExecNb() > 0) - BD()->CmdExecNb() = -1; -} - -//----------------------------------------------------------------------------- -void EasyMesh::MeshConvert() -{ - /* Default material */ - Shader *shader = Shader::Create(LOLFX_RESOURCE_NAME(shiny)); - - /* Push index buffer to GPU */ - IndexBuffer *ibo = new IndexBuffer(m_indices.Count() * sizeof(uint16_t)); - uint16_t *indices = (uint16_t *)ibo->Lock(0, 0); - for (int i = 0; i < m_indices.Count(); ++i) - indices[i] = m_indices[i]; - ibo->Unlock(); - - /* Push vertex buffer to GPU */ - struct Vertex - { - vec3 pos, normal; - u8vec4 color; - vec4 texcoord; - }; - - VertexDeclaration *vdecl = new VertexDeclaration( - VertexStream(VertexUsage::Position, - VertexUsage::Normal, - VertexUsage::Color, - VertexUsage::TexCoord)); - - VertexBuffer *vbo = new VertexBuffer(m_vert.Count() * sizeof(Vertex)); - Vertex *vert = (Vertex *)vbo->Lock(0, 0); - for (int i = 0; i < m_vert.Count(); ++i) - { - vert[i].pos = m_vert[i].m_coord, - vert[i].normal = m_vert[i].m_normal, - vert[i].color = (u8vec4)(m_vert[i].m_color * 255.f); - vert[i].texcoord = m_vert[i].m_texcoord; - } - vbo->Unlock(); - - /* Reference our new data in our submesh */ - m_submeshes.Push(new SubMesh(shader, vdecl)); - m_submeshes.Last()->SetIndexBuffer(ibo); - m_submeshes.Last()->SetVertexBuffer(0, vbo); - - m_state = MeshRender::CanRender; -} - //----------------------------------------------------------------------------- bool EasyMesh::SetRender(bool should_render) { diff --git a/src/easymesh/easymesh.h b/src/easymesh/easymesh.h index e7fa6d0f..94fdad8d 100644 --- a/src/easymesh/easymesh.h +++ b/src/easymesh/easymesh.h @@ -1,5 +1,6 @@ // -// Lol Engine +// EasyMesh: A class about generating 3D mesh without using the hands +// Mesh can be generated using C++ or lua code // // Copyright: (c) 2010-2013 Sam Hocevar // (c) 2009-2013 Cédric Lecacheur @@ -12,14 +13,10 @@ #pragma once -// -// The EasyMesh class -// ------------------ -// - #include "commandstack.h" #include "easymeshrender.h" #include "easymeshbuild.h" +#include "easymeshlua.h" namespace lol { @@ -81,12 +78,19 @@ class EasyMesh : public Mesh public: EasyMesh(); EasyMesh(const EasyMesh& em); + MeshRender GetMeshState() { return m_state; } + bool SetRender(bool should_render); + + //------------------------------------------------------------------------- + //Render-build operation + //------------------------------------------------------------------------- + void MeshConvert(); + //------------------------------------------------------------------------- + //Command-build (lua now) operations + //------------------------------------------------------------------------- bool Compile(char const *command, bool Execute = true); void ExecuteCmdStack(bool ExecAllStack = true); - void MeshConvert(); - MeshRender GetMeshState() { return m_state; } - bool SetRender(bool should_render); private: void UpdateVertexDict(array< int, int > &vertex_dict); diff --git a/src/easymesh/easymeshbuild.cpp b/src/easymesh/easymeshbuild.cpp index 2e469f7d..8b9ba027 100644 --- a/src/easymesh/easymeshbuild.cpp +++ b/src/easymesh/easymeshbuild.cpp @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Build: The code belonging to Vertex build operations // // Copyright: (c) 2010-2013 Sam Hocevar // (c) 2009-2013 Cédric Lecacheur @@ -10,11 +10,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include namespace lol diff --git a/src/easymesh/easymeshbuild.h b/src/easymesh/easymeshbuild.h index 2f16a771..5dc97ad8 100644 --- a/src/easymesh/easymeshbuild.h +++ b/src/easymesh/easymeshbuild.h @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Build: The code belonging to Vertex build operations // // Copyright: (c) 2009-2013 Benjamin "Touky" Huet // (c) 2010-2014 Sam Hocevar @@ -12,11 +12,6 @@ #pragma once -// -// The EasyMesh class -// ------------------ -// - namespace lol { diff --git a/src/easymesh/easymeshcsg.cpp b/src/easymesh/easymeshcsg.cpp index 4f2939bf..f24e45c5 100644 --- a/src/easymesh/easymeshcsg.cpp +++ b/src/easymesh/easymeshcsg.cpp @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Csg: The code belonging to CSG operations // // Copyright: (c) 2010-2015 Sam Hocevar // (c) 2009-2015 Cédric Lecacheur @@ -10,11 +10,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include namespace lol diff --git a/src/easymesh/easymeshcursor.cpp b/src/easymesh/easymeshcursor.cpp index d799e4be..2190b4c8 100644 --- a/src/easymesh/easymeshcursor.cpp +++ b/src/easymesh/easymeshcursor.cpp @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Cursor: The code belonging to Cursor operations // // Copyright: (c) 2010-2015 Sam Hocevar // (c) 2009-2015 Cédric Lecacheur @@ -10,11 +10,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include namespace lol diff --git a/src/easymesh/easymeshinternal.cpp b/src/easymesh/easymeshinternal.cpp index 320b74b3..c042f661 100644 --- a/src/easymesh/easymeshinternal.cpp +++ b/src/easymesh/easymeshinternal.cpp @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Internal: The code belonging to internal operations // // Copyright: (c) 2010-2015 Sam Hocevar // (c) 2009-2015 Cédric Lecacheur @@ -10,11 +10,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include namespace lol diff --git a/src/easymesh/easymeshlua.cpp b/src/easymesh/easymeshlua.cpp new file mode 100644 index 00000000..8f277785 --- /dev/null +++ b/src/easymesh/easymeshlua.cpp @@ -0,0 +1,185 @@ +// +// MY CLASS TYPE +// +// Copyright © 2009-2015 Benjamin "Touky" Huet +// +// This program is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include +#include "loldebug.h" + +using namespace lol; + +//----------------------------------------------------------------------------- +EasyMeshLuaLoader::EasyMeshLuaLoader() : Lolua::Loader() +{ + Lolua::State* l = GetLuaState(); + + //Registering demo object + Lolua::Object::Register(l); +} + +//----------------------------------------------------------------------------- +EasyMeshLuaLoader::~EasyMeshLuaLoader() +{ + +} + +//----------------------------------------------------------------------------- +EasyMeshLuaObject::EasyMeshLuaObject() : Lolua::ObjectDef() +{ +} + +//----------------------------------------------------------------------------- +EasyMeshLuaObject::~EasyMeshLuaObject() +{ +} + +//----------------------------------------------------------------------------- +EasyMeshLuaObject* EasyMeshLuaObject::New(Lolua::State* l, int arg_nb) +{ + UNUSED(l); + UNUSED(arg_nb); + return new EasyMeshLuaObject(); +} + +//------------------------------------------------------------------------- +const Lolua::ObjectLib* EasyMeshLuaObject::GetLib() +{ + static const Lolua::ObjectLib lib = Lolua::ObjectLib( + "EasyMesh", + { { nullptr, nullptr } }, + { { nullptr, nullptr } }, + { { nullptr, nullptr, nullptr } }); + return &lib; +} + +//----------------------------------------------------------------------------- +bool EasyMesh::Compile(char const *command, bool Execute) +{ + bool res = false; + // FIXME: make this work again +#if 0 + EasyMeshCompiler mc(*this); + BD()->Enable(MeshBuildOperation::CommandRecording); + if ((res = mc.ParseString(command))) + { + BD()->Disable(MeshBuildOperation::CommandRecording); + if (Execute) + ExecuteCmdStack(); + } +#endif + return res; +} + +//----------------------------------------------------------------------------- +#define EZSET(M0) BD()->CmdStack().GetValue(M0); +#define EZDEF_1(T0) T0 m0; EZSET(m0) +#define EZDEF_2(T0, T1) EZDEF_1(T0) T1 m1; EZSET(m1) +#define EZDEF_3(T0, T1, T2) EZDEF_2(T0, T1) T2 m2; EZSET(m2) +#define EZDEF_4(T0, T1, T2, T3) EZDEF_3(T0, T1, T2) T3 m3; EZSET(m3) +#define EZDEF_5(T0, T1, T2, T3, T4) EZDEF_4(T0, T1, T2, T3) T4 m4; EZSET(m4) +#define EZDEF_6(T0, T1, T2, T3, T4, T5) EZDEF_5(T0, T1, T2, T3, T4) T5 m5; EZSET(m5) +#define EZDEF_7(T0, T1, T2, T3, T4, T5, T6) EZDEF_6(T0, T1, T2, T3, T4, T5) T6 m6; EZSET(m6) +#define EZDEF_8(T0, T1, T2, T3, T4, T5, T6, T7) EZDEF_7(T0, T1, T2, T3, T4, T5, T6) T7 m7; EZSET(m7) +#define EZDEF_9(T0, T1, T2, T3, T4, T5, T6, T7, T8) EZDEF_8(T0, T1, T2, T3, T4, T5, T6, T7) T8 m8; EZSET(m8) +#define EZDEF_10(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) EZDEF_9(T0, T1, T2, T3, T4, T5, T6, T7, T8) T9 m9; EZSET(m9) + +//---- +#define EZCALL_1(F) F(); +#define EZCALL_2(F, T0) EZDEF_1(T0) F(m0); +#define EZCALL_3(F, T0, T1) EZDEF_2(T0, T1) F(m0, m1); +#define EZCALL_4(F, T0, T1, T2) EZDEF_3(T0, T1, T2) F(m0, m1, m2); +#define EZCALL_5(F, T0, T1, T2, T3) EZDEF_4(T0, T1, T2, T3) F(m0, m1, m2, m3); +#define EZCALL_6(F, T0, T1, T2, T3, T4) EZDEF_5(T0, T1, T2, T3, T4) F(m0, m1, m2, m3, m4); +#define EZCALL_7(F, T0, T1, T2, T3, T4, T5) EZDEF_6(T0, T1, T2, T3, T4, T5) F(m0, m1, m2, m3, m4, m5); +#define EZCALL_8(F, T0, T1, T2, T3, T4, T5, T6) EZDEF_7(T0, T1, T2, T3, T4, T5, T6) F(m0, m1, m2, m3, m4, m5, m6); +#define EZCALL_9(F, T0, T1, T2, T3, T4, T5, T6, T7) EZDEF_8(T0, T1, T2, T3, T4, T5, T6, T7) F(m0, m1, m2, m3, m4, m5, m6, m7); +#define EZCALL_10(F, T0, T1, T2, T3, T4, T5, T6, T7, T8) EZDEF_9(T0, T1, T2, T3, T4, T5, T6, T7, T8) F(m0, m1, m2, m3, m4, m5, m6, m7, m8); +#define EZCALL_11(F, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) EZDEF_10(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) F(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9); + +//---- +#define EZM_CALL_FUNC(...) \ + LOL_CALL(LOL_CAT(EZCALL_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__)) + +//----------------------------------------------------------------------------- +void EasyMesh::ExecuteCmdStack(bool ExecAllStack) +{ +#define DO_EXEC_CMD(MESH_CMD, FUNC_PARAMS) \ + case EasyMeshCmdType::MESH_CMD: \ + { EZM_CALL_FUNC FUNC_PARAMS; break; } + + BD()->Enable(MeshBuildOperation::CommandExecution); + if (ExecAllStack) + BD()->Cmdi() = 0; + + for (; BD()->Cmdi() < BD()->CmdStack().GetCmdNb() && BD()->CmdExecNb() != 0; ++BD()->Cmdi()) + { + if (BD()->CmdExecNb() > 0) + --BD()->CmdExecNb(); + + switch (BD()->CmdStack().GetCmd(BD()->Cmdi())) + { + DO_EXEC_CMD(MeshCsg, (MeshCsg, CSGUsage)) + DO_EXEC_CMD(LoopStart, (LoopStart, int)) + DO_EXEC_CMD(LoopEnd, (LoopEnd)) + DO_EXEC_CMD(OpenBrace, (OpenBrace)) + DO_EXEC_CMD(CloseBrace, (CloseBrace)) + DO_EXEC_CMD(ScaleWinding, (ToggleScaleWinding)) + DO_EXEC_CMD(QuadWeighting, (ToggleQuadWeighting)) + DO_EXEC_CMD(PostBuildNormal, (TogglePostBuildNormal)) + DO_EXEC_CMD(PreventVertCleanup, (ToggleVerticeNoCleanup)) + DO_EXEC_CMD(VerticesMerge, (VerticesMerge)) + DO_EXEC_CMD(VerticesSeparate, (VerticesSeparate)) + DO_EXEC_CMD(SetColorA, (SetCurColorA, vec4)) + DO_EXEC_CMD(SetColorB, (SetCurColorB, vec4)) + DO_EXEC_CMD(SetVertColor, (SetVertColor, vec4)) + DO_EXEC_CMD(Translate, (Translate, vec3)) + DO_EXEC_CMD(Rotate, (Rotate, float, vec3)) + DO_EXEC_CMD(RadialJitter, (RadialJitter, float)) + DO_EXEC_CMD(MeshTranform, (DoMeshTransform, MeshTransform, Axis, Axis, float, float, float, bool)) + DO_EXEC_CMD(Scale, (Scale, vec3)) + DO_EXEC_CMD(DupAndScale, (DupAndScale, vec3, bool)) + DO_EXEC_CMD(Chamfer, (Chamfer, float)) + DO_EXEC_CMD(SplitTriangles, (SplitTriangles, int)) + DO_EXEC_CMD(SmoothMesh, (SmoothMesh, int, int, int)) + DO_EXEC_CMD(AppendCylinder, (AppendCylinder, int, float, float, float, bool, bool, bool)) + DO_EXEC_CMD(AppendCapsule, (AppendCapsule, int, float, float)) + DO_EXEC_CMD(AppendTorus, (AppendTorus, int, float, float)) + DO_EXEC_CMD(AppendBox, (AppendBox, vec3, float, bool)) + DO_EXEC_CMD(AppendStar, (AppendStar, int, float, float, bool, bool)) + DO_EXEC_CMD(AppendExpandedStar, (AppendExpandedStar, int, float, float, float)) + DO_EXEC_CMD(AppendDisc, (AppendDisc, int, float, bool)) + DO_EXEC_CMD(AppendSimpleTriangle, (AppendSimpleTriangle, float, bool)) + DO_EXEC_CMD(AppendSimpleQuad, (AppendSimpleQuad, vec2, vec2, float, bool)) + DO_EXEC_CMD(AppendCog, (AppendCog, int, float, float, float, float, float, float, float, float, bool)) + default: + ASSERT(0, "Unknown command pseudo bytecode"); + } + } + BD()->Disable(MeshBuildOperation::CommandExecution); + + if (!BD()->IsEnabled(MeshBuildOperation::PreventVertCleanup)) + VerticesCleanup(); + + if (BD()->IsEnabled(MeshBuildOperation::PostBuildComputeNormals)) + ComputeNormals(0, (int)m_indices.Count()); + + BD()->Disable(MeshBuildOperation::PostBuildComputeNormals); + BD()->Disable(MeshBuildOperation::PreventVertCleanup); + + if (BD()->CmdExecNb() > 0) + BD()->CmdExecNb() = -1; +} + diff --git a/src/easymesh/easymeshlua.h b/src/easymesh/easymeshlua.h new file mode 100644 index 00000000..8ec23454 --- /dev/null +++ b/src/easymesh/easymeshlua.h @@ -0,0 +1,144 @@ +// +// MY CLASS TYPE +// +// Copyright © 2009-2015 Benjamin "Touky" Huet +// +// This program is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +namespace lol +{ + +//----------------------------------------------------------------------------- +class EasyMeshLuaLoader : public Lolua::Loader +{ +public: + EasyMeshLuaLoader(); + virtual ~EasyMeshLuaLoader(); +}; + +//----------------------------------------------------------------------------- +class EasyMeshLuaObject : public Lolua::ObjectDef +{ +public: + //------------------------------------------------------------------------- + EasyMeshLuaObject(); + virtual ~EasyMeshLuaObject(); + + //------------------------------------------------------------------------- + static EasyMeshLuaObject* New(Lolua::State* l, int arg_nb); + static const Lolua::ObjectLib* GetLib(); + + //------------------------------------------------------------------------- + /* + + (csgu|csgunion) { return token::T_CSGUNION; } + (csgs|CsgSub) { return token::T_CSGSUBSTRACT; } + (csgsl|CsgSubL) { return token::T_CSGSUBSTRACTLOSS; } + (csga|csgand) { return token::T_CSGAND; } + (csgx|csgxor) { return token::T_CSGXOR; } + + (lp|loop) { return token::T_LOOP; } + (sc|setcolor) { return token::T_COLOR; } + (sca|setcolora) { return token::T_ACOLOR; } + (scb|setcolorb) { return token::T_BCOLOR; } + (scv|setcolorv) { return token::T_VCOLOR; } + + (tsw|tglscalewind) { return token::T_TOGGLESCALEWINDING; } + (tqw|tglquadweight) { return token::T_TOGGLEQUADWEIGHTING; } + (tpbn|tglpstbuildnormal) { return token::T_TOGGLEPOSTBUILDNORMAL; } + (tvnc|tglvertnocleanup) { return token::T_TOGGLEVERTNOCLEANUP; } + + (vm|vertmerge) { return token::T_VERTMERGE; } + (vs|vertseparate) { return token::T_VERTSEPARATE; } + + (tx|translatex) { return token::T_TRANSLATEX; } + (ty|translatey) { return token::T_TRANSLATEY; } + (tz|translatez) { return token::T_TRANSLATEZ; } + (t|translate) { return token::T_TRANSLATE; } + (rx|rotatex) { return token::T_ROTATEX; } + (ry|rotatey) { return token::T_ROTATEY; } + (rz|rotatez) { return token::T_ROTATEZ; } + (r|rotate) { return token::T_ROTATE; } + (rj|radialjitter) { return token::T_RADIALJITTER; } + (tax|taperx) { return token::T_TAPERX; } + (tay|tapery) { return token::T_TAPERY; } + (taz|taperz) { return token::T_TAPERZ; } + (twx|twistx) { return token::T_TWISTX; } + (twy|twisty) { return token::T_TWISTY; } + (twz|twistz) { return token::T_TWISTZ; } + (shx|shearx) { return token::T_SHEARX; } + (shy|sheary) { return token::T_SHEARY; } + (shz|shearz) { return token::T_SHEARZ; } + (stx|stretchx) { return token::T_STRETCHX; } + (sty|stretchy) { return token::T_STRETCHY; } + (stz|stretchz) { return token::T_STRETCHZ; } + (bdxy|bendxy) { return token::T_BENDXY; } + (bdxz|bendxz) { return token::T_BENDXZ; } + (bdyx|bendyx) { return token::T_BENDYX; } + (bdyz|bendyz) { return token::T_BENDYZ; } + (bdzx|bendzx) { return token::T_BENDZX; } + (bdzy|bendzy) { return token::T_BENDZY; } + (sx|scalex) { return token::T_SCALEX; } + (sy|scaley) { return token::T_SCALEY; } + (sz|scalez) { return token::T_SCALEZ; } + (s|scale) { return token::T_SCALE; } + (mx|mirrorx) { return token::T_MIRRORX; } + (my|mirrory) { return token::T_MIRRORY; } + (mz|mirrorz) { return token::T_MIRRORZ; } + (ch|chamfer) { return token::T_CHAMFER; } + (splt|splittriangle) { return token::T_SPLITTRIANGLE; } + (smth|smooth) { return token::T_SMOOTHMESH; } + (dup|duplicate) { return token::T_DUPLICATE; } + + (ac|addcylinder) { return token::T_CYLINDER; } + (asph|addsphere) { return token::T_SPHERE; } + (acap|addcapsule) { return token::T_CAPSULE; } + (ato|addtorus) { return token::T_TORUS; } + (ab|addbox) { return token::T_BOX; } + (ascb|addsmoothchamfbox) { return token::T_SMOOTHCHAMFBOX; } + (afcb|addflatchamfbox) { return token::T_FLATCHAMFBOX; } + (as|addstar) { return token::T_STAR; } + (aes|addexpandedstar) { return token::T_EXPANDEDSTAR; } + (ad|adddisc) { return token::T_DISC; } + (at|addtriangle) { return token::T_TRIANGLE; } + (aq|addquad) { return token::T_QUAD; } + (acg|addcog) { return token::T_COG; } + */ + /* + + %{ *//* ======= BASE COLOR TYPES ========================================= *//* % +} +%{ *//* COLOR *//* %} +#[0-9a-fA-F]{3} { +uint32_t tmp = std::strtol(yytext + 1, nullptr, 16); +yylval->u32val = 0x11000000u * (tmp >> 8) +| 0x00110000u * ((tmp >> 4) & 0xf) +| 0x00001100u * (tmp & 0xf) +| 0x000000ffu; +return token::COLOR; } +#[0-9a-fA-F]{4} { +uint32_t tmp = std::strtol(yytext + 1, nullptr, 16); +yylval->u32val = 0x11000000u * (tmp >> 12) +| 0x00110000u * ((tmp >> 8) & 0xf) +| 0x00001100u * ((tmp >> 4) & 0xf) +| 0x00000011u * (tmp & 0xf); +return token::COLOR; } +#[0-9a-fA-F]{6} { +yylval->u32val = 0xffu +| 0x100u * (uint32_t)std::strtol(yytext + 1, nullptr, 16); +return token::COLOR; } +#[0-9a-fA-F]{8} { +yylval->u32val = (uint32_t)std::strtol(yytext + 1, nullptr, 16); +return token::COLOR; } +*/ + +}; + +} /* namespace lol */ diff --git a/src/easymesh/easymeshprimitive.cpp b/src/easymesh/easymeshprimitive.cpp index 65042856..5ecb9cce 100644 --- a/src/easymesh/easymeshprimitive.cpp +++ b/src/easymesh/easymeshprimitive.cpp @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Primitive: The code belonging to primitive operations // // Copyright: (c) 2010-2015 Sam Hocevar // (c) 2009-2015 Cédric Lecacheur @@ -10,11 +10,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include namespace lol diff --git a/src/easymesh/easymeshrender.cpp b/src/easymesh/easymeshrender.cpp index 93d38838..511f3139 100644 --- a/src/easymesh/easymeshrender.cpp +++ b/src/easymesh/easymeshrender.cpp @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Render: The code belonging to render operations // // Copyright: (c) 2010-2013 Sam Hocevar // (c) 2009-2013 Cédric Lecacheur @@ -10,11 +10,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include namespace lol @@ -28,6 +23,52 @@ LOLFX_RESOURCE_DECLARE(shinydebugnormal); LOLFX_RESOURCE_DECLARE(shinydebugUV); LOLFX_RESOURCE_DECLARE(shiny_SK); +//----------------------------------------------------------------------------- +void EasyMesh::MeshConvert() +{ + /* Default material */ + Shader *shader = Shader::Create(LOLFX_RESOURCE_NAME(shiny)); + + /* Push index buffer to GPU */ + IndexBuffer *ibo = new IndexBuffer(m_indices.Count() * sizeof(uint16_t)); + uint16_t *indices = (uint16_t *)ibo->Lock(0, 0); + for (int i = 0; i < m_indices.Count(); ++i) + indices[i] = m_indices[i]; + ibo->Unlock(); + + /* Push vertex buffer to GPU */ + struct Vertex + { + vec3 pos, normal; + u8vec4 color; + vec4 texcoord; + }; + + VertexDeclaration *vdecl = new VertexDeclaration( + VertexStream(VertexUsage::Position, + VertexUsage::Normal, + VertexUsage::Color, + VertexUsage::TexCoord)); + + VertexBuffer *vbo = new VertexBuffer(m_vert.Count() * sizeof(Vertex)); + Vertex *vert = (Vertex *)vbo->Lock(0, 0); + for (int i = 0; i < m_vert.Count(); ++i) + { + vert[i].pos = m_vert[i].m_coord, + vert[i].normal = m_vert[i].m_normal, + vert[i].color = (u8vec4)(m_vert[i].m_color * 255.f); + vert[i].texcoord = m_vert[i].m_texcoord; + } + vbo->Unlock(); + + /* Reference our new data in our submesh */ + m_submeshes.Push(new SubMesh(shader, vdecl)); + m_submeshes.Last()->SetIndexBuffer(ibo); + m_submeshes.Last()->SetVertexBuffer(0, vbo); + + m_state = MeshRender::CanRender; +} + //----------------------------------------------------------------------------- GpuShaderData::GpuShaderData() { diff --git a/src/easymesh/easymeshrender.h b/src/easymesh/easymeshrender.h index 69e45694..92f849ae 100644 --- a/src/easymesh/easymeshrender.h +++ b/src/easymesh/easymeshrender.h @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Render: The code belonging to render operations // // Copyright: (c) 2009-2013 Benjamin "Touky" Huet // (c) 2010-2013 Sam Hocevar @@ -12,11 +12,6 @@ #pragma once -// -// The EasyMesh class -// ------------------ -// - namespace lol { diff --git a/src/easymesh/easymeshtransform.cpp b/src/easymesh/easymeshtransform.cpp index b5a33b9b..7e7a8333 100644 --- a/src/easymesh/easymeshtransform.cpp +++ b/src/easymesh/easymeshtransform.cpp @@ -1,5 +1,5 @@ // -// Lol Engine +// EasyMesh-Transform: The code belonging to transform operations // // Copyright: (c) 2010-2015 Sam Hocevar // (c) 2009-2015 Cédric Lecacheur @@ -10,11 +10,6 @@ // http://www.wtfpl.net/ for more details. // -// -// The EasyMesh class -// ------------------ -// - #include namespace lol diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index a5c31181..2cab3865 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -107,6 +107,7 @@ + @@ -243,6 +244,7 @@ + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index 2297acf1..57fd0dfb 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -88,6 +88,9 @@ {3f420a7d-0538-463a-925b-3f8968bf628e} + + {25cc0513-ad71-4fbe-bd24-acc88aa66833} + @@ -403,8 +406,11 @@ easymesh + + easymesh + - application + lua @@ -757,8 +763,11 @@ lol\algorithm + + easymesh + - application + lua