[ADD] custom rendering system

This commit is contained in:
Menno
2021-05-18 11:20:57 +02:00
parent e46e26c729
commit 0bb4cc5e1d
29 changed files with 8840 additions and 805 deletions

View File

@@ -0,0 +1,129 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "ShaderProgram.h"
namespace shaders
{
ShaderProgram::ShaderProgram(std::string& vertexShader, std::string& fragmentShader)
{
vertexShaderID = loadShader(vertexShader, GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentShader, GL_FRAGMENT_SHADER);
programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
}
void ShaderProgram::init()
{
setAttributes();
glLinkProgram(programID);
glValidateProgram(programID);
getAllUniformLocations();
}
// This method will start the shaders
void ShaderProgram::start() const
{
glUseProgram(programID);
}
// This method will stop the shaders
void ShaderProgram::stop() const
{
glUseProgram(0);
}
// This method will clean up all the shaders
void ShaderProgram::cleanUp() const
{
stop();
glDetachShader(programID, vertexShaderID);
glDetachShader(programID, fragmentShaderID);
glDeleteShader(vertexShaderID);
glDeleteShader(fragmentShaderID);
glDeleteProgram(programID);
}
// This method sets an input variabele into the shaders
void ShaderProgram::setAttribute(const GLuint attribute, const char* variableName) const
{
glBindAttribLocation(programID, attribute, variableName);
}
void ShaderProgram::loadFloat(GLuint location, GLfloat value) const
{
glUniform1f(location, value);
}
void ShaderProgram::loadVector(GLuint location, glm::vec3 vector) const
{
// glUniform3f(location, vector.x, vector.y, vector.z);
glUniform3fv(location, 1, glm::value_ptr(vector));
}
void ShaderProgram::loadMatrix(GLuint location, glm::mat4 matrix) const
{
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
}
GLuint ShaderProgram::getUniformLocation(const GLchar* uniformName) const
{
return glGetUniformLocation(programID, uniformName);
}
// This method loads a shader into openGL
GLuint ShaderProgram::loadShader(const std::string& shaderString, const GLuint type) const
{
const char* shaderText = shaderString.c_str();
const GLuint shaderID = glCreateShader(type);
glShaderSource(shaderID, 1, &shaderText, NULL);
glCompileShader(shaderID);
GLint succes = 0;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &succes);
if (succes == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(shaderID, maxLength, &maxLength, &errorLog[0]);
for (std::vector<GLchar>::const_iterator i = errorLog.begin(); i != errorLog.end(); ++i)
{
std::cout << *i;
}
std::cout << std::endl;
std::cerr << "Could not compile shader" << std::endl;
cleanUp();
std::exit(-1);
}
return shaderID;
}
//// This method reads a shader from a file into a string
//std::string ShaderProgram::readFromFile(const std::string& file) const
//{
// std::string content;
// std::ifstream fileStream(file, std::ios::in);
// if (!fileStream.is_open()) {
// std::cerr << "Could not read file " << file << ". File does not exist." << std::endl;
// std::exit(-1);
// }
// std::string line;
// while (!fileStream.eof()) {
// std::getline(fileStream, line);
// content.append(line + "\n");
// }
// fileStream.close();
// return content;
//}
}

View File

@@ -0,0 +1,51 @@
#pragma once
#include <GL/glew.h>
#include <glm/gtc/matrix_transform.hpp>
#include <string>
/*
This abstract class represents a generic shader program.
*/
namespace shaders
{
class ShaderProgram
{
private:
GLuint programID;
GLuint vertexShaderID;
GLuint fragmentShaderID;
public:
ShaderProgram(std::string& vertexShader, std::string& fragmentShader);
virtual ~ShaderProgram() = default;
// Call this function after making the shaderprogram (sets all the attributes of the shader)
void init();
// Call this function before rendering
void start() const;
// Call this function after rendering
void stop() const;
// Call this function when closing the application
void cleanUp() const;
protected:
// Set the inputs of the vertex shader
virtual void setAttributes() const = 0;
void setAttribute(const GLuint attribute, const char* variableName) const;
// Loads value's (uniform variables) into the shader
void loadFloat(GLuint location, GLfloat value) const;
void loadVector(GLuint location, glm::vec3 vector) const;
void loadMatrix(GLuint location, glm::mat4 matrix) const;
virtual void getAllUniformLocations() = 0;
GLuint getUniformLocation(const GLchar* uniformName) const;
private:
GLuint loadShader(const std::string& shaderString, GLuint type) const;
//std::string readFromFile(const std::string& file) const;
};
}

