diff --git a/Escape-the-Dawn/Engine.h b/Escape-the-Dawn/Engine.h index 968dfed..283aa54 100644 --- a/Escape-the-Dawn/Engine.h +++ b/Escape-the-Dawn/Engine.h @@ -1,17 +1,12 @@ #include #include -#include -#define GLFW_INCLUDE_GLU -#include -#include +#include "Renderer.h" +#include "GameWorld.h" #include "logging.h" #include "glerror.h" -#include "Renderer.h" -#include "GameWorld.h" - class Engine { public: diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index ad8b4ce..6f3a242 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -40,7 +40,7 @@ - $(SolutionDir)\Libraries\openal-soft-1.15.1-bin\lib\Win32\Debug;$(SolutionDir)\Libraries\SOIL\lib\Debug;$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath) + $(BOOST_ROOT)\lib32-msvc-11.0;$(SolutionDir)\Libraries\openal-soft-1.15.1-bin\lib\Win32\Debug;$(SolutionDir)\Libraries\SOIL\lib\Debug;$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath) $(SolutionDir)bin\ $(SolutionDir)obj\$(ProjectName)\ $(ProjectName)-$(Configuration) @@ -50,7 +50,7 @@ $(BOOST_ROOT);$(SolutionDir)\Libraries\openal-soft-1.15.1-bin\include;$(SolutionDir)\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) - $(SolutionDir)\Libraries\openal-soft-1.15.1-bin\lib\Win32\Release;$(SolutionDir)\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath) + $(BOOST_ROOT)\lib32-msvc-11.0;$(SolutionDir)\Libraries\openal-soft-1.15.1-bin\lib\Win32\Release;$(SolutionDir)\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath) $(SolutionDir)bin\ $(SolutionDir)obj\$(ProjectName)\ $(ProjectName)-$(Configuration) diff --git a/Escape-the-Dawn/Model.cpp b/Escape-the-Dawn/Model.cpp index 56b5db8..4ae2126 100644 --- a/Escape-the-Dawn/Model.cpp +++ b/Escape-the-Dawn/Model.cpp @@ -3,12 +3,55 @@ Model::Model(const char* path) { - - Loadobj(path, Vertices, Normals, TextureCoords); CreateBuffers(Vertices, Normals, TextureCoords); } +Model::Model(OBJ &obj) +{ + OBJ::MaterialInfo* currentMaterial = nullptr; + TextureGroup* currentTexGroup = nullptr; + int index = 0; + for (auto face : obj.Faces) { + // New material + if (face.Material != currentMaterial) { + currentMaterial = face.Material; + // Load texture + std::shared_ptr texture = std::make_shared(currentMaterial->TextureFile); + // TODO: Load material parameters + // Create new texture group (start index of new group is upcoming index) + TextureGroup texGroup = { texture, index, index }; + TextureGroups.push_back(texGroup); + currentTexGroup = &TextureGroups.back(); + } + + // Face definitions + for (auto faceDef : face.Definitions) { + glm::vec3 vertex; + std::tie(vertex.x, vertex.y, vertex.z) = obj.Vertices.at(faceDef.VertexIndex - 1); + Vertices.push_back(vertex); + + if (faceDef.NormalIndex != 0) { + glm::vec3 normal; + std::tie(normal.x, normal.y, normal.z) = obj.Normals.at(faceDef.NormalIndex - 1); + Normals.push_back(normal); + } + + if (faceDef.TextureCoordIndex != 0) { + glm::vec2 texCoord; + // TODO: W-coord? + std::tie(texCoord.x, texCoord.y, std::ignore) = obj.TextureCoords.at(faceDef.TextureCoordIndex - 1); + TextureCoords.push_back(texCoord); + } + + currentTexGroup->EndIndex = index; + index++; + } + } + + CreateBuffers(Vertices, Normals, TextureCoords); +} + bool Model::Loadobj(const char* path, std::vector & out_vertices, std::vector &out_normals, std::vector & out_TextureCoords) { std::vector< unsigned int > vertexIndices, TextureCoordIndices, normalIndices; diff --git a/Escape-the-Dawn/Model.h b/Escape-the-Dawn/Model.h index 5ce4c11..cd529fc 100644 --- a/Escape-the-Dawn/Model.h +++ b/Escape-the-Dawn/Model.h @@ -8,7 +8,6 @@ #include #include -#define _CRT_SECURE_NO_WARNINGS #include #define GLFW_INCLUDE_GLU #include @@ -19,26 +18,42 @@ #include #include -#include "glerror.h" #include +#include + +#include "glerror.h" #include "Texture.h" +#include "OBJ.h" class Model { public: + Model(OBJ &obj); + Model(const char* path); + + struct TextureGroup + { + std::shared_ptr Texture; + unsigned int StartIndex; + unsigned int EndIndex; + }; + + GLuint VAO; + std::vector TextureGroups; + + std::vector> texture; + glm::mat4 GetMatrix(); std::vector Vertices; + +private: + std::vector Normals; std::vector TextureCoords; GLuint VertexBuffer; GLuint NormalBuffer; GLuint TextureCoordBuffer; - GLuint VAO; - - std::vector> texture; - - Model(const char* path); bool Loadobj( const char* path, @@ -47,8 +62,6 @@ public: std::vector & out_TextureCoords ); - glm::mat4 GetMatrix(); - void CreateBuffers( std::vector _Vertices, std::vector _Normals, diff --git a/Escape-the-Dawn/OBJ.cpp b/Escape-the-Dawn/OBJ.cpp index fb1ae79..4615466 100644 --- a/Escape-the-Dawn/OBJ.cpp +++ b/Escape-the-Dawn/OBJ.cpp @@ -5,13 +5,18 @@ OBJ::OBJ() m_CurrentMaterial = nullptr; } -void OBJ::LoadFromFile(std::string filename) +bool OBJ::LoadFromFile(std::string filename) { m_Path = boost::filesystem::path(filename); - LOG_INFO("Parsing .obj \"%s\"", m_Path.c_str()); // http://paulbourke.net/dataformats/obj/ std::ifstream file(m_Path.string()); + if (!file.is_open()) { + LOG_ERROR("Failed to open .obj \"%s\"", m_Path.string().c_str()); + return false; + } + + LOG_INFO("Parsing .obj \"%s\"", m_Path.string().c_str()); std::string line; while (std::getline(file, line)) { @@ -74,7 +79,7 @@ void OBJ::LoadFromFile(std::string filename) std::string faceDefString; while (ss >> faceDefString) { std::stringstream ss2(faceDefString); - FaceDefinition faceDef = { -1, -1, -1 }; + FaceDefinition faceDef = { 0, 0, 0 }; ss2 >> faceDef.VertexIndex; ss2.ignore(); // Ignore first delimiter @@ -94,13 +99,21 @@ void OBJ::LoadFromFile(std::string filename) Faces.push_back(face); } } + + return true; } void OBJ::ParseMaterial() { // http://paulbourke.net/dataformats/mtl/ std::ifstream file(m_MaterialPath.string()); - + if (!file.is_open()) { + LOG_ERROR("Failed to open .mtl \"%s\"", m_MaterialPath.string().c_str()); + return; + } + + LOG_INFO("Parsing .mtl \"%s\"", m_MaterialPath.string().c_str()); + std::string currentMaterialName; MaterialInfo* currentMaterial = nullptr; @@ -130,7 +143,7 @@ void OBJ::ParseMaterial() }; ss >> currentMaterialName; - LOG_INFO("Parsing material %s", currentMaterialName); + LOG_INFO("Parsing material %s", currentMaterialName.c_str()); Materials[currentMaterialName] = mat; currentMaterial = &Materials[currentMaterialName]; continue; diff --git a/Escape-the-Dawn/OBJ.h b/Escape-the-Dawn/OBJ.h index a8f241f..aa157fa 100644 --- a/Escape-the-Dawn/OBJ.h +++ b/Escape-the-Dawn/OBJ.h @@ -51,7 +51,7 @@ public: std::vector Faces; std::map Materials; - void LoadFromFile(std::string filename); + bool LoadFromFile(std::string filename); private: boost::filesystem::path m_Path; diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index 3268949..e6f01cd 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -49,7 +49,7 @@ void Renderer::Initialize() LoadContent(); } -void Renderer::Draw(double _dt) +void Renderer::Draw(double dt) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(1.0f, 1.0f, 0.0f, 1.0f); @@ -59,21 +59,27 @@ void Renderer::Draw(double _dt) glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 MVP; - for(int i = 0; i < ModelsToRender.size(); i++) + for (int i = 0; i < ModelsToRender.size(); i++) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, ModelsToRender[i]->model->texture[0]->texture); - MVP = cameraMatrix * ModelsToRender[i]->ModelMatrix; + ModelData* modelData = ModelsToRender.at(i); + auto model = modelData->model; + + MVP = cameraMatrix * modelData->ModelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(ModelsToRender[i]->ModelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelData->ModelMatrix)); glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "lightPosition"), 1, glm::value_ptr(glm::vec3(2.0f, 4.0f, 1.0f))); glUniform1f(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), 1.5f); glUniform1f(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), 0.0f); glUniform1f(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), 0.0f); - glBindVertexArray(ModelsToRender[i]->model->VAO); - glDrawArrays(GL_TRIANGLES, 0, ModelsToRender[i]->model->Vertices.size()); + + glBindVertexArray(model->VAO); + for (auto texGroup : model->TextureGroups) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texGroup.Texture->texture); + glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, (texGroup.EndIndex - texGroup.StartIndex + 1) * abs(sin(glfwGetTime()))); + } } ModelsToRender.clear(); @@ -90,7 +96,7 @@ void Renderer::AddTextToDraw() //Add to draw shit vector } -void Renderer::AddModelToDraw(Model* _model, glm::vec3 _position, glm::quat _orientation) +void Renderer::AddModelToDraw(std::shared_ptr _model, glm::vec3 _position, glm::quat _orientation) { glm::mat4 RotationMatrix = glm::toMat4(_orientation); glm::mat4 ModelMatrix = glm::translate(glm::mat4(), _position) * RotationMatrix ; diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index cfeaa9f..1077e3b 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -7,7 +7,6 @@ #include #include -#define _CRT_SECURE_NO_WARNINGS #include #define GLFW_INCLUDE_GLU #include @@ -36,10 +35,10 @@ public: struct ModelData { - Model* model; + std::shared_ptr model; glm::mat4 ModelMatrix; - ModelData(Model* _model, glm::mat4 _modelMatrix) : model(_model), ModelMatrix(_modelMatrix) - {} + ModelData(std::shared_ptr _model, glm::mat4 _modelMatrix) + : model(_model), ModelMatrix(_modelMatrix) {} }; std::vector ModelsToRender; @@ -50,7 +49,7 @@ public: void Draw(double dt); void DrawText(); - void AddModelToDraw(Model*, glm::vec3, glm::quat); + void AddModelToDraw(std::shared_ptr model, glm::vec3 position, glm::quat orientation); void AddTextToDraw(); void LoadContent(); diff --git a/Escape-the-Dawn/Sounds/hallelujah.wav b/Escape-the-Dawn/Sounds/hallelujah.wav index f103f96..9fc1cb4 100644 Binary files a/Escape-the-Dawn/Sounds/hallelujah.wav and b/Escape-the-Dawn/Sounds/hallelujah.wav differ diff --git a/Escape-the-Dawn/Systems/RenderSystem.cpp b/Escape-the-Dawn/Systems/RenderSystem.cpp index 1db226c..8687ff6 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.cpp +++ b/Escape-the-Dawn/Systems/RenderSystem.cpp @@ -15,16 +15,20 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p auto transformComponent = m_World->GetComponent(entity, "Transform"); if (transformComponent == nullptr) return; + + // Draw models auto modelComponent = m_World->GetComponent(entity, "Model"); if (modelComponent != nullptr) { if (m_CachedModels.find(modelComponent->ModelFile) == m_CachedModels.end()){ - m_CachedModels[modelComponent->ModelFile] = new Model(modelComponent->ModelFile); - } + m_CachedModels[modelComponent->ModelFile] = std::make_shared(OBJ(modelComponent->ModelFile)); + } - Model* model = m_CachedModels[modelComponent->ModelFile]; + auto model = m_CachedModels[modelComponent->ModelFile]; m_Renderer->AddModelToDraw(model, transformComponent->Position, transformComponent->Orientation); } + + // Update camera auto cameraComponent = m_World->GetComponent(entity, "Camera"); if (cameraComponent != nullptr) { @@ -35,8 +39,6 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p m_Renderer->GetCamera()->NearClip(cameraComponent->NearClip); m_Renderer->GetCamera()->FarClip(cameraComponent->FarClip); } - - } diff --git a/Escape-the-Dawn/Systems/RenderSystem.h b/Escape-the-Dawn/Systems/RenderSystem.h index fab25ea..3795e04 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.h +++ b/Escape-the-Dawn/Systems/RenderSystem.h @@ -19,7 +19,7 @@ namespace Systems RenderSystem(World* world, std::shared_ptr renderer) : System(world), m_Renderer(renderer) {} - std::unordered_map m_CachedModels; + std::unordered_map> m_CachedModels; void OnComponentCreated(std::string type, std:: shared_ptr component) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; diff --git a/Escape-the-Dawn/Texture.cpp b/Escape-the-Dawn/Texture.cpp index 8fa587c..de050c7 100644 --- a/Escape-the-Dawn/Texture.cpp +++ b/Escape-the-Dawn/Texture.cpp @@ -1,14 +1,14 @@ #include "Texture.h" -Texture::Texture( const char* path ) +Texture::Texture(std::string path) { Load(path); } -void Texture::Load( const char* path ) +void Texture::Load(std::string path) { - texture = SOIL_load_OGL_texture(path, 0, 0, SOIL_FLAG_INVERT_Y); + texture = SOIL_load_OGL_texture(path.c_str(), 0, 0, SOIL_FLAG_INVERT_Y); } void Texture::Bind() diff --git a/Escape-the-Dawn/Texture.h b/Escape-the-Dawn/Texture.h index bf27ff3..ba77847 100644 --- a/Escape-the-Dawn/Texture.h +++ b/Escape-the-Dawn/Texture.h @@ -1,6 +1,8 @@ #ifndef Texture_h__ #define Texture_h__ +#include + #include #define GLFW_INCLUDE_GLU #include @@ -10,13 +12,13 @@ class Texture { public: - Texture(const char* path); + Texture(std::string path); ~Texture(); GLuint texture; - void Load(const char* path); + void Load(std::string path); void Bind(); }; diff --git a/Escape-the-Dawn/glerror.h b/Escape-the-Dawn/glerror.h index 88f58a7..42432e4 100644 --- a/Escape-the-Dawn/glerror.h +++ b/Escape-the-Dawn/glerror.h @@ -1,8 +1,6 @@ #ifndef _GLERROR_H #define _GLERROR_H -#define GLFW_INCLUDE_GLU -#include #include #include "logging.h"