From 1497dccd63a84b7ee2b229b1ad9c5c02718f2a78 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 19 Mar 2019 23:06:27 +0800 Subject: *rename --- source/libs/asura-lib-core/graphics/shader.h | 125 +++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 source/libs/asura-lib-core/graphics/shader.h (limited to 'source/libs/asura-lib-core/graphics/shader.h') diff --git a/source/libs/asura-lib-core/graphics/shader.h b/source/libs/asura-lib-core/graphics/shader.h new file mode 100644 index 0000000..65f214e --- /dev/null +++ b/source/libs/asura-lib-core/graphics/shader.h @@ -0,0 +1,125 @@ +#ifndef __ASURA_ENGINE_SHADER_H__ +#define __ASURA_ENGINE_SHADER_H__ + +#include +#include + +#include "FileSystem/Reloadable.h" +#include "Scripting/Luax.hpp" +#include "Math/Vector2.hpp" +#include "Math/Vector3.hpp" +#include "Math/Vector4.h" +#include "Math/Matrix44.h" +#include "StringMap.hpp" +#include "scripting/portable.hpp" +#include "Color.h" +#include "Manager.hpp" +#include "Texture.h" +#include "GL.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + /// + /// 一个shader是一个在材质间共享的程序。Shader本身不保存uniforms和顶点数据,只提供设置uniforms和use着色器的方法。编辑 + /// 器针对每个shader,会通过shader代码找到声明的uniforms变量,并暴露给framework的material设置。 + /// + class Shader ASURA_FINAL + : public Scripting::Portable + , public Filesystem::Reloadable + { + public: + + Shader(); + + ~Shader(); + + /// + /// 从代码编译shader,编译时会先检测是否有上次缓存的uniforms location map。使用glAttachShader重新编译生成着色器, + /// 不会重新申请着色器程序。 + /// + bool Load(const std::string& vertexShader, const std::string& fragmentShader); + + /// + /// 将当期shader设置为活动 + /// + void Use(); + + /// + /// 将当期shader设置为非活动 + /// + void Unuse(); + + /// + /// 在已经知道uniform location的情况下,设置值。 + /// + void SetUniformFloat(uint loc, float value); + void SetUniformTexture(uint loc, const Texture& texture); + void SetUniformVector2(uint loc, const Math::Vector2f& vec2); + void SetUniformVector3(uint loc, const Math::Vector3f& vec3); + void SetUniformVector4(uint loc, const Math::Vector4f& vec4); + void SetUniformColor(uint loc, const Color& color); + void SetUniformMatrix44(uint loc, const Math::Matrix44& mat44); + + uint GetUniformLocation(const std::string& uniform); + + bool HasUniform(const std::string& uniform); + + GLuint GetGLProgramHandle(); + + /// + /// 获得texture unit数量,一般为16个 + /// + static uint GetGLTextureUnitCount(); + + private: + + /// + /// 当前活动的shader + /// + static Shader* mCurrentShader; + + /// + /// 设置内置变量: + /// vec2 Asura_Time x值为进入当前场景开始的时间,y值为上一帧的时间间隔 + /// vec2 Asura_RenderTargetSize RT的大小,以像素为单位 + /// Texture Asura_MainTexture 主纹理 + /// + void SetBuiltInUniforms(); + + /// + /// OpenGL shader program handle. + /// + GLuint mProgramHandle; + + //------------------------------------------------------------------------------------------------------------ + + public: + + LUAX_DECL_FACTORY(SimShader); + + LUAX_DECL_METHOD(l_Use); + LUAX_DECL_METHOD(l_Unuse); + LUAX_DECL_METHOD(l_Load); + LUAX_DECL_METHOD(l_HasUniform); + LUAX_DECL_METHOD(l_GetUniformLocation); + 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); + LUAX_DECL_METHOD(l_SetUniformColor); + + private: + + Luax::LuaxMemberRef mCodeLuaRef; + + }; + + } +} + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0