Shader refactor

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

View File

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

View File

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

View File

@ -35,10 +35,7 @@ void SubMesh::SetupSubMesh() {
} }
void SubMesh::Draw(Shader *shader) { void SubMesh::Draw(Shader *shader) {
shader->UpdateColor(material.usesColor, material.color); shader->UpdateMaterial(material);
shader->UpdateTex(material.usesTex, material.tex);
shader->UpdateAlphaScissor(material.alphaScissor);
shader->UpdateUnshaded(material.unshaded);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0);
glBindVertexArray(0); glBindVertexArray(0);

View File

@ -4,4 +4,8 @@
ScreenShader::ScreenShader() : Shader(screen_vert, screen_frag) {} 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";} Name ScreenShader::GetName() const {return "Screen Shader";}

View File

@ -4,4 +4,5 @@ class ScreenShader : public Shader {
public: public:
ScreenShader(); ScreenShader();
Name GetName() const; 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)); glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection));
} }
void Shader::UpdateColor(bool usesColor) { void Shader::UpdateMaterial(Material material) {
glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) usesColor); 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);
void Shader::UpdateColor(bool usesColor, Color color) { glUniform1i(glGetUniformLocation(id, "material.unshaded"), (int) material.unshaded);
glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) usesColor); glUniform1i(glGetUniformLocation(id, "material.cullBack"), (int) material.cullBack);
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::UpdateDirectionalLight(DirectionalLight directionalLight) { void Shader::UpdateDirectionalLight(DirectionalLight directionalLight) {

View File

@ -16,12 +16,8 @@ public:
void UpdateView(Matrix view); void UpdateView(Matrix view);
void UpdateModel(Matrix model); void UpdateModel(Matrix model);
void UpdateProjection(Matrix projection); void UpdateProjection(Matrix projection);
void UpdateColor(bool usesColor);
void UpdateColor(bool usesColor, Color color); void UpdateMaterial(Material material);
void UpdateTex(bool usesTex);
void UpdateTex(bool usesTex, Texture tex);
void UpdateAlphaScissor(cfloat alphaScissor);
void UpdateUnshaded(bool unshaded);
void UpdateDirectionalLight(DirectionalLight directionalLight); void UpdateDirectionalLight(DirectionalLight directionalLight);

View File

@ -131,7 +131,7 @@ int main() {
screen.Disable(); screen.Disable();
// // Render the screen // // Render the screen
screenShader->Use(); screenShader->Use();
screenShader->UpdateTex(true, screen.tex); screenShader->UpdateTex(screen.tex);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
screen.Draw(); screen.Draw();

View File

@ -27,7 +27,7 @@ function init()
camera.transform:Translate(0.0, 0.0, 10.0) camera.transform:Translate(0.0, 0.0, 10.0)
light = couch.DirectionalLight.new() 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.color = couch.Vector3(1.0, 1.0, 1.0)
light.ambient = 0.2 light.ambient = 0.2
light.diffuse = 1.0 light.diffuse = 1.0
@ -60,6 +60,9 @@ function init()
material = scaffold:GetMaterial(0) material = scaffold:GetMaterial(0)
material.alphaScissor = 0.9 material.alphaScissor = 0.9
scaffold:SetMaterial(0, material) scaffold:SetMaterial(0, material)
material = scaffold:GetMaterial(1)
material.cullBack = false
scaffold:SetMaterial(1, material)
material = scaffold:GetMaterial(1) material = scaffold:GetMaterial(1)
material.alphaScissor = 0.1 material.alphaScissor = 0.1

View File

@ -16,22 +16,30 @@ struct Material {
bool usesTex; bool usesTex;
float alphaScissor; float alphaScissor;
bool unshaded; bool unshaded;
bool cullBack;
}; };
uniform Material material; uniform Material material;
void main() { void main() {
FragColor = vec4(0.0); FragColor = vec4(0.0);
if (material.usesColor) { if (material.usesColor) {
FragColor += vec4(material.color, 1.0); FragColor += vec4(material.color, 1.0);
} }
if (material.usesTex) { if (material.usesTex) {
FragColor += texture(material.tex, UV); FragColor += texture(material.tex, UV);
} }
if (FragColor.w < material.alphaScissor) { if (FragColor.w < material.alphaScissor) {
discard; discard;
} }
if (material.cullBack && !gl_FrontFacing) {
discard;
}
if (!material.unshaded) { if (!material.unshaded) {
FragColor *= vec4(AMBIENT + DIFFUSE + SPECULAR, 1.0); FragColor *= vec4(AMBIENT + DIFFUSE + SPECULAR, 1.0);
} }

View File

@ -4,15 +4,8 @@ in vec2 UV;
out vec4 FragColor; out vec4 FragColor;
struct Material { uniform sampler2D tex;
vec3 color;
bool usesColor;
sampler2D tex;
bool usesTex;
};
uniform Material material;
void main() { void main() {
FragColor = texture(material.tex, UV); FragColor = texture(tex, UV);
} }