Add alpha scissor

This commit is contained in:
Dane Johnson 2021-01-20 20:49:12 -06:00
parent 70d68bd6b8
commit 2731289263
13 changed files with 42 additions and 6 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -26,6 +26,7 @@ struct Material {
bool usesColor;
Texture tex;
bool usesTex;
cfloat alphaScissor;
Material();
};

View File

@ -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;
}

View File

@ -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;}

View File

@ -3,3 +3,5 @@
#include "flat.frag.h"
FlatShader::FlatShader() : Shader(flat_vert, flat_frag) {}
Name FlatShader::GetName() const {return "Flat Shader";}

View File

@ -6,6 +6,7 @@
class FlatShader : public Shader {
public:
FlatShader();
Name GetName() const;
};
#endif /* FLATSHADER_H */

View File

@ -3,3 +3,5 @@
#include "screen.frag.h"
ScreenShader::ScreenShader() : Shader(screen_vert, screen_frag) {}
Name ScreenShader::GetName() const {return "Screen Shader";}

View File

@ -3,4 +3,5 @@
class ScreenShader : public Shader {
public:
ScreenShader();
Name GetName() const;
};

View File

@ -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";

View File

@ -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;
};

View File

@ -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

View File

@ -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;
}
}