couch/core/Shaders/Shader.cpp

102 lines
4.2 KiB
C++
Raw Permalink Normal View History

2021-01-13 10:42:57 -06:00
#include "Shader.h"
2021-03-04 14:29:48 -06:00
#include <sstream>
Shader::Shader(const char* vertexCode, const char* fragmentCode) {
2021-01-13 10:42:57 -06:00
Id vertex, fragment;
int success;
char infoLog[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vertexCode, NULL);
2021-01-13 10:42:57 -06:00
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cerr << "Vertex shader failed compilation\n" << infoLog << std::endl;
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fragmentCode, NULL);
2021-01-13 10:42:57 -06:00
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cerr << "Fragment shader failed compilation\n" << infoLog << std::endl;
}
id = glCreateProgram();
glAttachShader(id, vertex);
glAttachShader(id, fragment);
glLinkProgram(id);
glGetProgramiv(id, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(id, 512, NULL, infoLog);
std::cerr << "Shader program linking failed\n" << infoLog << std::endl;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}
void Shader::Use() {
glUseProgram(id);
}
2021-01-13 13:17:31 -06:00
void Shader::UpdateView(Matrix view) {
glUniformMatrix4fv(glGetUniformLocation(id, "VIEW"), 1, GL_FALSE, glm::value_ptr(view));
}
void Shader::UpdateModel(Matrix model) {
glUniformMatrix4fv(glGetUniformLocation(id, "MODEL"), 1, GL_FALSE, glm::value_ptr(model));
}
void Shader::UpdateProjection(Matrix projection) {
glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection));
}
2021-01-15 16:15:47 -06:00
void Shader::UpdateNormal(glm::mat3 normal) {
glUniformMatrix3fv(glGetUniformLocation(id, "NORMAL"), 1, GL_FALSE, glm::value_ptr(normal));
}
2021-01-22 15:27:32 -06:00
void Shader::UpdateMaterial(Material material) {
glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) material.usesTex);
if (material.usesTex) {
glBindTexture(GL_TEXTURE_2D, material.tex.id);
}
2021-01-22 18:44:43 -06:00
glUniform3fv(glGetUniformLocation(id, "material.ambient"), 1, (float*) &material.ambient);
glUniform3fv(glGetUniformLocation(id, "material.diffuse"), 1, (float*) &material.diffuse);
glUniform3fv(glGetUniformLocation(id, "material.specular"), 1, (float*) &material.specular);
2021-01-22 18:44:43 -06:00
glUniform1i(glGetUniformLocation(id, "material.shininess"), material.shininess);
2021-01-22 15:27:32 -06:00
glUniform1f(glGetUniformLocation(id, "material.alphaScissor"), material.alphaScissor);
glUniform1i(glGetUniformLocation(id, "material.unshaded"), (int) material.unshaded);
glUniform1i(glGetUniformLocation(id, "material.cullBack"), (int) material.cullBack);
2021-01-21 15:26:39 -06:00
}
void Shader::UpdateDirectionalLight(DirectionalLight directionalLight) {
2021-03-04 10:41:45 -06:00
glUniform3fv(glGetUniformLocation(id, "directionalLight.direction"), 1, glm::value_ptr(directionalLight.GetDirection()));
2021-03-02 11:48:18 -06:00
glUniform3fv(glGetUniformLocation(id, "directionalLight.color"), 1, glm::value_ptr(directionalLight.GetColor()));
2021-01-21 15:26:39 -06:00
2021-03-02 11:48:18 -06:00
glUniform1f(glGetUniformLocation(id, "directionalLight.ambient"), directionalLight.GetAmbient());
glUniform1f(glGetUniformLocation(id, "directionalLight.diffuse"), directionalLight.GetDiffuse());
glUniform1f(glGetUniformLocation(id, "directionalLight.specular"), directionalLight.GetSpecular());
2021-01-21 15:26:39 -06:00
}
2021-03-04 14:29:48 -06:00
void Shader::UpdatePointLights(PointLightList pointLights) {
for (int i = 0; i < pointLights.size() and i < NUM_POINT_LIGHTS; i++) {
glUniform3fv(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "pos").c_str()), 1, glm::value_ptr(pointLights[i]->GetTransform().position));
glUniform3fv(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "color").c_str()), 1, glm::value_ptr(pointLights[i]->GetColor()));
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "radius").c_str()), pointLights[i]->GetRadius());
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "ambient").c_str()), pointLights[i]->GetAmbient());
2021-03-04 14:49:48 -06:00
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "diffuse").c_str()), pointLights[i]->GetDiffuse());
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "specular").c_str()), pointLights[i]->GetSpecular());
2021-03-04 14:29:48 -06:00
}
}
2021-01-15 16:15:47 -06:00
Name Shader::GetName() const {
return "Unnamed Shader";
}