diff options
Diffstat (limited to 'Runtime')
-rw-r--r-- | Runtime/Graphics/GfxDevice.cpp | 57 | ||||
-rw-r--r-- | Runtime/Graphics/GfxDevice.h | 33 | ||||
-rw-r--r-- | Runtime/Graphics/Shader.cpp | 35 | ||||
-rw-r--r-- | Runtime/Graphics/Shader.h | 24 | ||||
-rw-r--r-- | Runtime/Graphics/UIQuad.cpp | 2 | ||||
-rw-r--r-- | Runtime/Graphics/UIQuad.h | 7 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindRef.cpp | 22 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindRef.h | 7 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindState.cpp | 6 | ||||
-rw-r--r-- | Runtime/Lua/LuaHelper.cpp | 132 | ||||
-rw-r--r-- | Runtime/Math/Matrix44.cpp | 4 | ||||
-rw-r--r-- | Runtime/Math/Matrix44.h | 16 | ||||
-rw-r--r-- | Runtime/Math/Rect.h | 9 | ||||
-rw-r--r-- | Runtime/Math/Vector3.cpp | 3 | ||||
-rw-r--r-- | Runtime/Math/Vector3.h | 11 | ||||
-rw-r--r-- | Runtime/Math/Vector4.cpp | 4 | ||||
-rw-r--r-- | Runtime/Math/Vector4.h | 32 | ||||
-rw-r--r-- | Runtime/Scripting/Rendering/Rendering.bind.cpp | 12 | ||||
-rw-r--r-- | Runtime/Scripting/Rendering/Shader.bind.cpp | 106 |
19 files changed, 436 insertions, 86 deletions
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 |