Material
This commit is contained in:
10
src/dawnopengl/display/shader/CMakeLists.txt
Normal file
10
src/dawnopengl/display/shader/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Shader.cpp
|
||||
)
|
93
src/dawnopengl/display/shader/Shader.cpp
Normal file
93
src/dawnopengl/display/shader/Shader.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Shader.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void Shader::compileShader(
|
||||
std::string vertexShader,
|
||||
std::string fragmentShader
|
||||
) {
|
||||
GLint isSuccess;
|
||||
int32_t maxLength;
|
||||
char error[1024];
|
||||
|
||||
// Load the vertex shader first
|
||||
this->shaderVertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
auto vertShaderC = vertexShader.c_str();
|
||||
glShaderSource(this->shaderVertex, 1, &vertShaderC, 0);
|
||||
glCompileShader(this->shaderVertex);
|
||||
|
||||
// Validate
|
||||
glGetShaderiv(this->shaderVertex, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(this->shaderVertex, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetShaderInfoLog(this->shaderVertex, maxLength, &maxLength, error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now load the Frag shader
|
||||
this->shaderFrag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
auto fragShaderC = fragmentShader.c_str();
|
||||
glShaderSource(this->shaderFrag, 1, &fragShaderC, 0);
|
||||
glCompileShader(this->shaderFrag);
|
||||
glGetShaderiv(this->shaderFrag, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(this->shaderFrag, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetShaderInfoLog(this->shaderFrag, maxLength, &maxLength, error);
|
||||
glDeleteShader(this->shaderVertex);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now create the shader program.
|
||||
this->shaderProgram = glCreateProgram();
|
||||
glAttachShader(this->shaderProgram, this->shaderVertex);
|
||||
glAttachShader(this->shaderProgram, this->shaderFrag);
|
||||
|
||||
//Bind, Verify & Use the shader program
|
||||
glLinkProgram(this->shaderProgram);
|
||||
glGetProgramiv(this->shaderProgram, GL_LINK_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetProgramiv(this->shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetProgramInfoLog(this->shaderProgram, maxLength, &maxLength, error);
|
||||
glDeleteShader(this->shaderVertex);
|
||||
glDeleteShader(this->shaderFrag);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Now parse out the variables.
|
||||
}
|
||||
|
||||
shaderparameter_t Shader::getParameterByName(std::string name) {
|
||||
return glGetUniformLocation(this->shaderProgram, name.c_str());
|
||||
}
|
||||
|
||||
void Shader::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) {
|
||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix));
|
||||
}
|
||||
|
||||
void Shader::setBoolean(shaderparameter_t uni, bool value) {
|
||||
glUniform1i(uni, value);
|
||||
}
|
||||
|
||||
void Shader::setColor(shaderparameter_t uniform, struct Color color) {
|
||||
glUniform4f(uniform, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
|
||||
void Shader::setVector3(shaderparameter_t uniform, glm::vec3 vector) {
|
||||
glUniform3f(uniform, vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
void Shader::bind() {
|
||||
if(this->shaderProgram == -1) throw "Shader has not yet been compiled";
|
||||
glUseProgram(this->shaderProgram);
|
||||
}
|
||||
|
||||
Shader::~Shader() {
|
||||
if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram);
|
||||
if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex);
|
||||
if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag);
|
||||
}
|
49
src/dawnopengl/display/shader/Shader.hpp
Normal file
49
src/dawnopengl/display/shader/Shader.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
// 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 "dawnopengl.hpp"
|
||||
#include "display/Color.hpp"
|
||||
|
||||
typedef GLuint shaderparameter_t;
|
||||
|
||||
namespace Dawn {
|
||||
class Shader : public IShader<shaderparameter_t> {
|
||||
private:
|
||||
/** Pointer to an uploaded vertex shader program */
|
||||
GLuint shaderVertex = -1;
|
||||
|
||||
/** Pointer to an uploaded fragment shader program */
|
||||
GLuint shaderFrag = -1;
|
||||
|
||||
/** Pointer to an uploaded shader program linked */
|
||||
GLuint shaderProgram = -1;
|
||||
|
||||
protected:
|
||||
void compileShader(
|
||||
std::string vertexShader,
|
||||
std::string fragmentShader
|
||||
);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Locate a shader parameter by its name.
|
||||
*
|
||||
* @param name Name of the parameter to get.
|
||||
* @return The shader parameter.
|
||||
*/
|
||||
shaderparameter_t getParameterByName(std::string name);
|
||||
|
||||
void bind() override;
|
||||
virtual void compile() = 0;
|
||||
void setMatrix(shaderparameter_t parameter, glm::mat4 matrix) override;
|
||||
void setBoolean(shaderparameter_t parameter, bool_t value) override;
|
||||
void setColor(shaderparameter_t parameter, struct Color color) override;
|
||||
void setVector3(shaderparameter_t parameter, glm::vec3 vector) override;
|
||||
|
||||
~Shader();
|
||||
};
|
||||
}
|
90
src/dawnopengl/display/shader/SimpleTexturedShader.hpp
Normal file
90
src/dawnopengl/display/shader/SimpleTexturedShader.hpp
Normal file
@ -0,0 +1,90 @@
|
||||
// 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 {
|
||||
private:
|
||||
shaderparameter_t paramProjection;
|
||||
shaderparameter_t paramView;
|
||||
shaderparameter_t paramModel;
|
||||
shaderparameter_t paramColor;
|
||||
shaderparameter_t paramTexture;
|
||||
shaderparameter_t paramHasTexture;
|
||||
|
||||
public:
|
||||
std::map<shaderparameter_t, enum ShaderParameterType>
|
||||
getParameters() override {
|
||||
std::map<shaderparameter_t, enum ShaderParameterType> ps;
|
||||
|
||||
ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR;
|
||||
// ps[paramTexture] SHADER_PARAMETER_TYPE_TEXTURE;
|
||||
// ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN;
|
||||
|
||||
return ps;
|
||||
}
|
||||
|
||||
void setDefaultParameters(Material &material) override {
|
||||
material.colorValues[this->paramColor] = COLOR_MAGENTA;
|
||||
}
|
||||
|
||||
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 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);
|
||||
this->setColor(this->paramColor, COLOR_WHITE);
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user