104 lines
3.1 KiB
C++
104 lines
3.1 KiB
C++
// 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.
|
|
}
|
|
|
|
void Shader::setTextureSlot(shaderparameter_t param, textureslot_t slot) {
|
|
glUniform1i(param, slot);
|
|
}
|
|
|
|
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::setTexture(
|
|
shaderparameter_t param,
|
|
std::shared_ptr<Texture> texture
|
|
) {
|
|
this->bindTexture(param, texture);
|
|
}
|
|
|
|
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);
|
|
} |