summaryrefslogtreecommitdiff
path: root/Runtime/Graphics
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Graphics')
-rw-r--r--Runtime/Graphics/GfxDevice.cpp24
-rw-r--r--Runtime/Graphics/GfxDevice.h3
-rw-r--r--Runtime/Graphics/Shader.h1
3 files changed, 28 insertions, 0 deletions
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);
};