diff options
author | chai <chaifix@163.com> | 2019-04-13 20:15:07 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-04-13 20:15:07 +0800 |
commit | 866e00474be3bfe0e7dac73b720af0b9ebf7109a (patch) | |
tree | 36c44e99352e2d582b4f2f1dbd4e9e672a54f2cf /source/modules/asura-core/graphics/shader.cpp | |
parent | b5b43bac50ad58949e70bcd1a34b1e6c4765fd51 (diff) |
*misc
Diffstat (limited to 'source/modules/asura-core/graphics/shader.cpp')
-rw-r--r-- | source/modules/asura-core/graphics/shader.cpp | 145 |
1 files changed, 89 insertions, 56 deletions
diff --git a/source/modules/asura-core/graphics/shader.cpp b/source/modules/asura-core/graphics/shader.cpp index cf3d4b2..a2e230a 100644 --- a/source/modules/asura-core/graphics/shader.cpp +++ b/source/modules/asura-core/graphics/shader.cpp @@ -17,21 +17,23 @@ namespace AsuraEngine const char* Shader::SemanticsName[] = { // uniforms - "asura_model_matrix", - "asura_view_matrix", - "asura_projection_matrix", - // attributes - "asura_position", - "asura_tangent", - "asura_normal", - "asura_texcoord0", - "asura_texcoord1", - "asura_texcoord2", - "asura_texcoord3", - "asura_color" + "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) { } @@ -116,28 +118,33 @@ namespace AsuraEngine void Shader::OnUse() { _texture_unit = 0; - // Disableöԣsetʱ - glDisableVertexAttribArray(mPosition); - glDisableVertexAttribArray(mTangent); - glDisableVertexAttribArray(mNormal); - glDisableVertexAttribArray(mColor); - glDisableVertexAttribArray(mTexcoord0); - glDisableVertexAttribArray(mTexcoord1); - glDisableVertexAttribArray(mTexcoord2); - glDisableVertexAttribArray(mTexcoord3); } void Shader::OnUnuse() { _texture_unit; } -/* - int Shader::GetAttributeLocation(const std::string& name) + + void Shader::DisableAttribArraies() { - GLint loc = glGetAttribLocation(mProgram, name.c_str()); - return loc; + 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()); @@ -210,6 +217,8 @@ namespace AsuraEngine 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()); } //void Shader::GetUniform() @@ -268,82 +277,106 @@ namespace AsuraEngine 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_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]); + 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]); - mColor = glGetAttribLocation(mProgram, SemanticsName[SEMANTICS_ATTRIBUTE_COLOR]); } - void Shader::SetAttribPosition(int size, GPUBuffer* vbo, GLsizei offset, GLsizei stride , bool normalized) + void Shader::SetAttribPosition(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized) { + GLsizei offset = offseti * vbo->GetDataTypeSize(); + GLsizei stride = stridei * vbo->GetDataTypeSize(); + glEnableVertexAttribArray(mPosition); vbo->Bind(); - glVertexAttribPointer(mPosition, size, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); + glVertexAttribPointer(mPosition, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); vbo->UnBind(); - glEnableVertexAttribArray(mPosition); + mEnabledAttributes |= ATTRIBUTE_POSITION; } - void Shader::SetAttribTangent(int size, GPUBuffer* vbo, GLsizei offset, GLsizei stride , bool normalized) + void Shader::SetAttribTangent(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized) { + GLsizei offset = offseti * vbo->GetDataTypeSize(); + GLsizei stride = stridei * vbo->GetDataTypeSize(); + glEnableVertexAttribArray(mTangent); vbo->Bind(); - glVertexAttribPointer(mTangent, size, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); + glVertexAttribPointer(mTangent, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); vbo->UnBind(); - glEnableVertexAttribArray(mTangent); + mEnabledAttributes |= ATTRIBUTE_TANGENT; } - void Shader::SetAttribNormal(int size, GPUBuffer* vbo, GLsizei offset, GLsizei stride , bool normalized) + 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, size, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); + glVertexAttribPointer(mNormal, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); vbo->UnBind(); - glEnableVertexAttribArray(mNormal); + mEnabledAttributes |= ATTRIBUTE_NORMAL; } - void Shader::SetAttribColor(int size, GPUBuffer* vbo, GLsizei offset, GLsizei stride , bool normalized) + 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, size, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); + glVertexAttribPointer(mColor, 4, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); vbo->UnBind(); - glEnableVertexAttribArray(mColor); + mEnabledAttributes |= ATTRIBUTE_COLOR; } - void Shader::SetAttribTexcoord0(int size, GPUBuffer* vbo, GLsizei offset, GLsizei stride , bool normalized) + 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, size, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); + glVertexAttribPointer(mTexcoord0, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); vbo->UnBind(); - glEnableVertexAttribArray(mTexcoord0); + mEnabledAttributes |= ATTRIBUTE_TEXCOORD0; } - void Shader::SetAttribTexcoord1(int size, GPUBuffer* vbo, GLsizei offset, GLsizei stride , bool normalized) + 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, size, vbo->GetDataType(), normalized, stride, (GLvoid*) offset); + glVertexAttribPointer(mTexcoord1, 2, vbo->GetDataType(), normalized, stride, (GLvoid*) offset); vbo->UnBind(); - glEnableVertexAttribArray(mTexcoord1); + mEnabledAttributes |= ATTRIBUTE_TEXCOORD1; } - void Shader::SetAttribTexcoord2(int size, GPUBuffer* vbo, GLsizei offset, GLsizei stride , bool normalized) + 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, size, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); + glVertexAttribPointer(mTexcoord2, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); vbo->UnBind(); - glEnableVertexAttribArray(mTexcoord2); + mEnabledAttributes |= ATTRIBUTE_TEXCOORD2; } - void Shader::SetAttribTexcoord3(int size, GPUBuffer* vbo, GLsizei offset, GLsizei stride , bool normalized) + void Shader::SetAttribTexcoord3(VertexBuffer* vbo, uint offseti, uint stridei,bool normalized) { + GLsizei offset = offseti * vbo->GetDataTypeSize(); + GLsizei stride = stridei * vbo->GetDataTypeSize(); + glEnableVertexAttribArray(mTexcoord3); vbo->Bind(); - glVertexAttribPointer(mTexcoord3, size, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); + glVertexAttribPointer(mTexcoord3, 2, vbo->GetDataType(), normalized, stride, (GLvoid*)offset); vbo->UnBind(); - glEnableVertexAttribArray(mTexcoord3); + mEnabledAttributes |= ATTRIBUTE_TEXCOORD3; } } |