From 6e09741d19899a2944f025a759a31ca771287513 Mon Sep 17 00:00:00 2001 From: h4570 Date: Mon, 28 Dec 2020 14:19:51 +0100 Subject: [PATCH] refactored texturelink, loadFrom(), mesh color --- CHANGELOG.MD | 14 +++++ src/engine/include/models/mesh.hpp | 7 +-- src/engine/include/models/mesh_frame.hpp | 26 ++++++++- src/engine/include/models/mesh_material.hpp | 22 +++++++- src/engine/include/models/texture.hpp | 51 +++++++---------- src/engine/include/models/texture_link.hpp | 9 +-- src/engine/include/modules/gif_sender.hpp | 4 +- .../include/modules/texture_repository.hpp | 19 ++----- src/engine/include/modules/vif_sender.hpp | 4 +- src/engine/models/mesh.cpp | 25 ++++----- src/engine/models/mesh_frame.cpp | 56 ++++++++++++++----- src/engine/models/mesh_material.cpp | 47 +++++++++++++--- src/engine/models/texture.cpp | 13 +---- src/engine/modules/gif_sender.cpp | 22 ++++---- src/engine/modules/renderer.cpp | 20 ++++--- src/engine/modules/texture_repository.cpp | 2 +- src/engine/modules/vif_sender.cpp | 22 ++++---- src/samples/dolphin/dolphin.cpp | 9 +-- src/samples/floors/managers/floor_manager.cpp | 17 +++--- src/samples/floors/ui.cpp | 4 +- 20 files changed, 239 insertions(+), 154 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index a08afdb..7908a61 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.?.?] - 2020-?-? + +### Added +- Id to MeshFrame + +### Changed +- Moved color from mesh to mesh material. +- OBJ Loader refactor +- Logic of loadFrom() + +### Removed +- Removed MeshId from TextureLink, TextureRepository + ## [1.29.1] - 2020-12-27 ### Added - Created libpacket2 in PS2SDK @@ -96,6 +109,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Makefile - Primitive floors sample +[1.?.?]: https://github.com/h4570/tyra/compare/v1.29.1-alpha...v1.?.?-alpha [1.29.1]: https://github.com/h4570/tyra/compare/1.1.0...v1.29.1-alpha [1.1.0]: https://github.com/h4570/tyra/compare/1.0.0...1.1.0 [1.0.0]: https://github.com/h4570/tyra/compare/1.0.0 diff --git a/src/engine/include/models/mesh.hpp b/src/engine/include/models/mesh.hpp index 1f9e7ea..64983fa 100644 --- a/src/engine/include/models/mesh.hpp +++ b/src/engine/include/models/mesh.hpp @@ -16,7 +16,6 @@ #include "./texture.hpp" #include "./mesh_frame.hpp" #include -#include #include #include #include "./anim_state.hpp" @@ -43,7 +42,6 @@ public: * When true, mesh materials are not drawn when they are outside of view frustum. */ u8 shouldBeFrustumCulled; - color_t color; clutbuffer_t clut; lod_t lod; @@ -176,6 +174,8 @@ public: void setAnimSpeed(const float &t_value) { animState.speed = t_value; } + const u8 &areFramesAllocated() const { return _areFramesAllocated; }; + /** * Check if this class loaded mesh data first. * Meshes which use loadFrom() method have false there. @@ -207,9 +207,8 @@ private: AnimState animState; MeshFrame *frames; u32 id, framesCount; - u8 _isMother; + u8 _isMother, _areFramesAllocated; Vector3 calc3Vectors[3]; - void setDefaultColor(); void setDefaultLODAndClut(); }; diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index da83909..e318831 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -35,6 +35,12 @@ public: // Getters // ---- + /** + * Auto generated unique Id. + * Core role of this variable is to select correct texture to draw + */ + const u32 &getId() const { return id; }; + const u32 &getVertexCount() const { return vertexCount; }; const u32 &getSTsCount() const { return stsCount; }; const u32 &getNormalsCount() const { return normalsCount; }; @@ -119,6 +125,18 @@ public: // Other // ---- + /** + * Check if this object is mother. + * Mother = object loaded with loadObj(), loadDff().. + * Have all mother data: vertex, st, etc.. + * Non-mother = object loaded with loadFrom(). + * Have reference copy to mother data: vertex, st, etc.. + * + * When you will destruct mother object, all vertex, st data will be deleted. + * When you will destruct non-mother object, all vertex, st data will be NOT deleted. + */ + const u8 &isMother() const { return _isMother; }; + const u8 &areSTsAllocated() const { return _areSTsAllocated; }; const u8 &areVerticesAllocated() const { return _areVerticesAllocated; }; const u8 &areNormalsAllocated() const { return _areNormalsAllocated; }; @@ -128,6 +146,9 @@ public: const u8 &areSTsPresent() const { return _areSTsPresent; }; const u8 &areNormalsPresent() const { return _areNormalsPresent; }; + /** Create reference copy (non-mother) */ + void copyFrom(MeshFrame *t_refCopy); + /** Set STs count and allocate memory. */ void allocateSTs(const u32 &t_val); @@ -152,14 +173,15 @@ public: private: BoundingBox *boundingBoxObj; - u8 _areSTsAllocated, + u8 _isMother, + _areSTsAllocated, _areVerticesAllocated, _areNormalsAllocated, _areMaterialsAllocated, _areSTsPresent, _areNormalsPresent, _isBoundingBoxCalculated; - u32 vertexCount, stsCount, normalsCount, materialsCount; + u32 vertexCount, stsCount, normalsCount, materialsCount, id; MeshMaterial *materials; Point __attribute__((aligned(16))) * sts; Vector3 __attribute__((aligned(16))) * vertices, *normals; diff --git a/src/engine/include/models/mesh_material.hpp b/src/engine/include/models/mesh_material.hpp index 3ba2621..d708c78 100644 --- a/src/engine/include/models/mesh_material.hpp +++ b/src/engine/include/models/mesh_material.hpp @@ -12,6 +12,7 @@ #define _TYRA_MESH_MATERIAL_ #include +#include #include "bounding_box.hpp" #include "./math/vector3.hpp" #include "./math/plane.hpp" @@ -29,6 +30,8 @@ public: MeshMaterial(); ~MeshMaterial(); + color_t color; + // ---- // Getters // ---- @@ -108,7 +111,23 @@ public: // Other // ---- + /** Create reference copy (non-mother) */ + void copyFrom(MeshMaterial *t_refCopy); + const u8 &isNameSet() const { return _isNameSet; }; + + /** + * Check if this object is mother. + * Mother = object loaded with loadObj(), loadDff().. + * Have all mother data: vertex faces, st faces, etc.. + * Non-mother = object loaded with loadFrom(). + * Have reference copy to mother data: vertex faces, st faces, etc.. + * + * When you will destruct mother object, all faces data will be deleted. + * When you will destruct non-mother object, all faces data will be NOT deleted. + */ + const u8 &isMother() const { return _isMother; }; + const u8 &areFacesAllocated() const { return _areFacesAllocated; }; /** Set faces count and allocate memory. */ @@ -125,10 +144,11 @@ public: u8 isInFrustum(Plane *t_frustumPlanes, const Vector3 &position); private: + void setDefaultColor(); BoundingBox *boundingBoxObj; u32 facesCount, id; u32 *vertexFaces, *stFaces, *normalFaces; - u8 _isNameSet, _areFacesAllocated, _isBoundingBoxCalculated; + u8 _isMother, _isNameSet, _areFacesAllocated, _isBoundingBoxCalculated; char *name; }; diff --git a/src/engine/include/models/texture.hpp b/src/engine/include/models/texture.hpp index 0b9aaaf..470c0b1 100644 --- a/src/engine/include/models/texture.hpp +++ b/src/engine/include/models/texture.hpp @@ -83,21 +83,18 @@ public: /** * Returns index of link. * -1 if not found. + * @param t_id + * For 3D: Mesh material id. + * For 2D: Sprite id. */ - const s32 getIndexOfLink(const u32 &t_meshId, const u32 &t_materialId) const + const s32 getIndexOfLink(const u32 &t_id) const { for (u32 i = 0; i < texLinks.size(); i++) - if (texLinks[i].materialId == t_materialId && texLinks[i].meshOrSpriteId == t_meshId) + if (texLinks[i].id == t_id) return i; return -1; }; - /** - * Returns index of link. - * -1 if not found. - */ - inline const s32 getIndexOfLink(const u32 &t_spriteId) const { return getIndexOfLink(t_spriteId, 0); }; - // ---- // Setters // ---- @@ -144,18 +141,15 @@ public: const u8 &isSizeSet() const { return _isSizeSet; }; - const u8 isLinkedWith(const u32 &t_meshId, const u32 &t_materialId) const + /** + * Check if texture is linked with Mesh/Sprite. + * @param t_id + * For 3D: Mesh material id. + * For 2D: Sprite id. + */ + const u8 isLinkedWith(const u32 &t_id) const { - s32 index = getIndexOfLink(t_meshId, t_materialId); - if (index != -1) - return true; - else - return false; - }; - - const u8 isLinkedWith(const u32 &t_spriteId) const - { - s32 index = getIndexOfLink(t_spriteId); + s32 index = getIndexOfLink(t_id); if (index != -1) return true; else @@ -164,18 +158,15 @@ public: void removeLinkByIndex(const u32 &t_index) { texLinks.erase(texLinks.begin() + t_index); } - void removeLinkBySprite(const u32 &t_spriteId) + /** + * Remove texture link with given Mesh/Sprite. + * @param t_id + * For 3D: Mesh material id. + * For 2D: Sprite id. + */ + void removeLinkById(const u32 &t_id) { - s32 index = getIndexOfLink(t_spriteId); - if (index != -1) - removeLinkByIndex(index); - else - PRINT_ERR("Cant remove link, because it was not found!"); - } - - void removeLinkByMesh(const u32 &t_meshId, const u32 &t_materialId) - { - s32 index = getIndexOfLink(t_meshId, t_materialId); + s32 index = getIndexOfLink(t_id); if (index != -1) removeLinkByIndex(index); else diff --git a/src/engine/include/models/texture_link.hpp b/src/engine/include/models/texture_link.hpp index 75c24b7..687c9c0 100644 --- a/src/engine/include/models/texture_link.hpp +++ b/src/engine/include/models/texture_link.hpp @@ -15,10 +15,11 @@ struct TextureLink { - /** Mesh id or sprite id */ - u32 meshOrSpriteId; - /** Mesh material id or 0 if is sprite */ - u32 materialId; + /** + * For 3D: Mesh material id. + * For 2D: Sprite id. + */ + u32 id; }; #endif diff --git a/src/engine/include/modules/gif_sender.hpp b/src/engine/include/modules/gif_sender.hpp index 0c7646b..baeee9c 100644 --- a/src/engine/include/modules/gif_sender.hpp +++ b/src/engine/include/modules/gif_sender.hpp @@ -34,7 +34,7 @@ public: ~GifSender(); void initPacket(u8 context); - void addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer); + void addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer, color_t *t_color); void addClear(zbuffer_t *t_zBuffer, color_t *t_rgb); void sendPacket(); void sendClear(zbuffer_t *t_zBuffer, color_t *t_rgb); @@ -54,7 +54,7 @@ private: float halfScreenW, halfScreenH; MATRIX localWorld, localScreen, localLight; - void calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, RenderData *t_renderData, LightBulb *t_bulbs, u16 t_bulbsCount); + void calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, RenderData *t_renderData, LightBulb *t_bulbs, u16 t_bulbsCount, color_t *t_color); void convertCalcs(u32 t_vertCount, VECTOR *t_vertices, VECTOR *t_colors, VECTOR *t_sts, color_t &t_color); void addCurrentCalcs(u32 &t_vertexCount); }; diff --git a/src/engine/include/modules/texture_repository.hpp b/src/engine/include/modules/texture_repository.hpp index b3e29ac..67cd672 100644 --- a/src/engine/include/modules/texture_repository.hpp +++ b/src/engine/include/modules/texture_repository.hpp @@ -45,23 +45,14 @@ public: /** * Returns single texture. * NULL if not found. + * @param t_id + * For 3D: Mesh material id. + * For 2D: Sprite id. */ - Texture *getBySprite(const u32 &t_spriteId) + Texture *getBySpriteOrMesh(const u32 &t_id) { for (u32 i = 0; i < textures.size(); i++) - if (textures[i]->isLinkedWith(t_spriteId)) - return textures[i]; - return NULL; - } - - /** - * Returns single texture. - * NULL if not found. - */ - Texture *getByMesh(const u32 &t_meshId, const u32 &t_materialId) - { - for (u32 i = 0; i < textures.size(); i++) - if (textures[i]->isLinkedWith(t_meshId, t_materialId)) + if (textures[i]->isLinkedWith(t_id)) return textures[i]; return NULL; } diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index c1fdbc6..f1db5fc 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -29,7 +29,7 @@ public: ~VifSender(); // TODO refactor - void drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 vertCount2, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer); + void drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 vertCount2, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer, color_t *t_color); void calcMatrix(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation); void drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mesh **t_meshes, const u32 &t_skip, const u32 &t_count); void enableWait() { isDrawWaitEnabled = true; } @@ -41,7 +41,7 @@ private: Light *light; void uploadMicroProgram(); void setDoubleBufferAddStaticData(); - void drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *t_textureBuffer, u8 t_addDrawWait); + void drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *t_textureBuffer, u8 t_addDrawWait, color_t *t_color); packet2_t *packets[2] __attribute__((aligned(64))); packet2_t *currPacket; /** diff --git a/src/engine/models/mesh.cpp b/src/engine/models/mesh.cpp index 5a47159..2419b9b 100644 --- a/src/engine/models/mesh.cpp +++ b/src/engine/models/mesh.cpp @@ -28,6 +28,7 @@ Mesh::Mesh() shouldBeFrustumCulled = true; shouldBeBackfaceCulled = false; shouldBeLighted = false; + _areFramesAllocated = false; _isMother = false; scale = 1.0F; framesCount = 0; @@ -40,13 +41,12 @@ Mesh::Mesh() animState.isStayFrameSet = false; animState.nextFrame = 0; animState.speed = 0.1F; - setDefaultColor(); setDefaultLODAndClut(); } Mesh::~Mesh() { - if (_isMother) + if (_areFramesAllocated) delete[] frames; } @@ -59,6 +59,7 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, const float &t_scale, con ObjLoader loader = ObjLoader(); framesCount = 1; frames = new MeshFrame[framesCount]; + _areFramesAllocated = true; char *part1 = String::createConcatenated(t_subfolder, t_objFile); // "folder/object" char *finalPath = String::createConcatenated(part1, ".obj"); // "folder/object.obj" loader.load(&frames[0], finalPath, t_scale, t_invertT); @@ -78,6 +79,7 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, const float &t_scale, con ObjLoader loader = ObjLoader(); framesCount = t_framesCount; frames = new MeshFrame[framesCount]; + _areFramesAllocated = true; char *part1 = String::createConcatenated(t_subfolder, t_objFile); // "folder/object" char *part2 = String::createConcatenated(part1, "_"); // "folder/object_" for (u32 i = 0; i < framesCount; i++) @@ -105,6 +107,7 @@ void Mesh::loadDff(char *t_subfolder, char *t_dffFile, const float &t_scale, con char *dffPath = String::createConcatenated(part1, ".dff"); framesCount = 1; frames = new MeshFrame[1]; + _areFramesAllocated = true; loader.load(frames, dffPath, t_scale, t_invertT); delete[] part1; delete[] dffPath; @@ -120,8 +123,11 @@ void Mesh::loadMD2(char *t_subfolder, char *t_md2File, const float &t_scale, con void Mesh::loadFrom(const Mesh &t_mesh) { - frames = t_mesh.frames; framesCount = t_mesh.framesCount; + frames = new MeshFrame[framesCount]; + _areFramesAllocated = true; + for (u32 i = 0; i < framesCount; i++) + frames[i].copyFrom(&t_mesh.getFrame(i)); } void Mesh::playAnimation(const u32 &t_startFrame, const u32 &t_endFrame) @@ -254,8 +260,7 @@ u32 Mesh::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals "r"(nextVerts[vertFaces[faceI]].xyz), "r"(nextVerts[vertFaces[faceI + 1]].xyz), "r"(nextVerts[vertFaces[faceI + 2]].xyz), - "f"(animState.interpolation) - : "$10"); + "f"(animState.interpolation)); } else { @@ -375,16 +380,6 @@ u8 Mesh::isInFrustum(Plane *t_frustumPlanes) return boxResult; } -/** Set's default object color + no transparency */ -void Mesh::setDefaultColor() -{ - color.r = 0x80; - color.g = 0x80; - color.b = 0x80; - color.a = 0x80; - color.q = 1.0F; -} - /** Sets texture level of details settings and CLUT settings */ void Mesh::setDefaultLODAndClut() { diff --git a/src/engine/models/mesh_frame.cpp b/src/engine/models/mesh_frame.cpp index e337cd2..ba96f6f 100644 --- a/src/engine/models/mesh_frame.cpp +++ b/src/engine/models/mesh_frame.cpp @@ -8,6 +8,7 @@ # Sandro Sobczyński */ +#include #include "../include/models/mesh_frame.hpp" #include "../include/utils/debug.hpp" @@ -17,6 +18,7 @@ MeshFrame::MeshFrame() { + id = rand() % 1000000; vertexCount = 0; stsCount = 0; normalsCount = 0; @@ -27,19 +29,23 @@ MeshFrame::MeshFrame() _areMaterialsAllocated = false; _areSTsPresent = false; _areNormalsPresent = false; + _isMother = true; } MeshFrame::~MeshFrame() { - if (_areSTsAllocated) - delete[] sts; - if (_areVerticesAllocated) - delete[] vertices; - if (_areNormalsAllocated) - delete[] normals; + if (_isMother) + { + if (_areSTsAllocated) + delete[] sts; + if (_areVerticesAllocated) + delete[] vertices; + if (_areNormalsAllocated) + delete[] normals; + delete boundingBoxObj; + } if (_areMaterialsAllocated) delete[] materials; - delete boundingBoxObj; } // ---- @@ -96,14 +102,12 @@ void MeshFrame::allocateMaterials(const u32 &t_val) void MeshFrame::calculateBoundingBoxes() { - Vector3 boundingBox[8]; if (!_areVerticesAllocated) - { PRINT_ERR("Can't calculate bounding box, because vertices were not allocated!"); - return; - } + for (u32 i = 0; i < materialsCount; i++) materials->calculateBoundingBox(vertices, vertexCount); + float lowX, lowY, lowZ, hiX, hiY, hiZ; lowX = hiX = vertices[0].x; lowY = hiY = vertices[0].y; @@ -126,6 +130,7 @@ void MeshFrame::calculateBoundingBoxes() hiZ = vertices[i].z; } + Vector3 boundingBox[8]; boundingBox[0].set(lowX, lowY, lowZ); boundingBox[1].set(lowX, lowY, hiZ); boundingBox[2].set(lowX, hiY, lowZ); @@ -137,7 +142,32 @@ void MeshFrame::calculateBoundingBoxes() boundingBox[7].set(hiX, hiY, hiZ); _isBoundingBoxCalculated = true; - //BoundingBox is declared on the heap to prevent any ill-formed default - //constructor instantiated BoundingBox objects. + // BoundingBox is declared on the heap to prevent any ill-formed default + // constructor instantiated BoundingBox objects. boundingBoxObj = new BoundingBox(boundingBox); } + +void MeshFrame::copyFrom(MeshFrame *t_refCopy) +{ + vertexCount = t_refCopy->vertexCount; + stsCount = t_refCopy->stsCount; + normalsCount = t_refCopy->normalsCount; + materialsCount = t_refCopy->materialsCount; + materials = new MeshMaterial[materialsCount]; + for (u32 i = 0; i < materialsCount; i++) + materials[i].copyFrom(&t_refCopy->materials[i]); + _areSTsAllocated = true; + _areVerticesAllocated = true; + _areNormalsAllocated = true; + _areMaterialsAllocated = true; + _areSTsPresent = t_refCopy->_areSTsPresent; + _areNormalsPresent = t_refCopy->_areNormalsPresent; + vertices = t_refCopy->vertices; + sts = t_refCopy->sts; + normals = t_refCopy->normals; + + boundingBoxObj = t_refCopy->boundingBoxObj; + _isBoundingBoxCalculated = true; + + _isMother = false; +} diff --git a/src/engine/models/mesh_material.cpp b/src/engine/models/mesh_material.cpp index d6108da..d5e6efd 100644 --- a/src/engine/models/mesh_material.cpp +++ b/src/engine/models/mesh_material.cpp @@ -25,18 +25,23 @@ MeshMaterial::MeshMaterial() _isNameSet = false; _areFacesAllocated = false; _isBoundingBoxCalculated = false; + _isMother = true; + setDefaultColor(); } MeshMaterial::~MeshMaterial() { - if (_areFacesAllocated) + if (_isMother) { - delete[] vertexFaces; - delete[] stFaces; - delete[] normalFaces; + if (_areFacesAllocated) + { + delete[] vertexFaces; + delete[] stFaces; + delete[] normalFaces; + } + if (_isNameSet) + delete[] name; } - if (_isNameSet) - delete[] name; } // ---- @@ -102,7 +107,6 @@ u8 MeshMaterial::isInFrustum(Plane *t_frustumPlanes, const Vector3 &position) void MeshMaterial::calculateBoundingBox(Vector3 *t_vertices, u32 t_vertCount) { - Vector3 boundingBox[8]; float lowX, lowY, lowZ, hiX, hiY, hiZ; lowX = hiX = t_vertices[vertexFaces[0]].x; lowY = hiY = t_vertices[vertexFaces[0]].y; @@ -124,6 +128,8 @@ void MeshMaterial::calculateBoundingBox(Vector3 *t_vertices, u32 t_vertCount) if (hiZ < t_vertices[vertexFaces[i]].z) hiZ = t_vertices[vertexFaces[i]].z; } + + Vector3 boundingBox[8]; boundingBox[0].set(lowX, lowY, lowZ); boundingBox[1].set(lowX, lowY, hiZ); boundingBox[2].set(lowX, hiY, lowZ); @@ -139,3 +145,30 @@ void MeshMaterial::calculateBoundingBox(Vector3 *t_vertices, u32 t_vertCount) //constructor instantiated BoundingBox objects. boundingBoxObj = new BoundingBox(boundingBox); } + +void MeshMaterial::copyFrom(MeshMaterial *t_refCopy) +{ + _isNameSet = true; + name = t_refCopy->name; + + _isBoundingBoxCalculated = true; + boundingBoxObj = t_refCopy->boundingBoxObj; + + vertexFaces = t_refCopy->vertexFaces; + stFaces = t_refCopy->stFaces; + normalFaces = t_refCopy->normalFaces; + facesCount = t_refCopy->facesCount; + _areFacesAllocated = true; + + _isMother = false; +} + +/** Set's default object color + no transparency */ +void MeshMaterial::setDefaultColor() +{ + color.r = 0x80; + color.g = 0x80; + color.b = 0x80; + color.a = 0x80; + color.q = 1.0F; +} diff --git a/src/engine/models/texture.cpp b/src/engine/models/texture.cpp index 8bfb78d..1c247b3 100644 --- a/src/engine/models/texture.cpp +++ b/src/engine/models/texture.cpp @@ -82,18 +82,9 @@ void Texture::setWrapSettings(const WrapSettings t_horizontal, const WrapSetting wrapSettings.vertical = t_vertical; } -void Texture::addLink(const u32 &t_meshId, const u32 &t_materialId) +void Texture::addLink(const u32 &t_id) { TextureLink link; - link.materialId = t_materialId; - link.meshOrSpriteId = t_meshId; - texLinks.push_back(link); -} - -void Texture::addLink(const u32 &t_spriteId) -{ - TextureLink link; - link.materialId = 0; - link.meshOrSpriteId = t_spriteId; + link.id = t_id; texLinks.push_back(link); } diff --git a/src/engine/modules/gif_sender.cpp b/src/engine/modules/gif_sender.cpp index ab55fc1..cf0eac5 100644 --- a/src/engine/modules/gif_sender.cpp +++ b/src/engine/modules/gif_sender.cpp @@ -131,16 +131,16 @@ void GifSender::addClear(zbuffer_t *t_zBuffer, color_t *t_rgb) } /** Adds meshes to current packet */ -void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer) +void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer, color_t *t_color) { packet2_chain_open_cnt(currentPacket, 0, 0, 0); packet2_update(currentPacket, draw_texture_sampling(currentPacket->next, 0, &t_mesh.lod)); packet2_update(currentPacket, draw_texturebuffer(currentPacket->next, 0, textureBuffer, &t_mesh.clut)); - packet2_update(currentPacket, draw_prim_start(currentPacket->next, 0, t_renderData->prim, &t_mesh.color)); + packet2_update(currentPacket, draw_prim_start(currentPacket->next, 0, t_renderData->prim, t_color)); xyz = new xyz_t[vertexCount]; rgbaq = new color_t[vertexCount]; st = new texel_t[vertexCount]; - calc3DObject(*t_renderData->projection, t_mesh, vertexCount, vertices, normals, coordinates, t_renderData, t_bulbs, t_bulbsCount); + calc3DObject(*t_renderData->projection, t_mesh, vertexCount, vertices, normals, coordinates, t_renderData, t_bulbs, t_bulbsCount, t_color); addCurrentCalcs(vertexCount); delete[] xyz; delete[] rgbaq; @@ -152,7 +152,7 @@ void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCoun /** Calculates 3D object data into xyz, rgbq, st * After it addCurrentCalcs() can be done */ -void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, RenderData *t_renderData, LightBulb *t_bulbs, u16 t_bulbsCount) +void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, RenderData *t_renderData, LightBulb *t_bulbs, u16 t_bulbsCount, color_t *t_color) { VECTOR *colors = new VECTOR[vertexCount]; VECTOR position, rotation; @@ -187,9 +187,9 @@ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount for (u32 i = 0; i < vertexCount; i++) { // Apply the light value to the colour. - colors[i][0] = (t_mesh.color.r * lights[i][0]); - colors[i][1] = (t_mesh.color.g * lights[i][1]); - colors[i][2] = (t_mesh.color.b * lights[i][2]); + colors[i][0] = (t_color->r * lights[i][0]); + colors[i][1] = (t_color->g * lights[i][1]); + colors[i][2] = (t_color->b * lights[i][2]); vector_clamp(colors[i], colors[i], 0.00F, 1.99F); } @@ -201,14 +201,14 @@ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount else for (u32 i = 0; i < vertexCount; i++) { - colors[i][0] = t_mesh.color.r / 128.0F; - colors[i][1] = t_mesh.color.g / 128.0F; - colors[i][2] = t_mesh.color.b / 128.0F; + colors[i][0] = t_color->r / 128.0F; + colors[i][1] = t_color->g / 128.0F; + colors[i][2] = t_color->b / 128.0F; } calculate_vertices(vertices, vertexCount, vertices, localScreen); - convertCalcs(vertexCount, vertices, colors, coordinates, t_mesh.color); + convertCalcs(vertexCount, vertices, colors, coordinates, *t_color); delete[] colors; } diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index b76c8be..635b611 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -105,7 +105,7 @@ void Renderer::changeTexture(Texture *t_tex) void Renderer::draw(Sprite &t_sprite) { - Texture *texture = textureRepo.getBySprite(t_sprite.getId()); + Texture *texture = textureRepo.getBySpriteOrMesh(t_sprite.getId()); texrect_t rect; float sizeX, sizeY; if (t_sprite.getMode() == MODE_REPEAT) @@ -242,17 +242,18 @@ void Renderer::drawByPath3(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) t_mesh.animate(); for (u32 i = 0; i < t_mesh.getMaterialsCount(); i++) { - if (t_mesh.shouldBeFrustumCulled && !t_mesh.getMaterial(i).isInFrustum(renderData.frustumPlanes, t_mesh.position)) + MeshMaterial *material = &t_mesh.getMaterial(i); + if (t_mesh.shouldBeFrustumCulled && !material->isInFrustum(renderData.frustumPlanes, t_mesh.position)) return; - Texture *tex = textureRepo.getByMesh(t_mesh.getId(), t_mesh.getMaterial(i).getId()); + Texture *tex = textureRepo.getBySpriteOrMesh(material->getId()); changeTexture(tex); gifSender->initPacket(context); - u32 vertCount = t_mesh.getMaterial(i).getFacesCount(); + u32 vertCount = material->getFacesCount(); VECTOR *vertices = new VECTOR[vertCount]; VECTOR *normals = new VECTOR[vertCount]; VECTOR *coordinates = new VECTOR[vertCount]; vertCount = t_mesh.getDrawData(i, vertices, normals, coordinates, *renderData.cameraPosition); - gifSender->addObject(&renderData, t_mesh, vertCount, vertices, normals, coordinates, t_bulbs, t_bulbsCount, &textureBuffer); + gifSender->addObject(&renderData, t_mesh, vertCount, vertices, normals, coordinates, t_bulbs, t_bulbsCount, &textureBuffer, &material->color); gifSender->sendPacket(); delete[] vertices; delete[] normals; @@ -315,16 +316,17 @@ void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) t_mesh.animate(); for (u32 i = 0; i < t_mesh.getMaterialsCount(); i++) { - if (t_mesh.shouldBeFrustumCulled && !t_mesh.getMaterial(i).isInFrustum(renderData.frustumPlanes, t_mesh.position)) + MeshMaterial *material = &t_mesh.getMaterial(i); + if (t_mesh.shouldBeFrustumCulled && !material->isInFrustum(renderData.frustumPlanes, t_mesh.position)) return; - u32 vertCount = t_mesh.getMaterial(i).getFacesCount(); + u32 vertCount = material->getFacesCount(); VECTOR vertices[vertCount] __attribute__((aligned(16))); VECTOR normals[vertCount] __attribute__((aligned(16))); VECTOR coordinates[vertCount] __attribute__((aligned(16))); - Texture *tex = textureRepo.getByMesh(t_mesh.getId(), t_mesh.getMaterial(i).getId()); + Texture *tex = textureRepo.getBySpriteOrMesh(material->getId()); changeTexture(tex); vertCount = t_mesh.getDrawData(i, vertices, normals, coordinates, rotatedCamera); - vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount, &textureBuffer); + vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount, &textureBuffer, &material->color); } } diff --git a/src/engine/modules/texture_repository.cpp b/src/engine/modules/texture_repository.cpp index 44112be..2b4f816 100644 --- a/src/engine/modules/texture_repository.cpp +++ b/src/engine/modules/texture_repository.cpp @@ -57,7 +57,7 @@ void TextureRepository::addByMesh(char *t_path, Mesh &mesh, TextureFormat t_form else pngLoader.load(*texture, t_path, mesh.getMaterial(i).getName(), ".png"); texture->setName(mesh.getMaterial(i).getName()); - texture->addLink(mesh.getId(), mesh.getMaterial(i).getId()); + texture->addLink(mesh.getMaterial(i).getId()); textures.push_back(texture); } } diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 394c653..88f88f5 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -83,7 +83,7 @@ void VifSender::calcMatrix(const RenderData &t_renderData, const Vector3 &t_posi modelViewProj = *t_renderData.projection * modelViewProj; } -void VifSender::drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 vertCount2, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer) +void VifSender::drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 vertCount2, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer, color_t *t_color) { // we have to split 3D object into small parts, because of small memory of VU1 @@ -97,7 +97,7 @@ void VifSender::drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 ver i -= 3; const u32 endI = i + (VU1_PACKAGE_VERTS_PER_BUFF - 1) > vertCount2 ? vertCount2 : i + (VU1_PACKAGE_VERTS_PER_BUFF - 1); - drawVertices(t_mesh, i, endI, vertices, coordinates, t_renderData->prim, textureBuffer, isDrawWaitEnabled ? endI == vertCount2 : false); + drawVertices(t_mesh, i, endI, vertices, coordinates, t_renderData->prim, textureBuffer, isDrawWaitEnabled ? endI == vertCount2 : false, t_color); if (endI == vertCount2) // if there are no more vertices to draw, break { i = vertCount2; @@ -130,7 +130,7 @@ void VifSender::setDoubleBufferAddStaticData() } /** Draw using PATH1 */ -void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *t_texBuff, u8 t_addDrawWait) +void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *t_texBuff, u8 t_addDrawWait, color_t *t_color) { const u32 vertCount = t_end - t_start; lastVertCount = vertCount; @@ -143,10 +143,10 @@ void VifSender::drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_ver packet2_utils_gs_add_lod(currPacket, &t_mesh.lod); packet2_utils_gs_add_texbuff_clut(currPacket, t_texBuff, &t_mesh.clut); packet2_utils_gs_add_prim_giftag(currPacket, t_prim, vertCount, DRAW_STQ2_REGLIST, 3, 0); - packet2_add_u32(currPacket, t_mesh.color.r); - packet2_add_u32(currPacket, t_mesh.color.g); - packet2_add_u32(currPacket, t_mesh.color.b); - packet2_add_u32(currPacket, t_mesh.color.a); + packet2_add_u32(currPacket, t_color->r); + packet2_add_u32(currPacket, t_color->g); + packet2_add_u32(currPacket, t_color->b); + packet2_add_u32(currPacket, t_color->a); u32 vif_added_bytes = packet2_utils_vu_close_unpack(currPacket); packet2_utils_vu_add_unpack_data(currPacket, vif_added_bytes, t_vertices + t_start, vertCount, true); vif_added_bytes += vertCount; @@ -183,10 +183,10 @@ void VifSender::drawTheSameWithOtherMatrices(const RenderData &t_renderData, Mes packet2_utils_vu_close_unpack(currMPacket); packet2_utils_vu_open_unpack(currMPacket, VU1_RGBA_ADDRESS, true); { - packet2_add_u32(currMPacket, t_meshes[i]->color.r); - packet2_add_u32(currMPacket, t_meshes[i]->color.g); - packet2_add_u32(currMPacket, t_meshes[i]->color.b); - packet2_add_u32(currMPacket, t_meshes[i]->color.a); + packet2_add_u32(currMPacket, t_meshes[i]->getMaterial(0).color.r); + packet2_add_u32(currMPacket, t_meshes[i]->getMaterial(0).color.g); + packet2_add_u32(currMPacket, t_meshes[i]->getMaterial(0).color.b); + packet2_add_u32(currMPacket, t_meshes[i]->getMaterial(0).color.a); } packet2_utils_vu_close_unpack(currMPacket); diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index f016115..be6f6a8 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -88,8 +88,7 @@ void Dolphin::onInit() water[i].shouldBeFrustumCulled = true; water[i].shouldBeBackfaceCulled = false; water[i].position.set(WATER_TILE_SCALE * 2.0F * spirals[i].x, WATER_LEVEL, WATER_TILE_SCALE * 2.0F * spirals[i].y); - texRepo->getByMesh(water[0].getId(), water[0].getMaterial(0).getId()) - ->addLink(water[i].getId(), water[i].getMaterial(0).getId()); + texRepo->getBySpriteOrMesh(water[0].getMaterial(0).getId())->addLink(water[i].getMaterial(0).getId()); waterDrawList[i] = &water[i]; } @@ -123,8 +122,7 @@ void Dolphin::onInit() oysters[i].mesh.loadFrom(oysters[0].mesh); oysters[i].mesh.shouldBeBackfaceCulled = false; oysters[i].mesh.shouldBeFrustumCulled = false; - texRepo->getByMesh(oysters[0].mesh.getId(), oysters[0].mesh.getMaterial(0).getId()) - ->addLink(oysters[i].mesh.getId(), oysters[i].mesh.getMaterial(0).getId()); + texRepo->getBySpriteOrMesh(oysters[0].mesh.getMaterial(0).getId())->addLink(oysters[i].mesh.getMaterial(0).getId()); oysters[i].mesh.position.set(i * -100, 5.0F, i * -100); } @@ -141,8 +139,7 @@ void Dolphin::onInit() mines[i].mesh.loadFrom(mines[0].mesh); mines[i].mesh.shouldBeBackfaceCulled = false; mines[i].mesh.shouldBeFrustumCulled = false; - texRepo->getByMesh(mines[0].mesh.getId(), mines[0].mesh.getMaterial(0).getId()) - ->addLink(mines[i].mesh.getId(), mines[i].mesh.getMaterial(0).getId()); + texRepo->getBySpriteOrMesh(mines[0].mesh.getMaterial(0).getId())->addLink(mines[i].mesh.getMaterial(0).getId()); mines[i].mesh.position.set(i * -10, 0.0F, i * -68); } diff --git a/src/samples/floors/managers/floor_manager.cpp b/src/samples/floors/managers/floor_manager.cpp index 2662527..d5f29ae 100644 --- a/src/samples/floors/managers/floor_manager.cpp +++ b/src/samples/floors/managers/floor_manager.cpp @@ -91,8 +91,7 @@ void FloorManager::initFloors() { floors[i].mesh.shouldBeFrustumCulled = true; floors[i].init(floors[0].mesh, spirals[i], i); - texRepo->getByMesh(floors[0].mesh.getId(), floors[0].mesh.getMaterial(0).getId()) - ->addLink(floors[i].mesh.getId(), floors[i].mesh.getMaterial(0).getId()); + texRepo->getBySpriteOrMesh(floors[0].mesh.getMaterial(0).getId())->addLink(floors[i].mesh.getMaterial(0).getId()); meshes[i] = &floors[i].mesh; } PRINT_LOG("Floors initialized!"); @@ -123,18 +122,18 @@ void FloorManager::update(Player &t_player) for (u16 i = 0; i < floorAmount; i++) { floors[i].animate(t_player); - + MeshMaterial *material = &floors[i].mesh.getMaterial(0); if (floors[i].isByAudioTriggered) { - floors[i].mesh.color.r = trigColor.r; - floors[i].mesh.color.g = trigColor.g; - floors[i].mesh.color.b = trigColor.b; + material->color.r = trigColor.r; + material->color.g = trigColor.g; + material->color.b = trigColor.b; } else { - floors[i].mesh.color.r = defaultColor.r; - floors[i].mesh.color.g = defaultColor.g; - floors[i].mesh.color.b = defaultColor.b; + material->color.r = defaultColor.r; + material->color.g = defaultColor.g; + material->color.b = defaultColor.b; } } } diff --git a/src/samples/floors/ui.cpp b/src/samples/floors/ui.cpp index 52c43ff..d398cd9 100644 --- a/src/samples/floors/ui.cpp +++ b/src/samples/floors/ui.cpp @@ -66,13 +66,13 @@ void Ui::update(const Player &player) if (!isTask1Done && player.getJumpCount() >= 10) { isTask1Done = true; - grayCheckboxTex->removeLinkBySprite(task1Checkbox.getId()); + grayCheckboxTex->removeLinkById(task1Checkbox.getId()); blueCheckboxTex->addLink(task1Checkbox.getId()); } if (!isTask2Done && player.getKilledEnemiesCount() >= 10) { isTask2Done = true; - grayCheckboxTex->removeLinkBySprite(task2Checkbox.getId()); + grayCheckboxTex->removeLinkById(task2Checkbox.getId()); blueCheckboxTex->addLink(task2Checkbox.getId()); } }