[EDIT] code style guide - shaders

This commit is contained in:
Menno
2021-05-18 11:46:19 +02:00
parent 0517f9e897
commit 41d0667390
6 changed files with 118 additions and 143 deletions

View File

@@ -16,7 +16,7 @@
#pragma comment(lib, "glew32s.lib") #pragma comment(lib, "glew32s.lib")
#pragma comment(lib, "opengl32.lib") #pragma comment(lib, "opengl32.lib")
static double updateDelta(); static double UpdateDelta();
static GLFWwindow* window; static GLFWwindow* window;
@@ -26,7 +26,7 @@ int main(void)
#pragma region OPENGL_SETTINGS #pragma region OPENGL_SETTINGS
if (!glfwInit()) if (!glfwInit())
throw "Could not inditialize glwf"; throw "Could not inditialize glwf";
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGT, "Eindopdracht - Menno Bil", NULL, NULL); window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGT, "SDBA", NULL, NULL);
if (!window) if (!window)
{ {
glfwTerminate(); glfwTerminate();
@@ -44,13 +44,13 @@ int main(void)
}); });
models::RawModel rawModel = LoadObjModel("res/Tree.obj"); models::RawModel raw_model = LoadObjModel("res/Tree.obj");
models::ModelTexture texture = { renderEngine::loader::LoadTexture("res/TreeTexture.png") }; models::ModelTexture texture = { renderEngine::loader::LoadTexture("res/TreeTexture.png") };
models::TexturedModel model = { rawModel, texture }; models::TexturedModel model = { raw_model, texture };
entities::Entity entity(model, glm::vec3(0, -5, -20), glm::vec3(0, 0, 0), 1); entities::Entity entity(model, glm::vec3(0, -5, -20), glm::vec3(0, 0, 0), 1);
shaders::StaticShader shader; shaders::StaticShader shader;
shader.init(); shader.Init();
renderEngine::renderer::Init(shader); renderEngine::renderer::Init(shader);
entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)); entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
@@ -59,35 +59,35 @@ int main(void)
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
// Update // Update
const double delta = updateDelta(); const double delta = UpdateDelta();
entity.increaseRotation(glm::vec3(0, 1, 0)); entity.increaseRotation(glm::vec3(0, 1, 0));
camera.move(window); camera.move(window);
// Render // Render
renderEngine::renderer::Prepare(); renderEngine::renderer::Prepare();
shader.start(); shader.Start();
shader.loadViewMatrix(camera); shader.LoadViewMatrix(camera);
renderEngine::renderer::Render(entity, shader); renderEngine::renderer::Render(entity, shader);
// Finish up // Finish up
shader.stop(); shader.Stop();
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
// Clean up // Clean up
shader.cleanUp(); shader.CleanUp();
renderEngine::loader::CleanUp(); renderEngine::loader::CleanUp();
glfwTerminate(); glfwTerminate();
return 0; return 0;
} }
static double updateDelta() static double UpdateDelta()
{ {
double currentTime = glfwGetTime(); double current_time = glfwGetTime();
static double lastFrameTime = currentTime; static double last_frame_time = current_time;
double deltaTime = currentTime - lastFrameTime; double delt_time = current_time - last_frame_time;
lastFrameTime = currentTime; last_frame_time = current_time;
return deltaTime; return delt_time;
} }

View File

