summaryrefslogtreecommitdiff
path: root/source/modules/asura-core/graphics/shader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/modules/asura-core/graphics/shader.cpp')
-rw-r--r--source/modules/asura-core/graphics/shader.cpp99
1 files changed, 55 insertions, 44 deletions
diff --git a/source/modules/asura-core/graphics/shader.cpp b/source/modules/asura-core/graphics/shader.cpp
index c26ddf1..e6779df 100644
--- a/source/modules/asura-core/graphics/shader.cpp
+++ b/source/modules/asura-core/graphics/shader.cpp
@@ -1,6 +1,5 @@
#include <asura-utils/exceptions/exception.h>
-#include "shader_source.h"
#include "shader.h"
using namespace std;
@@ -12,47 +11,49 @@ namespace AsuraEngine
Shader::Shader()
{
- //Fix: Ҫʱ
- //mProgram = glCreateProgram();
- //if (mProgram == 0)
- // throw Exception("Cannot create OpenGL shader program.");
-
- //mVertShader = glCreateShader(GL_VERTEX_SHADER);
- //if (mVertShader == 0)
- //{
- // glDeleteProgram(mProgram);
- // throw Exception("Cannot create OpenGL vertex shader.");
- //}
-
- //mFragShader = glCreateShader(GL_FRAGMENT_SHADER);
- //if (mFragShader == 0)
- //{
- // glDeleteProgram(mProgram);
- // glDeleteShader(mVertShader);
- // throw Exception("Cannot create OpenGL fragment shader.");
- //}
}
Shader::~Shader()
{
- glDeleteShader(mVertShader);
- glDeleteShader(mFragShader);
- glDeleteProgram(mProgram);
+ if(mVertShader != 0)
+ glDeleteShader(mVertShader);
+ if(mFragShader != 0)
+ glDeleteShader(mFragShader);
+ if(mProgram != 0)
+ glDeleteProgram(mProgram);
}
- bool Shader::Update(AEIO::DecodedData* db)
+ bool Shader::Load(const string& vert, const string& frag)
{
- if (!db) return false;
- ShaderSouce* shaderSource = static_cast<ShaderSouce*>(db);
- if (!shaderSource) return false;
-
GLenum err = GL_NO_ERROR;
- const GLchar* source;
GLint success;
string warnning = "";
+ if (mProgram == 0)
+ {
+ mProgram = glCreateProgram();
+ if (mProgram == 0)
+ throw Exception("Cannot create OpenGL shader program.");
+ }
+
+ if (mVertShader == 0)
+ {
+ mVertShader = glCreateShader(GL_VERTEX_SHADER);
+ if (mVertShader == 0)
+ throw Exception("Cannot create OpenGL vertex shader.");
+ }
+
+ if (mFragShader == 0)
+ {
+ mFragShader = glCreateShader(GL_FRAGMENT_SHADER);
+ if(mFragShader == 0)
+ throw Exception("Cannot create OpenGL fragment shader.");
+ }
+
+ const GLchar* source;
+
// Compile vertex shader.
- source = shaderSource->mVert.c_str();
+ source = vert.c_str();
glShaderSource(mVertShader, 1, &source, NULL);
glCompileShader(mVertShader);
glGetShaderiv(mVertShader, GL_COMPILE_STATUS, &success);
@@ -63,7 +64,7 @@ namespace AsuraEngine
}
// Compile fragment shader.
- source = shaderSource->mFrag.c_str();
+ source = frag.c_str();
glShaderSource(mFragShader, 1, &source, NULL);
glCompileShader(mFragShader);
glGetShaderiv(mFragShader, GL_COMPILE_STATUS, &success);
@@ -83,29 +84,27 @@ namespace AsuraEngine
warnning = GetProgramWarnings();
throw Exception("Link shader program failed:\n%s", warnning.c_str());
}
- }
- uint Shader::GetUniformLocation(const std::string& uniform)
- {
- return 0;
+ return true;
}
- GLuint Shader::GetGLProgramHandle()
+ uint Shader::GetUniformLocation(const std::string& uniform)
{
- return mProgram;
+ // This function returns -1 if name does not correspond to an active uniform variable
+ // in program or if name starts with the reserved prefix "gl_".
+ GLint loc = glGetUniformLocation(mProgram, uniform.c_str());
+ return loc;
}
- void Shader::Use()
+ bool Shader::HasUniform(const std::string& uniform)
{
- if (mProgram != 0)
- {
- gl.UseShader(this);
- }
+ GLint loc = glGetUniformLocation(mProgram, uniform.c_str());
+ return loc != -1;
}
- void Shader::Unuse()
+ GLuint Shader::GetGLProgramHandle()
{
- gl.UnuseShader();
+ return mProgram;
}
void Shader::SetUniformFloat(uint loc, float value)
@@ -138,6 +137,18 @@ namespace AsuraEngine
glUniform4f(loc, vec4.x, vec4.y, vec4.z, vec4.w);
}
+ void Shader::SetUniformMatrix44(uint loc, const Math::Matrix44& mat)
+ {
+ if (gl.state.shader == this)
+ glUniformMatrix4fv(loc, 1, GL_FALSE, mat.GetElements());
+ }
+
+ //void Shader::GetUniform()
+ //{
+ // //if(gl.state.shader == this)
+ // // glGetUniformfv()
+ //}
+
uint Shader::GetGLTextureUnitCount()
{
GLint maxTextureUnits = 0;