From 27312892633217fc72c4e58d91f0ac91fed09ca2 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 20 Jan 2021 20:49:12 -0600 Subject: [PATCH] Add alpha scissor --- CMakeLists.txt | 8 ++++++++ core/Material.cpp | 3 +-- core/Material.h | 1 + core/Mesh.cpp | 5 +++++ core/Mesh.h | 1 + core/Shaders/FlatShader.cpp | 2 ++ core/Shaders/FlatShader.h | 1 + core/Shaders/ScreenShader.cpp | 2 ++ core/Shaders/ScreenShader.h | 1 + core/Shaders/Shader.cpp | 3 +++ core/Shaders/Shader.h | 1 + demo/main.lua | 9 +++++++++ shaders/flat.frag | 11 +++++++---- 13 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1afffe..7059121 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,10 +27,18 @@ target_link_libraries(couch ${LUA_LIBRARIES}) include(UseSWIG) set_property(SOURCE scripting/couch.i PROPERTY CPLUSPLUS ON) +if (NOT WIN32) swig_add_library(couchlua TYPE SHARED LANGUAGE lua SOURCES scripting/couch.i scripting/lua/helpers.i) +else() + swig_add_library(couchlua + TYPE STATIC + LANGUAGE lua + SOURCES scripting/couch.i scripting/lua/helpers.i) +endif() + target_link_libraries(couchlua ${LUA_LIBRARIES}) target_link_libraries(couch couchlua) diff --git a/core/Material.cpp b/core/Material.cpp index c74f74f..c49dbbd 100644 --- a/core/Material.cpp +++ b/core/Material.cpp @@ -15,8 +15,6 @@ Color::Color(cfloat r, cfloat g, cfloat b) { Texture::Texture() {} Texture Texture::FromFile(const char *filename) { - // Lotta hocus pocus - // https://learnopengl.com/Getting-started/Textures Texture tex; glGenTextures(1, &tex.id); glBindTexture(GL_TEXTURE_2D, tex.id); @@ -43,4 +41,5 @@ Texture Texture::FromFile(const char *filename) { Material::Material() { usesColor = false; usesTex = false; + alphaScissor = 0.0f; } diff --git a/core/Material.h b/core/Material.h index dfec8ab..4e9a322 100644 --- a/core/Material.h +++ b/core/Material.h @@ -26,6 +26,7 @@ struct Material { bool usesColor; Texture tex; bool usesTex; + cfloat alphaScissor; Material(); }; diff --git a/core/Mesh.cpp b/core/Mesh.cpp index 136d001..e3513d9 100644 --- a/core/Mesh.cpp +++ b/core/Mesh.cpp @@ -35,6 +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); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0); glBindVertexArray(0); @@ -54,6 +55,10 @@ void Mesh::SetupMesh() { } } +Material Mesh::GetMaterial(int submesh) { + return submeshes[submesh]->material; +} + void Mesh::SetMaterial(int submesh, Material material) { submeshes[submesh]->material = material; } diff --git a/core/Mesh.h b/core/Mesh.h index 3e4267f..12facfa 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -38,6 +38,7 @@ class Mesh : public Spatial { public: Mesh(); ~Mesh(); + Material GetMaterial(int submesh); void SetMaterial(int submesh, Material material); static Mesh *FromFile(const char *filename); virtual bool IsDrawable() const {return true;} diff --git a/core/Shaders/FlatShader.cpp b/core/Shaders/FlatShader.cpp index 1030b87..002e888 100644 --- a/core/Shaders/FlatShader.cpp +++ b/core/Shaders/FlatShader.cpp @@ -3,3 +3,5 @@ #include "flat.frag.h" FlatShader::FlatShader() : Shader(flat_vert, flat_frag) {} + +Name FlatShader::GetName() const {return "Flat Shader";} diff --git a/core/Shaders/FlatShader.h b/core/Shaders/FlatShader.h index dd0e90b..2bdcfc5 100644 --- a/core/Shaders/FlatShader.h +++ b/core/Shaders/FlatShader.h @@ -6,6 +6,7 @@ class FlatShader : public Shader { public: FlatShader(); + Name GetName() const; }; #endif /* FLATSHADER_H */ diff --git a/core/Shaders/ScreenShader.cpp b/core/Shaders/ScreenShader.cpp index 5e7116d..13e7507 100644 --- a/core/Shaders/ScreenShader.cpp +++ b/core/Shaders/ScreenShader.cpp @@ -3,3 +3,5 @@ #include "screen.frag.h" ScreenShader::ScreenShader() : Shader(screen_vert, screen_frag) {} + +Name ScreenShader::GetName() const {return "Screen Shader";} diff --git a/core/Shaders/ScreenShader.h b/core/Shaders/ScreenShader.h index a3026ed..56f634c 100644 --- a/core/Shaders/ScreenShader.h +++ b/core/Shaders/ScreenShader.h @@ -3,4 +3,5 @@ class ScreenShader : public Shader { public: ScreenShader(); + Name GetName() const; }; diff --git a/core/Shaders/Shader.cpp b/core/Shaders/Shader.cpp index d6ef08c..0278ea5 100644 --- a/core/Shaders/Shader.cpp +++ b/core/Shaders/Shader.cpp @@ -69,6 +69,9 @@ 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); +} Name Shader::GetName() const { return "Unnamed Shader"; diff --git a/core/Shaders/Shader.h b/core/Shaders/Shader.h index 872cefe..f190066 100644 --- a/core/Shaders/Shader.h +++ b/core/Shaders/Shader.h @@ -19,6 +19,7 @@ public: void UpdateColor(bool usesColor, Color color); void UpdateTex(bool usesTex); void UpdateTex(bool usesTex, Texture tex); + void UpdateAlphaScissor(cfloat alphaScissor); virtual Name GetName() const; }; diff --git a/demo/main.lua b/demo/main.lua index 5a4b8d5..ef8de9b 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -52,6 +52,15 @@ function init() trough.transform.scale = trough.transform.scale * 3.0 scaffold = couch.TexturedMesh("scaffold.glb", "grate_floor_lowres.png", "railing.png") + + material = scaffold:GetMaterial(0) + material.alphaScissor = 0.9 + scaffold:SetMaterial(0, material) + + material = scaffold:GetMaterial(1) + material.alphaScissor = 0.1 + scaffold:SetMaterial(1, material) + couch.Node.GetRoot().children:Append(scaffold) scaffold.transform:Translate(-10.0, 0.0, 0.0) end diff --git a/shaders/flat.frag b/shaders/flat.frag index 29b52cf..f347399 100644 --- a/shaders/flat.frag +++ b/shaders/flat.frag @@ -2,24 +2,27 @@ in vec3 UV; +out vec4 FragColor; + struct Material { vec3 color; bool usesColor; sampler2D tex; bool usesTex; + float alphaScissor; }; uniform Material material; void main() { - gl_FragColor = vec4(0.0); + FragColor = vec4(0.0); if (material.usesColor) { - gl_FragColor += vec4(material.color, 1.0); + FragColor += vec4(material.color, 1.0); } if (material.usesTex) { - gl_FragColor += texture(material.tex, UV.xy / UV.z); + FragColor += texture(material.tex, UV.xy / UV.z); } - if (gl_FragColor.w < 0.99) { + if (FragColor.w < material.alphaScissor) { discard; } }