diff --git a/.gitmodules b/.gitmodules index f965af7..d94eda4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "lib/glm"] path = lib/glm url = https://github.com/g-truc/glm.git +[submodule "lib/tinyobjloader"] + path = lib/tinyobjloader + url = https://github.com/tinyobjloader/tinyobjloader.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 30758f9..26f5b89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,12 +51,15 @@ add_executable(Dicewars_Siedler src/main.cpp src/engine/layer/entities/Camera.cpp src/engine/layer/entities/Camera.h src/engine/platform/glfw/InputManager.cpp - src/engine/platform/glfw/InputManager.h) + src/engine/platform/glfw/InputManager.h + src/engine/renderer/loader/OBJLoader.cpp + src/engine/renderer/loader/OBJLoader.h) target_include_directories(Dicewars_Siedler PRIVATE lib/glfw/include lib/glm lib/stb_image + lib/tinyobjloader ) target_link_libraries(Dicewars_Siedler diff --git a/assets/stall/stallTexture.png b/assets/stall/stallTexture.png new file mode 100644 index 0000000..d4d6f6e Binary files /dev/null and b/assets/stall/stallTexture.png differ diff --git a/lib/tinyobjloader b/lib/tinyobjloader new file mode 160000 index 0000000..d56555b --- /dev/null +++ b/lib/tinyobjloader @@ -0,0 +1 @@ +Subproject commit d56555b026c1c7cec0f93f3ec7f1de2ff005c5ad diff --git a/src/engine/layer/GameLayer.cpp b/src/engine/layer/GameLayer.cpp index e010361..db137bf 100644 --- a/src/engine/layer/GameLayer.cpp +++ b/src/engine/layer/GameLayer.cpp @@ -6,6 +6,7 @@ #include "../platform/glfw/InputManager.h" #include "../renderer/Renderer.h" +#include "../renderer/loader/OBJLoader.h" #include "../renderer/model/TexturedModel.h" #include "../renderer/textures/ModelTexture.h" @@ -34,10 +35,8 @@ void GameLayer::onAttach() 1,0 //v3 }; - RawModel model = loader.loadToVAO(vertices, textureCoords, indices); - ModelTexture texture = ModelTexture(loader.loadTexture("assets/textures/texture.png")); - texturedModel = TexturedModel(std::make_shared(model), std::make_shared(texture)); - entity = std::make_unique(Entity(std::make_shared(texturedModel), glm::vec3(0,0,-1), 0,0,0, 1.f)); + texturedModel = *OBJLoader::loadModel("assets/stall/stall.obj", "assets/stall/stallTexture.png", loader); + entity = std::make_unique(Entity(std::make_shared(texturedModel), glm::vec3(0,0,-50), 0,0,0, 1.f)); camera = std::make_unique(); } @@ -54,6 +53,7 @@ void GameLayer::onUpdate() if (InputManager::isKeyPressed(GLFW_KEY_A)) moveDir.x -= 1.0f; if (InputManager::isKeyPressed(GLFW_KEY_D)) moveDir.x += 1.0f; + entity->increaseRotation(0,1,0); camera->move(moveDir, 0.02f); renderer.renderFrame(*entity, *camera); } diff --git a/src/engine/renderer/loader/Loader.cpp b/src/engine/renderer/loader/Loader.cpp index 9e984dc..620ff75 100644 --- a/src/engine/renderer/loader/Loader.cpp +++ b/src/engine/renderer/loader/Loader.cpp @@ -34,6 +34,10 @@ void Loader::cleanUp() { } } +ModelTexture Loader::loadTextureFromFile(const std::string &path) { + return ModelTexture(loadTexture(path)); +} + GLuint Loader::loadTexture(const std::string &path) { Texture2D texture = TextureLoader::loadTexture(path); textureIDs.push_back(texture.id); diff --git a/src/engine/renderer/loader/Loader.h b/src/engine/renderer/loader/Loader.h index 460be3e..e58a9a7 100644 --- a/src/engine/renderer/loader/Loader.h +++ b/src/engine/renderer/loader/Loader.h @@ -8,6 +8,7 @@ #include #include "../model/RawModel.h" +#include "../textures/ModelTexture.h" #include "glad/glad.h" @@ -18,9 +19,10 @@ public: ~Loader(); void cleanUp(); - GLuint loadTexture(const std::string &path); + ModelTexture loadTextureFromFile(const std::string& path); private: GLuint createVAO(); + GLuint loadTexture(const std::string &path); void storeDataInAttributeList(int attributeNumber, int coordinateSize, const std::vector &data); void bindIndicesBuffer(const std::vector &indices); void unbindVAO(); diff --git a/src/engine/renderer/loader/OBJLoader.cpp b/src/engine/renderer/loader/OBJLoader.cpp new file mode 100644 index 0000000..5fbbd13 --- /dev/null +++ b/src/engine/renderer/loader/OBJLoader.cpp @@ -0,0 +1,54 @@ +// +// Created by sebastian on 07.02.26. +// + +#include "OBJLoader.h" + +#define TINYOBJLOADER_IMPLEMENTATION +#include "tiny_obj_loader.h" + +std::shared_ptr OBJLoader::loadModel(const std::string &modelPath, const std::string& texturePath, Loader &loader) { + tinyobj::attrib_t attrib; + std::vector shapes; + std::vector materials; + std::string warn, err; + if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, modelPath.c_str())) { + throw std::runtime_error(warn + err); + } + + if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, modelPath.c_str())) { + throw std::runtime_error("Failed to load OBJ: " + warn + err); + } + + std::vector vertices; + std::vector uvs; + std::vector indices; + + int indexOffset = 0; + for (const auto& shape : shapes) { + for (const auto& index : shape.mesh.indices) { + // Vertex-Position + vertices.push_back(attrib.vertices[3*index.vertex_index + 0]); + vertices.push_back(attrib.vertices[3*index.vertex_index + 1]); + vertices.push_back(attrib.vertices[3*index.vertex_index + 2]); + + // UV-Koordinaten + if (!attrib.texcoords.empty()) { + uvs.push_back(attrib.texcoords[2*index.texcoord_index + 0]); + uvs.push_back(attrib.texcoords[2*index.texcoord_index + 1]); + } else { + uvs.push_back(0.0f); + uvs.push_back(0.0f); + } + + // Index + indices.push_back(indexOffset); + indexOffset++; + } + } + + RawModel rawModel = loader.loadToVAO(vertices, uvs, indices); + ModelTexture modelTexture = loader.loadTextureFromFile(texturePath); + TexturedModel texturedModel = TexturedModel(std::make_shared(rawModel), std::make_shared(modelTexture)); + return std::make_shared(texturedModel); +} diff --git a/src/engine/renderer/loader/OBJLoader.h b/src/engine/renderer/loader/OBJLoader.h new file mode 100644 index 0000000..5f0d4c2 --- /dev/null +++ b/src/engine/renderer/loader/OBJLoader.h @@ -0,0 +1,21 @@ +// +// Created by sebastian on 07.02.26. +// + +#ifndef OBJLOADER_H +#define OBJLOADER_H +#include + +#include "Loader.h" +#include "../model/TexturedModel.h" + + +class OBJLoader { +public: + static std::shared_ptr loadModel(const std::string &modelPath, const std::string &texturePath, Loader &loader); + +}; + + + +#endif //OBJLOADER_H