Skip to content

Commit

Permalink
abstracted shader from material
Browse files Browse the repository at this point in the history
It will be useful when it comes to postprocessing
  • Loading branch information
BarthPaleologue committed Sep 27, 2023
1 parent 65bed1e commit 7cc8fea
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 122 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ add_executable(featherGL ${SRCS}
core/Transformable.h
core/Settings.h
core/Settings.cpp
core/lights/DebugLight.h)
core/lights/DebugLight.h
core/PostProcess.h
core/Shader.h)

file(COPY assets DESTINATION ${CMAKE_BINARY_DIR})

Expand Down
11 changes: 0 additions & 11 deletions assets/shaders/default/fragment.glsl

This file was deleted.

10 changes: 10 additions & 0 deletions assets/shaders/invertPostProcess/fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
out vec4 frag_color;
in vec2 vUV;

uniform sampler2D screenTexture;

void main() {
vec4 screenColor = texture(screenTexture, vUV);
vec3 color = vec3(1.0) - screenColor.rgb;
frag_color = vec4(color, screenColor.a);
}
File renamed without changes.
1 change: 1 addition & 0 deletions core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Engine::Engine(int windowWidth, int windowHeight, const char *name = "Feather Pr

setClearColor(0.1f, 0.1f, 0.1f, 1.0f);

glEnable(GL_FRAMEBUFFER_SRGB);
glDepthFunc(GL_LESS); // Specify the depth test for the z-buffer
glEnable(GL_DEPTH_TEST); // Enable the z-buffer test in the rasterization
glEnable(GL_BLEND);
Expand Down
12 changes: 12 additions & 0 deletions core/PostProcess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by barth on 27/09/23.
//

#ifndef FEATHERGL_POSTPROCESS_H
#define FEATHERGL_POSTPROCESS_H

class PostProcess {

};

#endif //FEATHERGL_POSTPROCESS_H
112 changes: 112 additions & 0 deletions core/Shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// Created by barth on 27/09/23.
//

#ifndef FEATHERGL_SHADER_H
#define FEATHERGL_SHADER_H

#include <string>
#include <glm/fwd.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include "glad/glad.h"
#include "Observable.h"
#include "Texture.h"

class Shader {
public:
explicit Shader(const char *shaderFolder) {
std::string vertexPath;
vertexPath.append(shaderFolder);
vertexPath += "/vertex.glsl";

std::string fragmentPath;
fragmentPath.append(shaderFolder);
fragmentPath += "/fragment.glsl";

loadFileToBuffer(vertexPath.c_str(), _vertexShaderCode);
loadFileToBuffer(fragmentPath.c_str(), _fragmentShaderCode);
}

void compile() {
_program = glCreateProgram();

std::string versionedVertexShaderCode = "#version 400\n" + _vertexShaderCode;
std::string versionedFragmentShaderCode = "#version 400\n" + _fragmentShaderCode;


GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char *vertexShaderCString = versionedVertexShaderCode.c_str();
glShaderSource(vs, 1, &vertexShaderCString, nullptr);
glCompileShader(vs);

GLint success;
GLchar infoLog[512];
glGetShaderiv(vs, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vs, 512, nullptr, infoLog);
std::cout << "ERROR in compiling GL_VERTEX_SHADER\n" << infoLog << std::endl;
}


GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fragmentShaderCString = versionedFragmentShaderCode.c_str();
glShaderSource(fs, 1, &fragmentShaderCString, nullptr);
glCompileShader(fs);

glGetShaderiv(fs, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fs, 512, nullptr, infoLog);
std::cout << "ERROR in compiling GL_FRAGMENT_SHADER\n" << infoLog << std::endl;
}

glAttachShader(_program, fs);
glAttachShader(_program, vs);
glLinkProgram(_program);
}

virtual void bind() {
glUseProgram(_program);
onBindObservable.notifyObservers();
}

virtual void unbind() {}

void setMat4(const char *uniformName, const glm::mat4 *matrix) const {
glUniformMatrix4fv(glGetUniformLocation(_program, uniformName), 1, GL_FALSE, glm::value_ptr(*matrix));
}

void setVec3(const char *uniformName, const glm::vec3 *vector) const {
glUniform3fv(glGetUniformLocation(_program, uniformName), 1, glm::value_ptr(*vector));
}

void setInt(const char *uniformName, int integer) const {
glUniform1i(glGetUniformLocation(_program, uniformName), integer);
}

void setFloat(const char *uniformName, float value) const {
glUniform1f(glGetUniformLocation(_program, uniformName), value);
}

void setBool(const char *uniformName, bool value) const {
glUniform1i(glGetUniformLocation(_program, uniformName), value);
}

void bindTexture(const char *uniformName, Texture *texture, int id) const {
glUniform1i(glGetUniformLocation(_program, uniformName), id);
texture->bind(id);
}

void setDefine(const char *defineName) {
_fragmentShaderCode = "#define " + std::string(defineName) + "\n" + _fragmentShaderCode;
_vertexShaderCode = "#define " + std::string(defineName) + "\n" + _vertexShaderCode;
}

