From e2f6bd720d8e97c2ded0832a2b4dd743fd75f0e2 Mon Sep 17 00:00:00 2001 From: Menno Date: Fri, 21 May 2021 08:43:32 +0200 Subject: [PATCH] [EDIT] renaming the static_shader --- src/entities/Camera.h | 4 + src/main.cpp | 6 +- src/renderEngine/Renderer.cpp | 4 +- src/renderEngine/Renderer.h | 6 +- src/renderEngine/obj_loader.cpp | 216 +++++++++--------- src/renderEngine/obj_loader.h | 8 +- .../{static_shader.cpp => entity_shader.cpp} | 18 +- .../{static_shader.h => entity_shader.h} | 6 +- wk2_fps.vcxproj | 4 +- wk2_fps.vcxproj.filters | 12 +- 10 files changed, 149 insertions(+), 135 deletions(-) rename src/shaders/{static_shader.cpp => entity_shader.cpp} (88%) rename src/shaders/{static_shader.h => entity_shader.h} (87%) diff --git a/src/entities/Camera.h b/src/entities/Camera.h index b85f0f0..25ae994 100644 --- a/src/entities/Camera.h +++ b/src/entities/Camera.h @@ -5,6 +5,10 @@ namespace entities { + /* + * This class represents the viewport of the game. The whole game is seen through this class + */ + class Camera { private: diff --git a/src/main.cpp b/src/main.cpp index 3d7e14a..e2870a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,7 @@ #include "renderEngine/loader.h" #include "renderEngine/obj_loader.h" #include "renderEngine/renderer.h" -#include "shaders/static_shader.h" +#include "shaders/entity_shader.h" #include "toolbox/toolbox.h" #pragma comment(lib, "glfw3.lib") @@ -46,7 +46,7 @@ int main(void) }); - models::RawModel raw_model = LoadObjModel("res/Tree.obj"); + models::RawModel raw_model = render_engine::LoadObjModel("res/Tree.obj"); models::ModelTexture texture = { render_engine::loader::LoadTexture("res/TreeTexture.png") }; texture.shine_damper = 10; texture.reflectivity = 1; @@ -55,7 +55,7 @@ int main(void) entities::Light light(glm::vec3(0, 0, -30), glm::vec3(1, 1, 1)); - shaders::StaticShader shader; + shaders::EntityShader shader; shader.Init(); render_engine::renderer::Init(shader); diff --git a/src/renderEngine/Renderer.cpp b/src/renderEngine/Renderer.cpp index 93dc0a6..3b7bdc0 100644 --- a/src/renderEngine/Renderer.cpp +++ b/src/renderEngine/Renderer.cpp @@ -15,7 +15,7 @@ namespace render_engine /* This function will load the projectionMatrix into the shader */ - void Init(shaders::StaticShader& shader) + void Init(shaders::EntityShader& shader) { // Faces which are not facing the camera are not rendered glEnable(GL_CULL_FACE); @@ -43,7 +43,7 @@ namespace render_engine /* This function will Render a Model on the screen. */ - void Render(entities::Entity& entity, shaders::StaticShader& shader) + void Render(entities::Entity& entity, shaders::EntityShader& shader) { const models::TexturedModel model = entity.GetModel(); const models::RawModel raw_model = model.raw_model; diff --git a/src/renderEngine/Renderer.h b/src/renderEngine/Renderer.h index 17358fb..8d1e144 100644 --- a/src/renderEngine/Renderer.h +++ b/src/renderEngine/Renderer.h @@ -1,7 +1,7 @@ #pragma once #include "../entities/entity.h" -#include "../shaders/static_shader.h" +#include "../shaders/entity_shader.h" namespace render_engine { @@ -10,7 +10,7 @@ namespace render_engine /* Call this function when starting the program */ - void Init(shaders::StaticShader& shader); + void Init(shaders::EntityShader& shader); /* Call this function before rendering. @@ -20,6 +20,6 @@ namespace render_engine /* Call this function when wanting to Render a mesh to the screen. */ - void Render(entities::Entity& entity, shaders::StaticShader& shader); + void Render(entities::Entity& entity, shaders::EntityShader& shader); } } \ No newline at end of file diff --git a/src/renderEngine/obj_loader.cpp b/src/renderEngine/obj_loader.cpp index 5d66301..bfc3699 100644 --- a/src/renderEngine/obj_loader.cpp +++ b/src/renderEngine/obj_loader.cpp @@ -6,124 +6,128 @@ #include "loader.h" #include "obj_loader.h" -static void Split(const std::string& s, char delim, std::vector& elems) +namespace render_engine { - std::stringstream ss; - ss.str(s); - std::string item; - while (getline(ss, item, delim)) { - elems.push_back(item); - } -} - -static std::vector Split(const std::string& s, char delim) -{ - std::vector elems; - Split(s, delim, elems); - return elems; -} - -static void ProcessVertex(const std::vector& vertex_data, - const std::vector& normals, - const std::vector& textures, - std::vector& indices, - std::vector& texture_array, - std::vector& normal_array) -{ - GLuint current_vertex_pointer = std::stoi(vertex_data.at(0)) - 1; - indices.push_back(current_vertex_pointer); - - glm::vec2 current_texture = textures.at(std::stoi(vertex_data.at(1)) - 1); - texture_array[(current_vertex_pointer * 2) % texture_array.size()] = current_texture.x; - texture_array[(current_vertex_pointer * 2 + 1) % texture_array.size()] = 1 - current_texture.y; - - glm::vec3 current_norm = normals.at(std::stoi(vertex_data.at(2)) - 1); - normal_array[current_vertex_pointer * 3] = current_norm.x; - normal_array[current_vertex_pointer * 3 + 1] = current_norm.y; - normal_array[current_vertex_pointer * 3 + 2] = current_norm.z; -} - -models::RawModel LoadObjModel(std::string file_name) -{ - std::ifstream inFile (file_name); - if ( !inFile.is_open() ) + static void Split(const std::string& s, char delim, std::vector& elems) { - throw std::runtime_error ( "Could not open model file " + file_name + ".obj!" ); + std::stringstream ss; + ss.str(s); + std::string item; + while (getline(ss, item, delim)) { + elems.push_back(item); + } } - std::vector vertices; - std::vector normals; - std::vector textures; - std::vector indices; - std::vector vertex_array; - std::vector normal_array; - std::vector texture_array; - std::string line; - - try + + static std::vector Split(const std::string& s, char delim) { - while (std::getline(inFile, line)) + std::vector elems; + Split(s, delim, elems); + return elems; + } + + static void ProcessVertex(const std::vector& vertex_data, + const std::vector& normals, + const std::vector& textures, + std::vector& indices, + std::vector& texture_array, + std::vector& normal_array) + { + GLuint current_vertex_pointer = std::stoi(vertex_data.at(0)) - 1; + indices.push_back(current_vertex_pointer); + + glm::vec2 current_texture = textures.at(std::stoi(vertex_data.at(1)) - 1); + texture_array[(current_vertex_pointer * 2) % texture_array.size()] = current_texture.x; + texture_array[(current_vertex_pointer * 2 + 1) % texture_array.size()] = 1 - current_texture.y; + + glm::vec3 current_norm = normals.at(std::stoi(vertex_data.at(2)) - 1); + normal_array[current_vertex_pointer * 3] = current_norm.x; + normal_array[current_vertex_pointer * 3 + 1] = current_norm.y; + normal_array[current_vertex_pointer * 3 + 2] = current_norm.z; + } + + models::RawModel LoadObjModel(std::string file_name) + { + std::ifstream inFile(file_name); + if (!inFile.is_open()) { - std::vector split_line = Split(line, ' '); - if (split_line.at(0) == "v") + throw std::runtime_error("Could not open model file " + file_name + ".obj!"); + } + std::vector vertices; + std::vector normals; + std::vector textures; + std::vector indices; + std::vector vertex_array; + std::vector normal_array; + std::vector texture_array; + std::string line; + + try + { + while (std::getline(inFile, line)) { - glm::vec3 vertex; - vertex.x = std::stof(split_line.at(1)); - vertex.y = std::stof(split_line.at(2)); - vertex.z = std::stof(split_line.at(3)); - vertices.push_back(vertex); + std::vector split_line = Split(line, ' '); + if (split_line.at(0) == "v") + { + glm::vec3 vertex; + vertex.x = std::stof(split_line.at(1)); + vertex.y = std::stof(split_line.at(2)); + vertex.z = std::stof(split_line.at(3)); + vertices.push_back(vertex); + } + else if (split_line.at(0) == "vt") + { + glm::vec2 texture; + texture.x = std::stof(split_line.at(1)); + texture.y = std::stof(split_line.at(2)); + textures.push_back(texture); + } + else if (split_line.at(0) == "vn") + { + glm::vec3 normal; + normal.x = std::stof(split_line.at(1)); + normal.y = std::stof(split_line.at(2)); + normal.z = std::stof(split_line.at(3)); + normals.push_back(normal); + } + else if (split_line.at(0) == "f") + { + normal_array = std::vector(vertices.size() * 3); + texture_array = std::vector(textures.size() * 2); + break; + } } - else if (split_line.at(0) == "vt") + + while (true) { - glm::vec2 texture; - texture.x = std::stof(split_line.at(1)); - texture.y = std::stof(split_line.at(2)); - textures.push_back(texture); - } - else if (split_line.at(0) == "vn") - { - glm::vec3 normal; - normal.x = std::stof(split_line.at(1)); - normal.y = std::stof(split_line.at(2)); - normal.z = std::stof(split_line.at(3)); - normals.push_back(normal); - } - else if (split_line.at(0) == "f") - { - normal_array = std::vector(vertices.size() * 3); - texture_array = std::vector(textures.size() * 2); - break; + std::vector split = Split(line, ' '); + std::vector vertex1 = Split(split.at(1), '/'); + std::vector vertex2 = Split(split.at(2), '/'); + std::vector vertex3 = Split(split.at(3), '/'); + ProcessVertex(vertex1, normals, textures, indices, texture_array, normal_array); + ProcessVertex(vertex2, normals, textures, indices, texture_array, normal_array); + ProcessVertex(vertex3, normals, textures, indices, texture_array, normal_array); + if (!std::getline(inFile, line)) + { + break; + } } } - - while (true) + catch (const std::exception& e) { - std::vector split = Split(line, ' '); - std::vector vertex1 = Split(split.at(1), '/'); - std::vector vertex2 = Split(split.at(2), '/'); - std::vector vertex3 = Split(split.at(3), '/'); - ProcessVertex(vertex1, normals, textures, indices, texture_array, normal_array); - ProcessVertex(vertex2, normals, textures, indices, texture_array, normal_array); - ProcessVertex(vertex3, normals, textures, indices, texture_array, normal_array); - if (!std::getline(inFile, line)) - { - break; - } + // Always go in here } - } catch (const std::exception& e) - { - // Always go in here - } - inFile.close(); - - vertex_array = std::vector( vertices.size() * 3 ); - int p = 0; - for ( auto& vertex : vertices ) - { - vertex_array[p++] = vertex.x; - vertex_array[p++] = vertex.y; - vertex_array[p++] = vertex.z; - } + inFile.close(); - return render_engine::loader::LoadToVAO( vertex_array, texture_array, normal_array, indices); + vertex_array = std::vector(vertices.size() * 3); + int p = 0; + for (auto& vertex : vertices) + { + vertex_array[p++] = vertex.x; + vertex_array[p++] = vertex.y; + vertex_array[p++] = vertex.z; + } + + return render_engine::loader::LoadToVAO(vertex_array, texture_array, normal_array, indices); + } } \ No newline at end of file diff --git a/src/renderEngine/obj_loader.h b/src/renderEngine/obj_loader.h index c349f75..59a2a7b 100644 --- a/src/renderEngine/obj_loader.h +++ b/src/renderEngine/obj_loader.h @@ -3,4 +3,10 @@ #include #include "../models/model.h" -models::RawModel LoadObjModel(std::string file_name); \ No newline at end of file +namespace render_engine +{ + /* + * This function retrieves an .obj file, loads it into the VBO and returns a RawModel + */ + models::RawModel LoadObjModel(std::string file_name); +} \ No newline at end of file diff --git a/src/shaders/static_shader.cpp b/src/shaders/entity_shader.cpp similarity index 88% rename from src/shaders/static_shader.cpp rename to src/shaders/entity_shader.cpp index 28994c0..cc70573 100644 --- a/src/shaders/static_shader.cpp +++ b/src/shaders/entity_shader.cpp @@ -1,4 +1,4 @@ -#include "static_shader.h" +#include "entity_shader.h" #include "../toolbox/toolbox.h" namespace shaders @@ -90,39 +90,39 @@ namespace shaders )"; - StaticShader::StaticShader(): ShaderProgram(vertex_shader, fragment_shader) + EntityShader::EntityShader(): ShaderProgram(vertex_shader, fragment_shader) { } - void StaticShader::LoadModelMatrix(const glm::mat4& matrix) const + void EntityShader::LoadModelMatrix(const glm::mat4& matrix) const { LoadMatrix(location_model_matrix, matrix); } - void StaticShader::LoadProjectionMatrix(const glm::mat4& projection) const + void EntityShader::LoadProjectionMatrix(const glm::mat4& projection) const { LoadMatrix(location_projection_matrix, projection); } - void StaticShader::LoadViewMatrix(entities::Camera& camera) const + void EntityShader::LoadViewMatrix(entities::Camera& camera) const { const glm::mat4 view_matrix = toolbox::CreateViewMatrix(camera); LoadMatrix(location_view_matrix, view_matrix); } - void StaticShader::LoadLight(entities::Light& light) const + void EntityShader::LoadLight(entities::Light& light) const { LoadVector(location_light_position, light.GetPosition()); LoadVector(location_light_color, light.GetColor()); } - void StaticShader::LoadShineVariables(float shine_damper, float reflectivity) const + void EntityShader::LoadShineVariables(float shine_damper, float reflectivity) const { LoadFloat(location_shine_damper, shine_damper); LoadFloat(location_reflectivity, reflectivity); } - void StaticShader::SetAttributes() const + void EntityShader::SetAttributes() const { // Load the position VBO and textureCoords VBO from the VAO into the shader "in" variables SetAttribute(0, "position"); @@ -130,7 +130,7 @@ namespace shaders SetAttribute(2, "normal"); } - void StaticShader::GetAllUniformLocations() + void EntityShader::GetAllUniformLocations() { // Get the locations from the uniform variables from the shaders location_model_matrix = GetUniformLocation("model_matrix"); diff --git a/src/shaders/static_shader.h b/src/shaders/entity_shader.h similarity index 87% rename from src/shaders/static_shader.h rename to src/shaders/entity_shader.h index 939da59..3f7a3b7 100644 --- a/src/shaders/static_shader.h +++ b/src/shaders/entity_shader.h @@ -6,12 +6,12 @@ #include "../entities/light.h" /* - This class does represents the shaders for the models. + This class handles the shaders for the entities. */ namespace shaders { - class StaticShader : public ShaderProgram + class EntityShader : public ShaderProgram { private: GLuint location_model_matrix; @@ -23,7 +23,7 @@ namespace shaders GLuint location_reflectivity; public: - StaticShader(); + EntityShader(); void LoadModelMatrix(const glm::mat4& matrix) const; void LoadProjectionMatrix(const glm::mat4& projection) const; diff --git a/wk2_fps.vcxproj b/wk2_fps.vcxproj index 4b8cc1e..d284abf 100644 --- a/wk2_fps.vcxproj +++ b/wk2_fps.vcxproj @@ -26,7 +26,7 @@ - + @@ -38,7 +38,7 @@ - + diff --git a/wk2_fps.vcxproj.filters b/wk2_fps.vcxproj.filters index e540659..570d12e 100644 --- a/wk2_fps.vcxproj.filters +++ b/wk2_fps.vcxproj.filters @@ -33,15 +33,15 @@ Source Files - - Source Files - Source Files Source Files + + Source Files + @@ -65,9 +65,6 @@ Header Files - - Header Files - Header Files @@ -77,5 +74,8 @@ Header Files + + Header Files + \ No newline at end of file