View File

@@ -0,0 +1,88 @@
#include "StaticShader.h"
#include "../toolbox/Toolbox.h"
namespace shaders
{
static std::string vertexShader = R"(
#version 400 core
// The VertexShader is run for each vertex on the screen.
// Position of the vertex
in vec3 position;
// Coordinates of the texture
in vec2 textureCoords;
// Equal to the textureCoords
out vec2 passTextureCoords;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
void main(void)
{
// Tell OpenGL where to render the vertex
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
// Pass the textureCoords directly to the fragment shader
passTextureCoords = textureCoords;
}
)";
static std::string fragmentShader = R"(
#version 400 core
// The FragmentShader is run for each pixel in a face on the screen.
// Interpolated textureCoordinates of the vertex (relative to the distance to each vertex)
in vec2 passTextureCoords;
// Final color of the pixel
out vec4 outColor;
// The texture of the model
uniform sampler2D textureSampler;
void main(void)
{
outColor = texture(textureSampler, passTextureCoords);
}
)";
StaticShader::StaticShader(): ShaderProgram(vertexShader, fragmentShader)
{
}
void StaticShader::loadModelMatrix(const glm::mat4& matrix) const
{
loadMatrix(location_modelMatrix, matrix);
}
void StaticShader::loadProjectionMatrix(const glm::mat4& projection) const
{
loadMatrix(location_projectionMatrix, projection);
}
void StaticShader::loadViewMatrix(entities::Camera& camera) const
{
const glm::mat4 viewMatrix = toolbox::createViewMatrix(camera);
loadMatrix(location_viewMatrix, viewMatrix);
}
void StaticShader::setAttributes() const
{
setAttribute(0, "position");
setAttribute(1, "textureCoords");
}
void StaticShader::getAllUniformLocations()
{
location_modelMatrix = getUniformLocation("modelMatrix");
location_projectionMatrix = getUniformLocation("projectionMatrix");
location_viewMatrix = getUniformLocation("viewMatrix");
}
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include <string>
#include <glm/gtc/matrix_transform.hpp>
#include "ShaderProgram.h"
#include "../entities/Camera.h"
/*
This class does represents the shaders for the models.
*/
namespace shaders
{
class StaticShader : public ShaderProgram
{
private:
GLuint location_modelMatrix;
GLuint location_projectionMatrix;
GLuint location_viewMatrix;
public:
StaticShader();
void loadModelMatrix(const glm::mat4& matrix) const;
void loadProjectionMatrix(const glm::mat4& projection) const;
void loadViewMatrix(entities::Camera& camera) const;
protected:
void setAttributes() const override;
void getAllUniformLocations() override;
};
}

View File

@@ -0,0 +1,17 @@
#version 400 core
// The FragmentShader is run for each pixel in a face on the screen.
// Interpolated textureCoordinates of the vertex (relative to the distance to each vertex)
in vec2 passTextureCoords;
// Final color of the pixel
out vec4 outColor;
// The texture of the model
uniform sampler2D textureSampler;
void main(void)
{
outColor = texture(textureSampler, passTextureCoords);
}

View File

@@ -0,0 +1,25 @@
#version 400 core
// The VertexShader is run for each vertex on the screen.
// Position of the vertex
in vec3 position;
// Coordinates of the texture
in vec2 textureCoords;
// Equal to the textureCoords
out vec2 passTextureCoords;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
void main(void)
{
// Tell OpenGL where to render the vertex
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
// Pass the textureCoords directly to the fragment shader
passTextureCoords = textureCoords;
}