From 8501a85f490a2298281e9652fa8a63b0240ed091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Sobczy=C5=84ski?= Date: Thu, 29 Oct 2020 21:53:08 +0100 Subject: [PATCH] moved texturebuffer outside of spec --- src/engine/include/loaders/obj_loader.hpp | 5 +- src/engine/include/models/math/point.hpp | 6 +- src/engine/include/models/math/vector3.hpp | 2 +- src/engine/include/models/mesh_frame.hpp | 86 +++++++++++++++++++++ src/engine/include/models/mesh_material.hpp | 73 +++++++++++++---- src/engine/include/models/mesh_spec.hpp | 5 +- src/engine/include/models/obj_model.hpp | 26 +------ src/engine/include/modules/gif_sender.hpp | 2 +- src/engine/include/modules/renderer.hpp | 6 +- src/engine/include/modules/vif_sender.hpp | 4 +- src/engine/include/utils/debug.hpp | 2 +- src/engine/loaders/obj_loader.cpp | 65 ++++++++-------- src/engine/models/math/point.cpp | 25 ++++-- src/engine/models/math/vector3.cpp | 8 +- src/engine/models/mesh.cpp | 17 ++-- src/engine/models/mesh_frame.cpp | 72 ++++++++++++++++- src/engine/models/mesh_material.cpp | 25 +++--- src/engine/models/mesh_spec.cpp | 27 ------- src/engine/models/obj_model.cpp | 20 ++--- src/engine/modules/gif_sender.cpp | 4 +- src/engine/modules/renderer.cpp | 43 ++++++++--- src/engine/modules/vif_sender.cpp | 20 ++--- 22 files changed, 363 insertions(+), 180 deletions(-) diff --git a/src/engine/include/loaders/obj_loader.hpp b/src/engine/include/loaders/obj_loader.hpp index 76841cf..410ffe4 100644 --- a/src/engine/include/loaders/obj_loader.hpp +++ b/src/engine/include/loaders/obj_loader.hpp @@ -12,6 +12,7 @@ #define _TYRA_OBJ_LOADER_ #include "../models/obj_model.hpp" +#include "../models/mesh_frame.hpp" #include /** Class responsible for loading&parsing .obj 3D files */ @@ -22,10 +23,10 @@ public: ObjLoader(); ~ObjLoader(); - void load(Frame *o_result, char *t_fileName, float t_scale); + void load(MeshFrame *o_result, char *t_fileName, float t_scale, u8 invertT); private: - void allocateObjMemory(FILE *t_file, Frame *t_result); + void allocateObjMemory(FILE *t_file, MeshFrame *t_result); }; #endif diff --git a/src/engine/include/models/math/point.hpp b/src/engine/include/models/math/point.hpp index e993ae2..097ad23 100644 --- a/src/engine/include/models/math/point.hpp +++ b/src/engine/include/models/math/point.hpp @@ -26,11 +26,13 @@ public: float xy[2] __attribute__((__aligned__(16))); }; - Point(float x, float y); + Point(float t_x, float t_y); + Point(const Point &v); Point(); ~Point(); - void set(float x, float y); + void set(float t_x, float t_y); + void set(const Point &v); void print(); }; diff --git a/src/engine/include/models/math/vector3.hpp b/src/engine/include/models/math/vector3.hpp index b8996ba..9351ad9 100644 --- a/src/engine/include/models/math/vector3.hpp +++ b/src/engine/include/models/math/vector3.hpp @@ -31,7 +31,7 @@ public: float xyz[3] __attribute__((__aligned__(16))); }; - Vector3(float x, float y, float z); + Vector3(float t_x, float t_y, float t_z); Vector3(const Vector3 &v); Vector3(); ~Vector3(); diff --git a/src/engine/include/models/mesh_frame.hpp b/src/engine/include/models/mesh_frame.hpp index f8d25f2..a5c0afa 100644 --- a/src/engine/include/models/mesh_frame.hpp +++ b/src/engine/include/models/mesh_frame.hpp @@ -12,6 +12,9 @@ #define _TYRA_MESH_FRAME_ #include +#include "math/point.hpp" +#include "math/vector3.hpp" +#include "./mesh_material.hpp" class MeshFrame { @@ -20,7 +23,90 @@ public: MeshFrame(); ~MeshFrame(); + // ---- + // Getters + // ---- + + const u32 &getVertexCount() const { return vertexCount; }; + const u32 &getSTsCount() const { return stsCount; }; + const u32 &getNormalsCount() const { return normalsCount; }; + const u32 &getMaterialsCount() const { return materialsCount; }; + + /** Returns vertex. 3 vertices = 1 triangle. */ + Vector3 &getVertex(const u32 &i) const { return vertices[i]; }; + + /** Returns texture coordinate. */ + Point &getST(const u32 &i) const { return sts[i]; }; + + /** Returns normal vector. Used for lighting. */ + Vector3 &getNormal(const u32 &i) const { return normals[i]; }; + + /** Returns material, which is a mesh "subgroup". */ + MeshMaterial &getMaterial(const u32 &i) const { return materials[i]; }; + + /** Array of vertices. Size of getVertexCount() */ + Vector3 *getVertices() const { return vertices; }; + + /** Array of vertices. Size of getSTsCount() */ + Point *getSTs() const { return sts; }; + + /** Array of vertices. Size of getNormalsCount() */ + Vector3 *getNormals() const { return normals; }; + + /** Array of materials. Size of getMaterialsCount() */ + MeshMaterial *getMaterials() const { return materials; }; + + // ---- + // Setters + // ---- + + /** + * Do not call this method unless you know what you do. + * Should be called by data loader. + */ + void setVertex(const u32 &t_index, const Vector3 &t_val) { vertices[t_index].set(t_val); } + + /** + * Do not call this method unless you know what you do. + * Should be called by data loader. + */ + void setNormal(const u32 &t_index, const Vector3 &t_val) { normals[t_index].set(t_val); } + + /** + * Do not call this method unless you know what you do. + * Should be called by data loader. + */ + void setST(const u32 &t_index, const Point &t_val) { sts[t_index].set(t_val); } + + // ---- + // Other + // ---- + + const u8 &areSTsAllocated() const { return _areSTsAllocated; }; + const u8 &areVerticesAllocated() const { return _areVerticesAllocated; }; + const u8 &areNormalsAllocated() const { return _areNormalsAllocated; }; + const u8 &areMaterialsAllocated() const { return _areMaterialsAllocated; }; + + /** Set STs count and allocate memory. */ + void allocateSTs(const u32 &t_val); + + /** Set vertex count and allocate memory. */ + void allocateVertices(const u32 &t_val); + + /** Set normals count and allocate memory. */ + void allocateNormals(const u32 &t_val); + + /** Set materials count and allocate memory. */ + void allocateMaterials(const u32 &t_val); + private: + u8 _areSTsAllocated, _areVerticesAllocated, _areNormalsAllocated, _areMaterialsAllocated; + u32 vertexCount, stsCount, normalsCount, materialsCount; + MeshMaterial *materials; + Point *sts __attribute__((aligned(16))); + Vector3 + *vertices __attribute__((aligned(16))), + *normals __attribute__((aligned(16))); }; #endif diff --git a/src/engine/include/models/mesh_material.hpp b/src/engine/include/models/mesh_material.hpp index a511b39..027292c 100644 --- a/src/engine/include/models/mesh_material.hpp +++ b/src/engine/include/models/mesh_material.hpp @@ -13,6 +13,11 @@ #include +/** Class which contains draw data for mesh part. + * Mesh can have many materials. + * For example, car can have three materials: + * body, tires and windows. + */ class MeshMaterial { @@ -20,44 +25,78 @@ public: MeshMaterial(); ~MeshMaterial(); + // ---- + // Getters + // ---- + /** Material name. */ char *getName() const { return name; }; + + /** + * Auto generated unique Id. + * Core role of this variable is to select correct texture to draw + */ + const u32 &getId() const { return id; }; + const u32 &getFacesCount() const { return facesCount; }; + /** Indexes of vertices. Each 3 faces will give you vertices of next triangle. */ - const u32 &getVertexFace(const u32 &i) const { return vertexFaces[i]; }; + u32 &getVertexFace(const u32 &i) const { return vertexFaces[i]; }; + /** Indexes of texture coords. Each 3 faces will give you texture coords of next triangle. */ - const u32 &getStFace(const u32 &i) const { return stFaces[i]; }; + u32 &getSTFace(const u32 &i) const { return stFaces[i]; }; + /** Indexes of normal vectors used for lighting. Each 3 faces will give you normal vectors of next triangle. */ - const u32 &getNormalFace(const u32 &i) const { return normalFaces[i]; }; + u32 &getNormalFace(const u32 &i) const { return normalFaces[i]; }; + /** Array of vertex faces. Size of getFacesCount() */ - const u32 *getVertexFaces() const { return vertexFaces; }; + u32 *getVertexFaces() const { return vertexFaces; }; + /** Array of texture coords faces. Size of getFacesCount() */ - const u32 *getStFaces() const { return stFaces; }; + u32 *getSTFaces() const { return stFaces; }; + /** Array of normal vector faces. Size of getFacesCount() */ - const u32 *getNormalFaces() const { return normalFaces; }; + u32 *getNormalFaces() const { return normalFaces; }; - /** Set faces count and allocate faces memory. */ - void setFacesCount(const u32 &t_val); + // ---- + // Setters + // ---- - /** Set vertex face. Do not call this method until you know what you do. - * Should be called by data loader. */ + /** + * Do not call this method unless you know what you do. + * Should be called by data loader. + */ void setVertexFace(const u32 &t_index, const u32 &t_val) { vertexFaces[t_index] = t_val; } - /** Set st face. Do not call this method until you know what you do. - * Should be called by data loader. */ - void setStFace(const u32 &t_index, const u32 &t_val) { stFaces[t_index] = t_val; } + /** + * Do not call this method unless you know what you do. + * Should be called by data loader. + */ + void setSTFace(const u32 &t_index, const u32 &t_val) { stFaces[t_index] = t_val; } - /** Set vertex face. Do not call this method until you know what you do. - * Should be called by data loader. */ + /** + * Do not call this method unless you know what you do. + * Should be called by data loader. + */ void setNormalFace(const u32 &t_index, const u32 &t_val) { normalFaces[t_index] = t_val; } /** Set material name. */ void setName(char *t_val); + // ---- + // Other + // ---- + + const u8 &isNameSet() const { return _isNameSet; }; + const u8 &areFacesAllocated() const { return _areFacesAllocated; }; + + /** Set faces count and allocate memory. */ + void allocateFaces(const u32 &t_val); + private: - u32 facesCount; + u32 facesCount, id; u32 *vertexFaces, *stFaces, *normalFaces; - u8 isNameAllocated, areFacesAllocated; + u8 _isNameSet, _areFacesAllocated; char *name; }; diff --git a/src/engine/include/models/mesh_spec.hpp b/src/engine/include/models/mesh_spec.hpp index 90debac..514d2f4 100644 --- a/src/engine/include/models/mesh_spec.hpp +++ b/src/engine/include/models/mesh_spec.hpp @@ -22,20 +22,17 @@ class MeshSpec { public: - texbuffer_t textureBuffer; clutbuffer_t clut; lod_t lod; MeshSpec(); ~MeshSpec(); - void allocateTextureBuffer(u16 t_width, u16 t_height); - void deallocateTextureBuffer(); + void allocateMemory(); void setupLodAndClut(); Texture *textures; private: - u8 isTextureVRAMAllocated; }; #endif diff --git a/src/engine/include/models/obj_model.hpp b/src/engine/include/models/obj_model.hpp index cafeef2..c4acd58 100644 --- a/src/engine/include/models/obj_model.hpp +++ b/src/engine/include/models/obj_model.hpp @@ -17,29 +17,7 @@ #include "utils/debug.hpp" #include "./anim_state.hpp" #include "./mesh_material.hpp" - -class Frame2 -{ -public: - Frame2() { printf("Frame2 constructor\n"); }; - ~Frame2() { printf("Frame2 destructor\n"); }; - u16 number; - u32 verticesCount, coordinatesCount, normalsCount, materialsCount; - Vector3 *vertices __attribute__((aligned(16))), - *coordinates __attribute__((aligned(16))), - *normals __attribute__((aligned(16))); - MeshMaterial *materials; -}; - -struct Frame -{ - u16 number; - u32 verticesCount, coordinatesCount, normalsCount, materialsCount; - Vector3 *vertices __attribute__((aligned(16))), - *coordinates __attribute__((aligned(16))), - *normals __attribute__((aligned(16))); - MeshMaterial *materials; -}; +#include "./mesh_frame.hpp" /** Class which have common types for all 3D objects */ class ObjModel @@ -49,7 +27,7 @@ public: /** File name without extension */ char *filename; u16 frameCount; - Frame *frames; + MeshFrame *frames; AnimState animState; ObjModel(char *t_objFile); diff --git a/src/engine/include/modules/gif_sender.hpp b/src/engine/include/modules/gif_sender.hpp index a154b16..5350336 100644 --- a/src/engine/include/modules/gif_sender.hpp +++ b/src/engine/include/modules/gif_sender.hpp @@ -32,7 +32,7 @@ public: ~GifSender(); void initPacket(u8 context); - void addObjects(RenderData *t_renderData, Mesh **t_objects3D, u32 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount); + void addObjects(RenderData *t_renderData, Mesh **t_objects3D, u32 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer); void addClear(zbuffer_t *t_zBuffer); void sendPacket(); void sendClear(zbuffer_t *t_zBuffer); diff --git a/src/engine/include/modules/renderer.hpp b/src/engine/include/modules/renderer.hpp index a120c4e..be059c4 100644 --- a/src/engine/include/modules/renderer.hpp +++ b/src/engine/include/modules/renderer.hpp @@ -56,13 +56,15 @@ public: void endFrame(float fps); private: + void allocateTextureBuffer(u16 t_width, u16 t_height); + void deallocateTextureBuffer(); void changeTexture(Mesh *t_mesh, u8 t_textureIndex); void flipBuffers(); void beginFrameIfNeeded(); - u8 isFrameEmpty; + u8 isFrameEmpty, isTextureVRAMAllocated; Matrix perspective; RenderData renderData; - + texbuffer_t textureBuffer; u32 lastTextureId; GifSender *gifSender; VifSender *vifSender; diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index b03cf7d..59dbd57 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -28,10 +28,10 @@ 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); + 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); private: - void drawVertices(Mesh *t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim); + void drawVertices(Mesh *t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *textureBuffer); MATRIX localWorld, localScreen; VECTOR position, rotation; diff --git a/src/engine/include/utils/debug.hpp b/src/engine/include/utils/debug.hpp index 2f7bc61..106d75a 100644 --- a/src/engine/include/utils/debug.hpp +++ b/src/engine/include/utils/debug.hpp @@ -16,6 +16,6 @@ #include #define PRINT_LOG(TEXT) printf("LOG: " TEXT " (" __FILE__ ")\n") -#define PRINT_ERR(TEXT) printf("---\n--- ---\n--- --- --- ERR: " TEXT " (" __FILE__ ")\n--- ---\n---\n") +#define PRINT_ERR(TEXT) printf("\n====================================\n| ERROR: " TEXT "\n| File : " __FILE__ "\n====================================\n\n") #endif diff --git a/src/engine/loaders/obj_loader.cpp b/src/engine/loaders/obj_loader.cpp index 89fb317..1e46608 100644 --- a/src/engine/loaders/obj_loader.cpp +++ b/src/engine/loaders/obj_loader.cpp @@ -30,7 +30,7 @@ ObjLoader::~ObjLoader() {} * Notice: At this moment textures names are MATERIAL names from .obj file! * Notice 2: Faces MUST be triangulated (check out blender export settings). */ -void ObjLoader::load(Frame *o_result, char *t_filename, float t_scale) +void ObjLoader::load(MeshFrame *o_result, char *t_filename, float t_scale, u8 invertT) { FILE *file = fopen(t_filename, "rb"); if (file == NULL) @@ -49,25 +49,27 @@ void ObjLoader::load(Frame *o_result, char *t_filename, float t_scale) { Vector3 vector = Vector3(); fscanf(file, "%f %f %f\n", &vector.x, &vector.y, &vector.z); - o_result->vertices[verticesI++] = vector * t_scale; + o_result->setVertex(verticesI++, vector * t_scale); } else if (strcmp(lineHeader, "vt") == 0) { - Vector3 vector = Vector3(); - fscanf(file, "%f %f\n", &vector.x, &vector.y); - o_result->coordinates[cordsI++] = vector; + Point point = Point(); + fscanf(file, "%f %f\n", &point.x, &point.y); + if (invertT) + point.y = 1.0F - point.y; + o_result->setST(cordsI++, point); } else if (strcmp(lineHeader, "vn") == 0) { Vector3 vector = Vector3(); fscanf(file, "%f %f %f\n", &vector.x, &vector.y, &vector.z); - o_result->normals[normalsI++] = vector; + o_result->setNormal(normalsI++, vector); } else if (strcmp(lineHeader, "usemtl") == 0) { char temp[30]; fscanf(file, "%s\n", temp); - o_result->materials[++materialsI].setName(temp); + o_result->getMaterial(++materialsI).setName(temp); faceI = 0; } else if (strcmp(lineHeader, "f") == 0) @@ -80,17 +82,17 @@ void ObjLoader::load(Frame *o_result, char *t_filename, float t_scale) PRINT_ERR(".obj can't be read by this simple parser. Try exporting with other options"); else { - o_result->materials[materialsI].setVertexFace(faceI, vertexIndex[0] - 1); - o_result->materials[materialsI].setVertexFace(faceI + 1, vertexIndex[1] - 1); - o_result->materials[materialsI].setVertexFace(faceI + 2, vertexIndex[2] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI, vertexIndex[0] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 1, vertexIndex[1] - 1); + o_result->getMaterial(materialsI).setVertexFace(faceI + 2, vertexIndex[2] - 1); - o_result->materials[materialsI].setStFace(faceI, coordIndex[0] - 1); - o_result->materials[materialsI].setStFace(faceI + 1, coordIndex[1] - 1); - o_result->materials[materialsI].setStFace(faceI + 2, coordIndex[2] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI, coordIndex[0] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI + 1, coordIndex[1] - 1); + o_result->getMaterial(materialsI).setSTFace(faceI + 2, coordIndex[2] - 1); - o_result->materials[materialsI].setNormalFace(faceI, normalIndex[0] - 1); - o_result->materials[materialsI].setNormalFace(faceI + 1, normalIndex[1] - 1); - o_result->materials[materialsI].setNormalFace(faceI + 2, normalIndex[2] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI, normalIndex[0] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI + 1, normalIndex[1] - 1); + o_result->getMaterial(materialsI).setNormalFace(faceI + 2, normalIndex[2] - 1); faceI += 3; } } @@ -102,12 +104,12 @@ void ObjLoader::load(Frame *o_result, char *t_filename, float t_scale) } /** Calculate how many vertices(v), coordinates(vt), normals(vn) and faces(f) have .obj file */ -void ObjLoader::allocateObjMemory(FILE *t_file, Frame *o_result) +void ObjLoader::allocateObjMemory(FILE *t_file, MeshFrame *o_result) { - o_result->verticesCount = 0; - o_result->coordinatesCount = 0; - o_result->normalsCount = 0; - o_result->materialsCount = 0; + u32 vertexCount = 0; + u32 stsCount = 0; + u32 normalsCount = 0; + u32 materialsCount = 0; while (1) { @@ -116,19 +118,23 @@ void ObjLoader::allocateObjMemory(FILE *t_file, Frame *o_result) if (res != EOF) { if (strcmp(lineHeader, "v") == 0) - o_result->verticesCount += 1; + vertexCount += 1; else if (strcmp(lineHeader, "vt") == 0) - o_result->coordinatesCount += 1; + stsCount += 1; else if (strcmp(lineHeader, "vn") == 0) - o_result->normalsCount += 1; + normalsCount += 1; else if (strcmp(lineHeader, "usemtl") == 0) - o_result->materialsCount += 1; + materialsCount += 1; } else break; } - o_result->materials = new MeshMaterial[o_result->materialsCount]; + o_result->allocateVertices(vertexCount); + o_result->allocateNormals(normalsCount); + o_result->allocateSTs(stsCount); + o_result->allocateMaterials(materialsCount); + s16 currentMatI = -1; fseek(t_file, 0, SEEK_SET); @@ -142,7 +148,7 @@ void ObjLoader::allocateObjMemory(FILE *t_file, Frame *o_result) if (strcmp(lineHeader, "usemtl") == 0) { if (currentMatI >= 0) // Skip -1 - o_result->materials[currentMatI].setFacesCount(facesCounter); + o_result->getMaterial(currentMatI).allocateFaces(facesCounter); currentMatI++; } else if (strcmp(lineHeader, "f") == 0) @@ -152,10 +158,7 @@ void ObjLoader::allocateObjMemory(FILE *t_file, Frame *o_result) break; } - o_result->materials[currentMatI].setFacesCount(facesCounter); // Allocate last one + o_result->getMaterial(currentMatI).allocateFaces(facesCounter); // Allocate last one - o_result->vertices = new Vector3[o_result->verticesCount]; - o_result->coordinates = new Vector3[o_result->coordinatesCount]; - o_result->normals = new Vector3[o_result->normalsCount]; // o_result->isMemoryAllocated = true; } diff --git a/src/engine/models/math/point.cpp b/src/engine/models/math/point.cpp index 9c76468..230ec99 100644 --- a/src/engine/models/math/point.cpp +++ b/src/engine/models/math/point.cpp @@ -17,10 +17,17 @@ // ---- /** Create by specifying values */ -Point::Point(float x, float y) +Point::Point(float t_x, float t_y) { - this->x = x; - this->y = y; + x = t_x; + y = t_y; +} + +/** Create with other point values */ +Point::Point(const Point &v) +{ + x = v.x; + y = v.y; } /** Create empty point */ @@ -36,10 +43,16 @@ Point::~Point() {} // Methods // ---- -void Point::set(float x, float y) +void Point::set(float t_x, float t_y) { - this->x = x; - this->y = y; + x = x; + y = y; +} + +void Point::set(const Point &v) +{ + x = v.x; + y = v.y; } void Point::print() diff --git a/src/engine/models/math/vector3.cpp b/src/engine/models/math/vector3.cpp index b5c95ea..b1a4712 100644 --- a/src/engine/models/math/vector3.cpp +++ b/src/engine/models/math/vector3.cpp @@ -18,11 +18,11 @@ // ---- /** Create by specifying 3 points */ -Vector3::Vector3(float x, float y, float z) +Vector3::Vector3(float t_x, float t_y, float t_z) { - this->x = x; - this->y = y; - this->z = z; + x = t_x; + y = t_y; + z = t_z; } /** Create with another vector values */ diff --git a/src/engine/models/mesh.cpp b/src/engine/models/mesh.cpp index ce57d77..aea4123 100644 --- a/src/engine/models/mesh.cpp +++ b/src/engine/models/mesh.cpp @@ -62,7 +62,6 @@ void Mesh::loadDff(char *t_subfolder, char *t_dffFile, Vector3 &t_initPos, float setDefaultColor(); isDffLoaded = true; loadTextures(t_subfolder, ".bmp"); - spec->allocateTextureBuffer(spec->textures[0].width, spec->textures[0].height); // wtf? } void Mesh::setDff(Vector3 &t_initPos, DffModel *t_dffModel, MeshSpec *t_spec) @@ -85,7 +84,7 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float obj = new ObjModel(t_objFile); obj->frameCount = framesAmount; - obj->frames = new Frame[framesAmount]; + obj->frames = new MeshFrame[framesAmount]; char *part1 = String::createConcatenated(t_subfolder, t_objFile); // "folder/object" char *part2 = String::createConcatenated(part1, "_"); // "folder/object_" @@ -98,7 +97,7 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float char *part4 = String::createWithLeadingZeros(part3); // "000001" char *part5 = String::createConcatenated(part2, part4); // "folder/object_000001" char *finalPath = String::createConcatenated(part5, ".obj"); // "folder/object_000001.obj" - loader.load(&obj->frames[i], finalPath, t_scale); + loader.load(&obj->frames[i], finalPath, t_scale, false); delete[] part3; delete[] part4; delete[] part5; @@ -108,11 +107,10 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float delete[] part2; position = t_initPos; - setVerticesReference(obj->getFacesCount(), obj->frames[0].vertices); + setVerticesReference(obj->getFacesCount(), obj->frames[0].getVertices()); setDefaultColor(); isObjLoaded = true; loadTextures(t_subfolder, ".bmp"); - spec->allocateTextureBuffer(spec->textures[0].width, spec->textures[0].height); // wtf? } void Mesh::setObj(Vector3 &t_initPos, ObjModel *t_objModel, MeshSpec *t_spec) @@ -121,7 +119,7 @@ void Mesh::setObj(Vector3 &t_initPos, ObjModel *t_objModel, MeshSpec *t_spec) spec = t_spec; isSpecInitialized = true; obj = t_objModel; - setVerticesReference(obj->getFacesCount(), obj->frames[0].vertices); + setVerticesReference(obj->getFacesCount(), obj->frames[0].getVertices()); setDefaultColor(); isObjLoaded = true; } @@ -180,10 +178,10 @@ void Mesh::loadTextures(char *t_subfolder, char *t_extension) BmpLoader bmpLoader = BmpLoader(); if (isObjLoaded) { - spec->textures = new Texture[obj->frames[0].materialsCount]; - for (u8 i = 0; i < obj->frames[0].materialsCount; i++) + spec->textures = new Texture[obj->frames[0].getMaterialsCount()]; + for (u8 i = 0; i < obj->frames[0].getMaterialsCount(); i++) { - bmpLoader.load(spec->textures[i], t_subfolder, obj->frames[0].materials[i].getName(), t_extension); + bmpLoader.load(spec->textures[i], t_subfolder, obj->frames[0].getMaterial(i).getName(), t_extension); setDefaultWrapSettings(spec->textures[i].wrapSettings); } } @@ -234,7 +232,6 @@ void Mesh::loadMD2(char *t_subfolder, char *t_md2File, Vector3 &t_initPos, float setDefaultColor(); isMd2Loaded = true; loadTextures(t_subfolder, ".bmp"); - spec->allocateTextureBuffer(spec->textures[0].width, spec->textures[0].height); // wtf? } /** Set's default object color + no transparency */ diff --git a/src/engine/models/mesh_frame.cpp b/src/engine/models/mesh_frame.cpp index 17ebf2e..fc587f2 100644 --- a/src/engine/models/mesh_frame.cpp +++ b/src/engine/models/mesh_frame.cpp @@ -15,10 +15,78 @@ // Constructors/Destructors // ---- -MeshFrame::MeshFrame() {} +MeshFrame::MeshFrame() +{ + vertexCount = 0; + stsCount = 0; + normalsCount = 0; + materialsCount = 0; + _areSTsAllocated = false; + _areVerticesAllocated = false; + _areNormalsAllocated = false; + _areMaterialsAllocated = false; +} -MeshFrame::~MeshFrame() {} +MeshFrame::~MeshFrame() +{ + if (_areSTsAllocated) + delete[] sts; + if (_areVerticesAllocated) + delete[] vertices; + if (_areNormalsAllocated) + delete[] normals; + if (_areMaterialsAllocated) + delete[] materials; +} // ---- // Methods // ---- + +void MeshFrame::allocateSTs(const u32 &t_val) +{ + if (_areSTsAllocated) + { + PRINT_ERR("Can't allocate STs, because were already set!"); + return; + } + stsCount = t_val; + sts = new Point[t_val]; + _areSTsAllocated = true; +} + +void MeshFrame::allocateVertices(const u32 &t_val) +{ + if (_areVerticesAllocated) + { + PRINT_ERR("Can't allocate vertices, because were already set!"); + return; + } + vertexCount = t_val; + vertices = new Vector3[t_val]; + _areVerticesAllocated = true; +} + +void MeshFrame::allocateNormals(const u32 &t_val) +{ + if (_areNormalsAllocated) + { + PRINT_ERR("Can't allocate normals, because were already set!"); + return; + } + normalsCount = t_val; + normals = new Vector3[t_val]; + _areNormalsAllocated = true; +} + +void MeshFrame::allocateMaterials(const u32 &t_val) +{ + if (_areMaterialsAllocated) + { + PRINT_ERR("Can't allocate materials, because were already set!"); + return; + } + materialsCount = t_val; + materials = new MeshMaterial[t_val]; + _areMaterialsAllocated = true; +} diff --git a/src/engine/models/mesh_material.cpp b/src/engine/models/mesh_material.cpp index 91077e5..fa68c86 100644 --- a/src/engine/models/mesh_material.cpp +++ b/src/engine/models/mesh_material.cpp @@ -11,6 +11,7 @@ #include "../include/models/mesh_material.hpp" #include "../include/utils/debug.hpp" #include "../include/utils/string.hpp" +#include // ---- // Constructors/Destructors @@ -18,51 +19,49 @@ MeshMaterial::MeshMaterial() { + id = rand() % 100000; facesCount = 0; - isNameAllocated = false; - areFacesAllocated = false; + _isNameSet = false; + _areFacesAllocated = false; } MeshMaterial::~MeshMaterial() { - - if (areFacesAllocated) + if (_areFacesAllocated) { delete[] vertexFaces; delete[] stFaces; delete[] normalFaces; } - if (isNameAllocated) - { + if (_isNameSet) delete[] name; - } } // ---- // Methods // ---- -void MeshMaterial::setFacesCount(const u32 &t_val) +void MeshMaterial::allocateFaces(const u32 &t_val) { - if (areFacesAllocated) + if (_areFacesAllocated) { - PRINT_ERR("Can't set faces, because faces were already set!"); + PRINT_ERR("Can't allocate faces, because were already set!"); return; } facesCount = t_val; stFaces = new u32[t_val]; normalFaces = new u32[t_val]; vertexFaces = new u32[t_val]; - areFacesAllocated = true; + _areFacesAllocated = true; } void MeshMaterial::setName(char *t_val) { - if (isNameAllocated) + if (_isNameSet) { PRINT_ERR("Can't set name, because was already set!"); return; } name = String::createCopy(t_val); - isNameAllocated = true; + _isNameSet = true; } diff --git a/src/engine/models/mesh_spec.cpp b/src/engine/models/mesh_spec.cpp index 48d00ce..383885f 100644 --- a/src/engine/models/mesh_spec.cpp +++ b/src/engine/models/mesh_spec.cpp @@ -29,39 +29,12 @@ MeshSpec::MeshSpec() /** Release memory if object was initialized */ MeshSpec::~MeshSpec() { - if (this->isTextureVRAMAllocated == true) - graph_vram_free(this->textureBuffer.address); } // ---- // Methods // ---- -/** Configure and allocate vRAM for texture buffer */ -void MeshSpec::allocateTextureBuffer(u16 t_width, u16 t_height) -{ - this->textureBuffer.width = t_width; - this->textureBuffer.psm = GS_PSM_24; - this->textureBuffer.address = graph_vram_allocate(t_width, t_height, GS_PSM_24, GRAPH_ALIGN_BLOCK); - if (this->textureBuffer.address <= 1) - PRINT_ERR("Texture buffer allocation error. No memory!"); - this->textureBuffer.info.width = draw_log2(t_width); - this->textureBuffer.info.height = draw_log2(t_height); - this->textureBuffer.info.components = TEXTURE_COMPONENTS_RGB; - this->textureBuffer.info.function = TEXTURE_FUNCTION_MODULATE; - this->isTextureVRAMAllocated = true; -} - -/** Configure and allocate vRAM for texture buffer */ -void MeshSpec::deallocateTextureBuffer() -{ - if (this->isTextureVRAMAllocated) - { - graph_vram_free(textureBuffer.address); - this->isTextureVRAMAllocated = false; - } -} - /** Sets texture level of details settings and CLUT settings */ void MeshSpec::setupLodAndClut() { diff --git a/src/engine/models/obj_model.cpp b/src/engine/models/obj_model.cpp index 65ebe92..a809fc9 100644 --- a/src/engine/models/obj_model.cpp +++ b/src/engine/models/obj_model.cpp @@ -55,8 +55,8 @@ ObjModel::~ObjModel() u32 ObjModel::getFacesCount() { u32 result = 0; // TODO - for (u16 i = 0; i < frames[0].materialsCount; i++) - result += frames[0].materials[i].getFacesCount(); + for (u16 i = 0; i < frames[0].getMaterialsCount(); i++) + result += frames[0].getMaterial(i).getFacesCount(); return result; } @@ -76,9 +76,9 @@ u32 ObjModel::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_nor { #define CURR_FRAME frames[animState.currentFrame] #define NEXT_FRAME frames[animState.nextFrame] -#define MATERIAL CURR_FRAME.materials[t_materialIndex] -#define CURR_VERT CURR_FRAME.vertices[MATERIAL.getVertexFace(matI + vertI)] -#define NEXT_VERT NEXT_FRAME.vertices[MATERIAL.getVertexFace(matI + vertI)] +#define MATERIAL CURR_FRAME.getMaterial(t_materialIndex) +#define CURR_VERT CURR_FRAME.getVertex(MATERIAL.getVertexFace(matI + vertI)) +#define NEXT_VERT NEXT_FRAME.getVertex(MATERIAL.getVertexFace(matI + vertI)) u32 addedFaces = 0; for (u32 matI = 0; matI < MATERIAL.getFacesCount(); matI += 3) @@ -99,13 +99,13 @@ u32 ObjModel::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_nor o_vertices[addedFaces][2] = calc3Vectors[vertI].z; o_vertices[addedFaces][3] = 1.0F; - o_normals[addedFaces][0] = CURR_FRAME.normals[MATERIAL.getNormalFace(matI + vertI)].x; - o_normals[addedFaces][1] = CURR_FRAME.normals[MATERIAL.getNormalFace(matI + vertI)].y; - o_normals[addedFaces][2] = CURR_FRAME.normals[MATERIAL.getNormalFace(matI + vertI)].z; + o_normals[addedFaces][0] = CURR_FRAME.getNormal(MATERIAL.getNormalFace(matI + vertI)).x; + o_normals[addedFaces][1] = CURR_FRAME.getNormal(MATERIAL.getNormalFace(matI + vertI)).y; + o_normals[addedFaces][2] = CURR_FRAME.getNormal(MATERIAL.getNormalFace(matI + vertI)).z; o_normals[addedFaces][3] = 1.0F; - o_coordinates[addedFaces][0] = CURR_FRAME.coordinates[MATERIAL.getStFace(matI + vertI)].x; - o_coordinates[addedFaces][1] = CURR_FRAME.coordinates[MATERIAL.getStFace(matI + vertI)].y; + o_coordinates[addedFaces][0] = CURR_FRAME.getST(MATERIAL.getSTFace(matI + vertI)).x; + o_coordinates[addedFaces][1] = CURR_FRAME.getST(MATERIAL.getSTFace(matI + vertI)).y; o_coordinates[addedFaces][2] = 1.0F; o_coordinates[addedFaces++][3] = 1.0F; } diff --git a/src/engine/modules/gif_sender.cpp b/src/engine/modules/gif_sender.cpp index 16fbc12..046b548 100644 --- a/src/engine/modules/gif_sender.cpp +++ b/src/engine/modules/gif_sender.cpp @@ -129,7 +129,7 @@ void GifSender::addClear(zbuffer_t *t_zBuffer) * @param objects3D Array of 3D objects pointers * @param amount Amount of 3D objects */ -void GifSender::addObjects(RenderData *t_renderData, Mesh **t_objects3D, u32 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount) +void GifSender::addObjects(RenderData *t_renderData, Mesh **t_objects3D, u32 t_amount, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer) { if (!isAnyObjectAdded) { @@ -141,7 +141,7 @@ void GifSender::addObjects(RenderData *t_renderData, Mesh **t_objects3D, u32 t_a q++; q = draw_texture_sampling(q, 0, &t_objects3D[0]->spec->lod); - q = draw_texturebuffer(q, 0, &t_objects3D[0]->spec->textureBuffer, &t_objects3D[0]->spec->clut); + q = draw_texturebuffer(q, 0, textureBuffer, &t_objects3D[0]->spec->clut); dw = (u64 *)draw_prim_start(q, 0, t_renderData->prim, &t_objects3D[0]->color); for (u32 i = 0; i < t_amount; i++) diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index e02a72e..926c76a 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -88,14 +88,39 @@ void Renderer::setPrim() PRINT_LOG("Prim set!"); } +/** Configure and allocate vRAM for texture buffer */ +void Renderer::allocateTextureBuffer(u16 t_width, u16 t_height) +{ + textureBuffer.width = t_width; + textureBuffer.psm = GS_PSM_24; + textureBuffer.address = graph_vram_allocate(t_width, t_height, GS_PSM_24, GRAPH_ALIGN_BLOCK); + if (textureBuffer.address <= 1) + PRINT_ERR("Texture buffer allocation error. No memory!"); + textureBuffer.info.width = draw_log2(t_width); + textureBuffer.info.height = draw_log2(t_height); + textureBuffer.info.components = TEXTURE_COMPONENTS_RGB; + textureBuffer.info.function = TEXTURE_FUNCTION_MODULATE; + isTextureVRAMAllocated = true; +} + +/** Configure and allocate vRAM for texture buffer */ +void Renderer::deallocateTextureBuffer() +{ + if (isTextureVRAMAllocated) + { + graph_vram_free(textureBuffer.address); + isTextureVRAMAllocated = false; + } +} + void Renderer::changeTexture(Mesh *t_mesh, u8 t_textureIndex) { if (t_mesh->spec->textures[t_textureIndex].id != lastTextureId) { lastTextureId = t_mesh->spec->textures[t_textureIndex].id; - t_mesh->spec->deallocateTextureBuffer(); - t_mesh->spec->allocateTextureBuffer(t_mesh->spec->textures[t_textureIndex].width, t_mesh->spec->textures[t_textureIndex].height); - GifSender::sendTexture(t_mesh->spec->textures[t_textureIndex], &t_mesh->spec->textureBuffer); + deallocateTextureBuffer(); + allocateTextureBuffer(t_mesh->spec->textures[t_textureIndex].width, t_mesh->spec->textures[t_textureIndex].height); + GifSender::sendTexture(t_mesh->spec->textures[t_textureIndex], &textureBuffer); } } @@ -134,7 +159,7 @@ void Renderer::drawByPath3(Mesh **t_meshes, u16 t_amount, LightBulb *t_bulbs, u1 gifSender->initPacket(context); // TODO changeTexture(t_meshes[0], 0); - gifSender->addObjects(&renderData, t_meshes, t_amount, t_bulbs, t_bulbsCount); + gifSender->addObjects(&renderData, t_meshes, t_amount, t_bulbs, t_bulbsCount, &textureBuffer); gifSender->sendPacket(); draw_wait_finish(); } @@ -146,7 +171,7 @@ void Renderer::drawByPath3(Mesh *t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) changeTexture(t_mesh, 0); gifSender->initPacket(context); // TODO - gifSender->addObjects(&renderData, &t_mesh, 1, t_bulbs, t_bulbsCount); + gifSender->addObjects(&renderData, &t_mesh, 1, t_bulbs, t_bulbsCount, &textureBuffer); gifSender->sendPacket(); draw_wait_finish(); } @@ -182,18 +207,18 @@ void Renderer::draw(Mesh *t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) if (t_mesh->isObjLoaded) { t_mesh->obj->animate(); - for (u32 i = 0; i < t_mesh->obj->frames[0].materialsCount; i++) + for (u32 i = 0; i < t_mesh->obj->frames[0].getMaterialsCount(); i++) { changeTexture(t_mesh, i); vertCount = t_mesh->getDrawData(i, vertices, normals, coordinates, *renderData.cameraPosition); - vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount); + vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount, &textureBuffer); } } else if (t_mesh->isMd2Loaded) { changeTexture(t_mesh, 0); vertCount = t_mesh->getDrawData(0, vertices, normals, coordinates, *renderData.cameraPosition); - vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount); + vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount, &textureBuffer); } else if (t_mesh->isDffLoaded) for (u32 i = 0; i < t_mesh->dff->clump.geometryList.geometries[0].extension.materialSplit.header.splitCount; i++) @@ -201,7 +226,7 @@ void Renderer::draw(Mesh *t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) const u32 currentTexI = t_mesh->dff->clump.geometryList.geometries[0].extension.materialSplit.splitInformation[i].materialIndex; changeTexture(t_mesh, currentTexI); vertCount = t_mesh->getDrawData(i, vertices, normals, coordinates, *renderData.cameraPosition); - vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount); + vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount, &textureBuffer); } delete[] vertices; delete[] normals; diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 4035989..ce66909 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -37,7 +37,7 @@ VifSender::~VifSender() {} // Methods // ---- -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) +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) { if (t_mesh->shouldBeFrustumCulled == 1 && !t_mesh->isInFrustum(t_renderData->frustumPlanes)) return; @@ -60,7 +60,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); + drawVertices(t_mesh, i, endI, vertices, coordinates, t_renderData->prim, textureBuffer); if (endI == vertCount2) // if there are no more vertices to draw, break { i = vertCount2; @@ -74,7 +74,7 @@ void VifSender::drawMesh(RenderData *t_renderData, Matrix t_perspective, u32 ver } /** 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) +void VifSender::drawVertices(Mesh *t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *textureBuffer) { const u32 vertCount = t_end - t_start; vu1.addListBeginning(); @@ -100,13 +100,13 @@ void VifSender::drawVertices(Mesh *t_mesh, u32 t_start, u32 t_end, VECTOR *t_ver vu1.add128( // tex -> buff + clut GS_SET_TEX0( - t_mesh->spec->textureBuffer.address >> 6, - t_mesh->spec->textureBuffer.width >> 6, - t_mesh->spec->textureBuffer.psm, - t_mesh->spec->textureBuffer.info.width, - t_mesh->spec->textureBuffer.info.height, - t_mesh->spec->textureBuffer.info.components, - t_mesh->spec->textureBuffer.info.function, + textureBuffer->address >> 6, + textureBuffer->width >> 6, + textureBuffer->psm, + textureBuffer->info.width, + textureBuffer->info.height, + textureBuffer->info.components, + textureBuffer->info.function, t_mesh->spec->clut.address >> 6, t_mesh->spec->clut.psm, t_mesh->spec->clut.storage_mode,