added texture swapping

This commit is contained in:
Sandro Sobczyński
2020-10-31 22:14:35 +01:00
parent 885e8b4f2b
commit f9dc79caf1
6 changed files with 48 additions and 42 deletions
+30 -11
View File
@@ -15,6 +15,7 @@
#include <draw_sampling.h> #include <draw_sampling.h>
#include "./texture_wrap_settings.hpp" #include "./texture_wrap_settings.hpp"
#include "./texture_link.hpp" #include "./texture_link.hpp"
#include <vector>
/** /**
* Class which contains texture data. * Class which contains texture data.
@@ -44,7 +45,7 @@ public:
const u8 &getHeight() const { return height; }; const u8 &getHeight() const { return height; };
const u32 &getTextureLinksCount() const { return texLinkCount; }; u32 getTextureLinksCount() const { return static_cast<u32>(texLinks.size()); };
texwrap_t *getWrapSettings() { return &wrapSettings; }; texwrap_t *getWrapSettings() { return &wrapSettings; };
@@ -67,7 +68,19 @@ public:
char *getName() const { return name; }; char *getName() const { return name; };
/** Array of texture links. Size of getTextureLinksCount() */ /** Array of texture links. Size of getTextureLinksCount() */
TextureLink *getTextureLinks() const { return texLinks; }; const std::vector<TextureLink> &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 // Setters
@@ -108,27 +121,33 @@ public:
/** Assign texture to mesh and mesh material */ /** Assign texture to mesh and mesh material */
void addLink(const u32 &t_meshId, const u32 &t_materialId); 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 &isNameSet() const { return _isNameSet; };
const u8 &isSizeSet() const { return _isSizeSet; }; const u8 &isSizeSet() const { return _isSizeSet; };
const u8 isLinkedWith(const u32 &t_meshId, const u32 &t_materialId) const const u8 isLinkedWith(const u32 &t_meshId, const u32 &t_materialId) const
{ {
for (u32 i = 0; i < texLinkCount; i++) s32 index = getIndexOfLink(t_meshId, t_materialId);
if (texLinks[i].materialId == t_materialId && texLinks[i].meshId == t_meshId) if (index != -1)
return true; return true;
return false; 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: private:
void setDefaultWrapSettings(); void setDefaultWrapSettings();
texwrap_t wrapSettings; texwrap_t wrapSettings;
char *name; char *name;
TextureLink *texLinks; std::vector<TextureLink> texLinks;
u32 id, texLinkCount; u32 id;
u8 width, height, _isNameSet, _isSizeSet; u8 width, height, _isNameSet, _isSizeSet;
unsigned char *data; unsigned char *data;
}; };
+3
View File
@@ -40,7 +40,10 @@ void BmpLoader::load(MeshTexture &o_texture, char *t_subfolder, char *t_name, ch
FILE *file = fopen(t_path, "rb"); FILE *file = fopen(t_path, "rb");
if (file == NULL) if (file == NULL)
{
PRINT_ERR("Failed to load .bmp file!"); PRINT_ERR("Failed to load .bmp file!");
return;
}
unsigned char header[54]; unsigned char header[54];
fread(header, sizeof(unsigned char), 54, file); fread(header, sizeof(unsigned char), 54, file);
+6 -30
View File
@@ -21,7 +21,6 @@
MeshTexture::MeshTexture() MeshTexture::MeshTexture()
{ {
id = rand() % 1000000; id = rand() % 1000000;
texLinkCount = 0;
_isSizeSet = false; _isSizeSet = false;
_isNameSet = false; _isNameSet = false;
setDefaultWrapSettings(); setDefaultWrapSettings();
@@ -29,8 +28,8 @@ MeshTexture::MeshTexture()
MeshTexture::~MeshTexture() MeshTexture::~MeshTexture()
{ {
if (texLinkCount > 0) if (getTextureLinksCount() > 0)
delete[] texLinks; texLinks.clear();
if (_isNameSet) if (_isNameSet)
delete[] name; delete[] name;
if (_isSizeSet) 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) void MeshTexture::addLink(const u32 &t_meshId, const u32 &t_materialId)
{ {
TextureLink *savedLinks = texLinks; TextureLink link;
texLinks = new TextureLink[texLinkCount + 1]; link.materialId = t_materialId;
for (u32 i = 0; i < texLinkCount; i++) link.meshId = t_meshId;
{ texLinks.push_back(link);
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--;
} }
+1
View File
@@ -83,6 +83,7 @@ void Renderer::deallocateTextureBuffer()
void Renderer::changeTexture(Mesh *t_mesh, u32 t_materialId) void Renderer::changeTexture(Mesh *t_mesh, u32 t_materialId)
{ {
MeshTexture *tex = textureRepo.getByMesh(t_mesh->id, t_materialId); MeshTexture *tex = textureRepo.getByMesh(t_mesh->id, t_materialId);
if (tex != NULL) if (tex != NULL)
{ {
@@ -28,6 +28,10 @@ TextureRepository::~TextureRepository() {}
// Methods // 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) MeshTexture *TextureRepository::add(char *t_subfolder, char *t_name)
{ {
increaseArray(texturesCount + 1); increaseArray(texturesCount + 1);
+4 -1
View File
@@ -94,7 +94,10 @@ void Ari::onInit()
Vector3 testpos = Vector3(0.0F, 10.0F, 0.0F); Vector3 testpos = Vector3(0.0F, 10.0F, 0.0F);
test->loadObj("objanim/", "untitled", testpos, 10.0F, 2); test->loadObj("objanim/", "untitled", testpos, 10.0F, 2);
texRepo->addByMesh("objanim/", *test); 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->playAnimation(0, 1);
test->shouldBeBackfaceCulled = true; test->shouldBeBackfaceCulled = true;
// test->shouldBeFrustumCulled = true; // test->shouldBeFrustumCulled = true;