Shader refactor

This commit is contained in:
Dane Johnson 2021-01-22 15:27:32 -06:00
parent b08552c9fa
commit 1ee5ed3e88
11 changed files with 38 additions and 42 deletions

View File

@ -43,4 +43,5 @@ Material::Material() {
usesTex = false;
alphaScissor = 0.0f;
unshaded = false;
cullBack = true;
}

View File

@ -28,6 +28,7 @@ struct Material {
bool usesTex;
cfloat alphaScissor;
bool unshaded;
bool cullBack;
Material();
};

View File

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

View File

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

View File

@ -4,4 +4,5 @@ class ScreenShader : public Shader {
public:
ScreenShader();
Name GetName() const;
void UpdateTex(Texture texture);
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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