Observable<> onBindObservable{};
private:
GLuint _program{};
std::string _vertexShaderCode{};
std::string _fragmentShaderCode{};
};

#endif //FEATHERGL_SHADER_H
86 changes: 2 additions & 84 deletions core/materials/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,10 @@
#include "Material.h"
#include "glad/glad.h"

Material::Material(const char *shaderFolder) {
std::string vertexPath;
vertexPath.append(shaderFolder);
vertexPath += "/vertex.glsl";

std::string fragmentPath;
fragmentPath.append(shaderFolder);
fragmentPath += "/fragment.glsl";

loadFileToBuffer(vertexPath.c_str(), _vertexShaderCode);
loadFileToBuffer(fragmentPath.c_str(), _fragmentShaderCode);
}

void Material::compile() {
_program = glCreateProgram();

std::string versionedVertexShaderCode = "#version 400\n" + _vertexShaderCode;
std::string versionedFragmentShaderCode = "#version 400\n" + _fragmentShaderCode;


GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char *vertexShaderCString = versionedVertexShaderCode.c_str();
glShaderSource(vs, 1, &vertexShaderCString, nullptr);
glCompileShader(vs);

GLint success;
GLchar infoLog[512];
glGetShaderiv(vs, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vs, 512, nullptr, infoLog);
std::cout << "ERROR in compiling GL_VERTEX_SHADER\n" << infoLog << std::endl;
}


GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fragmentShaderCString = versionedFragmentShaderCode.c_str();
glShaderSource(fs, 1, &fragmentShaderCString, nullptr);
glCompileShader(fs);

glGetShaderiv(fs, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fs, 512, nullptr, infoLog);
std::cout << "ERROR in compiling GL_FRAGMENT_SHADER\n" << infoLog << std::endl;
}

glAttachShader(_program, fs);
glAttachShader(_program, vs);
glLinkProgram(_program);
}
Material::Material(const char *shaderFolder) : Shader(shaderFolder) {}

void Material::bind() {
glUseProgram(_program);
Shader::bind();

if (_isBackFaceCullingEnabled) {
glEnable(GL_CULL_FACE); // Enables face culling (based on the orientation defined by the CW/CCW enumeration).
Expand All @@ -67,38 +19,4 @@ void Material::bind() {
}

glPolygonMode(GL_FRONT_AND_BACK, _isWireframe ? GL_LINE : GL_FILL);

onBindObservable.notifyObservers();
}

void Material::setMat4(const char *uniformName, const glm::mat4 *matrix) const {
glUniformMatrix4fv(glGetUniformLocation(_program, uniformName), 1, GL_FALSE, glm::value_ptr(*matrix));
}

void Material::setVec3(const char *uniformName, const glm::vec3 *vector) const {
glUniform3fv(glGetUniformLocation(_program, uniformName), 1, glm::value_ptr(*vector));
}

void Material::bindTexture(const char *uniformName, Texture *texture, int id) const {
glUniform1i(glGetUniformLocation(_program, uniformName), id);
texture->bind(id);
}

void Material::unbind() {}

void Material::setDefine(const char *defineName) {
_fragmentShaderCode = "#define " + std::string(defineName) + "\n" + _fragmentShaderCode;
_vertexShaderCode = "#define " + std::string(defineName) + "\n" + _vertexShaderCode;
}

void Material::setInt(const char *uniformName, int integer) const {
glUniform1i(glGetUniformLocation(_program, uniformName), integer);
}

void Material::setFloat(const char *uniformName, float value) const {
glUniform1f(glGetUniformLocation(_program, uniformName), value);
}

void Material::setBool(const char *uniformName, bool value) const {
glUniform1i(glGetUniformLocation(_program, uniformName), value);
}
29 changes: 3 additions & 26 deletions core/materials/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,13 @@
#include "glm/ext/matrix_float4x4.hpp"
#include "Texture.h"
#include "Observable.h"
#include "Shader.h"

class Material {
class Material : public Shader {
public:
explicit Material(const char *shaderFolder);

void compile();

virtual void bind();

virtual void unbind();

void setMat4(const char *uniformName, const glm::mat4 *matrix) const;

void setVec3(const char *uniformName, const glm::vec3 *vector) const;

void setInt(const char *uniformName, int integer) const;

void setFloat(const char *uniformName, float value) const;

void setBool(const char *uniformName, bool value) const;

void bindTexture(const char *uniformName, Texture *texture, int id) const;

void setDefine(const char *defineName);
void bind() override;

bool setWireframe(bool enabled) {
_isWireframe = enabled;
Expand All @@ -52,13 +35,7 @@ class Material {
_isBackFaceCullingEnabled = enabled;
}

Observable<> onBindObservable{};

private:
GLuint _program;
std::string _vertexShaderCode;
std::string _fragmentShaderCode;

bool _isWireframe = false;
bool _isBackFaceCullingEnabled = true;
};
Expand Down

0 comments on commit 7cc8fea

Please sign in to comment.