From f9dc79caf1a7ea6a74429e54f9b55f3177e82151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Sobczy=C5=84ski?= Date: Sat, 31 Oct 2020 22:14:35 +0100 Subject: [PATCH] added texture swapping --- src/engine/include/models/mesh_texture.hpp | 41 ++++++++++++++++------ src/engine/loaders/bmp_loader.cpp | 3 ++ src/engine/models/mesh_texture.cpp | 36 ++++--------------- src/engine/modules/renderer.cpp | 1 + src/engine/modules/texture_repository.cpp | 4 +++ src/samples/ari/ari.cpp | 5 ++- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/src/engine/include/models/mesh_texture.hpp b/src/engine/include/models/mesh_texture.hpp index f23e649..5a3183c 100644 --- a/src/engine/include/models/mesh_texture.hpp +++ b/src/engine/include/models/mesh_texture.hpp @@ -15,6 +15,7 @@ #include #include "./texture_wrap_settings.hpp" #include "./texture_link.hpp" +#include /** * Class which contains texture data. @@ -44,7 +45,7 @@ public: const u8 &getHeight() const { return height; }; - const u32 &getTextureLinksCount() const { return texLinkCount; }; + u32 getTextureLinksCount() const { return static_cast(texLinks.size()); }; texwrap_t *getWrapSettings() { return &wrapSettings; }; @@ -67,7 +68,19 @@ public: char *getName() const { return name; }; /** Array of texture links. Size of getTextureLinksCount() */ - TextureLink *getTextureLinks() const { return texLinks; }; + const std::vector &getTextureLinks() const { return texLinks; }; + + /** + * Returns index of link. + * -1 if not found. + */ + const s32 getIndexOfLink(const u32 &t_meshId, const u32 &t_materialId) const + { + for (u32 i = 0; i < texLinks.size(); i++) + if (texLinks[i].materialId == t_materialId && texLinks[i].meshId == t_meshId) + return i; + return -1; + }; // ---- // Setters @@ -108,27 +121,33 @@ public: /** Assign texture to mesh and mesh material */ void addLink(const u32 &t_meshId, const u32 &t_materialId); - /** Remove mesh and mesh material assignment */ - void removeLink(const u32 &t_meshId, const u32 &t_materialId); - const u8 &isNameSet() const { return _isNameSet; }; const u8 &isSizeSet() const { return _isSizeSet; }; const u8 isLinkedWith(const u32 &t_meshId, const u32 &t_materialId) const { - for (u32 i = 0; i < texLinkCount; i++) - if (texLinks[i].materialId == t_materialId && texLinks[i].meshId == t_meshId) - return true; - return false; + s32 index = getIndexOfLink(t_meshId, t_materialId); + if (index != -1) + return true; + else + return false; }; + void removeLink(const u32 &t_index) { texLinks.erase(texLinks.begin() + t_index); } + + void removeLink(const u32 &t_meshId, const u32 &t_materialId) + { + u32 index = getIndexOfLink(t_meshId, t_materialId); + removeLink(index); + } + private: void setDefaultWrapSettings(); texwrap_t wrapSettings; char *name; - TextureLink *texLinks; - u32 id, texLinkCount; + std::vector texLinks; + u32 id; u8 width, height, _isNameSet, _isSizeSet; unsigned char *data; }; diff --git a/src/engine/loaders/bmp_loader.cpp b/src/engine/loaders/bmp_loader.cpp index acbf705..e0fba90 100644 --- a/src/engine/loaders/bmp_loader.cpp +++ b/src/engine/loaders/bmp_loader.cpp @@ -40,7 +40,10 @@ void BmpLoader::load(MeshTexture &o_texture, char *t_subfolder, char *t_name, ch FILE *file = fopen(t_path, "rb"); if (file == NULL) + { PRINT_ERR("Failed to load .bmp file!"); + return; + } unsigned char header[54]; fread(header, sizeof(unsigned char), 54, file); diff --git a/src/engine/models/mesh_texture.cpp b/src/engine/models/mesh_texture.cpp index 5e57af4..406e2aa 100644 --- a/src/engine/models/mesh_texture.cpp +++ b/src/engine/models/mesh_texture.cpp @@ -21,7 +21,6 @@ MeshTexture::MeshTexture() { id = rand() % 1000000; - texLinkCount = 0; _isSizeSet = false; _isNameSet = false; setDefaultWrapSettings(); @@ -29,8 +28,8 @@ MeshTexture::MeshTexture() MeshTexture::~MeshTexture() { - if (texLinkCount > 0) - delete[] texLinks; + if (getTextureLinksCount() > 0) + texLinks.clear(); if (_isNameSet) delete[] name; if (_isSizeSet) @@ -83,31 +82,8 @@ void MeshTexture::setWrapSettings(const WrapSettings t_horizontal, const WrapSet void MeshTexture::addLink(const u32 &t_meshId, const u32 &t_materialId) { - TextureLink *savedLinks = texLinks; - texLinks = new TextureLink[texLinkCount + 1]; - for (u32 i = 0; i < texLinkCount; i++) - { - texLinks[i].materialId = savedLinks[i].materialId; - texLinks[i].meshId = savedLinks[i].meshId; - } - if (texLinkCount != 0) - delete[] savedLinks; - texLinkCount++; - texLinks[texLinkCount - 1].materialId = t_materialId; - texLinks[texLinkCount - 1].meshId = t_meshId; -} - -void MeshTexture::removeLink(const u32 &t_meshId, const u32 &t_materialId) -{ - TextureLink *savedLinks = texLinks; - texLinks = new TextureLink[texLinkCount - 1]; - u32 savedCount = 0; - for (u32 i = 0; i < texLinkCount; i++) - if (savedLinks[i].materialId != t_materialId && savedLinks[i].meshId != t_meshId) - { - texLinks[savedCount++].materialId = savedLinks[i].materialId; - texLinks[savedCount++].meshId = savedLinks[i].meshId; - } - delete[] savedLinks; - texLinkCount--; + TextureLink link; + link.materialId = t_materialId; + link.meshId = t_meshId; + texLinks.push_back(link); } diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index ca6988f..c33c9aa 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -83,6 +83,7 @@ void Renderer::deallocateTextureBuffer() void Renderer::changeTexture(Mesh *t_mesh, u32 t_materialId) { + MeshTexture *tex = textureRepo.getByMesh(t_mesh->id, t_materialId); if (tex != NULL) { diff --git a/src/engine/modules/texture_repository.cpp b/src/engine/modules/texture_repository.cpp index 447f00f..6617f6b 100644 --- a/src/engine/modules/texture_repository.cpp +++ b/src/engine/modules/texture_repository.cpp @@ -28,6 +28,10 @@ TextureRepository::~TextureRepository() {} // Methods // ---- +/** Add unlinked texture. + * @param t_subfolder Relative path. Ex.: "textures/" + * @param t_name Filename without extension. Ex.: "water" + */ MeshTexture *TextureRepository::add(char *t_subfolder, char *t_name) { increaseArray(texturesCount + 1); diff --git a/src/samples/ari/ari.cpp b/src/samples/ari/ari.cpp index 846c07a..1319793 100644 --- a/src/samples/ari/ari.cpp +++ b/src/samples/ari/ari.cpp @@ -94,7 +94,10 @@ void Ari::onInit() Vector3 testpos = Vector3(0.0F, 10.0F, 0.0F); test->loadObj("objanim/", "untitled", testpos, 10.0F, 2); texRepo->addByMesh("objanim/", *test); - // MeshTexture **textures = texRepo->getAll(); + MeshTexture **textures = texRepo->getAll(); + textures[0]->removeLink(test->id, test->getMaterial(0).getId()); + MeshTexture *tex = texRepo->add("objanim/", "water"); + tex->addLink(test->id, test->getMaterial(0).getId()); test->playAnimation(0, 1); test->shouldBeBackfaceCulled = true; // test->shouldBeFrustumCulled = true;