From 41d066739061808021c0ffce9f4a39862308183a Mon Sep 17 00:00:00 2001 From: Menno Date: Tue, 18 May 2021 11:46:19 +0200 Subject: [PATCH] [EDIT] code style guide - shaders --- src/main.cpp | 32 +++++----- src/renderEngine/Renderer.cpp | 8 +-- src/shaders/ShaderProgram.cpp | 108 ++++++++++++++-------------------- src/shaders/ShaderProgram.h | 36 ++++++------ src/shaders/StaticShader.cpp | 61 ++++++++++--------- src/shaders/StaticShader.h | 16 ++--- 6 files changed, 118 insertions(+), 143 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4b26afc..7041579 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ #pragma comment(lib, "glew32s.lib") #pragma comment(lib, "opengl32.lib") -static double updateDelta(); +static double UpdateDelta(); static GLFWwindow* window; @@ -26,7 +26,7 @@ int main(void) #pragma region OPENGL_SETTINGS if (!glfwInit()) 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) { 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::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); shaders::StaticShader shader; - shader.init(); + shader.Init(); renderEngine::renderer::Init(shader); entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)); @@ -59,35 +59,35 @@ int main(void) while (!glfwWindowShouldClose(window)) { // Update - const double delta = updateDelta(); + const double delta = UpdateDelta(); entity.increaseRotation(glm::vec3(0, 1, 0)); camera.move(window); // Render renderEngine::renderer::Prepare(); - shader.start(); - shader.loadViewMatrix(camera); + shader.Start(); + shader.LoadViewMatrix(camera); renderEngine::renderer::Render(entity, shader); // Finish up - shader.stop(); + shader.Stop(); glfwSwapBuffers(window); glfwPollEvents(); } // Clean up - shader.cleanUp(); + shader.CleanUp(); renderEngine::loader::CleanUp(); glfwTerminate(); return 0; } -static double updateDelta() +static double UpdateDelta() { - double currentTime = glfwGetTime(); - static double lastFrameTime = currentTime; - double deltaTime = currentTime - lastFrameTime; - lastFrameTime = currentTime; - return deltaTime; + double current_time = glfwGetTime(); + static double last_frame_time = current_time; + double delt_time = current_time - last_frame_time; + last_frame_time = current_time; + return delt_time; } \ No newline at end of file diff --git a/src/renderEngine/Renderer.cpp b/src/renderEngine/Renderer.cpp index 3660234..8ebff7e 100644 --- a/src/renderEngine/Renderer.cpp +++ b/src/renderEngine/Renderer.cpp @@ -21,9 +21,9 @@ namespace renderEngine const glm::mat4 projectionMatrix = glm::perspective(glm::radians(FOV), (WINDOW_WIDTH / WINDOW_HEIGT), NEAR_PLANE, FAR_PLANE); - shader.start(); - shader.loadProjectionMatrix(projectionMatrix); - shader.stop(); + shader.Start(); + shader.LoadProjectionMatrix(projectionMatrix); + shader.Stop(); } /* @@ -53,7 +53,7 @@ namespace renderEngine // Load the transformation of the model into the shader const glm::mat4 modelMatrix = toolbox::CreateModelMatrix(entity.getPosition(), entity.getRotation(), entity.getScale()); - shader.loadModelMatrix(modelMatrix); + shader.LoadModelMatrix(modelMatrix); // Draw the model glActiveTexture(GL_TEXTURE0); diff --git a/src/shaders/ShaderProgram.cpp b/src/shaders/ShaderProgram.cpp index a564079..263e158 100644 --- a/src/shaders/ShaderProgram.cpp +++ b/src/shaders/ShaderProgram.cpp @@ -9,121 +9,99 @@ 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); - fragmentShaderID = loadShader(fragmentShader, GL_FRAGMENT_SHADER); - programID = glCreateProgram(); - glAttachShader(programID, vertexShaderID); - glAttachShader(programID, fragmentShaderID); + vertex_shader_id = LoadShader(vertex_shader, GL_VERTEX_SHADER); + fragment_shader_id = LoadShader(fragment_shader, GL_FRAGMENT_SHADER); + program_id = glCreateProgram(); + glAttachShader(program_id, vertex_shader_id); + glAttachShader(program_id, fragment_shader_id); } - void ShaderProgram::init() + void ShaderProgram::Init() { - setAttributes(); - glLinkProgram(programID); - glValidateProgram(programID); - getAllUniformLocations(); + SetAttributes(); + glLinkProgram(program_id); + glValidateProgram(program_id); + GetAllUniformLocations(); } - // This method will start the shaders - void ShaderProgram::start() const + // This method will Start the shaders + void ShaderProgram::Start() const { - glUseProgram(programID); + glUseProgram(program_id); } - // This method will stop the shaders - void ShaderProgram::stop() const + // This method will Stop the shaders + void ShaderProgram::Stop() const { glUseProgram(0); } // This method will clean up all the shaders - void ShaderProgram::cleanUp() const + void ShaderProgram::CleanUp() const { - stop(); - glDetachShader(programID, vertexShaderID); - glDetachShader(programID, fragmentShaderID); - glDeleteShader(vertexShaderID); - glDeleteShader(fragmentShaderID); - glDeleteProgram(programID); + Stop(); + glDetachShader(program_id, vertex_shader_id); + glDetachShader(program_id, fragment_shader_id); + glDeleteShader(vertex_shader_id); + glDeleteShader(fragment_shader_id); + glDeleteProgram(program_id); } // 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); } - 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)); } - 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)); } - 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 - 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 GLuint shaderID = glCreateShader(type); - glShaderSource(shaderID, 1, &shaderText, NULL); - glCompileShader(shaderID); + const char* shader_text = shader_string.c_str(); + const GLuint shader_id = glCreateShader(type); + glShaderSource(shader_id, 1, &shader_text, NULL); + glCompileShader(shader_id); GLint succes = 0; - glGetShaderiv(shaderID, GL_COMPILE_STATUS, &succes); + glGetShaderiv(shader_id, GL_COMPILE_STATUS, &succes); if (succes == GL_FALSE) { - GLint maxLength = 0; - glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &maxLength); + GLint max_length = 0; + glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &max_length); - std::vector errorLog(maxLength); - glGetShaderInfoLog(shaderID, maxLength, &maxLength, &errorLog[0]); - for (std::vector::const_iterator i = errorLog.begin(); i != errorLog.end(); ++i) + std::vector error_log(max_length); + glGetShaderInfoLog(shader_id, max_length, &max_length, &error_log[0]); + for (std::vector::const_iterator i = error_log.begin(); i != error_log.end(); ++i) { std::cout << *i; } std::cout << std::endl; std::cerr << "Could not compile shader" << std::endl; - cleanUp(); + CleanUp(); 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; - //} } diff --git a/src/shaders/ShaderProgram.h b/src/shaders/ShaderProgram.h index 6d9c08d..b6a04c3 100644 --- a/src/shaders/ShaderProgram.h +++ b/src/shaders/ShaderProgram.h @@ -1,12 +1,11 @@ #pragma once #include -#include #include /* - This abstract class represents a generic shader program. + * This abstract class represents a generic shader program. */ namespace shaders @@ -14,38 +13,37 @@ namespace shaders class ShaderProgram { private: - GLuint programID; - GLuint vertexShaderID; - GLuint fragmentShaderID; + GLuint program_id; + GLuint vertex_shader_id; + GLuint fragment_shader_id; public: - ShaderProgram(std::string& vertexShader, std::string& fragmentShader); + ShaderProgram(std::string& vertex_shader, std::string& fragment_shader); virtual ~ShaderProgram() = default; // Call this function after making the shaderprogram (sets all the attributes of the shader) - void init(); + void Init(); // Call this function before rendering - void start() const; + void Start() const; // Call this function after rendering - void stop() const; + void Stop() const; // Call this function when closing the application - void cleanUp() const; + 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; + virtual void SetAttributes() const = 0; + void SetAttribute(const GLuint attribute, const char* variable_name) 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; + 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; + virtual void GetAllUniformLocations() = 0; + GLuint GetUniformLocation(const GLchar* uniform_name) const; private: - GLuint loadShader(const std::string& shaderString, GLuint type) const; - //std::string readFromFile(const std::string& file) const; + GLuint LoadShader(const std::string& shader_string, GLuint type) const; }; } \ No newline at end of file diff --git a/src/shaders/StaticShader.cpp b/src/shaders/StaticShader.cpp index b04cb71..72b20a6 100644 --- a/src/shaders/StaticShader.cpp +++ b/src/shaders/StaticShader.cpp @@ -3,7 +3,7 @@ namespace shaders { - static std::string vertexShader = R"( + static std::string vertex_shader = R"( #version 400 core // The VertexShader is run for each vertex on the screen. @@ -11,78 +11,77 @@ namespace shaders // Position of the vertex in vec3 position; // Coordinates of the texture - in vec2 textureCoords; + in vec2 texture_coords; - // Equal to the textureCoords - out vec2 passTextureCoords; - - uniform mat4 modelMatrix; - uniform mat4 projectionMatrix; - uniform mat4 viewMatrix; + // Equal to the texture_coords + out vec2 pass_texture_coords; + uniform mat4 model_matrix; + uniform mat4 projection_matrix; + uniform mat4 view_matrix; void main(void) { // 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 - passTextureCoords = textureCoords; + // Pass the texture_coords directly to the fragment shader + pass_texture_coords = texture_coords; } )"; - static std::string fragmentShader = R"( + static std::string fragment_shader = 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; + in vec2 pass_texture_coords; // Final color of the pixel - out vec4 outColor; + out vec4 out_color; // The texture of the model - uniform sampler2D textureSampler; + uniform sampler2D texture_sampler; 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); - loadMatrix(location_viewMatrix, viewMatrix); + const glm::mat4 view_matrix = toolbox::CreateViewMatrix(camera); + LoadMatrix(location_view_matrix, view_matrix); } - void StaticShader::setAttributes() const + void StaticShader::SetAttributes() const { - setAttribute(0, "position"); - setAttribute(1, "textureCoords"); + SetAttribute(0, "position"); + SetAttribute(1, "texture_coords"); } - void StaticShader::getAllUniformLocations() + void StaticShader::GetAllUniformLocations() { - location_modelMatrix = getUniformLocation("modelMatrix"); - location_projectionMatrix = getUniformLocation("projectionMatrix"); - location_viewMatrix = getUniformLocation("viewMatrix"); + location_model_matrix = GetUniformLocation("model_matrix"); + location_projection_matrix = GetUniformLocation("projection_matrix"); + location_view_matrix = GetUniformLocation("view_matrix"); } } diff --git a/src/shaders/StaticShader.h b/src/shaders/StaticShader.h index 159457a..07d8373 100644 --- a/src/shaders/StaticShader.h +++ b/src/shaders/StaticShader.h @@ -14,19 +14,19 @@ namespace shaders class StaticShader : public ShaderProgram { private: - GLuint location_modelMatrix; - GLuint location_projectionMatrix; - GLuint location_viewMatrix; + GLuint location_model_matrix; + GLuint location_projection_matrix; + GLuint location_view_matrix; public: StaticShader(); - void loadModelMatrix(const glm::mat4& matrix) const; - void loadProjectionMatrix(const glm::mat4& projection) const; - void loadViewMatrix(entities::Camera& camera) const; + 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; + void SetAttributes() const override; + void GetAllUniformLocations() override; }; } \ No newline at end of file