From 684f71790401727cc45f4dad1822ddae46305072 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Feb 2019 09:07:37 +0800 Subject: +widgets --- Source/Asura.Engine/Graphics/Shader.h | 90 ++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 22 deletions(-) (limited to 'Source/Asura.Engine/Graphics/Shader.h') diff --git a/Source/Asura.Engine/Graphics/Shader.h b/Source/Asura.Engine/Graphics/Shader.h index e22fc66..02d9f46 100644 --- a/Source/Asura.Engine/Graphics/Shader.h +++ b/Source/Asura.Engine/Graphics/Shader.h @@ -1,62 +1,108 @@ #ifndef __ASURA_ENGINE_SHADER_H__ #define __ASURA_ENGINE_SHADER_H__ -#include "luax/luax.h" +#include +#include -#include "Map.h" +#include "luax/luax.h" #include "StringMap.hpp" #include "Object.h" #include "Color.h" #include "Manager.hpp" +#include "Texture.h" +#include "Math/Vector2.h" +#include "Math/Vector3.h" +#include "Math/Vector4.h" +#include "GL.h" namespace AsuraEngine { namespace Graphics { - class Material; - /// - /// 一个shader是一个在材质间共享的程序。Shader本身不保存uniforms和顶点数据,只保存uniforms location。 + /// 一个shader是一个在材质间共享的程序。Shader本身不保存uniforms和顶点数据,只提供设置uniforms和use着色器的方法。编辑器针对每个 + /// shader,会通过shader代码找到声明的uniforms变量,并暴露给framework的material设置。 /// - class Shader + class Shader : virtual public Object { public: Shader(); ~Shader(); + + /// + /// 从代码编译shader,编译时会先检测是否有上次缓存的uniforms location map + /// + void Load(const std::string& vertexShader, const std::string& fragmentShader); - private: + /// + /// 将当期shader设置为活动 + /// + void Use(); - friend class Material; + /// + /// 将当期shader设置为非活动 + /// + void UnUse(); /// - /// 保存了所有shader的uniforms变量的ID映射。 + /// 一系列设置uniforms变量的方法 /// - static StringMap sUniformsID; + void SetUniformFloat(const std::string& uniform, float value); + void SetUniformTexture(const std::string& uniform, const Texture& texture); + void SetUniformVector2(const std::string& uniform, const Math::Vector2& vec2); + void SetUniformVector3(const std::string& uniform, const Math::Vector3& vec3); + void SetUniformVector4(const std::string& uniform, const Math::Vector4& vec4); /// - /// 获得uniform变量的ID。 + /// 在已经知道uniform location的情况下,设置值。 /// - static int GetUniformID(const String& name); + void SetUniformFloat(uint loc, float value); + void SetUniformTexture(uint loc, const Texture& texture); + void SetUniformVector2(uint loc, const Math::Vector2& vec2); + void SetUniformVector3(uint loc, const Math::Vector3& vec3); + void SetUniformVector4(uint loc, const Math::Vector4& vec4); +/* /// - /// 设置内置变量: - /// vec2 Asura_Time x值为进入当前场景开始的时间,y值为上一帧的时间间隔 - /// vec2 Asura_RenderTargetSize RT的大小,以像素为单位 - /// Texture Asura_MainTexture 主纹理 + /// 注:后来考虑这个功能放在framework里面实现,更方便,而且shader在editor里也会用到。 + /// 清空缓存的unifrom location,用于重新编译shader,更新最新的uniform location /// - void SetBuiltInUniforms(); + void ClearUniformLocation(); +*/ + /// + /// 获得program id + /// + GLint GetNativeHandle(); + + uint GetUniformLocation(const std::string& uniform); + + private: /// - /// 映射uniforms ID到location。 + /// 设置内置变量: + /// vec2 Asura_Time x值为进入当前场景开始的时间,y值为上一帧的时间间隔 + /// vec2 Asura_RenderTargetSize RT的大小,以像素为单位 + /// Texture Asura_MainTexture 主纹理 /// - Map mLocations; + void SetBuiltInUniforms(); - }; + // OpenGL着色器标识符program id + GLuint mPID; + //// Uniform的locations + //StringMap mUniformLocation; - class ShaderManager final : public Manager - { + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + + LUAX_DECL_FACTORY(SimShader); + + LUAX_DECL_METHOD(l_SetBuiltInUniforms); + LUAX_DECL_METHOD(l_SetUniformFloat); + LUAX_DECL_METHOD(l_SetUniformTexture); + LUAX_DECL_METHOD(l_SetUniformVector2); + LUAX_DECL_METHOD(l_SetUniformVector3); + LUAX_DECL_METHOD(l_SetUniformVector4); }; -- cgit v1.1-26-g67d0