summaryrefslogtreecommitdiff
path: root/Runtime/Scripting
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-25 03:22:49 +0800
committerchai <chaifix@163.com>2021-10-25 03:22:49 +0800
commit0816cd70ca1a213b6ed872bcf3c0bf0912473722 (patch)
treeede23a398e78106551434ca762362ca04fd1ba8c /Runtime/Scripting
parent180b715e72ffc292ebbc4695f8a63ce449004d27 (diff)
*misc
Diffstat (limited to 'Runtime/Scripting')
-rw-r--r--Runtime/Scripting/GL/GL.bind.cpp239
1 files changed, 230 insertions, 9 deletions
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;