summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-29 15:02:46 +0800
committerchai <chaifix@163.com>2021-10-29 15:02:46 +0800
commit796b4b05ec62eb5d58a634854998f485072e8a2b (patch)
tree30f188382f14b3b9c69fe5846262a39a8b4664cc
parent91c32cb173201ac8803a1e4452e8342969b8e484 (diff)
*passing texture to glsl
-rw-r--r--Data/DefaultContent/Libraries/GameLab/Engine/Rendering/init.lua11
-rw-r--r--Data/Scripts/Editor/AssetBrowser.lua28
-rw-r--r--Runtime/Graphics/GfxDevice.cpp24
-rw-r--r--Runtime/Graphics/GfxDevice.h3
-rw-r--r--Runtime/Graphics/Shader.h1
-rw-r--r--Runtime/Scripting/GL/GL.bind.cpp16
-rw-r--r--Runtime/Scripting/Rendering/Rendering.bind.cpp8
-rw-r--r--Runtime/Scripting/Rendering/Shader.bind.cpp19
8 files changed, 93 insertions, 17 deletions
diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/init.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/init.lua
index d9b4e3e..caefccf 100644
--- a/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/init.lua
+++ b/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/init.lua
@@ -3,6 +3,8 @@ GameLab.Engine.Rendering = m
local import = GameLab.import(...)
+local Shader = GameLab.Engine.Rendering.Shader
+
m.Color = import("Color")
m.Color32 = import("Color32")
@@ -10,4 +12,13 @@ m.LoadTexture = function(path)
end
+m.SetMatrix44 = Shader.SetMatrix44
+m.SetVector2 = Shader.SetVector2
+m.SetVector3 = Shader.SetVector3
+m.SetVector4 = Shader.SetVector4
+m.SetTexture = Shader.SetTexture
+
+m.UseShader = Shader.Use
+m.UnuseShader = Shader.Unuse
+
return m \ No newline at end of file
diff --git a/Data/Scripts/Editor/AssetBrowser.lua b/Data/Scripts/Editor/AssetBrowser.lua
index b936702..34337b1 100644
--- a/Data/Scripts/Editor/AssetBrowser.lua
+++ b/Data/Scripts/Editor/AssetBrowser.lua
@@ -2,6 +2,7 @@ local Debug = GameLab.Debug
local AssetBrowser = GameLab.Editor.GUI.EditorWindow.Extend("GameLab.Editor.AssetBrowser")
local GL = GameLab.Engine.GL
local Matrix44 = GameLab.Engine.Math.Matrix44
+local Engine = GameLab.Engine
local inspect = require("inspect")
AssetBrowser.Ctor = function(self)
@@ -21,37 +22,49 @@ layout (location = 1) in vec2 vUV;
uniform mat4 mvp;
+out vec2 uv;
+
void main()
{
vec4 clip = mvp * vec4(vPos, -1, 1.0);
gl_Position = clip;
+ uv = vUV;
}
VSH_END
FSH_BEGIN
+uniform sampler2D uiTex;
+
+in vec2 uv;
out vec4 FragColor;
void main()
{
- FragColor = vec4(1,1,1,1);
+ FragColor = texture(uiTex, uv);
}
FSH_END
]]
+local tex
+
AssetBrowser.OnGUI = function(self)
+ if tex == nil then
+ tex = Engine.Resource.LoadTexture("./Resources/Images/brickwall.jpg")
+ end
+
if shader == nil then
- shader = GameLab.Engine.Rendering.Shader.New(glsl)
+ shader = 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.ClearColor({0.1,0.1,0.1,1})
GL.Clear(GL.EBufferType.ColorBuffer)
-- GL.Color({1,1,0,1})
-- GL.LoadPixelMatrix(-250, 250, -300, 300)
@@ -61,10 +74,11 @@ AssetBrowser.OnGUI = function(self)
-- GL.Vertex({250,0,-1})
-- GL.End()
- shader:Use()
- shader:SetMatrix44("mvp", ortho)
- GameLab.Engine.Rendering.DrawUIQuad({0, 0, 100, 100})
-
+ Engine.Rendering.UseShader(shader)
+ Engine.Rendering.SetMatrix44(shader, "mvp", ortho)
+ Engine.Rendering.SetTexture(shader, "tex", tex)
+ Engine.Rendering.DrawUIQuad({0, 0, 100, 100})
+ Engine.Rendering.ResetUniformState()
end
AssetBrowser.OnFocus = function(self)
diff --git a/Runtime/Graphics/GfxDevice.cpp b/Runtime/Graphics/GfxDevice.cpp
index e4747d8..bd8dd9d 100644
--- a/Runtime/Graphics/GfxDevice.cpp
+++ b/Runtime/Graphics/GfxDevice.cpp
@@ -1,7 +1,11 @@
+#include <vector>
#include "GfxDevice.h"
static bool deviceInited = false;
+static const std::vector<byte> s_AvailableTextureUnitPreset = {0,1,2,3,4,5,6,7}; // 最多支持8个贴图
+static std::vector<byte> s_AvailableTextureUnit = s_AvailableTextureUnitPreset;
+
GfxDevice g_GfxDevice;
GfxDevice::GfxDevice()
@@ -121,6 +125,26 @@ void GfxDevice::SetUniformMat4(const char* name, Internal::Matrix44 mat4)
glUniformMatrix4fv(loc, 1, GL_TRUE, &mat4.m[0][0]);
}
+void GfxDevice::SetUniformTexture(const char* name, Texture* texture)
+{
+ if (s_AvailableTextureUnit.size() == 0)
+ {
+ log_error("No available texture unit. Too many textures or forget invoke ResetUniformsState()");
+ return;
+ }
+ int texUnit = s_AvailableTextureUnit.back();
+ s_AvailableTextureUnit.pop_back();
+ glActiveTexture(GL_TEXTURE0 + texUnit);
+ glBindTexture(GL_TEXTURE_2D, texture->GetGpuID());
+ GLint loc = glGetUniformLocation(m_Shader.GetID(), name);
+ glUniform1i(loc, texUnit);
+}
+
+void GfxDevice::ResetUniformsState()
+{
+ s_AvailableTextureUnit = s_AvailableTextureUnitPreset;
+}
+
void GfxDevice::BeginFrame()
{
m_IsInsideFrame = true;
diff --git a/Runtime/Graphics/GfxDevice.h b/Runtime/Graphics/GfxDevice.h
index ef7788c..68cd6c0 100644
--- a/Runtime/Graphics/GfxDevice.h
+++ b/Runtime/Graphics/GfxDevice.h
@@ -67,6 +67,9 @@ public:
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 SetUniformTexture(const char* name, Texture* texture);
+
+ void ResetUniformsState();
void BeginFrame();
void EndFrame();
diff --git a/Runtime/Graphics/Shader.h b/Runtime/Graphics/Shader.h
index 4e0dc37..8a8c1f5 100644
--- a/Runtime/Graphics/Shader.h
+++ b/Runtime/Graphics/Shader.h
@@ -46,6 +46,7 @@ private:
LUA_BIND_DECL_METHOD(_SetVector4);
LUA_BIND_DECL_METHOD(_SetMatrix3);
LUA_BIND_DECL_METHOD(_SetMatrix4);
+ LUA_BIND_DECL_METHOD(_SetTexture);
//LUA_BIND_DECL_METHOD(_SetColor);
};
diff --git a/Runtime/Scripting/GL/GL.bind.cpp b/Runtime/Scripting/GL/GL.bind.cpp
index 903e706..40093e9 100644
--- a/Runtime/Scripting/GL/GL.bind.cpp
+++ b/Runtime/Scripting/GL/GL.bind.cpp
@@ -54,10 +54,10 @@ 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);
+ float r = state.GetField<float>(-1, "r", 0);
+ float g = state.GetField<float>(-1, "g", 0);
+ float b = state.GetField<float>(-1, "b", 0);
+ float a = state.GetField<float>(-1, "a", 0);
glClearColor(r, g, b, a);
}
else if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color32", -1))
@@ -70,10 +70,10 @@ int ClearColor(lua_State* L)
}
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);
+ float r = state.GetField<float>(-1, 1, 0);
+ float g = state.GetField<float>(-1, 2, 0);
+ float b = state.GetField<float>(-1, 3, 0);
+ float a = state.GetField<float>(-1, 4, 0);
glClearColor(r, g, b, a);
}
else
diff --git a/Runtime/Scripting/Rendering/Rendering.bind.cpp b/Runtime/Scripting/Rendering/Rendering.bind.cpp
index 197ff0a..cb294a3 100644
--- a/Runtime/Scripting/Rendering/Rendering.bind.cpp
+++ b/Runtime/Scripting/Rendering/Rendering.bind.cpp
@@ -2,6 +2,7 @@
#include "Runtime/Graphics/Texture.h"
#include "Runtime/Graphics/ImageData.h"
#include "Runtime/Graphics/UIQuad.h"
+#include "Runtime/Graphics/GfxDevice.h"
// Rendering.DrawUIQuad({})
static int DrawUIQuad(lua_State* L)
@@ -13,8 +14,15 @@ static int DrawUIQuad(lua_State* L)
return 0;
}
+static int ResetUniformState(lua_State* L)
+{
+ g_GfxDevice.ResetUniformsState();
+ return 0;
+}
+
static luaL_Reg funcs[] = {
{"DrawUIQuad", DrawUIQuad},
+ {"ResetUniformState", ResetUniformState},
{0, 0}
};
diff --git a/Runtime/Scripting/Rendering/Shader.bind.cpp b/Runtime/Scripting/Rendering/Shader.bind.cpp
index 89f1ebb..83f8477 100644
--- a/Runtime/Scripting/Rendering/Shader.bind.cpp
+++ b/Runtime/Scripting/Rendering/Shader.bind.cpp
@@ -15,12 +15,13 @@ LUA_BIND_REGISTRY(Shader)
{ "Use", _Use },
{ "Unuse", _UnUse },
//{ "SetColor", _SetColor },
+ { "SetTexture", _SetTexture },
{ "SetVector2", _SetVector2 },
{ "SetVector3", _SetVector3 },
{ "SetVector4", _SetVector4 },
//{ "SetMatrix3", _SetMatrix3 },
{ "SetMatrix44", _SetMatrix4 }
- );
+ );
}
LUA_BIND_POSTPROCESS(Shader)
@@ -160,4 +161,18 @@ LUA_BIND_IMPL_METHOD(Shader, _SetMatrix4)
g_GfxDevice.SetUniformMat4(name, m4);
return 1;
-} \ No newline at end of file
+}
+
+// shader:SetTexture(name,texture)
+LUA_BIND_IMPL_METHOD(Shader, _SetTexture)
+{
+ LUA_BIND_PREPARE(L, Shader);
+ LUA_BIND_CHECK(L, "USU");
+
+ cc8* name = state.GetValue(2, "");
+ Texture* tex = state.GetUserdata<Texture>();
+
+ g_GfxDevice.SetUniformTexture(name, tex);
+
+ return 1;
+}