diff options
author | chai <chaifix@163.com> | 2021-10-25 03:22:49 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-25 03:22:49 +0800 |
commit | 0816cd70ca1a213b6ed872bcf3c0bf0912473722 (patch) | |
tree | ede23a398e78106551434ca762362ca04fd1ba8c | |
parent | 180b715e72ffc292ebbc4695f8a63ce449004d27 (diff) |
*misc
26 files changed, 400 insertions, 65 deletions
diff --git a/Documents/Lua.xlsx b/Documents/Lua.xlsx Binary files differindex a73900b..e318fef 100644 --- a/Documents/Lua.xlsx +++ b/Documents/Lua.xlsx diff --git a/Editor/EditorMain.cpp b/Editor/EditorMain.cpp index 7686fa5..555867a 100644 --- a/Editor/EditorMain.cpp +++ b/Editor/EditorMain.cpp @@ -15,26 +15,12 @@ void ErrorHandle(cc8* msg) log_error(std::string("[Lua] ") + msg);
}
-void OnRegisterNativeClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName)
-{
- // 填充类型的元数据
- lua_newtable(state);
- int typeIdx = state.GetTop();
- state.Push("native");
- state.SetField(typeIdx, "mode");
- state.Push(clsName);
- state.SetField(typeIdx, "name");
- state.Push(pkgName);
- state.SetField(typeIdx, "package");
- lua_setfield(state, cls, "_type");
-}
-
void InitLuaState(LuaBind::VM& vm)
{
vm.Setup();
vm.OpenLibs();
- LuaBind::onRegisterNativeClass = OnRegisterNativeClass;
+ LuaBind::onRegisterNativeClass = LuaHelper::OnRegisterNativeClass;
if (!SetupGameLabEditorScripting(vm.GetMainThread()))
{
diff --git a/Editor/GUI/GUIWindow.cpp b/Editor/GUI/GUIWindow.cpp index df28785..621eb54 100644 --- a/Editor/GUI/GUIWindow.cpp +++ b/Editor/GUI/GUIWindow.cpp @@ -44,8 +44,10 @@ LRESULT CALLBACK GUIWindow::GUIViewWndProc(HWND hWnd, UINT message, WPARAM wPara case WM_PAINT: { log_info("WndProc", "WM_PAINT"); + self->SetAsRenderContext(); self->OnPaint(); + BeginPaint(self->m_Handle, &ps); EndPaint(self->m_Handle, &ps); UpdateWindow(self->m_Handle); @@ -104,6 +106,8 @@ LRESULT CALLBACK GUIWindow::GUIViewWndProc(HWND hWnd, UINT message, WPARAM wPara { SetFocus(hWnd); + self->DoPaint(); + if (/*processInput && !focused*/ true) { //focused = (GetFocus() == hWnd); @@ -323,7 +327,7 @@ void GUIWindow::OnLostFocus() void GUIWindow::OnPaint() { - InvokeLuaCallback(m_Script, "OnPaint"); + InvokeLuaCallback(m_Script, "OnGUI"); } void GUIWindow::SetPosition(Rect position) diff --git a/Resources/.vscode/launch.json b/Resources/.vscode/launch.json index 4248412..94088ea 100644 --- a/Resources/.vscode/launch.json +++ b/Resources/.vscode/launch.json @@ -13,12 +13,12 @@ "cwd": "${workspaceFolder}", "luaFileExtension": "", "connectionPort": 8818, - "stopOnEntry": true, + "stopOnEntry": false, "useCHook": true, "autoPathMode": true, // 鑷姩鍚姩缂栬緫鍣 - // https://stackoverflow.com/questions/34698230/how-to-run-multiple-commands-via-start-command/34698365 - "preLaunchTask": "LaunchEditor", "postDebugTask": "KillEditor" + "program": "${workspaceFolder}/../Build/Editor.exe", "postDebugTask": "KillEditor" + //"preLaunchTask": "LaunchEditor", "postDebugTask": "KillEditor" }, { "type": "lua", @@ -30,7 +30,7 @@ "packagePath": [], "luaFileExtension": "", "connectionPort": 8820, - "stopOnEntry": true, + "stopOnEntry": false, "useCHook": true } ] diff --git a/Resources/.vscode/tasks.json b/Resources/.vscode/tasks.json index a7dffbb..6409b18 100644 --- a/Resources/.vscode/tasks.json +++ b/Resources/.vscode/tasks.json @@ -9,7 +9,11 @@ { "label": "KillEditor", "type": "shell", - "command": "${workspaceFolder}/.vscode/KillEditor.bat" + "command": "${workspaceFolder}/.vscode/KillEditor.bat", + "isBackground": true, + "presentation": { + "reveal": "never" // 鍦ㄧ粓绔噷闅愯棌 + }, } ] }
\ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/GameLab/Class.lua b/Resources/DefaultContent/Libraries/GameLab/Class.lua index 5a3a962..62369e1 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Class.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Class.lua @@ -1,11 +1,14 @@ -- Declare class
-local _class = function (className, pkg)
+local _class = function (className)
local class = {}
+ local pkgName = (type(className) == "string") and string.match(className, "^(.+)%.%w+$") or ""
+ local shortName = (type(className) == "string") and string.match(className, "%.*(%w+)$") or ""
class._type = { -- 绫诲瀷鍏冩暟鎹紝GameLab鐨勭被鍨嬮兘浼氬寘鍚繖浜
mode = "lua",
- name = className,
- package = pkg,
+ name = shortName or "",
+ package = pkgName or "",
+ fullName = className
}
class.__index = class
class.New = function(...)
@@ -17,11 +20,11 @@ local _class = function (className, pkg) return class
end
--- 鎻愪緵ClassName鍜孭kgName浣滀负绫诲瀷鐨勫厓鏁版嵁锛屽彲浠ョ暀绌猴紝浣嗘槸鏈濂芥彁渚
-local Class = function(className, pkg)
- local cls = _class(className, pkg)
- cls.Extend = function(childName, childPkg)
- local child = _class(childName, childPkg)
+-- 鎻愪緵ClassName鍜孭kgName浣滀负绫诲瀷鐨勫厓鏁版嵁锛屽彲浠ョ暀绌猴紝浣嗘槸鏈濂芥彁渚涳紝浼氫綔涓虹被鍨嬪垽鏂殑渚濇嵁
+local Class = function(className)
+ local cls = _class(className)
+ cls.Extend = function(childName)
+ local child = _class(childName)
if cls then
setmetatable(child, cls)
child.base = cls
diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Math.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Math.lua index 4ec067e..0bbe7f0 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Math.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Math.lua @@ -1,6 +1,6 @@ -- 鏁板鍑芥暟 -GameLab.Engine.Math = GameLab.Engine.Math or {} -local m = GameLab.Engine.Math +local m = GameLab.Engine.Math or {} +GameLab.Engine.Math = m m.Abs = function(n) diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Matrix3x3.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Matrix3x3.lua index f7dc352..b0aad47 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Matrix3x3.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Matrix3x3.lua @@ -1,4 +1,4 @@ -local Matrix3x3 = GameLab.Class("Matrix3x3", "GameLab.Engine.Math") +local Matrix3x3 = GameLab.Class("GameLab.Engine.Math.Matrix3x3") Matrix3x3.Ctor = function(self) diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Matrix4x4.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Matrix4x4.lua index e69de29..8e4d70a 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Matrix4x4.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Matrix4x4.lua @@ -0,0 +1,31 @@ +local Matrix4x4 = GameLab.Class("GameLab.Engine.Math.Matrix4x4") + +Matrix4x4.Ctor = function(self) + for r = 0, 3 do + for c = 0, 3 do + self["m"..r..c] = 0 + end + end +end + +Matrix4x4.SetRow = function(self, row, value) + +end + +Matrix4x4.SetColum = function(self, colum, value) + +end + +Matrix4x4.Set = function(self, row, colum ,value) + +end + +Matrix4x4.GetColum = function(self, colum) + +end + +Matrix4x4.GetRow = function(self, row) + +end + +return Matrix4x4
\ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector2.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector2.lua index dc961b2..5697797 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector2.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector2.lua @@ -1,4 +1,4 @@ -local Vector2 = GameLab.Class("Vector2", "GameLab.Engine.Math")
+local Vector2 = GameLab.Class("GameLab.Engine.Math.Vector2")
diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector3.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector3.lua index f355a65..caa28a4 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector3.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector3.lua @@ -1,4 +1,4 @@ -local Vector3 = GameLab.Class("Vector3", "GameLab.Engine.Math")
+local Vector3 = GameLab.Class("GameLab.Engine.Math.Vector3")
diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua index 84a114f..e232c93 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua @@ -1,4 +1,4 @@ -local Vector4 = GameLab.Class("Vector4", "GameLab.Engine.Math")
+local Vector4 = GameLab.Class("GameLab.Engine.Math.Vector4")
Vector4.Ctor = function (self, x, y, z, w)
self.x = x or 0
diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Rendering/Color.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Rendering/Color.lua index 56063f6..fb3e773 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Rendering/Color.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Rendering/Color.lua @@ -1,4 +1,4 @@ -local Color = GameLab.Class("Color", "GameLab.Engine.Rendering") +local Color = GameLab.Class("GameLab.Engine.Rendering.Color") GameLab.Engine.Rendering.Color = Color -- 閬垮厤require寰幆 local Color32 = GameLab.Engine.Rendering.Color32 or require("GameLab.Engine.Rendering.Color32") diff --git a/Resources/DefaultContent/Libraries/GameLab/Engine/Rendering/Color32.lua b/Resources/DefaultContent/Libraries/GameLab/Engine/Rendering/Color32.lua index ce3967c..c17187a 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Engine/Rendering/Color32.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Engine/Rendering/Color32.lua @@ -1,4 +1,4 @@ -local Color32 = GameLab.Class("Color32", "GameLab.Engine.Rendering") +local Color32 = GameLab.Class("GameLab.Engine.Rendering.Color32") GameLab.Engine.Rendering.Color32 = Color32 -- 閬垮厤require寰幆 local Color = GameLab.Engine.Rendering.Color or require("GameLab.Engine.Rendering.Color") diff --git a/Resources/Editor.exe b/Resources/Editor.exe Binary files differdeleted file mode 100644 index 7d4ab9d..0000000 --- a/Resources/Editor.exe +++ /dev/null diff --git a/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua b/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua index 22869e5..4f0525f 100644 --- a/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua +++ b/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua @@ -1,4 +1,4 @@ -local EditorWindow = GameLab.Class("EditorWindow", "GameLab.Editor.GUI")
+local EditorWindow = GameLab.Class("GameLab.Editor.GUI.EditorWindow")
EditorWindow.Ctor = function(self, title)
self.title = title -- 缂栬緫鍣ㄥ悕绉
diff --git a/Resources/Scripts/Editor/AssetBrowser.lua b/Resources/Scripts/Editor/AssetBrowser.lua index 06661a2..3d03302 100644 --- a/Resources/Scripts/Editor/AssetBrowser.lua +++ b/Resources/Scripts/Editor/AssetBrowser.lua @@ -1,5 +1,6 @@ local Debug = GameLab.Debug
-local AssetBrowser = GameLab.Editor.GUI.EditorWindow.Extend("AssetBrowser", "GameLab.Editor")
+local AssetBrowser = GameLab.Editor.GUI.EditorWindow.Extend("GameLab.Editor.AssetBrowser")
+local GL = GameLab.Engine.GL
AssetBrowser.Ctor = function(self)
self.base.Ctor(self, "AssetBrowser")
@@ -7,7 +8,15 @@ AssetBrowser.Ctor = function(self) end
AssetBrowser.OnGUI = function(self)
-
+ Debug.Log("AssetBrowser.OnGUI()")
+ GL.ClearColor({0,0,0,1})
+ GL.Clear(GL.EBufferType.ColorBuffer)
+ GL.Color({1,1,0,1})
+ GL.Begin(GL.EPrimitiveType.Triangles)
+ GL.Vertex({0,0,-1})
+ GL.Vertex({0,1,-1})
+ GL.Vertex({1,0,-1})
+ GL.End()
end
AssetBrowser.OnFocus = function(self)
diff --git a/Resources/Scripts/EditorApplication.lua b/Resources/Scripts/EditorApplication.lua index a6515ba..e91725a 100644 --- a/Resources/Scripts/EditorApplication.lua +++ b/Resources/Scripts/EditorApplication.lua @@ -37,8 +37,6 @@ guiWindow:SetInstance(wnd) local v = GameLab.Engine.Math.Vector4.New(1,2,3,4) Debug.Log(inspect(v)) -local V4 = GameLab.Engine.Math.Vector4.Extend("V4", "GameLab.Engine.Math") - Debug.Log(EditorWindowManager.name) local c = Rendering.Color.New(1,1,1,1) @@ -46,7 +44,8 @@ Debug.Log(inspect(c)) Debug.Log(inspect(GL.EBufferType)) -GL.Clear() +GL.ClearColor({1,1,1,1}) +GL.Clear(GL.EBufferType.ColorBuffer) while true do diff --git a/Resources/Shaders/UI.glsl b/Resources/Shaders/UI.glsl new file mode 100644 index 0000000..12a94db --- /dev/null +++ b/Resources/Shaders/UI.glsl @@ -0,0 +1,12 @@ +VERTEX_SHADER_BEGIN + + +VERTEX_SHADER_END + + +FRAGMENT_SHADER_BEGIN + + + + +FRAGMENT_SHADER_END
\ No newline at end of file diff --git a/Runtime/Debug/Log.cpp b/Runtime/Debug/Log.cpp index 08c7acf..cc5b2e3 100644 --- a/Runtime/Debug/Log.cpp +++ b/Runtime/Debug/Log.cpp @@ -5,7 +5,11 @@ using namespace std; -//long g_LogTags = LogTag::All; +#ifdef GAMELAB_WIN +#include <windows.h> +static HANDLE s_ConsoleHandle = 0; +#endif + unordered_set<string> s_OpenTags; #ifdef GAMELAB_DEBUG @@ -29,20 +33,47 @@ void log_open_tag(std::string tag) } // https://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c +// https://en.wikipedia.org/wiki/ANSI_escape_code#Windows_and_DOS +// https://stackoverflow.com/questions/9262270/color-console-output-with-c-in-windows +// https://blog.csdn.net/odaynot/article/details/7722240 void log_info(std::string log) { - cout << "\x1B[97m" << currentDateTime() << " [Info] " << log << "\033[0m" << endl; +#ifdef GAMELAB_WIN + if (s_ConsoleHandle == 0) { + s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + } + SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); + cout << currentDateTime() << " [Info] " << log << endl; +#else + cout << "\x1B[97m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl; +#endif } void log_warning(std::string log) { - cout << "\x1B[93m" << currentDateTime() << " [Wanning] " << log << "\033[0m" << endl; +#ifdef GAMELAB_WIN + if (s_ConsoleHandle == 0) { + s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + } + SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); + cout << currentDateTime() << " [Info] " << log << endl; +#else + cout << "\x1B[93m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl; +#endif } void log_error(std::string log) { - cout << "\x1B[91m" << currentDateTime() << " [Error] " << log << "\033[0m" << endl; +#ifdef GAMELAB_WIN + if (s_ConsoleHandle == 0) { + s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + } + SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << currentDateTime() << " [Info] " << log << endl; +#else + cout << "\x1B[91m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl; +#endif } void log_error_null_param(std::string funcName, std::string param) diff --git a/Runtime/Debug/Log.h b/Runtime/Debug/Log.h index d66f705..aaaaa78 100644 --- a/Runtime/Debug/Log.h +++ b/Runtime/Debug/Log.h @@ -1,14 +1,6 @@ #pragma once #include <string> -//enum LogTag : unsigned long -//{ -// -// All = ~0 -//}; -// -//extern long g_LogTags; - void log_open_tag(std::string tag); void log_info(std::string log); diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index c9183ff..7c5f043 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -284,6 +284,12 @@ namespace LuaBind return ((t == LUA_TNONE) || (t == LUA_TNIL)); } + bool State::IsTable(int idx) + { + int check = lua_type(mState, idx); + return check == LUA_TTABLE; + } + bool State::IsTableOrUserdata(int idx) { int check = lua_type(mState, idx); diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h index c81ba8c..e7e2807 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.h +++ b/Runtime/Lua/LuaBind/LuaBindState.h @@ -85,6 +85,7 @@ namespace LuaBind bool IsNil(int idx); bool IsNilOrNone(int idx); + bool IsTable(int idx); bool IsTableOrUserdata(int idx); bool IsTrueOrNotNil(int idx); bool IsType(int idx, int type); diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp index e458e28..2044ba2 100644 --- a/Runtime/Lua/LuaHelper.cpp +++ b/Runtime/Lua/LuaHelper.cpp @@ -25,4 +25,38 @@ Vector2 State::GetValue < Vector2 >(int idx, const Vector2 value) int LuaHelper::Call(const char* func, const char* params, ...)
{
return 1;
+}
+
+void LuaHelper::OnRegisterNativeClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName)
+{
+ // 填充类型的元数据
+ lua_newtable(state);
+ int typeIdx = state.GetTop();
+ state.Push("native");
+ state.SetField(typeIdx, "mode");
+ state.Push(clsName);
+ state.SetField(typeIdx, "name");
+ state.Push(pkgName);
+ state.SetField(typeIdx, "package");
+ state.Push(pkgName + '.' + clsName);
+ state.SetField(typeIdx, "fullName");
+ lua_setfield(state, cls, "_type");
+}
+
+bool LuaHelper::IsType(LuaBind::State& state, const char* typeName, int idx)
+{
+ int top = state.GetTop();
+ cc8* type = luaL_typename(state, idx);
+ if (strcmp(type, typeName) == 0)
+ return true;
+ state.GetField(idx, "_type");
+ if (!state.IsTable(-1))
+ {
+ state.Pop(1);
+ return false;
+ }
+ std::string tname = state.GetField(-1, "fullName", "");
+ bool bIsType = tname == typeName;
+ state.SetTop(top);
+ return bIsType;
}
\ No newline at end of file diff --git a/Runtime/Lua/LuaHelper.h b/Runtime/Lua/LuaHelper.h index 10d9421..56dae54 100644 --- a/Runtime/Lua/LuaHelper.h +++ b/Runtime/Lua/LuaHelper.h @@ -10,6 +10,8 @@ class LuaHelper public:
static int Call(const char* func, const char* params, ...);
-};
+ static bool IsType(LuaBind::State& state, const char* typeName, int idx);
+ static void OnRegisterNativeClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName);
+};
\ No newline at end of file diff --git a/Runtime/Scripting/GL/GL.bind.cpp b/Runtime/Scripting/GL/GL.bind.cpp index 9bc4204..f167237 100644 --- a/Runtime/Scripting/GL/GL.bind.cpp +++ b/Runtime/Scripting/GL/GL.bind.cpp @@ -1,5 +1,5 @@ #include "Runtime/Graphics/OpenGL.h" -#include "Runtime/Lua/LuaBind/LuaBind.h" +#include "Runtime/Lua/LuaHelper.h" #include "Runtime/Debug/Log.h" using namespace LuaBind; @@ -8,21 +8,231 @@ using namespace LuaBind; enum EBufferType { - COLOR_BUFFER = 1, - DEPTH_BUFFER = 2, - STENCIL_BUFFER = 3, + ColorBuffer = 1, + DepthBuffer = 2, + StencilBuffer = 3, }; -// GL.Clear([GL.EBufferType.COLOR_BUFFER, GL.EBufferType.DEPTH_BUFFER, GL.EBufferType.STENCIL_BUFFER]) +enum EPrimitiveType +{ + Triangles = 1, + Lines = 2, + Points = 3, +}; + +enum EMatrixMode +{ + Projection = 1, + ModelView = 2, +}; + +// GL.Clear([GL.EBufferType.ColorBuffer, GL.EBufferType.DepthBuffer, GL.EBufferType.StencilBuffer]) int Clear(lua_State* L) { LUA_BIND_STATE(L); - + GLint clearFlag = 0; + int n = state.GetTop(); + for (int i = 1; i <= n; ++i) + { + int buffer = state.GetValue(i, 0); + if (buffer == EBufferType::ColorBuffer) + clearFlag |= GL_COLOR_BUFFER_BIT; + else if(buffer == EBufferType::DepthBuffer) + clearFlag |= GL_DEPTH_BUFFER_BIT; + else if (buffer == EBufferType::StencilBuffer) + clearFlag |= GL_STENCIL_BUFFER_BIT; + } + glClear(clearFlag); + return 0; +} + +// GL.ClearColor(color32) +// GL.ClearColor(color) +// GL.ClearColor({r,g,b,a}) +int ClearColor(lua_State* L) +{ + LUA_BIND_STATE(L); + if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color", -1)) + { + float r = state.GetField(-1, "r", 0); + float g = state.GetField(-1, "g", 0); + float b = state.GetField(-1, "b", 0); + float a = state.GetField(-1, "a", 0); + glClearColor(r, g, b, a); + } + else if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color32", -1)) + { + float r = state.GetField(-1, "r", 0) / 255.f; + float g = state.GetField(-1, "g", 0) / 255.f; + float b = state.GetField(-1, "b", 0) / 255.f; + float a = state.GetField(-1, "a", 0) / 255.f; + glClearColor(r, g, b, a); + } + else if (LuaHelper::IsType(state, "table", -1)) + { + float r = state.GetField(-1, 1, 0); + float g = state.GetField(-1, 2, 0); + float b = state.GetField(-1, 3, 0); + float a = state.GetField(-1, 4, 0); + glClearColor(r, g, b, a); + } + else + { + state.ErrorType(1, "Color or Color32 or color table"); + } + return 0; +} + +// GL.Begin([EPrimitiveType.Triangles, EPrimitiveType.Lines]); +int Begin(lua_State* L) +{ + LUA_BIND_STATE(L); + int primitive = state.GetValue(1, (int)EPrimitiveType::Triangles); + if (primitive == EPrimitiveType::Triangles) + { + glBegin(GL_TRIANGLES); + } + else if (primitive == EPrimitiveType::Lines) + { + glBegin(GL_LINES); + } + else if(primitive == EPrimitiveType::Points) + { + glBegin(GL_POINTS); + } + else + { + luaL_error(state, "wrong primitive type %d" , primitive); + } + return 0; +} + +int End(lua_State* L) +{ + glEnd(); + return 0; +} + +// GL.Vertex([vector3, vector3Table]) +int Vertex(lua_State* L) +{ + LUA_BIND_STATE(L); + if (LuaHelper::IsType(state, "GameLab.Engine.Math.Vector3", 1)) + { + float x = state.GetField(-1, "x", 0); + float y = state.GetField(-1, "y", 0); + float z = state.GetField(-1, "z", 0); + glVertex3f(x, y, z); + } + else if (LuaHelper::IsType(state, "table", -1)) + { + float x = state.GetField(-1, 1, 0); + float y = state.GetField(-1, 2, 0); + float z = state.GetField(-1, 3, 0); + glVertex3f(x, y, z); + } + else + { + state.ErrorType(1, "Vector3 or vector3 table"); + } + return 0; +} + +// GL.Color([Color, Color32, Color table ]) +int Color(lua_State* L) +{ + LUA_BIND_STATE(L); + int top = state.GetTop(); + if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color", -1)) + { + float r = state.GetField(-1, "r", 0); + float g = state.GetField(-1, "g", 0); + float b = state.GetField(-1, "b", 0); + float a = state.GetField(-1, "a", 0); + glColor4f(r, g, b, a); + } + else if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color32", -1)) + { + float r = state.GetField(-1, "r", 0) / 255.f; + float g = state.GetField(-1, "g", 0) / 255.f; + float b = state.GetField(-1, "b", 0) / 255.f; + float a = state.GetField(-1, "a", 0) / 255.f; + glColor4f(r, g, b, a); + } + else if (LuaHelper::IsType(state, "table", -1)) + { + float r = state.GetField(-1, 1, 0); + float g = state.GetField(-1, 2, 0); + float b = state.GetField(-1, 3, 0); + float a = state.GetField(-1, 4, 0); + glColor4f(r, g, b, a); + } + else + { + state.ErrorType(1, "Color or Color32 or color table"); + } + state.SetTop(top); + return 0; +} + +int LoadIdentity(lua_State* L) +{ + glLoadIdentity(); + return 0; +} + +int PushMatrix(lua_State* L) +{ + glPushMatrix(); + return 0; +} + +int PopMatrix(lua_State* L) +{ + glPopMatrix(); + return 0; +} + +int MatrixMode(lua_State* L) +{ + LUA_BIND_STATE(L); + int matrixMode = state.GetValue(1, (int)EMatrixMode::ModelView); + if (matrixMode == EMatrixMode::ModelView) + { + glMatrixMode(GL_MODELVIEW); + } + else if (matrixMode == EMatrixMode::Projection) + { + glMatrixMode(GL_PROJECTION); + } + else + { + luaL_error(state, "wrong matrix type %d", matrixMode); + } + return 0; +} + +// GL.LoadMatrix([Matrix4x4, mat44ColumnMajorTable]) +int LoadMatrix(lua_State* L) +{ + LUA_BIND_STATE(L); + return 0; } static luaL_Reg glFuncs[] = { + {"ClearColor", ClearColor}, {"Clear", Clear}, + // immediate mode apis + {"Color", Color}, + {"Begin", Begin}, + {"End", End}, + {"Vertex", Vertex }, + {"LoadIdentity", LoadIdentity}, + {"PushMatrix", PushMatrix}, + {"PopMatrix", PopMatrix}, + {"MatrixMode", MatrixMode}, + {"LoadMatrix", LoadMatrix}, {0, 0} }; @@ -41,9 +251,20 @@ int luaopen_GameLab_Engine_GL(lua_State* L) state.RegisterMethods(glFuncs); LUA_BIND_REGISTER_ENUM(state, "EBufferType", - { "ColorBuffer", EBufferType::COLOR_BUFFER }, - { "DepthBuffer", EBufferType::DEPTH_BUFFER}, - { "StencilBuffer", EBufferType::STENCIL_BUFFER} + { "ColorBuffer", EBufferType::ColorBuffer }, + { "DepthBuffer", EBufferType::DepthBuffer}, + { "StencilBuffer", EBufferType::StencilBuffer} + ); + + LUA_BIND_REGISTER_ENUM(state, "EPrimitiveType", + { "Triangles", EPrimitiveType::Triangles }, + { "Lines", EPrimitiveType::Lines }, + { "Points", EPrimitiveType::Points } + ); + + LUA_BIND_REGISTER_ENUM(state, "EMatrixMode", + { "MatrixView", EMatrixMode::ModelView }, + { "Projection", EMatrixMode::Projection} ); return 1; |