@@ -21,9 +21,9 @@ namespace renderEngine
const glm::mat4 projectionMatrix = const glm::mat4 projectionMatrix =
glm::perspective(glm::radians(FOV), (WINDOW_WIDTH / WINDOW_HEIGT), NEAR_PLANE, FAR_PLANE); glm::perspective(glm::radians(FOV), (WINDOW_WIDTH / WINDOW_HEIGT), NEAR_PLANE, FAR_PLANE);
shader.start(); shader.Start();
shader.loadProjectionMatrix(projectionMatrix); shader.LoadProjectionMatrix(projectionMatrix);
shader.stop(); shader.Stop();
} }
/* /*
@@ -53,7 +53,7 @@ namespace renderEngine
// Load the transformation of the model into the shader // Load the transformation of the model into the shader
const glm::mat4 modelMatrix = toolbox::CreateModelMatrix(entity.getPosition(), entity.getRotation(), entity.getScale()); const glm::mat4 modelMatrix = toolbox::CreateModelMatrix(entity.getPosition(), entity.getRotation(), entity.getScale());
shader.loadModelMatrix(modelMatrix); shader.LoadModelMatrix(modelMatrix);
// Draw the model // Draw the model
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);

View File

@@ -9,121 +9,99 @@
namespace shaders namespace shaders
{ {
ShaderProgram::ShaderProgram(std::string& vertexShader, std::string& fragmentShader) ShaderProgram::ShaderProgram(std::string& vertex_shader, std::string& fragment_shader)
{ {
vertexShaderID = loadShader(vertexShader, GL_VERTEX_SHADER); vertex_shader_id = LoadShader(vertex_shader, GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentShader, GL_FRAGMENT_SHADER); fragment_shader_id = LoadShader(fragment_shader, GL_FRAGMENT_SHADER);
programID = glCreateProgram(); program_id = glCreateProgram();
glAttachShader(programID, vertexShaderID); glAttachShader(program_id, vertex_shader_id);
glAttachShader(programID, fragmentShaderID); glAttachShader(program_id, fragment_shader_id);
} }
void ShaderProgram::init() void ShaderProgram::Init()
{ {
setAttributes(); SetAttributes();
glLinkProgram(programID); glLinkProgram(program_id);
glValidateProgram(programID); glValidateProgram(program_id);
getAllUniformLocations(); GetAllUniformLocations();
} }
// This method will start the shaders // This method will Start the shaders
void ShaderProgram::start() const void ShaderProgram::Start() const
{ {
glUseProgram(programID); glUseProgram(program_id);
} }
// This method will stop the shaders // This method will Stop the shaders
void ShaderProgram::stop() const void ShaderProgram::Stop() const
{ {
glUseProgram(0); glUseProgram(0);
} }
// This method will clean up all the shaders // This method will clean up all the shaders
void ShaderProgram::cleanUp() const void ShaderProgram::CleanUp() const
{ {
stop(); Stop();
glDetachShader(programID, vertexShaderID); glDetachShader(program_id, vertex_shader_id);
glDetachShader(programID, fragmentShaderID); glDetachShader(program_id, fragment_shader_id);
glDeleteShader(vertexShaderID); glDeleteShader(vertex_shader_id);
glDeleteShader(fragmentShaderID); glDeleteShader(fragment_shader_id);
glDeleteProgram(programID); glDeleteProgram(program_id);
} }
// This method sets an input variabele into the shaders // This method sets an input variabele into the shaders
void ShaderProgram::setAttribute(const GLuint attribute, const char* variableName) const void ShaderProgram::SetAttribute(const GLuint attribute, const char* variable_name) const
{ {
glBindAttribLocation(programID, attribute, variableName); glBindAttribLocation(program_id, attribute, variable_name);
} }
void ShaderProgram::loadFloat(GLuint location, GLfloat value) const void ShaderProgram::LoadFloat(GLuint location, GLfloat value) const
{ {
glUniform1f(location, value); glUniform1f(location, value);
} }
void ShaderProgram::loadVector(GLuint location, glm::vec3 vector) const void ShaderProgram::LoadVector(GLuint location, glm::vec3 vector) const
{ {
// glUniform3f(location, vector.x, vector.y, vector.z);
glUniform3fv(location, 1, glm::value_ptr(vector)); glUniform3fv(location, 1, glm::value_ptr(vector));
} }
void ShaderProgram::loadMatrix(GLuint location, glm::mat4 matrix) const void ShaderProgram::LoadMatrix(GLuint location, glm::mat4 matrix) const
{ {
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix)); glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
} }
GLuint ShaderProgram::getUniformLocation(const GLchar* uniformName) const GLuint ShaderProgram::GetUniformLocation(const GLchar* uniform_name) const
{ {
return glGetUniformLocation(programID, uniformName); return glGetUniformLocation(program_id, uniform_name);
} }
// This method loads a shader into openGL // This method loads a shader into openGL
GLuint ShaderProgram::loadShader(const std::string& shaderString, const GLuint type) const GLuint ShaderProgram::LoadShader(const std::string& shader_string, const GLuint type) const
{ {
const char* shaderText = shaderString.c_str(); const char* shader_text = shader_string.c_str();
const GLuint shaderID = glCreateShader(type); const GLuint shader_id = glCreateShader(type);
glShaderSource(shaderID, 1, &shaderText, NULL); glShaderSource(shader_id, 1, &shader_text, NULL);
glCompileShader(shaderID); glCompileShader(shader_id);
GLint succes = 0; GLint succes = 0;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &succes); glGetShaderiv(shader_id, GL_COMPILE_STATUS, &succes);
if (succes == GL_FALSE) if (succes == GL_FALSE)
{ {
GLint maxLength = 0; GLint max_length = 0;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &maxLength); glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &max_length);
std::vector<GLchar> errorLog(maxLength); std::vector<GLchar> error_log(max_length);
glGetShaderInfoLog(shaderID, maxLength, &maxLength, &errorLog[0]); glGetShaderInfoLog(shader_id, max_length, &max_length, &error_log[0]);
for (std::vector<GLchar>::const_iterator i = errorLog.begin(); i != errorLog.end(); ++i) for (std::vector<GLchar>::const_iterator i = error_log.begin(); i != error_log.end(); ++i)
{ {
std::cout << *i; std::cout << *i;
} }
std::cout << std::endl; std::cout << std::endl;
std::cerr << "Could not compile shader" << std::endl; std::cerr << "Could not compile shader" << std::endl;
cleanUp(); CleanUp();
std::exit(-1); std::exit(-1);
} }
return shaderID; return shader_id;
} }
//// 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

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

View File

@@ -3,7 +3,7 @@
namespace shaders namespace shaders
{ {
static std::string vertexShader = R"( static std::string vertex_shader = R"(
#version 400 core #version 400 core
// The VertexShader is run for each vertex on the screen. // The VertexShader is run for each vertex on the screen.
@@ -11,78 +11,77 @@ namespace shaders
// Position of the vertex // Position of the vertex
in vec3 position; in vec3 position;
// Coordinates of the texture // Coordinates of the texture
in vec2 textureCoords; in vec2 texture_coords;
// Equal to the textureCoords // Equal to the texture_coords
out vec2 passTextureCoords; out vec2 pass_texture_coords;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 model_matrix;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
void main(void) void main(void)
{ {
// Tell OpenGL where to render the vertex // Tell OpenGL where to render the vertex
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0); gl_Position = projection_matrix * view_matrix * model_matrix * vec4(position, 1.0);
// Pass the textureCoords directly to the fragment shader // Pass the texture_coords directly to the fragment shader
passTextureCoords = textureCoords; pass_texture_coords = texture_coords;
} }
)"; )";
static std::string fragmentShader = R"( static std::string fragment_shader = R"(
#version 400 core #version 400 core
// The FragmentShader is run for each pixel in a face on the screen. // 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) // Interpolated textureCoordinates of the vertex (relative to the distance to each vertex)
in vec2 passTextureCoords; in vec2 pass_texture_coords;
// Final color of the pixel // Final color of the pixel
out vec4 outColor; out vec4 out_color;
// The texture of the model // The texture of the model
uniform sampler2D textureSampler; uniform sampler2D texture_sampler;
void main(void) void main(void)
{ {
outColor = texture(textureSampler, passTextureCoords); out_color = texture(texture_sampler, pass_texture_coords);
} }
)"; )";
StaticShader::StaticShader(): ShaderProgram(vertexShader, fragmentShader) StaticShader::StaticShader(): ShaderProgram(vertex_shader, fragment_shader)
{ {
} }
void StaticShader::loadModelMatrix(const glm::mat4& matrix) const void StaticShader::LoadModelMatrix(const glm::mat4& matrix) const
{ {
loadMatrix(location_modelMatrix, matrix); LoadMatrix(location_model_matrix, matrix);
} }
void StaticShader::loadProjectionMatrix(const glm::mat4& projection) const void StaticShader::LoadProjectionMatrix(const glm::mat4& projection) const
{ {
loadMatrix(location_projectionMatrix, projection); LoadMatrix(location_projection_matrix, projection);
} }
void StaticShader::loadViewMatrix(entities::Camera& camera) const void StaticShader::LoadViewMatrix(entities::Camera& camera) const
{ {
const glm::mat4 viewMatrix = toolbox::CreateViewMatrix(camera); const glm::mat4 view_matrix = toolbox::CreateViewMatrix(camera);
loadMatrix(location_viewMatrix, viewMatrix); LoadMatrix(location_view_matrix, view_matrix);
} }
void StaticShader::setAttributes() const void StaticShader::SetAttributes() const
{ {
setAttribute(0, "position"); SetAttribute(0, "position");
setAttribute(1, "textureCoords"); SetAttribute(1, "texture_coords");
} }
void StaticShader::getAllUniformLocations() void StaticShader::GetAllUniformLocations()
{ {
location_modelMatrix = getUniformLocation("modelMatrix"); location_model_matrix = GetUniformLocation("model_matrix");
location_projectionMatrix = getUniformLocation("projectionMatrix"); location_projection_matrix = GetUniformLocation("projection_matrix");
location_viewMatrix = getUniformLocation("viewMatrix"); location_view_matrix = GetUniformLocation("view_matrix");
} }
} }

View File

@@ -14,19 +14,19 @@ namespace shaders
class StaticShader : public ShaderProgram class StaticShader : public ShaderProgram
{ {
private: private:
GLuint location_modelMatrix; GLuint location_model_matrix;
GLuint location_projectionMatrix; GLuint location_projection_matrix;
GLuint location_viewMatrix; GLuint location_view_matrix;
public: public:
StaticShader(); StaticShader();
void loadModelMatrix(const glm::mat4& matrix) const; void LoadModelMatrix(const glm::mat4& matrix) const;
void loadProjectionMatrix(const glm::mat4& projection) const; void LoadProjectionMatrix(const glm::mat4& projection) const;
void loadViewMatrix(entities::Camera& camera) const; void LoadViewMatrix(entities::Camera& camera) const;
protected: protected:
void setAttributes() const override; void SetAttributes() const override;
void getAllUniformLocations() override; void GetAllUniformLocations() override;
}; };
} }