From 591250b3d5738bb928a8f6ce4aa26839ce0978df Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 22 Jan 2021 15:27:32 -0600 Subject: [PATCH] Shader refactor --- core/Material.cpp | 1 + core/Material.h | 1 + core/Mesh.cpp | 5 +---- core/Shaders/ScreenShader.cpp | 4 ++++ core/Shaders/ScreenShader.h | 1 + core/Shaders/Shader.cpp | 34 +++++++++++++--------------------- core/Shaders/Shader.h | 8 ++------ core/couch.cpp | 2 +- demo/main.lua | 5 ++++- shaders/flat.frag | 8 ++++++++ shaders/screen.frag | 11 ++--------- 11 files changed, 38 insertions(+), 42 deletions(-) diff --git a/core/Material.cpp b/core/Material.cpp index d2b2020..7f26434 100644 --- a/core/Material.cpp +++ b/core/Material.cpp @@ -43,4 +43,5 @@ Material::Material() { usesTex = false; alphaScissor = 0.0f; unshaded = false; + cullBack = true; } diff --git a/core/Material.h b/core/Material.h index 36a3c54..9ffda86 100644 --- a/core/Material.h +++ b/core/Material.h @@ -28,6 +28,7 @@ struct Material { bool usesTex; cfloat alphaScissor; bool unshaded; + bool cullBack; Material(); }; diff --git a/core/Mesh.cpp b/core/Mesh.cpp index 52a6641..d310217 100644 --- a/core/Mesh.cpp +++ b/core/Mesh.cpp @@ -35,10 +35,7 @@ void SubMesh::SetupSubMesh() { } void SubMesh::Draw(Shader *shader) { - shader->UpdateColor(material.usesColor, material.color); - shader->UpdateTex(material.usesTex, material.tex); - shader->UpdateAlphaScissor(material.alphaScissor); - shader->UpdateUnshaded(material.unshaded); + shader->UpdateMaterial(material); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0); glBindVertexArray(0); diff --git a/core/Shaders/ScreenShader.cpp b/core/Shaders/ScreenShader.cpp index 13e7507..41851a9 100644 --- a/core/Shaders/ScreenShader.cpp +++ b/core/Shaders/ScreenShader.cpp @@ -4,4 +4,8 @@ ScreenShader::ScreenShader() : Shader(screen_vert, screen_frag) {} +void ScreenShader::UpdateTex(Texture tex){ + glBindTexture(GL_TEXTURE_2D, tex.id); +} + Name ScreenShader::GetName() const {return "Screen Shader";} diff --git a/core/Shaders/ScreenShader.h b/core/Shaders/ScreenShader.h index 56f634c..f9a5f0c 100644 --- a/core/Shaders/ScreenShader.h +++ b/core/Shaders/ScreenShader.h @@ -4,4 +4,5 @@ class ScreenShader : public Shader { public: ScreenShader(); Name GetName() const; + void UpdateTex(Texture texture); }; diff --git a/core/Shaders/Shader.cpp b/core/Shaders/Shader.cpp index e827dfb..d05a4c9 100644 --- a/core/Shaders/Shader.cpp +++ b/core/Shaders/Shader.cpp @@ -53,27 +53,19 @@ void Shader::UpdateProjection(Matrix projection) { glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection)); } -void Shader::UpdateColor(bool usesColor) { - glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) usesColor); -} - -void Shader::UpdateColor(bool usesColor, Color color) { - glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) usesColor); - glUniform3f(glGetUniformLocation(id, "material.color"), color.r, color.g, color.b); -} - -void Shader::UpdateTex(bool usesTex) { - glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) usesTex); -} -void Shader::UpdateTex(bool usesTex, Texture tex) { - glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) usesTex); - glBindTexture(GL_TEXTURE_2D, tex.id); -} -void Shader::UpdateAlphaScissor(cfloat alphaScissor) { - glUniform1f(glGetUniformLocation(id, "material.alphaScissor"), alphaScissor); -} -void Shader::UpdateUnshaded(bool unshaded) { - glUniform1i(glGetUniformLocation(id, "material.unshaded"), (int) unshaded); +void Shader::UpdateMaterial(Material material) { + glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) material.usesColor); + glUniform3f(glGetUniformLocation(id, "material.color"), + material.color.r, + material.color.g, + material.color.b); + glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) material.usesTex); + if (material.usesTex) { + glBindTexture(GL_TEXTURE_2D, material.tex.id); + } + glUniform1f(glGetUniformLocation(id, "material.alphaScissor"), material.alphaScissor); + glUniform1i(glGetUniformLocation(id, "material.unshaded"), (int) material.unshaded); + glUniform1i(glGetUniformLocation(id, "material.cullBack"), (int) material.cullBack); } void Shader::UpdateDirectionalLight(DirectionalLight directionalLight) { diff --git a/core/Shaders/Shader.h b/core/Shaders/Shader.h index c08d796..0e5e9b8 100644 --- a/core/Shaders/Shader.h +++ b/core/Shaders/Shader.h @@ -16,13 +16,9 @@ public: void UpdateView(Matrix view); void UpdateModel(Matrix model); void UpdateProjection(Matrix projection); - void UpdateColor(bool usesColor); - void UpdateColor(bool usesColor, Color color); - void UpdateTex(bool usesTex); - void UpdateTex(bool usesTex, Texture tex); - void UpdateAlphaScissor(cfloat alphaScissor); - void UpdateUnshaded(bool unshaded); + void UpdateMaterial(Material material); + void UpdateDirectionalLight(DirectionalLight directionalLight); virtual Name GetName() const; diff --git a/core/couch.cpp b/core/couch.cpp index ace6bce..ac5ca5c 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -131,7 +131,7 @@ int main() { screen.Disable(); // // Render the screen screenShader->Use(); - screenShader->UpdateTex(true, screen.tex); + screenShader->UpdateTex(screen.tex); glViewport(0, 0, width, height); screen.Draw(); diff --git a/demo/main.lua b/demo/main.lua index 79df4d9..0a30a7b 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -27,7 +27,7 @@ function init() camera.transform:Translate(0.0, 0.0, 10.0) light = couch.DirectionalLight.new() - light.direction = couch.Vector3(0.0, -1.0, 0.0) + light.direction = couch.Vector3(0.0, -1.0, 1.0) light.color = couch.Vector3(1.0, 1.0, 1.0) light.ambient = 0.2 light.diffuse = 1.0 @@ -60,6 +60,9 @@ function init() material = scaffold:GetMaterial(0) material.alphaScissor = 0.9 scaffold:SetMaterial(0, material) + material = scaffold:GetMaterial(1) + material.cullBack = false + scaffold:SetMaterial(1, material) material = scaffold:GetMaterial(1) material.alphaScissor = 0.1 diff --git a/shaders/flat.frag b/shaders/flat.frag index ef55898..b6399aa 100644 --- a/shaders/flat.frag +++ b/shaders/flat.frag @@ -16,22 +16,30 @@ struct Material { bool usesTex; float alphaScissor; bool unshaded; + bool cullBack; }; uniform Material material; void main() { FragColor = vec4(0.0); + if (material.usesColor) { FragColor += vec4(material.color, 1.0); } + if (material.usesTex) { FragColor += texture(material.tex, UV); } + if (FragColor.w < material.alphaScissor) { discard; } + if (material.cullBack && !gl_FrontFacing) { + discard; + } + if (!material.unshaded) { FragColor *= vec4(AMBIENT + DIFFUSE + SPECULAR, 1.0); } diff --git a/shaders/screen.frag b/shaders/screen.frag index 2194903..f2fa834 100644 --- a/shaders/screen.frag +++ b/shaders/screen.frag @@ -4,15 +4,8 @@ in vec2 UV; out vec4 FragColor; -struct Material { - vec3 color; - bool usesColor; - sampler2D tex; - bool usesTex; -}; - -uniform Material material; +uniform sampler2D tex; void main() { - FragColor = texture(material.tex, UV); + FragColor = texture(tex, UV); }