ADD: Load OBJ Models

This commit is contained in:
sebastian 2026-02-07 19:15:40 +01:00
parent 11497c6683
commit 0163373a9e
9 changed files with 94 additions and 6 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "lib/glm"] [submodule "lib/glm"]
path = lib/glm path = lib/glm
url = https://github.com/g-truc/glm.git url = https://github.com/g-truc/glm.git
[submodule "lib/tinyobjloader"]
path = lib/tinyobjloader
url = https://github.com/tinyobjloader/tinyobjloader.git

View File

@ -51,12 +51,15 @@ add_executable(Dicewars_Siedler src/main.cpp
src/engine/layer/entities/Camera.cpp src/engine/layer/entities/Camera.cpp
src/engine/layer/entities/Camera.h src/engine/layer/entities/Camera.h
src/engine/platform/glfw/InputManager.cpp 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 target_include_directories(Dicewars_Siedler PRIVATE
lib/glfw/include lib/glfw/include
lib/glm lib/glm
lib/stb_image lib/stb_image
lib/tinyobjloader
) )
target_link_libraries(Dicewars_Siedler target_link_libraries(Dicewars_Siedler

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

1
lib/tinyobjloader Submodule

@ -0,0 +1 @@
Subproject commit d56555b026c1c7cec0f93f3ec7f1de2ff005c5ad

View File

@ -6,6 +6,7 @@
#include "../platform/glfw/InputManager.h" #include "../platform/glfw/InputManager.h"
#include "../renderer/Renderer.h" #include "../renderer/Renderer.h"
#include "../renderer/loader/OBJLoader.h"
#include "../renderer/model/TexturedModel.h" #include "../renderer/model/TexturedModel.h"
#include "../renderer/textures/ModelTexture.h" #include "../renderer/textures/ModelTexture.h"
@ -34,10 +35,8 @@ void GameLayer::onAttach()
1,0 //v3 1,0 //v3
}; };
RawModel model = loader.loadToVAO(vertices, textureCoords, indices); texturedModel = *OBJLoader::loadModel("assets/stall/stall.obj", "assets/stall/stallTexture.png", loader);
ModelTexture texture = ModelTexture(loader.loadTexture("assets/textures/texture.png")); entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-50), 0,0,0, 1.f));
texturedModel = TexturedModel(std::make_shared<RawModel>(model), std::make_shared<ModelTexture>(texture));
entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-1), 0,0,0, 1.f));
camera = std::make_unique<Camera>(); camera = std::make_unique<Camera>();
} }
@ -54,6 +53,7 @@ void GameLayer::onUpdate()
if (InputManager::isKeyPressed(GLFW_KEY_A)) moveDir.x -= 1.0f; if (InputManager::isKeyPressed(GLFW_KEY_A)) moveDir.x -= 1.0f;
if (InputManager::isKeyPressed(GLFW_KEY_D)) moveDir.x += 1.0f; if (InputManager::isKeyPressed(GLFW_KEY_D)) moveDir.x += 1.0f;
entity->increaseRotation(0,1,0);
camera->move(moveDir, 0.02f); camera->move(moveDir, 0.02f);
renderer.renderFrame(*entity, *camera); renderer.renderFrame(*entity, *camera);
} }

View File

@ -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) { GLuint Loader::loadTexture(const std::string &path) {
Texture2D texture = TextureLoader::loadTexture(path); Texture2D texture = TextureLoader::loadTexture(path);
textureIDs.push_back(texture.id); textureIDs.push_back(texture.id);

View File

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include "../model/RawModel.h" #include "../model/RawModel.h"
#include "../textures/ModelTexture.h"
#include "glad/glad.h" #include "glad/glad.h"
@ -18,9 +19,10 @@ public:
~Loader(); ~Loader();
void cleanUp(); void cleanUp();
GLuint loadTexture(const std::string &path); ModelTexture loadTextureFromFile(const std::string& path);
private: private:
GLuint createVAO(); GLuint createVAO();
GLuint loadTexture(const std::string &path);
void storeDataInAttributeList(int attributeNumber, int coordinateSize, const std::vector<float> &data); void storeDataInAttributeList(int attributeNumber, int coordinateSize, const std::vector<float> &data);
void bindIndicesBuffer(const std::vector<int> &indices); void bindIndicesBuffer(const std::vector<int> &indices);
void unbindVAO(); void unbindVAO();

View File

@ -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<TexturedModel> OBJLoader::loadModel(const std::string &modelPath, const std::string& texturePath, Loader &loader) {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> 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<float> vertices;
std::vector<float> uvs;
std::vector<int> 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>(rawModel), std::make_shared<ModelTexture>(modelTexture));
return std::make_shared<TexturedModel>(texturedModel);
}

View File

@ -0,0 +1,21 @@
//
// Created by sebastian on 07.02.26.
//
#ifndef OBJLOADER_H
#define OBJLOADER_H
#include <memory>
#include "Loader.h"
#include "../model/TexturedModel.h"
class OBJLoader {
public:
static std::shared_ptr<TexturedModel> loadModel(const std::string &modelPath, const std::string &texturePath, Loader &loader);
};
#endif //OBJLOADER_H