Dawn/src/dawnopengl/display/shader/SimpleTexturedShader.hpp
2022-10-24 22:28:53 -07:00

101 lines
3.1 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/Shader.hpp"
#include "scene/components/Components.hpp"
namespace Dawn {
class SimpleTexturedShader : public Shader {
public:
shaderparameter_t paramProjection;
shaderparameter_t paramView;
shaderparameter_t paramModel;
shaderparameter_t paramColor;
shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture;
std::map<shaderparameter_t, enum ShaderParameterType>
getParameters() override {
std::map<shaderparameter_t, enum ShaderParameterType> ps;
ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR;
ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN;
ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE;
return ps;
}
void setDefaultParameters(Material &material) override {
material.colorValues[this->paramColor] = COLOR_WHITE;
}
void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override {
this->setMatrix(this->paramProjection, proj);
this->setMatrix(this->paramView, view);
}
void setMeshParameters(glm::mat4 transform) override {
this->setMatrix(this->paramModel, transform);
}
void bindTexture(
shaderparameter_t param,
Texture *texture
) override {
if(texture == nullptr) {
this->setBoolean(this->paramHasTexture, false);
} else {
this->setBoolean(this->paramHasTexture, true);
this->setTextureSlot(param, 0x00);
texture->bind(0x00);
}
}
void compile() override {
this->compileShader(
// Vertex Shader
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"uniform mat4 u_Proj;\n"
"uniform mat4 u_View;\n"
"uniform mat4 u_Model;\n"
"out vec2 o_TextCoord;\n"
"void main() {\n"
"gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"}",
// Fragment Shader
"#version 330 core\n"
"out vec4 o_Color;\n"
"in vec2 o_TextCoord;\n"
"uniform vec4 u_Color;\n"
"uniform sampler2D u_Text;\n"
"uniform bool u_HasTexture;\n"
"void main() {\n"
"if(u_HasTexture) {\n"
"o_Color = texture(u_Text, o_TextCoord) * u_Color;\n"
"} else {\n"
"o_Color = u_Color;"
"}\n"
"}\n"
);
this->paramProjection = this->getParameterByName("u_Proj");
this->paramView = this->getParameterByName("u_View");
this->paramModel = this->getParameterByName("u_Model");
this->paramColor = this->getParameterByName("u_Color");
this->paramTexture = this->getParameterByName("u_Text");
this->paramHasTexture = this->getParameterByName("u_HasTexture");
this->setBoolean(this->paramHasTexture, false);
}
};
}