diff --git a/src/engine/include/models/mesh_texture.hpp b/src/engine/include/models/mesh_texture.hpp index 82324a1..bf1b751 100644 --- a/src/engine/include/models/mesh_texture.hpp +++ b/src/engine/include/models/mesh_texture.hpp @@ -13,6 +13,7 @@ #include #include +#include "./texture_wrap_settings.hpp" /** * Class which contains texture data @@ -31,23 +32,100 @@ public: // Getters // ---- + /** + * Auto generated unique Id. + * Core role of this variable is to dont send + * again texture in renderer, when last sent texture id + * by renderer will be equal to this id. + */ + const u32 &getId() const { return id; }; + + /** + * Core role of this variable is to assign texture to correct material. + * For example you can have two textures for single material. + * Example: Same player can have two textures (team A, team B) + */ + const u32 &getMaterialId() const { return materialId; }; + + const u8 &getWidth() const { return width; }; + + const u8 &getHeight() const { return height; }; + + texwrap_t *getWrapSettings() { return &wrapSettings; }; + + /** + * Returns always width * width * 3. + * 3 because of RGB + */ + u32 getDataSize() const { return width * height * 3; }; + + /** + * Texture data, used by renderer. + * Array of size getDataSize(). + */ + unsigned char *getData() const { return data; }; + + /** + * Get texture name. + * For "textures/abc.bmp" result will be: "abc" + */ + char *getName() const { return name; }; + // ---- // Setters // ---- + /** + * Core role of this variable is to assign texture to correct material. + * For example you can have two textures for single material. + * Example: Same player can have two textures (team A, team B) + */ + void setMaterialId(const u32 &t_val) { materialId = t_val; } + + /** + * Set texture size and allocate texture data + * Do not call this method unless you know what you do. + * Should be called by data loader. + */ + void setSize(const u8 &t_width, const u8 &t_height); + + /** + * Do not call this method unless you know what you do. + * Should be called by data loader. + */ + void setHeight(const u8 &t_val) { height = t_val; } + + /** + * Set texture name. + * Should be the file name without extension + */ + void setData(const u32 &t_index, const unsigned char &t_val) { data[t_index] = t_val; } + + /** + * Set texture name. + * Should be the file name without extension + */ + void setName(char *t_val); + + /** Set texture wrapping */ + void setWrapSettings(const WrapSettings t_horizontal, const WrapSettings t_vertical); + // ---- // Other // ---- - // TODO + const u8 &isNameSet() const { return _isNameSet; }; - u32 id; - unsigned char *data; - texwrap_t wrapSettings; - char *name; - u8 width, height; + const u8 &isSizeSet() const { return _isSizeSet; }; private: + void setDefaultWrapSettings(); + + texwrap_t wrapSettings; + char *name; + u32 id, materialId; + u8 width, height, _isNameSet, _isSizeSet; + unsigned char *data; }; #endif diff --git a/src/engine/include/models/texture_wrap_settings.hpp b/src/engine/include/models/texture_wrap_settings.hpp new file mode 100644 index 0000000..d012ecc --- /dev/null +++ b/src/engine/include/models/texture_wrap_settings.hpp @@ -0,0 +1,22 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#ifndef _TYRA_TEXTURE_WRAP_SETTINGS_ +#define _TYRA_TEXTURE_WRAP_SETTINGS_ + +enum WrapSettings +{ + Repeat = 0, + Clamp = 1, + RegionClamp = 2, + RegionRepeat = 3 +}; + +#endif diff --git a/src/engine/loaders/bmp_loader.cpp b/src/engine/loaders/bmp_loader.cpp index 503894a..c225b8f 100644 --- a/src/engine/loaders/bmp_loader.cpp +++ b/src/engine/loaders/bmp_loader.cpp @@ -32,6 +32,7 @@ BmpLoader::~BmpLoader() {} */ void BmpLoader::load(MeshTexture &o_texture, char *t_subfolder, char *t_name, char *t_extension) { + o_texture.setName(t_name); char *t_path_part = String::createConcatenated(t_subfolder, t_name); char *t_path = String::createConcatenated(t_path_part, t_extension); delete[] t_path_part; @@ -46,10 +47,7 @@ void BmpLoader::load(MeshTexture &o_texture, char *t_subfolder, char *t_name, ch u32 width = *(u32 *)&header[18]; u32 height = *(u32 *)&header[22]; - o_texture.id = rand() % 100000; - o_texture.width = width; - o_texture.height = height; - o_texture.data = new unsigned char[width * height * 3]; + o_texture.setSize(width, height); printf("BMPLoader - width: %d | height: %d\n", width, height); if (width > 128 || height > 128) PRINT_ERR("Given texture is big for PS2. Please strict to 128x128 max"); @@ -70,9 +68,9 @@ void BmpLoader::load(MeshTexture &o_texture, char *t_subfolder, char *t_name, ch data[j] = data[j + 2]; data[j + 2] = tmp; - o_texture.data[x] = data[j]; - o_texture.data[x + 1] = data[j + 1]; - o_texture.data[x + 2] = data[j + 2]; + o_texture.setData(x, data[j]); + o_texture.setData(x + 1, data[j + 1]); + o_texture.setData(x + 2, data[j + 2]); x += 3; } } diff --git a/src/engine/models/mesh.cpp b/src/engine/models/mesh.cpp index 5a19074..427ac6c 100644 --- a/src/engine/models/mesh.cpp +++ b/src/engine/models/mesh.cpp @@ -166,12 +166,12 @@ u32 Mesh::getVertexCount() void Mesh::setDefaultWrapSettings(texwrap_t &t_wrapSettings) { - t_wrapSettings.horizontal = WRAP_REPEAT; - t_wrapSettings.vertical = WRAP_REPEAT; - t_wrapSettings.maxu = 0; - t_wrapSettings.maxv = 0; - t_wrapSettings.minu = 0; - t_wrapSettings.minv = 0; + // t_wrapSettings.horizontal = WRAP_REPEAT; + // t_wrapSettings.vertical = WRAP_REPEAT; + // t_wrapSettings.maxu = 0; + // t_wrapSettings.maxv = 0; + // t_wrapSettings.minu = 0; + // t_wrapSettings.minv = 0; } void Mesh::loadTextures(char *t_subfolder, char *t_extension) @@ -183,14 +183,14 @@ void Mesh::loadTextures(char *t_subfolder, char *t_extension) for (u8 i = 0; i < obj->frames[0].getMaterialsCount(); i++) { bmpLoader.load(spec->textures[i], t_subfolder, obj->frames[0].getMaterial(i).getName(), t_extension); - setDefaultWrapSettings(spec->textures[i].wrapSettings); + // setDefaultWrapSettings(spec->textures[i].wrapSettings); } } else if (isMd2Loaded) { spec->textures = new MeshTexture[1]; bmpLoader.load(spec->textures[0], t_subfolder, md2->filename, t_extension); - setDefaultWrapSettings(spec->textures[0].wrapSettings); + // setDefaultWrapSettings(spec->textures[0].wrapSettings); } else if (isDffLoaded) { @@ -199,7 +199,7 @@ void Mesh::loadTextures(char *t_subfolder, char *t_extension) for (u8 j = 0; j < dff->clump.geometryList.geometries[0].materialList.materials[i].data.textureCount; j++) { bmpLoader.load(spec->textures[i], t_subfolder, dff->clump.geometryList.geometries[0].materialList.materials[i].textures[j].textureName.text, t_extension); - setDefaultWrapSettings(spec->textures[i].wrapSettings); + // setDefaultWrapSettings(spec->textures[i].wrapSettings); } } else diff --git a/src/engine/models/mesh_texture.cpp b/src/engine/models/mesh_texture.cpp index 86fb8e5..e204254 100644 --- a/src/engine/models/mesh_texture.cpp +++ b/src/engine/models/mesh_texture.cpp @@ -9,7 +9,10 @@ */ #include "../include/models/mesh_texture.hpp" +#include "../include/utils/string.hpp" #include "../include/utils/debug.hpp" +#include +#include // ---- // Constructors/Destructors @@ -17,12 +20,61 @@ MeshTexture::MeshTexture() { + id = rand() % 100000; + materialId = 0; + _isSizeSet = false; + _isNameSet = false; + setDefaultWrapSettings(); } MeshTexture::~MeshTexture() { + if (_isNameSet) + delete[] name; + if (_isSizeSet) + delete[] data; } // ---- // Methods // ---- + +void MeshTexture::setSize(const u8 &t_width, const u8 &t_height) +{ + if (_isSizeSet) + { + PRINT_ERR("Can't set size, because was already set!"); + return; + } + width = t_width; + height = t_height; + data = new unsigned char[getDataSize()]; + _isSizeSet = true; +} + +void MeshTexture::setName(char *t_val) +{ + if (_isNameSet) + { + PRINT_ERR("Can't set name, because was already set!"); + return; + } + name = String::createCopy(t_val); + _isNameSet = true; +} + +void MeshTexture::setDefaultWrapSettings() +{ + wrapSettings.horizontal = WRAP_REPEAT; + wrapSettings.vertical = WRAP_REPEAT; + wrapSettings.maxu = 0; + wrapSettings.maxv = 0; + wrapSettings.minu = 0; + wrapSettings.minv = 0; +} + +void MeshTexture::setWrapSettings(const WrapSettings t_horizontal, const WrapSettings t_vertical) +{ + wrapSettings.horizontal = t_horizontal; + wrapSettings.vertical = t_vertical; +} diff --git a/src/engine/modules/gif_sender.cpp b/src/engine/modules/gif_sender.cpp index 9361605..9fc4b76 100644 --- a/src/engine/modules/gif_sender.cpp +++ b/src/engine/modules/gif_sender.cpp @@ -56,10 +56,10 @@ void GifSender::sendTexture(MeshTexture &texture, texbuffer_t *t_texBuffer) const u16 packetSize = 40; packet_t *packet = packet_init(packetSize, PACKET_NORMAL); qword_t *q = packet->data; - q = draw_texture_transfer(q, texture.data, texture.width, texture.height, GS_PSM_24, t_texBuffer->address, t_texBuffer->width); + q = draw_texture_transfer(q, texture.getData(), texture.getWidth(), texture.getHeight(), GS_PSM_24, t_texBuffer->address, t_texBuffer->width); DMATAG_CNT(q, 2, 0, 0, 0); q++; - q = draw_texture_wrapping(q, 0, &texture.wrapSettings); + q = draw_texture_wrapping(q, 0, texture.getWrapSettings()); q = draw_texture_flush(q); dma_channel_send_chain(DMA_CHANNEL_GIF, packet->data, q - packet->data, 0, 0); dma_wait_fast(); diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 926c76a..a534d27 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -115,11 +115,11 @@ void Renderer::deallocateTextureBuffer() void Renderer::changeTexture(Mesh *t_mesh, u8 t_textureIndex) { - if (t_mesh->spec->textures[t_textureIndex].id != lastTextureId) + if (t_mesh->spec->textures[t_textureIndex].getId() != lastTextureId) { - lastTextureId = t_mesh->spec->textures[t_textureIndex].id; + lastTextureId = t_mesh->spec->textures[t_textureIndex].getId(); deallocateTextureBuffer(); - allocateTextureBuffer(t_mesh->spec->textures[t_textureIndex].width, t_mesh->spec->textures[t_textureIndex].height); + allocateTextureBuffer(t_mesh->spec->textures[t_textureIndex].getWidth(), t_mesh->spec->textures[t_textureIndex].getHeight()); GifSender::sendTexture(t_mesh->spec->textures[t_textureIndex], &textureBuffer); } }