summaryrefslogtreecommitdiff
path: root/source/modules/asura-core/graphics/shader.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-05-02 21:42:09 +0800
committerchai <chaifix@163.com>2019-05-02 21:42:09 +0800
commit59a0e32991b5b714b6bdba504b6fbacdcd4b907a (patch)
tree763d10da51491ea88416e159651e97c9193673d8 /source/modules/asura-core/graphics/shader.cpp
parent866e00474be3bfe0e7dac73b720af0b9ebf7109a (diff)
*misc
Diffstat (limited to 'source/modules/asura-core/graphics/shader.cpp')
-rw-r--r--source/modules/asura-core/graphics/shader.cpp151
1 files changed, 11 insertions, 140 deletions
diff --git a/source/modules/asura-core/graphics/shader.cpp b/source/modules/asura-core/graphics/shader.cpp
index a2e230a..a0c14b4 100644
--- a/source/modules/asura-core/graphics/shader.cpp
+++ b/source/modules/asura-core/graphics/shader.cpp
@@ -15,25 +15,7 @@ namespace AsuraEngine
///
static int _texture_unit;
- const char* Shader::SemanticsName[] = {
- // uniforms
- "asura_model_matrix", // mat4
- "asura_view_matrix", // mat4
- "asura_projection_matrix", // mat4
- "asura_draw_color", // vec4
- // attributes
- "asura_position", // vec2
- "asura_tangent", // vec2
- "asura_normal", // vec2
- "asura_texcoord0", // vec2
- "asura_texcoord1", // vec2
- "asura_texcoord2", // vec2
- "asura_texcoord3", // vec2
- "asura_color" // vec4
- };
-
Shader::Shader()
- : mEnabledAttributes(0)
{
}
@@ -109,9 +91,6 @@ namespace AsuraEngine
throw Exception("Link shader program failed:\n%s", warnning.c_str());
}
- // linklocations
- UpdateSemanticsLocations();
-
return true;
}
@@ -125,26 +104,6 @@ namespace AsuraEngine
_texture_unit;
}
- void Shader::DisableAttribArraies()
- {
- if(mEnabledAttributes & ATTRIBUTE_POSITION)
- glDisableVertexAttribArray(mPosition);
- if (mEnabledAttributes & ATTRIBUTE_TANGENT)
- glDisableVertexAttribArray(mTangent);
- if (mEnabledAttributes & ATTRIBUTE_NORMAL)
- glDisableVertexAttribArray(mNormal);
- if (mEnabledAttributes & ATTRIBUTE_COLOR)
- glDisableVertexAttribArray(mColor);
- if (mEnabledAttributes & ATTRIBUTE_TEXCOORD0)
- glDisableVertexAttribArray(mTexcoord0);
- if (mEnabledAttributes & ATTRIBUTE_TEXCOORD1)
- glDisableVertexAttribArray(mTexcoord1);
- if (mEnabledAttributes & ATTRIBUTE_TEXCOORD2)
- glDisableVertexAttribArray(mTexcoord2);
- if (mEnabledAttributes & ATTRIBUTE_TEXCOORD3)
- glDisableVertexAttribArray(mTexcoord3);
- }
-
uint Shader::GetUniformLocation(const std::string& uniform)
{
GLint loc = glGetUniformLocation(mProgram, uniform.c_str());
@@ -211,14 +170,10 @@ namespace AsuraEngine
glUniformMatrix4fv(loc, 1, GL_FALSE, mat.GetElements());
}
- void Shader::SetBuiltInUniforms()
+ void Shader::SetUniformColor(uint loc, const Color& color)
{
- // model\view\projection matrix
- SetUniformMatrix44(mMVP[MATRIX_MODE_MODEL], gl.GetMatrix(MATRIX_MODE_MODEL));
- SetUniformMatrix44(mMVP[MATRIX_MODE_VIEW], gl.GetMatrix(MATRIX_MODE_VIEW));
- SetUniformMatrix44(mMVP[MATRIX_MODE_PROJECTION], gl.GetMatrix(MATRIX_MODE_PROJECTION));
- // draw color
- SetUniformVector4(mDrawColor, gl.GetDrawColor());
+ if (gl.state.shader == this)
+ glUniform4f(loc, color.r, color.g, color.b, color.a);
}
//void Shader::GetUniform()
@@ -274,109 +229,25 @@ namespace AsuraEngine
return warnings;
}
- void Shader::UpdateSemanticsLocations()
- {
- // mvplocation
- mMVP[MATRIX_MODE_MODEL] = glGetUniformLocation(mProgram, SemanticsName[SEMANTICS_UNIFORM_MODEL_MATRIX]);
- mMVP[MATRIX_MODE_VIEW] = glGetUniformLocation(mProgram, SemanticsName[SEMANTICS_UNIFORM_VIEW_MATRIX]);
- mMVP[MATRIX_MODE_PROJECTION] = glGetUniformLocation(mProgram, SemanticsName[SEMANTICS_UNIFORM_PROJECTION_MATRIX]);
-
- mPosition = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_POSITION]);
- mTangent = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_TANGENT]);
- mNormal = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_NORMAL]);
- mColor = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_COLOR]);
- mTexcoord0 = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_TEXCOORD0]);
- mTexcoord1 = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_TEXCOORD1]);
- mTexcoord2 = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_TEXCOORD2]);
- mTexcoord3 = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_TEXCOORD3]);
- }
-
- void Shader::SetAttribPosition(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized)
+ void Shader::SetAttribute(int loc, VertexBuffer* vbo, uint offseti, uint stridei, bool normalized)
{
GLsizei offset = offseti * vbo->GetDataTypeSize();
GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(mPosition);
+ glEnableVertexAttribArray(loc);
vbo->Bind();
- glVertexAttribPointer(mPosition, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
+ glVertexAttribPointer(loc, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
vbo->UnBind();
- mEnabledAttributes |= ATTRIBUTE_POSITION;
}
- void Shader::SetAttribTangent(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized)
+ int Shader::GetAttributeLocation(const std::string& attribute)
{
- GLsizei offset = offseti * vbo->GetDataTypeSize();
- GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(mTangent);
- vbo->Bind();
- glVertexAttribPointer(mTangent, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
- vbo->UnBind();
- mEnabledAttributes |= ATTRIBUTE_TANGENT;
- }
-
- void Shader::SetAttribNormal(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized)
- {
- GLsizei offset = offseti * vbo->GetDataTypeSize();
- GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(mNormal);
- vbo->Bind();
- glVertexAttribPointer(mNormal, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
- vbo->UnBind();
- mEnabledAttributes |= ATTRIBUTE_NORMAL;
- }
-
- void Shader::SetAttribColor(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized)
- {
- GLsizei offset = offseti * vbo->GetDataTypeSize();
- GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(mColor);
- vbo->Bind();
- glVertexAttribPointer(mColor, 4, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
- vbo->UnBind();
- mEnabledAttributes |= ATTRIBUTE_COLOR;
- }
-
- void Shader::SetAttribTexcoord0(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized)
- {
- GLsizei offset = offseti * vbo->GetDataTypeSize();
- GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(mTexcoord0);
- vbo->Bind();
- glVertexAttribPointer(mTexcoord0, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
- vbo->UnBind();
- mEnabledAttributes |= ATTRIBUTE_TEXCOORD0;
- }
-
- void Shader::SetAttribTexcoord1(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized)
- {
- GLsizei offset = offseti * vbo->GetDataTypeSize();
- GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(mTexcoord1);
- vbo->Bind();
- glVertexAttribPointer(mTexcoord1, 2, vbo->GetDataType(), normalized, stride, (GLvoid*) offset);
- vbo->UnBind();
- mEnabledAttributes |= ATTRIBUTE_TEXCOORD1;
- }
-
- void Shader::SetAttribTexcoord2(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized)
- {
- GLsizei offset = offseti * vbo->GetDataTypeSize();
- GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(mTexcoord2);
- vbo->Bind();
- glVertexAttribPointer(mTexcoord2, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
- vbo->UnBind();
- mEnabledAttributes |= ATTRIBUTE_TEXCOORD2;
+ int loc = glGetAttribLocation(mProgram, attribute.c_str());
+ return loc;
}
- void Shader::SetAttribTexcoord3(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized)
+ void Shader::DisableAttribute(int loc)
{
- GLsizei offset = offseti * vbo->GetDataTypeSize();
- GLsizei stride = stridei * vbo->GetDataTypeSize();
- glEnableVertexAttribArray(mTexcoord3);
- vbo->Bind();
- glVertexAttribPointer(mTexcoord3, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset);
- vbo->UnBind();
- mEnabledAttributes |= ATTRIBUTE_TEXCOORD3;
+ glDisableVertexAttribArray(loc);
}
}