summaryrefslogtreecommitdiff
path: root/source/modules/asura-core/graphics/shader.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-07-31 21:35:12 +0800
committerchai <chaifix@163.com>2019-07-31 21:35:12 +0800
commit084623519e95f0ab0cf4bc328b5fa736d679c5bd (patch)
tree9d409dceda50335e9fb881fc5107c9c1c561f988 /source/modules/asura-core/graphics/shader.cpp
parent012a44bd13ab41d056e7d3884a39027b6cea62b5 (diff)
*修改名称空间风格
Diffstat (limited to 'source/modules/asura-core/graphics/shader.cpp')
-rw-r--r--source/modules/asura-core/graphics/shader.cpp464
1 files changed, 231 insertions, 233 deletions
diff --git a/source/modules/asura-core/graphics/shader.cpp b/source/modules/asura-core/graphics/shader.cpp
index 8606e79..eff62f0 100644
--- a/source/modules/asura-core/graphics/shader.cpp
+++ b/source/modules/asura-core/graphics/shader.cpp
@@ -5,280 +5,278 @@
using namespace std;
-namespace AsuraEngine
+namespace_begin(AsuraEngine)
+namespace_begin(Graphics)
+
+// texture unit
+static uint8 texUnit = 0;
+
+Shader::Shader()
+{
+}
+
+Shader::~Shader()
{
- namespace Graphics
+ if(m_VertShader) glDeleteShader(m_VertShader);
+ if(m_FragShader) glDeleteShader(m_FragShader);
+ if(m_Program) glDeleteProgram(m_Program);
+}
+
+void Shader::SetActive(Shader* shader)
+{
+ gfx.SetActiveShader(shader);
+}
+
+Shader* Shader::GetActive()
+{
+ return gfx.GetActiveShader();
+}
+
+bool Shader::Load(const string& vert, const string& frag)
+{
+ string warnning = "";
+
+ if (!m_Program)
{
+ m_Program = glCreateProgram();
+ if (!m_Program)
+ throw Exception("Cannot create OpenGL shader program.");
+ }
- // texture unit
- static uint8 texUnit = 0;
+ if (!CompileVertexShader(vert, warnning))
+ {
+ throw Exception("Compile vertex shader failed:%s", warnning);
+ }
- Shader::Shader()
- {
- }
+ if (!CompileFragementShader(frag, warnning))
+ {
+ throw Exception("Compile fragment shader failed:%s", warnning);
+ }
- Shader::~Shader()
- {
- if(m_VertShader) glDeleteShader(m_VertShader);
- if(m_FragShader) glDeleteShader(m_FragShader);
- if(m_Program) glDeleteProgram(m_Program);
- }
+ glAttachShader(m_Program, m_VertShader);
+ glAttachShader(m_Program, m_FragShader);
- void Shader::SetActive(Shader* shader)
- {
- gfx.SetActiveShader(shader);
- }
+ glLinkProgram(m_Program);
+ GLint success;
+ glGetProgramiv(m_Program, GL_LINK_STATUS, &success);
+ if (success == GL_FALSE)
+ {
+ warnning = GetProgramWarnings();
+ throw Exception("Link shader program failed:\n%s", warnning.c_str());
+ }
- Shader* Shader::GetActive()
- {
- return gfx.GetActiveShader();
- }
+ return true;
+}
- bool Shader::Load(const string& vert, const string& frag)
+bool Shader::CompileVertexShader(const string& vert, string& outError)
+{
+ if (!m_VertShader)
+ {
+ m_VertShader = glCreateShader(GL_VERTEX_SHADER);
+ if (!m_VertShader)
{
- string warnning = "";
-
- if (!m_Program)
- {
- m_Program = glCreateProgram();
- if (!m_Program)
- throw Exception("Cannot create OpenGL shader program.");
- }
-
- if (!CompileVertexShader(vert, warnning))
- {
- throw Exception("Compile vertex shader failed:%s", warnning);
- }
-
- if (!CompileFragementShader(frag, warnning))
- {
- throw Exception("Compile fragment shader failed:%s", warnning);
- }
-
- glAttachShader(m_Program, m_VertShader);
- glAttachShader(m_Program, m_FragShader);
-
- glLinkProgram(m_Program);
- GLint success;
- glGetProgramiv(m_Program, GL_LINK_STATUS, &success);
- if (success == GL_FALSE)
- {
- warnning = GetProgramWarnings();
- throw Exception("Link shader program failed:\n%s", warnning.c_str());
- }
-
- return true;
+ outError = "Cannot create OpenGL Vertex shader.";
+ return false;
}
+ }
- bool Shader::CompileVertexShader(const string& vert, string& outError)
- {
- if (!m_VertShader)
- {
- m_VertShader = glCreateShader(GL_VERTEX_SHADER);
- if (!m_VertShader)
- {
- outError = "Cannot create OpenGL Vertex shader.";
- return false;
- }
- }
-
- const GLchar* source = vert.c_str();
- GLint success;
-
- glShaderSource(m_VertShader, 1, &source, NULL);
- glCompileShader(m_VertShader);
- glGetShaderiv(m_VertShader, GL_COMPILE_STATUS, &success);
- if (success == GL_FALSE)
- {
- outError = GetShaderWarnings(m_VertShader);
- return false;
- }
-
- return true;
- }
+ const GLchar* source = vert.c_str();
+ GLint success;
- bool Shader::CompileFragementShader(const string& frag, string& outError)
- {
- if (!m_FragShader)
- {
- m_FragShader = glCreateShader(GL_FRAGMENT_SHADER);
- if (!m_FragShader)
- {
- outError = "Cannot create OpenGL fragment shader.";
- return false;
- }
- }
-
- const GLchar* source = frag.c_str();
- GLint success;
-
- source = frag.c_str();
- glShaderSource(m_FragShader, 1, &source, NULL);
- glCompileShader(m_FragShader);
- glGetShaderiv(m_FragShader, GL_COMPILE_STATUS, &success);
- if (success == GL_FALSE)
- {
- outError = GetShaderWarnings(m_FragShader);
- return false;
- }
-
- return true;
- }
+ glShaderSource(m_VertShader, 1, &source, NULL);
+ glCompileShader(m_VertShader);
+ glGetShaderiv(m_VertShader, GL_COMPILE_STATUS, &success);
+ if (success == GL_FALSE)
+ {
+ outError = GetShaderWarnings(m_VertShader);
+ return false;
+ }
- void Shader::OnEnable()
- {
- texUnit = 0;
- }
+ return true;
+}
- void Shader::OnDisable()
+bool Shader::CompileFragementShader(const string& frag, string& outError)
+{
+ if (!m_FragShader)
+ {
+ m_FragShader = glCreateShader(GL_FRAGMENT_SHADER);
+ if (!m_FragShader)
{
- texUnit = 0;
+ outError = "Cannot create OpenGL fragment shader.";
+ return false;
}
+ }
- void Shader::OnUsed()
- {
- texUnit = 0;
- }
+ const GLchar* source = frag.c_str();
+ GLint success;
- uint Shader::GetUniformLocation(const std::string& uniform)
- {
- GLint loc = glGetUniformLocation(m_Program, uniform.c_str());
- return loc;
- }
+ source = frag.c_str();
+ glShaderSource(m_FragShader, 1, &source, NULL);
+ glCompileShader(m_FragShader);
+ glGetShaderiv(m_FragShader, GL_COMPILE_STATUS, &success);
+ if (success == GL_FALSE)
+ {
+ outError = GetShaderWarnings(m_FragShader);
+ return false;
+ }
- bool Shader::HasUniform(const std::string& uniform)
- {
- GLint loc = glGetUniformLocation(m_Program, uniform.c_str());
- return loc != -1;
- }
+ return true;
+}
- GLuint Shader::GetGLProgram()
- {
- return m_Program;
- }
+void Shader::OnEnable()
+{
+ texUnit = 0;
+}
- void Shader::SetUniformFloat(uint loc, float value)
- {
- if(gfx.GetActiveShader() == this)
- glUniform1f(loc, value);
- }
+void Shader::OnDisable()
+{
+ texUnit = 0;
+}
- bool Shader::SetUniformTexture(uint loc, const Texture& texture)
- {
- if (gfx.GetActiveShader() != this)
- return false;
-
- gfx.WipeError();
- glActiveTexture(GL_TEXTURE0 + texUnit);
- if (gfx.HasError())
- return false;
- GLint tex = texture.GetGLTexture();
- glBindTexture(GL_TEXTURE_2D, tex);
- if (gfx.HasError())
- return false;
- glUniform1i(loc, texUnit);
- if (gfx.HasError())
- return false;
- ++texUnit;
- }
+void Shader::OnUsed()
+{
+ texUnit = 0;
+}
- void Shader::SetUniformVector2(uint loc, const Math::Vector2f& vec2)
- {
- if (gfx.GetActiveShader() == this)
- glUniform2f(loc, vec2.x, vec2.y);
- }
+uint Shader::GetUniformLocation(const std::string& uniform)
+{
+ GLint loc = glGetUniformLocation(m_Program, uniform.c_str());
+ return loc;
+}
- void Shader::SetUniformVector3(uint loc, const Math::Vector3f& vec3)
- {
- if (gfx.GetActiveShader() == this)
- glUniform3f(loc, vec3.x, vec3.y, vec3.z);
- }
+bool Shader::HasUniform(const std::string& uniform)
+{
+ GLint loc = glGetUniformLocation(m_Program, uniform.c_str());
+ return loc != -1;
+}
- void Shader::SetUniformVector4(uint loc, const Math::Vector4f& vec4)
- {
- if (gfx.GetActiveShader() == this)
- glUniform4f(loc, vec4.x, vec4.y, vec4.z, vec4.w);
- }
+GLuint Shader::GetGLProgram()
+{
+ return m_Program;
+}
- void Shader::SetUniformMatrix44(uint loc, const Math::Matrix44& mat)
- {
- if (gfx.GetActiveShader() == this)
- glUniformMatrix4fv(loc, 1, GL_FALSE, mat.GetElements());
- }
+void Shader::SetUniformFloat(uint loc, float value)
+{
+ if(gfx.GetActiveShader() == this)
+ glUniform1f(loc, value);
+}
- void Shader::SetUniformColor(uint loc, const Color& color)
- {
- if (gfx.GetActiveShader() == this)
- glUniform4f(loc, color.r, color.g, color.b, color.a);
- }
+bool Shader::SetUniformTexture(uint loc, const Texture& texture)
+{
+ if (gfx.GetActiveShader() != this)
+ return false;
+
+ gfx.WipeError();
+ glActiveTexture(GL_TEXTURE0 + texUnit);
+ if (gfx.HasError())
+ return false;
+ GLint tex = texture.GetGLTexture();
+ glBindTexture(GL_TEXTURE_2D, tex);
+ if (gfx.HasError())
+ return false;
+ glUniform1i(loc, texUnit);
+ if (gfx.HasError())
+ return false;
+ ++texUnit;
+}
+
+void Shader::SetUniformVector2(uint loc, const Math::Vector2f& vec2)
+{
+ if (gfx.GetActiveShader() == this)
+ glUniform2f(loc, vec2.x, vec2.y);
+}
- uint Shader::GetGLTextureUnitCount()
- {
- GLint maxTextureUnits;
- glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
- return (uint)maxTextureUnits;
- }
+void Shader::SetUniformVector3(uint loc, const Math::Vector3f& vec3)
+{
+ if (gfx.GetActiveShader() == this)
+ glUniform3f(loc, vec3.x, vec3.y, vec3.z);
+}
- std::string Shader::GetProgramWarnings()
- {
- GLint strsize, nullpos;
- glGetProgramiv(m_Program, GL_INFO_LOG_LENGTH, &strsize);
+void Shader::SetUniformVector4(uint loc, const Math::Vector4f& vec4)
+{
+ if (gfx.GetActiveShader() == this)
+ glUniform4f(loc, vec4.x, vec4.y, vec4.z, vec4.w);
+}
- if (strsize == 0)
- return "";
+void Shader::SetUniformMatrix44(uint loc, const Math::Matrix44& mat)
+{
+ if (gfx.GetActiveShader() == this)
+ glUniformMatrix4fv(loc, 1, GL_FALSE, mat.GetElements());
+}
- char *tempstr = new char[strsize];
+void Shader::SetUniformColor(uint loc, const Color& color)
+{
+ if (gfx.GetActiveShader() == this)
+ glUniform4f(loc, color.r, color.g, color.b, color.a);
+}
- memset(tempstr, '\0', strsize);
- glGetProgramInfoLog(m_Program, strsize, &nullpos, tempstr);
- tempstr[nullpos] = '\0';
+uint Shader::GetGLTextureUnitCount()
+{
+ GLint maxTextureUnits;
+ glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
+ return (uint)maxTextureUnits;
+}
- std::string warnings(tempstr);
- delete[] tempstr;
+std::string Shader::GetProgramWarnings()
+{
+ GLint strsize, nullpos;
+ glGetProgramiv(m_Program, GL_INFO_LOG_LENGTH, &strsize);
- return warnings;
- }
+ if (strsize == 0)
+ return "";
- std::string Shader::GetShaderWarnings(GLuint shader)
- {
- GLint strsize, nullpos;
- glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &strsize);
+ char *tempstr = new char[strsize];
- if (strsize == 0)
- return "";
+ memset(tempstr, '\0', strsize);
+ glGetProgramInfoLog(m_Program, strsize, &nullpos, tempstr);
+ tempstr[nullpos] = '\0';
- char *tempstr = new char[strsize];
+ std::string warnings(tempstr);
+ delete[] tempstr;
- memset(tempstr, '\0', strsize);
- glGetShaderInfoLog(shader, strsize, &nullpos, tempstr);
- tempstr[nullpos] = '\0';
+ return warnings;
+}
- std::string warnings(tempstr);
- delete[] tempstr;
+std::string Shader::GetShaderWarnings(GLuint shader)
+{
+ GLint strsize, nullpos;
+ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &strsize);
- return warnings;
- }
+ if (strsize == 0)
+ return "";
- void Shader::SetAttribute(int loc, VertexBuffer* vbo, uint offseti, uint stridei, bool normalized)
- {
- GLsizei offset = offseti * vbo->GetDataTypeSize();
- GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(loc);
- vbo->Bind();
- glVertexAttribPointer(loc, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
- vbo->UnBind();
- }
+ char *tempstr = new char[strsize];
- int Shader::GetAttributeLocation(const std::string& attribute)
- {
- int loc = glGetAttribLocation(m_Program, attribute.c_str());
- return loc;
- }
+ memset(tempstr, '\0', strsize);
+ glGetShaderInfoLog(shader, strsize, &nullpos, tempstr);
+ tempstr[nullpos] = '\0';
- void Shader::DisableAttribute(int loc)
- {
- glDisableVertexAttribArray(loc);
- }
+ std::string warnings(tempstr);
+ delete[] tempstr;
- }
-} \ No newline at end of file
+ return warnings;
+}
+
+void Shader::SetAttribute(int loc, VertexBuffer* vbo, uint offseti, uint stridei, bool normalized)
+{
+ GLsizei offset = offseti * vbo->GetDataTypeSize();
+ GLsizei stride = stridei * vbo->GetDataTypeSize();
+ glEnableVertexAttribArray(loc);
+ vbo->Bind();
+ glVertexAttribPointer(loc, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
+ vbo->UnBind();
+}
+
+int Shader::GetAttributeLocation(const std::string& attribute)
+{
+ int loc = glGetAttribLocation(m_Program, attribute.c_str());
+ return loc;
+}
+
+void Shader::DisableAttribute(int loc)
+{
+ glDisableVertexAttribArray(loc);
+}
+
+namespace_end
+namespace_end \ No newline at end of file