diff options
author | chai <chaifix@163.com> | 2021-10-29 13:36:49 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-29 13:36:49 +0800 |
commit | 91c32cb173201ac8803a1e4452e8342969b8e484 (patch) | |
tree | 5e78c485b5fcfcf839a2348667597d7e10476214 | |
parent | 1f92d4c389cceba6f90261d9cb29885c8a3ca24c (diff) |
*GLSL test
32 files changed, 531 insertions, 176 deletions
diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Math/Rect.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Math/Rect.lua new file mode 100644 index 0000000..2aeb0f7 --- /dev/null +++ b/Data/DefaultContent/Libraries/GameLab/Engine/Math/Rect.lua @@ -0,0 +1,10 @@ +local Rect = GameLab.Class("GameLab.Engine.Math.Rect")
+
+Rect.Ctor = function(self, x, y, width, height)
+ self.x = x or 0
+ self.y = y or 0
+ self.width = width or 0
+ self.height = height or 0
+end
+
+return Rect
\ No newline at end of file diff --git a/Data/Resources/Shaders/Editor-UI.glsl b/Data/Resources/Shaders/Editor-UI.glsl index 4edfe73..493ed9b 100644 --- a/Data/Resources/Shaders/Editor-UI.glsl +++ b/Data/Resources/Shaders/Editor-UI.glsl @@ -1,42 +1,30 @@ #version 330 core -uniform vec2 screenSize; + +uniform vec4 screenSize; +uniform vec4 windowSize; VSH_BEGIN -layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColor; -layout (location = 2) in vec2 aTexCoord; +layout (location = 0) in vec2 vPos; +layout (location = 1) in vec2 vUV; -out vec3 ourColor; -out vec2 TexCoord; +uniform mat4 mvp; void main() { - gl_Position = vec4(aPos, 1.0); - ourColor = aColor; - TexCoord = vec2(aTexCoord.x, aTexCoord.y); + vec4 clip = mvp * vec4(vPos, -1, 1.0); + gl_Position = clip; } VSH_END - FSH_BEGIN out vec4 FragColor; -in vec3 ourColor; -in vec2 TexCoord; - -uniform float mixValue; - -// texture samplers -uniform sampler2D texture1; -uniform sampler2D texture2; - void main() { - // linearly interpolate between both textures - FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), mixValue); + FragColor = vec4(1,1,1,1); } FSH_END diff --git a/Data/Scripts/Editor/AssetBrowser.lua b/Data/Scripts/Editor/AssetBrowser.lua index e6e4a4a..b936702 100644 --- a/Data/Scripts/Editor/AssetBrowser.lua +++ b/Data/Scripts/Editor/AssetBrowser.lua @@ -8,17 +8,63 @@ AssetBrowser.Ctor = function(self) self.base.Ctor(self, "AssetBrowser")
end
+local shader = nil
+
+local glsl = [[
+
+#version 330 core
+
+VSH_BEGIN
+
+layout (location = 0) in vec2 vPos;
+layout (location = 1) in vec2 vUV;
+
+uniform mat4 mvp;
+
+void main()
+{
+ vec4 clip = mvp * vec4(vPos, -1, 1.0);
+ gl_Position = clip;
+}
+
+VSH_END
+
+FSH_BEGIN
+
+out vec4 FragColor;
+
+void main()
+{
+ FragColor = vec4(1,1,1,1);
+}
+FSH_END
+
+]]
+
AssetBrowser.OnGUI = function(self)
+
+ if shader == nil then
+ shader = GameLab.Engine.Rendering.Shader.New(glsl)
+ end
+
+ local ortho = Matrix44.New()
+ ortho:SetOrtho(-200, 200, -200, 200, 0.1, 10)
+
Debug.Log("AssetBrowser.OnGUI()")
GL.ClearColor({0,0,0,1})
GL.Clear(GL.EBufferType.ColorBuffer)
- GL.Color({1,1,0,1})
- GL.LoadPixelMatrix(-250, 250, -300, 300)
- GL.Begin(GL.EPrimitiveType.Triangles)
- GL.Vertex({0,0,-1})
- GL.Vertex({0,300,-1})
- GL.Vertex({250,0,-1})
- GL.End()
+ -- GL.Color({1,1,0,1})
+ -- GL.LoadPixelMatrix(-250, 250, -300, 300)
+ -- GL.Begin(GL.EPrimitiveType.Triangles)
+ -- GL.Vertex({0,0,-1})
+ -- GL.Vertex({0,300,-1})
+ -- GL.Vertex({250,0,-1})
+ -- GL.End()
+
+ shader:Use()
+ shader:SetMatrix44("mvp", ortho)
+ GameLab.Engine.Rendering.DrawUIQuad({0, 0, 100, 100})
+
end
AssetBrowser.OnFocus = function(self)
diff --git a/Data/Scripts/EditorApplication.lua b/Data/Scripts/EditorApplication.lua index a233d88..75749df 100644 --- a/Data/Scripts/EditorApplication.lua +++ b/Data/Scripts/EditorApplication.lua @@ -3,6 +3,8 @@ local inspect = require "inspect" local AssetBrowser = require "./Scripts/Editor/AssetBrowser" local EditorWindowManager = require "./Scripts/EditorGUI/EditorWindowManager" +local Editor = GameLab.Editor +local Engine = GameLab.Engine local Resource = GameLab.Engine.Resource local Rendering = GameLab.Engine.Rendering local Debug = GameLab.Debug @@ -23,7 +25,7 @@ app:SetMainWindow(mainWindow) local guiWindow = GUI.GUIWindow.New() guiWindow:SetContainerWindow(mainWindow) -guiWindow:SetPosition({0,0, 500, 400}) +guiWindow:SetPosition({0,0, 400, 400}) collectgarbage() @@ -40,7 +42,7 @@ Debug.Log(inspect(v)) Debug.Log(EditorWindowManager.name) -local c = Rendering.Color.New(1,1,1,1) +local c = Engine.Rendering.Color.New(1,1,1,1) Debug.Log(inspect(c)) Debug.Log(inspect(GL.EBufferType)) @@ -64,13 +66,13 @@ GameLab.IO.ReadFilesAsync(files, function() Debug.Log("finished") end) -local imgData = Resource.LoadImageData("./Resources/Images/brickwall.jpg") +local imgData = Engine.Resource.LoadImageData("./Resources/Images/brickwall.jpg") Debug.Log(tostring(imgData:GetWidth())) Debug.Log(tostring(imgData:GetHeight())) -local tex = Resource.LoadTexture("./Resources/Images/brickwall.jpg") +local tex = Engine.Resource.LoadTexture("./Resources/Images/brickwall.jpg") -local request = Resource.LoadImageDataAsync("./Resources/Images/brickwall.jpg") +local request = Engine.Resource.LoadImageDataAsync("./Resources/Images/brickwall.jpg") local vsh = [[ #version 330 core @@ -109,55 +111,6 @@ local fsh = [[ } ]] -local glsl = [[ - -#version 330 core -uniform vec2 screenSize; - -VSH_BEGIN - -layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColor; -layout (location = 2) in vec2 aTexCoord; - -out vec3 ourColor; -out vec2 TexCoord; - -void main() -{ - gl_Position = vec4(aPos, 1.0); - ourColor = aColor; - TexCoord = vec2(aTexCoord.x, aTexCoord.y); -} - -VSH_END - - -FSH_BEGIN - -out vec4 FragColor; - -in vec3 ourColor; -in vec2 TexCoord; - -uniform float mixValue; - -// texture samplers -uniform sampler2D texture1; -uniform sampler2D texture2; - -void main() -{ - // linearly interpolate between both textures - FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), mixValue); -} - -FSH_END - -]] - -local shader = Rendering.Shader.New(glsl) - while true do app:OnStep() diff --git a/Documents/Lua.xlsx b/Documents/Lua.xlsx Binary files differindex e318fef..204327d 100644 --- a/Documents/Lua.xlsx +++ b/Documents/Lua.xlsx diff --git a/Documents/OpenGL.xlsx b/Documents/OpenGL.xlsx Binary files differnew file mode 100644 index 0000000..a4e4017 --- /dev/null +++ b/Documents/OpenGL.xlsx diff --git a/Editor/GUI/ContainerWindow.cpp b/Editor/GUI/ContainerWindow.cpp index 96e5a1b..62c6cb7 100644 --- a/Editor/GUI/ContainerWindow.cpp +++ b/Editor/GUI/ContainerWindow.cpp @@ -274,7 +274,7 @@ bool ContainerWindow::SetRenderContext() } // 初始化,创建窗口 -void ContainerWindow::Init(Rect pixelRect, int showMode, const Internal::Vector2& minSize, const Internal::Vector2& maxSize, std::string name) +void ContainerWindow::Init(Internal::Rect pixelRect, int showMode, const Internal::Vector2& minSize, const Internal::Vector2& maxSize, std::string name) { // Aux windows are mac only. on windows they look just like normal utility windows. if (showMode == kShowAuxWindow) diff --git a/Editor/GUI/EditorWindows.h b/Editor/GUI/EditorWindows.h index 9628af8..79e047c 100644 --- a/Editor/GUI/EditorWindows.h +++ b/Editor/GUI/EditorWindows.h @@ -56,7 +56,7 @@ public: ContainerWindow(LuaBind::VM* vm); ~ContainerWindow(); - void Init(Rect size, int showMode, const Internal::Vector2& minSize, const Internal::Vector2& maxSize, std::string name = ""); + void Init(Internal::Rect size, int showMode, const Internal::Vector2& minSize, const Internal::Vector2& maxSize, std::string name = ""); void SetTitle(const char* title); void SetIcon(LPCSTR iconName); void SetAsRenderContext(); @@ -85,7 +85,7 @@ private: bool m_IsClosing; bool m_InMenuLoop; bool m_CloseFromScriptDontShutdown; - Rect m_InternalRect; + Internal::Rect m_InternalRect; POINT m_MinSize; POINT m_MaxSize; @@ -140,7 +140,7 @@ public: void DoPaint(); void SetContainerWindow(ContainerWindow* wnd); void Focus(); - void SetPosition(Rect position); + void SetPosition(Internal::Rect position); void SetAsRenderContext(); void OnFocus(); diff --git a/Editor/GUI/GUIWindow.cpp b/Editor/GUI/GUIWindow.cpp index 621eb54..13249ac 100644 --- a/Editor/GUI/GUIWindow.cpp +++ b/Editor/GUI/GUIWindow.cpp @@ -200,7 +200,7 @@ void GUIWindow::Init(std::string name) //DWORD windowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_MINIMIZEBOX; DWORD extendedStyle = WS_EX_TOOLWINDOW; - Rect pixelRect; + Internal::Rect pixelRect; pixelRect.x = 0; pixelRect.y = 0; pixelRect.width = 32; @@ -330,7 +330,7 @@ void GUIWindow::OnPaint() InvokeLuaCallback(m_Script, "OnGUI"); } -void GUIWindow::SetPosition(Rect position) +void GUIWindow::SetPosition(Internal::Rect position) { log_info("GUIWindow::SetPosition()"); RECT rc; diff --git a/Editor/Scripting/EditorGUI/ContainerWindow.bind.cpp b/Editor/Scripting/EditorGUI/ContainerWindow.bind.cpp index 531128b..5906788 100644 --- a/Editor/Scripting/EditorGUI/ContainerWindow.bind.cpp +++ b/Editor/Scripting/EditorGUI/ContainerWindow.bind.cpp @@ -56,7 +56,7 @@ LUA_BIND_IMPL_METHOD(ContainerWindow, ContainerWindow::_New) ContainerWindow* wnd = new ContainerWindow(state.GetVM()); - Rect rect = state.GetValue<Rect>(state, Rect()); + Internal::Rect rect = state.GetValue<Internal::Rect>(state, Internal::Rect()); int showMode = state.GetValue<int>(2, 0); Internal::Vector2 min = state.GetValue<Internal::Vector2>(state, Internal::Vector2()); Internal::Vector2 max = state.GetValue<Internal::Vector2>(state, Internal::Vector2()); diff --git a/Editor/Scripting/EditorGUI/GUIWindow.bind.cpp b/Editor/Scripting/EditorGUI/GUIWindow.bind.cpp index e991398..7e31da5 100644 --- a/Editor/Scripting/EditorGUI/GUIWindow.bind.cpp +++ b/Editor/Scripting/EditorGUI/GUIWindow.bind.cpp @@ -62,7 +62,7 @@ LUA_BIND_IMPL_METHOD(GUIWindow, _SetPosition) if (!state.CheckParams(1, "UT")) return 0; - Rect rect; + Internal::Rect rect; rect.x = state.GetField<float>(2, 1, 0); rect.y = state.GetField<float>(2, 2, 0); rect.width = state.GetField<float>(2, 3, 0); diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj index ed406b2..c09ba4b 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj +++ b/Projects/VisualStudio/Editor/Editor.vcxproj @@ -210,6 +210,7 @@ <ClCompile Include="..\..\..\Runtime\Math\Matrix44.cpp" />
<ClCompile Include="..\..\..\Runtime\Math\Vector2.cpp" />
<ClCompile Include="..\..\..\Runtime\Math\Vector3.cpp" />
+ <ClCompile Include="..\..\..\Runtime\Math\Vector4.cpp" />
<ClCompile Include="..\..\..\Runtime\Profiling\FrameStats.cpp" />
<ClCompile Include="..\..\..\Runtime\Scripting\Common\Common.bind.cpp" />
<ClCompile Include="..\..\..\Runtime\Scripting\Common\DataBuffer.bind.cpp" />
@@ -302,6 +303,7 @@ <ClInclude Include="..\..\..\Runtime\Math\Rect.h" />
<ClInclude Include="..\..\..\Runtime\Math\Vector2.h" />
<ClInclude Include="..\..\..\Runtime\Math\Vector3.h" />
+ <ClInclude Include="..\..\..\Runtime\Math\Vector4.h" />
<ClInclude Include="..\..\..\Runtime\Profiling\FrameStats.h" />
<ClInclude Include="..\..\..\Runtime\Profiling\Profiler.h" />
<ClInclude Include="..\..\..\Runtime\Threading\Job.h" />
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters index 2614d08..6809215 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters +++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters @@ -375,6 +375,9 @@ <ClCompile Include="..\..\..\Runtime\Math\Matrix44.cpp">
<Filter>Runtime\Math</Filter>
</ClCompile>
+ <ClCompile Include="..\..\..\Runtime\Math\Vector4.cpp">
+ <Filter>Runtime\Math</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Editor\GUI\Dock.h">
@@ -635,6 +638,9 @@ <ClInclude Include="..\..\..\Runtime\Math\Matrix44.h">
<Filter>Runtime\Math</Filter>
</ClInclude>
+ <ClInclude Include="..\..\..\Runtime\Math\Vector4.h">
+ <Filter>Runtime\Math</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\Runtime\Lua\LuaBind\LuaBindClass.inc">
diff --git a/Runtime/Graphics/GfxDevice.cpp b/Runtime/Graphics/GfxDevice.cpp index dd077b8..e4747d8 100644 --- a/Runtime/Graphics/GfxDevice.cpp +++ b/Runtime/Graphics/GfxDevice.cpp @@ -64,6 +64,63 @@ void GfxDevice::Clear(int clearFlag) } +void GfxDevice::UseShader(LuaBind::State& state, Shader* shader, int idx) +{ + if (shader == NULL) + return; + + GLuint id = shader->GetID(); + if (id == 0) + return; + + glUseProgram(id); + + m_Shader.shader = shader; + m_Shader.ref.SetRef(state, idx); +} + +void GfxDevice::UnuseShader() +{ + if (m_Shader) + { + m_Shader.shader = NULL; + m_Shader.ref.UnRef(); + } + glUseProgram(0); +} + +void GfxDevice::SetUniformVec2(const char* name, Internal::Vector2 vec2) +{ + if (!m_Shader) + return; + GLint loc = glGetUniformLocation(m_Shader.GetID(), name); + glUniform2f(loc, vec2.x, vec2.y); +} + +void GfxDevice::SetUniformVec3(const char* name, Internal::Vector3 vec3) +{ + if (!m_Shader) + return; + GLint loc = glGetUniformLocation(m_Shader.GetID(), name); + glUniform3f(loc, vec3.x, vec3.y, vec3.z); +} + +void GfxDevice::SetUniformVec4(const char* name, Internal::Vector4 vec4) +{ + if (!m_Shader) + return; + GLint loc = glGetUniformLocation(m_Shader.GetID(), name); + glUniform4f(loc, vec4.x, vec4.y, vec4.z, vec4.w); +} + +void GfxDevice::SetUniformMat4(const char* name, Internal::Matrix44 mat4) +{ + if (!m_Shader) + return; + GLint loc = glGetUniformLocation(m_Shader.GetID(), name); + glUniformMatrix4fv(loc, 1, GL_TRUE, &mat4.m[0][0]); +} + void GfxDevice::BeginFrame() { m_IsInsideFrame = true; diff --git a/Runtime/Graphics/GfxDevice.h b/Runtime/Graphics/GfxDevice.h index 0931eae..ef7788c 100644 --- a/Runtime/Graphics/GfxDevice.h +++ b/Runtime/Graphics/GfxDevice.h @@ -4,6 +4,11 @@ #include "../Utilities/NonCopyable.h" #include "../Utilities/Type.h" #include "../Utilities/Assert.h" +#include "../Graphics/Shader.h" +#include "../Math/Vector2.h" +#include "../Math/Vector3.h" +#include "../Math/Vector4.h" +#include "../Math/Matrix44.h" #include "Texture.h" #include "DeviceDefine.h" @@ -17,6 +22,23 @@ struct GfxDeviceSetting ETextureWrapMode wrapMode; // 默认的贴图平铺模式 }; +// 当前绑定的shader +struct ShaderState +{ + LuaBind::StrongRef ref; // 在lua中的引用 + Shader* shader; + operator bool() + { + return ref && shader != NULL; + } + GLuint GetID() { + if (shader == nullptr) + return 0; + GLint id = shader->GetID(); + return id; + } +}; + // 对渲染相关API的封装 class GfxDevice : public NonCopyable { @@ -37,7 +59,14 @@ public: void SetAntiAliasing(int level = 0); - void Clear(int clearFlag); + void Clear(int clearFlag); + + void UseShader(LuaBind::State& state, Shader* shader, int idx); + void UnuseShader(); + void SetUniformVec2(const char* name, Internal::Vector2 vec2); + void SetUniformVec3(const char* name, Internal::Vector3 vec3); + void SetUniformVec4(const char* name, Internal::Vector4 vec4); + void SetUniformMat4(const char* name, Internal::Matrix44 mat4); void BeginFrame(); void EndFrame(); @@ -62,6 +91,8 @@ private: EStencilOp m_StencilOp; byte m_StencilMask; + ShaderState m_Shader; // 当前绑定的shader + // 贴图默认设置 ETextureFilterMode m_DefaultFilterMode; ETextureWrapMode m_DefaultWrapMode; diff --git a/Runtime/Graphics/Shader.cpp b/Runtime/Graphics/Shader.cpp index bf7faf6..2c3d686 100644 --- a/Runtime/Graphics/Shader.cpp +++ b/Runtime/Graphics/Shader.cpp @@ -35,15 +35,13 @@ void checkCompileshaderErrorors(GLuint shader, std::string type) } } -Shader::Shader(LuaBind::VM*vm, bool keepSrc) +Shader::Shader(LuaBind::VM*vm) : NativeClass<Shader>(vm) - , m_KeepSrc(keepSrc) { } -Shader::Shader(LuaBind::VM*vm, std::string& glslShader, bool keepSrc) +Shader::Shader(LuaBind::VM*vm, std::string& glslShader) : NativeClass<Shader>(vm) - , m_KeepSrc(keepSrc) { // stl的string会在大小超过阈值的情况下在栈里分配,并用RAII保证释放 std::string vsh ; @@ -56,14 +54,13 @@ Shader::Shader(LuaBind::VM*vm, std::string& glslShader, bool keepSrc) { throw ShaderCompileExecption(e.what()); } - CompileProgram(vsh.c_str(), fsh.c_str(), keepSrc); + CompileProgram(vsh.c_str(), fsh.c_str()); } -Shader::Shader(LuaBind::VM* vm, const char* vert, const char* frag, bool keepSrc) +Shader::Shader(LuaBind::VM* vm, const char* vert, const char* frag) : NativeClass<Shader>(vm) - , m_KeepSrc(keepSrc) { - CompileProgram(vert, frag, keepSrc); + CompileProgram(vert, frag); } void Shader::CompileProgram(const char* vert, const char* frag, bool keepSrc) @@ -86,12 +83,6 @@ void Shader::CompileProgram(const char* vert, const char* frag, bool keepSrc) glAttachShader(m_ProgramID, m_FragID); glLinkProgram(m_ProgramID); checkCompileshaderErrorors(m_FragID, "PROGRAM"); - // keep src? - if (keepSrc) - { - m_VertSrc = vert; - m_FragSrc = frag; - } } Shader::~Shader() @@ -118,12 +109,6 @@ void Shader::ReCompile(std::string& vert, std::string frag) glAttachShader(m_ProgramID, m_FragID); glLinkProgram(m_ProgramID); checkCompileshaderErrorors(m_FragID, "PROGRAM"); - // - if (m_KeepSrc) - { - m_VertSrc = vert; - m_FragSrc = frag; - } } void Shader::ReCompileVert(std::string& vert) @@ -139,11 +124,6 @@ void Shader::ReCompileVert(std::string& vert) glAttachShader(m_ProgramID, m_FragID); glLinkProgram(m_ProgramID); checkCompileshaderErrorors(m_FragID, "PROGRAM"); - // - if (m_KeepSrc) - { - m_VertSrc = vert; - } } void Shader::ReCompileFrag(std::string frag) @@ -159,11 +139,6 @@ void Shader::ReCompileFrag(std::string frag) glAttachShader(m_ProgramID, m_FragID); glLinkProgram(m_ProgramID); checkCompileshaderErrorors(m_FragID, "PROGRAM"); - // - if (m_KeepSrc) - { - m_FragSrc = frag; - } } bool Shader::IsValid() diff --git a/Runtime/Graphics/Shader.h b/Runtime/Graphics/Shader.h index 614e127..4e0dc37 100644 --- a/Runtime/Graphics/Shader.h +++ b/Runtime/Graphics/Shader.h @@ -11,9 +11,9 @@ class Shader : public LuaBind::NativeClass<Shader>
{
public:
- Shader(LuaBind::VM*vm, bool keepSrc = false)/*throw(ShaderCompileExecption)*/;
- Shader(LuaBind::VM*vm, std::string& glslShader, bool keepSrc = false)/*throw(ShaderCompileExecption)*/;
- Shader(LuaBind::VM*vm, const char* vert, const char* frag, bool keepSrc = false)/*throw(ShaderCompileExecption)*/;
+ Shader(LuaBind::VM*vm)/*throw(ShaderCompileExecption)*/;
+ Shader(LuaBind::VM*vm, std::string& glslShader)/*throw(ShaderCompileExecption)*/;
+ Shader(LuaBind::VM*vm, const char* vert, const char* frag)/*throw(ShaderCompileExecption)*/;
~Shader();
void ReCompile(std::string& vert, std::string frag)/*throw(ShaderCompileExecption)*/;
@@ -27,11 +27,6 @@ public: private:
void CompileProgram(const char* vert, const char* frag, bool keepSrc = false);
- bool m_KeepSrc;
-
- std::string m_VertSrc;
- std::string m_FragSrc;
-
GLint m_ProgramID;
GLint m_FragID;
GLint m_VertID;
@@ -42,9 +37,16 @@ private: LUA_BIND_DECL_METHOD(_ReCompile);
LUA_BIND_DECL_METHOD(_ReCompileVert);
LUA_BIND_DECL_METHOD(_ReCompileFrag);
- LUA_BIND_DECL_METHOD(_IsValid);
- LUA_BIND_DECL_METHOD(_GetVertCode);
- LUA_BIND_DECL_METHOD(_GetFragCode);
+ LUA_BIND_DECL_METHOD(_IsValid);
+ LUA_BIND_DECL_METHOD(_Use);
+ LUA_BIND_DECL_METHOD(_UnUse);
+
+ LUA_BIND_DECL_METHOD(_SetVector2);
+ LUA_BIND_DECL_METHOD(_SetVector3);
+ LUA_BIND_DECL_METHOD(_SetVector4);
+ LUA_BIND_DECL_METHOD(_SetMatrix3);
+ LUA_BIND_DECL_METHOD(_SetMatrix4);
+ //LUA_BIND_DECL_METHOD(_SetColor);
};
diff --git a/Runtime/Graphics/UIQuad.cpp b/Runtime/Graphics/UIQuad.cpp index fb5964f..73cf645 100644 --- a/Runtime/Graphics/UIQuad.cpp +++ b/Runtime/Graphics/UIQuad.cpp @@ -49,7 +49,7 @@ void UIQuad::Draw() for (int i = 0; i < nVerts; ++i) { - dst[i].position.Set(pos[3 * i], pos[3 * i + 1]); + dst[i].position.Set(pos[2 * i], pos[2 * i + 1]); dst[i].uv.Set(uv[2 * i], uv[2 * i + 1]); } diff --git a/Runtime/Graphics/UIQuad.h b/Runtime/Graphics/UIQuad.h index 166b662..a0d77a5 100644 --- a/Runtime/Graphics/UIQuad.h +++ b/Runtime/Graphics/UIQuad.h @@ -3,6 +3,13 @@ class UIQuad { public : + UIQuad(float l, float r, float t, float b) + { + m_Left = l; + m_Right = r; + m_Top = t; + m_Bottom = b; + } void Draw(); private: diff --git a/Runtime/Lua/LuaBind/LuaBindRef.cpp b/Runtime/Lua/LuaBind/LuaBindRef.cpp index d0f2766..313824e 100644 --- a/Runtime/Lua/LuaBind/LuaBindRef.cpp +++ b/Runtime/Lua/LuaBind/LuaBindRef.cpp @@ -3,6 +3,12 @@ namespace LuaBind { + UniversalRef::UniversalRef(RefMode mode) + : mRefID(LUA_NOREF) + , mMode(mode) + , mOwner(NULL) // 延后设置 + { + } UniversalRef::UniversalRef(LuaBind::VM* vm, RefMode mode) : mRefID(LUA_NOREF) @@ -43,7 +49,11 @@ namespace LuaBind void UniversalRef::SetRef(LuaBind::State& state, int idx) { - assert(state.GetVM() == mOwner); + //assert(state.GetVM() == mOwner); + // 延后设置vm + if (mOwner == NULL) { + mOwner = state.GetVM(); + } VM* vm = mOwner; if (!vm) return; @@ -83,11 +93,21 @@ namespace LuaBind return true; } + StrongRef::StrongRef() + : UniversalRef(STRONG_REF) + { + } + StrongRef::StrongRef(LuaBind::VM* vm) : UniversalRef(vm, STRONG_REF) { } + WeakRef::WeakRef() + : UniversalRef(WEAK_REF) + { + } + WeakRef::WeakRef(LuaBind::VM* vm) : UniversalRef(vm, WEAK_REF) { diff --git a/Runtime/Lua/LuaBind/LuaBindRef.h b/Runtime/Lua/LuaBind/LuaBindRef.h index 793559e..a19e5bf 100644 --- a/Runtime/Lua/LuaBind/LuaBindRef.h +++ b/Runtime/Lua/LuaBind/LuaBindRef.h @@ -18,6 +18,7 @@ namespace LuaBind WEAK_REF }; + UniversalRef(RefMode mode = STRONG_REF); // 延后到SetRef设置vm UniversalRef(LuaBind::VM* vm, RefMode mode = STRONG_REF); virtual ~UniversalRef(); @@ -46,6 +47,9 @@ namespace LuaBind class StrongRef: public UniversalRef { public: + // 延后到SetRef设置vm + StrongRef(); + StrongRef(LuaBind::VM* vm); }; @@ -54,6 +58,9 @@ namespace LuaBind class WeakRef : public UniversalRef { public: + // 延后到SetRef设置vm + WeakRef(); + WeakRef(LuaBind::VM* vm); }; diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index 9210768..384cba2 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -602,6 +602,12 @@ namespace LuaBind case '+': if (type == LUA_TNIL) expected = false; break; + + // nil + case '!': + if (type != LUA_TNIL) expected = false; + break; + } if (!expected) { diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp index a47d528..e7247c0 100644 --- a/Runtime/Lua/LuaHelper.cpp +++ b/Runtime/Lua/LuaHelper.cpp @@ -1,27 +1,141 @@ #include "LuaHelper.h"
+#include "Runtime/Math/Vector2.h"
+#include "Runtime/Math/Vector3.h"
+#include "Runtime/Math/Vector4.h"
+#include "Runtime/Math/Matrix44.h"
+
using namespace LuaBind;
template <>
-Rect State::GetValue < Rect >(int idx, const Rect value)
+Internal::Rect State::GetValue < Internal::Rect >(int idx, const Internal::Rect value)
{
- Rect rect;
- rect.x = GetField<float>(idx, 1, 0);
- rect.y = GetField<float>(idx, 2, 0);
- rect.width = GetField<float>(idx, 3, 0);
- rect.height = GetField<float>(idx, 4, 0);
+ Internal::Rect rect = value;
+ if (LuaHelper::IsType(*this, "GameLab.Engine.Math.Rect", idx))
+ {
+ rect.x = GetField<float>(idx, "x", 0);
+ rect.y = GetField<float>(idx, "y", 0);
+ rect.width = GetField<float>(idx, "z", 0);
+ rect.height = GetField<float>(idx, "w", 0);
+ }
+ else
+ {
+ rect.x = GetField<float>(idx, 1, 0);
+ rect.y = GetField<float>(idx, 2, 0);
+ rect.width = GetField<float>(idx, 3, 0);
+ rect.height = GetField<float>(idx, 4, 0);
+ }
return rect;
}
template <>
Internal::Vector2 State::GetValue < Internal::Vector2 >(int idx, const Internal::Vector2 value)
{
- Internal::Vector2 v2;
- v2.x = GetField<float>(idx, 1, 0);
- v2.y = GetField<float>(idx, 2, 0);
+ Internal::Vector2 v2 = value;
+ if (LuaHelper::IsType(*this, "GameLab.Engine.Math.Vector2", idx))
+ {
+ v2.x = GetField<float>(idx, "x", 0);
+ v2.y = GetField<float>(idx, "y", 0);
+ }
+ else
+ {
+ v2.x = GetField<float>(idx, 1, 0);
+ v2.y = GetField<float>(idx, 2, 0);
+ }
return v2;
}
+template <>
+Internal::Vector3 State::GetValue < Internal::Vector3 >(int idx, const Internal::Vector3 value)
+{
+ Internal::Vector3 v3 = value;
+ if (LuaHelper::IsType(*this, "GameLab.Engine.Math.Vector3", idx))
+ {
+ v3.x = GetField<float>(idx, "x", 0);
+ v3.y = GetField<float>(idx, "y", 0);
+ v3.z = GetField<float>(idx, "z", 0);
+ }
+ else
+ {
+ v3.x = GetField<float>(idx, 1, 0);
+ v3.y = GetField<float>(idx, 2, 0);
+ v3.z = GetField<float>(idx, 3, 0);
+ }
+ return v3;
+}
+
+template <>
+Internal::Vector4 State::GetValue < Internal::Vector4 >(int idx, const Internal::Vector4 value)
+{
+ Internal::Vector4 v4 = value;
+ if (LuaHelper::IsType(*this, "GameLab.Engine.Math.Vector4", idx))
+ {
+ v4.x = GetField<float>(idx, "x", 0);
+ v4.y = GetField<float>(idx, "y", 0);
+ v4.z = GetField<float>(idx, "z", 0);
+ v4.w = GetField<float>(idx, "w", 0);
+ }
+ else
+ {
+ v4.x = GetField<float>(idx, 1, 0);
+ v4.y = GetField<float>(idx, 2, 0);
+ v4.z = GetField<float>(idx, 3, 0);
+ v4.w = GetField<float>(idx, 4, 0);
+ }
+ return v4;
+}
+
+template <>
+Internal::Matrix44 State::GetValue < Internal::Matrix44 >(int idx, const Internal::Matrix44 value)
+{
+ Internal::Matrix44 m4 = value;
+ if (LuaHelper::IsType(*this, "GameLab.Engine.Math.Matrix44", idx))
+ {
+ m4.m[0][0] = GetField<float>(idx, "m00", 0);
+ m4.m[0][1] = GetField<float>(idx, "m01", 0);
+ m4.m[0][2] = GetField<float>(idx, "m02", 0);
+ m4.m[0][3] = GetField<float>(idx, "m03", 0);
+
+ m4.m[1][0] = GetField<float>(idx, "m10", 0);
+ m4.m[1][1] = GetField<float>(idx, "m11", 0);
+ m4.m[1][2] = GetField<float>(idx, "m12", 0);
+ m4.m[1][3] = GetField<float>(idx, "m13", 0);
+
+ m4.m[2][0] = GetField<float>(idx, "m20", 0);
+ m4.m[2][1] = GetField<float>(idx, "m21", 0);
+ m4.m[2][2] = GetField<float>(idx, "m22", 0);
+ m4.m[2][3] = GetField<float>(idx, "m23", 0);
+
+ m4.m[3][0] = GetField<float>(idx, "m30", 0);
+ m4.m[3][1] = GetField<float>(idx, "m31", 0);
+ m4.m[3][2] = GetField<float>(idx, "m32", 0);
+ m4.m[3][3] = GetField<float>(idx, "m33", 0);
+ }
+ else
+ {
+ m4.m[0][0] = GetField<float>(idx, 1, 0);
+ m4.m[0][1] = GetField<float>(idx, 2, 0);
+ m4.m[0][2] = GetField<float>(idx, 3, 0);
+ m4.m[0][3] = GetField<float>(idx, 4, 0);
+
+ m4.m[1][0] = GetField<float>(idx, 5, 0);
+ m4.m[1][1] = GetField<float>(idx, 6, 0);
+ m4.m[1][2] = GetField<float>(idx, 7, 0);
+ m4.m[1][3] = GetField<float>(idx, 8, 0);
+
+ m4.m[2][0] = GetField<float>(idx, 9, 0);
+ m4.m[2][1] = GetField<float>(idx, 10, 0);
+ m4.m[2][2] = GetField<float>(idx, 11, 0);
+ m4.m[2][3] = GetField<float>(idx, 12, 0);
+
+ m4.m[3][0] = GetField<float>(idx, 13, 0);
+ m4.m[3][1] = GetField<float>(idx, 14, 0);
+ m4.m[3][2] = GetField<float>(idx, 15, 0);
+ m4.m[3][3] = GetField<float>(idx, 16, 0);
+ }
+ return m4;
+}
+
int LuaHelper::Call(const char* func, const char* params, ...)
{
return 1;
diff --git a/Runtime/Math/Matrix44.cpp b/Runtime/Math/Matrix44.cpp index e69de29..5b72342 100644 --- a/Runtime/Math/Matrix44.cpp +++ b/Runtime/Math/Matrix44.cpp @@ -0,0 +1,4 @@ +#include "Matrix44.h"
+
+
+Internal::Matrix44 Internal::Matrix44::identity;
\ No newline at end of file diff --git a/Runtime/Math/Matrix44.h b/Runtime/Math/Matrix44.h index 1ec92fd..0fbf4a7 100644 --- a/Runtime/Math/Matrix44.h +++ b/Runtime/Math/Matrix44.h @@ -2,8 +2,20 @@ namespace Internal { - class Matrix44 + struct Matrix44 { + Matrix44() + { + m[0][0] = 1; + m[1][1] = 1; + m[2][2] = 1; + m[3][3] = 1; + } + + float m[4][4]; // 行主项 + + static Matrix44 identity; }; -} + +}
\ No newline at end of file diff --git a/Runtime/Math/Rect.h b/Runtime/Math/Rect.h index 4725375..80170d6 100644 --- a/Runtime/Math/Rect.h +++ b/Runtime/Math/Rect.h @@ -1,6 +1,9 @@ #pragma once -struct Rect +namespace Internal { - int x, y, width, height; -};
\ No newline at end of file + struct Rect + { + int x, y, width, height; + }; +}
\ No newline at end of file diff --git a/Runtime/Math/Vector3.cpp b/Runtime/Math/Vector3.cpp index 8b13789..13bc9fd 100644 --- a/Runtime/Math/Vector3.cpp +++ b/Runtime/Math/Vector3.cpp @@ -1 +1,4 @@ +#include "Vector3.h" +Internal::Vector3 Internal::Vector3::one = Internal::Vector3(1, 1, 1); +Internal::Vector3 Internal::Vector3::zero = Internal::Vector3(0, 0, 0); diff --git a/Runtime/Math/Vector3.h b/Runtime/Math/Vector3.h index c0a35f3..e81e880 100644 --- a/Runtime/Math/Vector3.h +++ b/Runtime/Math/Vector3.h @@ -7,7 +7,12 @@ namespace Internal struct Vector3 { float x, y, z; - + Vector3(float x = 0, float y = 0, float z = 0) + { + this->x = x; + this->y = y; + this->z = z; + } inline void Set(float x, float y, float z) { this->x = x; @@ -15,6 +20,10 @@ namespace Internal this->z = z; } + + static Vector3 zero; + static Vector3 one; + }; diff --git a/Runtime/Math/Vector4.cpp b/Runtime/Math/Vector4.cpp new file mode 100644 index 0000000..1d392b7 --- /dev/null +++ b/Runtime/Math/Vector4.cpp @@ -0,0 +1,4 @@ +#include "Vector4.h" + +Internal::Vector4 Internal::Vector4::one = Internal::Vector4(1, 1, 1, 1); +Internal::Vector4 Internal::Vector4::zero = Internal::Vector4(0, 0, 0, 0); diff --git a/Runtime/Math/Vector4.h b/Runtime/Math/Vector4.h new file mode 100644 index 0000000..c0f99f2 --- /dev/null +++ b/Runtime/Math/Vector4.h @@ -0,0 +1,32 @@ +#pragma once + +namespace Internal +{ + + struct Vector4 + { + float x, y, z, w; + + Vector4(float x = 0, float y = 0, float z = 0, float w = 0) + { + this->x = x; + this->y = y; + this->z = z; + this->w = w; + } + + inline void Set(float x, float y, float z, float w) + { + this->x = x; + this->y = y; + this->z = z; + this->w = w; + } + + + static Vector4 zero; + static Vector4 one; + }; + + +} diff --git a/Runtime/Scripting/Rendering/Rendering.bind.cpp b/Runtime/Scripting/Rendering/Rendering.bind.cpp index 9bb6242..197ff0a 100644 --- a/Runtime/Scripting/Rendering/Rendering.bind.cpp +++ b/Runtime/Scripting/Rendering/Rendering.bind.cpp @@ -1,8 +1,20 @@ #include "Runtime/Graphics/Shader.h" #include "Runtime/Graphics/Texture.h" #include "Runtime/Graphics/ImageData.h" +#include "Runtime/Graphics/UIQuad.h" + +// Rendering.DrawUIQuad({}) +static int DrawUIQuad(lua_State* L) +{ + LUA_BIND_STATE(L); + Internal::Rect rect = state.GetValue<Internal::Rect>(1, Internal::Rect()); + UIQuad quad = UIQuad(rect.x, rect.x + rect.width, rect.y, rect.y + rect.height); + quad.Draw(); + return 0; +} static luaL_Reg funcs[] = { + {"DrawUIQuad", DrawUIQuad}, {0, 0} }; diff --git a/Runtime/Scripting/Rendering/Shader.bind.cpp b/Runtime/Scripting/Rendering/Shader.bind.cpp index 1605be5..89f1ebb 100644 --- a/Runtime/Scripting/Rendering/Shader.bind.cpp +++ b/Runtime/Scripting/Rendering/Shader.bind.cpp @@ -1,5 +1,6 @@ #include "Runtime/Graphics/Shader.h" #include "Runtime/Debug/Log.h" +#include "Runtime/Graphics/GfxDevice.h" using namespace LuaBind; @@ -11,9 +12,15 @@ LUA_BIND_REGISTRY(Shader) { "ReCompileVert", _ReCompileVert }, { "ReCompileFrag", _ReCompileFrag }, { "IsValid", _IsValid }, - { "GetVertCode", _GetVertCode }, - { "GetFragCode", _GetFragCode } - ); + { "Use", _Use }, + { "Unuse", _UnUse }, + //{ "SetColor", _SetColor }, + { "SetVector2", _SetVector2 }, + { "SetVector3", _SetVector3 }, + { "SetVector4", _SetVector4 }, + //{ "SetMatrix3", _SetMatrix3 }, + { "SetMatrix44", _SetMatrix4 } + ); } LUA_BIND_POSTPROCESS(Shader) @@ -25,29 +32,16 @@ LUA_BIND_POSTPROCESS(Shader) LUA_BIND_IMPL_METHOD(Shader, _New) { LUA_BIND_STATE(L, Shader); - LUA_BIND_CHECK(L, "SS*|S*"); + LUA_BIND_CHECK(L, "SS!|S!"); try { Shader* shader = NULL; - if (state.CheckParams(1, "SSB")) - { - cc8* vert = state.GetValue<cc8*>(-3, ""); - cc8* frag = state.GetValue<cc8*>(-2, ""); - bool keepCode = state.GetValue<bool>(-1, false); - shader = new Shader(state.GetVM(), vert, frag, keepCode); - } - else if(state.CheckParams(1, "SS")) + if(state.CheckParams(1, "SS!")) { cc8* vert = state.GetValue<cc8*>(-2, ""); cc8* frag = state.GetValue<cc8*>(-1, ""); shader = new Shader(state.GetVM(), vert, frag); } - else if (state.CheckParams(1, "SB")) - { - std::string glsl = state.GetValue<cc8*>(1, ""); - bool keepCode = state.GetValue<bool>(2, false); - shader = new Shader(state.GetVM(), glsl, keepCode); - } - else if (state.CheckParams(1, "S")) + else if (state.CheckParams(1, "S!")) { std::string glsl = state.GetValue<cc8*>(1, ""); shader = new Shader(state.GetVM(), glsl); @@ -96,16 +90,74 @@ LUA_BIND_IMPL_METHOD(Shader, _IsValid) return 1; } -LUA_BIND_IMPL_METHOD(Shader, _GetVertCode) +LUA_BIND_IMPL_METHOD(Shader, _Use) { - LUA_BIND_PREPARE(L, Shader); - state.Push(self->m_VertSrc.c_str()); - return 1; + LUA_BIND_PREPARE(L, Shader); + g_GfxDevice.UseShader(state, self, 1); + state.Push(true); + return 1; } -LUA_BIND_IMPL_METHOD(Shader, _GetFragCode) +LUA_BIND_IMPL_METHOD(Shader, _UnUse) { - LUA_BIND_PREPARE(L, Shader); - state.Push(self->m_FragSrc.c_str()); - return 1; + LUA_BIND_PREPARE(L, Shader); + g_GfxDevice.UnuseShader(); + state.Push(true); + return 1; +} + +// shader:SetVector2(name, vec2) +// shader:SetVector2(name, {}) +LUA_BIND_IMPL_METHOD(Shader, _SetVector2) +{ + LUA_BIND_PREPARE(L, Shader); + LUA_BIND_CHECK(L, "UST"); + + cc8* name = state.GetValue(2, ""); + Internal::Vector2 v2 = state.GetValue<Internal::Vector2>(3, Internal::Vector2::zero); + + g_GfxDevice.SetUniformVec2(name, v2); + return 1; +} + +// shader:SetVector3(name, vec3) +// shader:SetVector3(name, {}) +LUA_BIND_IMPL_METHOD(Shader, _SetVector3) +{ + LUA_BIND_PREPARE(L, Shader); + LUA_BIND_CHECK(L, "UST"); + + cc8* name = state.GetValue(2, ""); + Internal::Vector3 v3 = state.GetValue<Internal::Vector3>(3, Internal::Vector3::zero); + + g_GfxDevice.SetUniformVec3(name, v3); + return 1; +} + +// shader:SetVector4(name, vec4) +// shader:SetVector4(name, {}) +LUA_BIND_IMPL_METHOD(Shader, _SetVector4) +{ + LUA_BIND_PREPARE(L, Shader); + LUA_BIND_CHECK(L, "UST"); + + cc8* name = state.GetValue(2, ""); + Internal::Vector4 v4 = state.GetValue<Internal::Vector4>(3, Internal::Vector4::zero); + + g_GfxDevice.SetUniformVec4(name, v4); + return 1; +} + +// shader:SetMatrix4(name, mat4) +// shader:SetMatrix4(name, {}) +LUA_BIND_IMPL_METHOD(Shader, _SetMatrix4) +{ + LUA_BIND_PREPARE(L, Shader); + LUA_BIND_CHECK(L, "UST"); + + cc8* name = state.GetValue(2, ""); + Internal::Matrix44 m4 = state.GetValue<Internal::Matrix44>(3, Internal::Matrix44::identity); + + g_GfxDevice.SetUniformMat4(name, m4); + return 1; }
\ No newline at end of file |