diff --git a/src/engine/include/loaders/obj_loader.hpp b/src/engine/include/loaders/obj_loader.hpp index 410ffe4..b578875 100644 --- a/src/engine/include/loaders/obj_loader.hpp +++ b/src/engine/include/loaders/obj_loader.hpp @@ -11,7 +11,6 @@ #ifndef _TYRA_OBJ_LOADER_ #define _TYRA_OBJ_LOADER_ -#include "../models/obj_model.hpp" #include "../models/mesh_frame.hpp" #include diff --git a/src/engine/include/models/mesh.hpp b/src/engine/include/models/mesh.hpp index d4c1c47..1858349 100644 --- a/src/engine/include/models/mesh.hpp +++ b/src/engine/include/models/mesh.hpp @@ -13,10 +13,10 @@ #include "math/vector3.hpp" #include "math/plane.hpp" -#include "obj_model.hpp" #include "dff_model.hpp" #include "md2_model.hpp" #include "./mesh_texture.hpp" +#include "./mesh_frame.hpp" #include #include #include @@ -31,7 +31,6 @@ public: u8 shouldBeLighted, shouldBeBackfaceCulled, shouldBeFrustumCulled; MD2Model *md2; - ObjModel *obj; DffModel *dff; float scale; color_t color; @@ -39,8 +38,8 @@ public: Mesh(); ~Mesh(); - void loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float t_scale, u16 framesAmount); - void setObj(Vector3 &t_initPos, ObjModel *t_objModel); + void loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float t_scale, u16 t_framesCount); + // void setObj(Vector3 &t_initPos); // TODO void loadDff(char *t_subfolder, char *t_dffFile, Vector3 &t_initPos, float t_scale); void setDff(Vector3 &t_initPos, DffModel *t_dffModel); void loadMD2(char *t_subfolder, char *t_md2File, Vector3 &t_initPos, float t_scale); @@ -57,21 +56,32 @@ public: u32 id; MeshTexture *textures; - /** Array of materials. Size of getMaterialsCount() */ - MeshMaterial *getMaterials() const { return obj->frames[0].getMaterials(); }; + // NOWE TODO - const u32 &getMaterialsCount() const { return obj->frames[0].getMaterialsCount(); }; + u16 framesCount; + MeshFrame *frames; + AnimState animState; + void animate(); + u32 getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled); + u32 getFacesCount(); + u8 isMemoryAllocated; + + /** Array of materials. Size of getMaterialsCount() */ + MeshMaterial *getMaterials() const { return frames[0].getMaterials(); }; + + const u32 &getMaterialsCount() const { return frames[0].getMaterialsCount(); }; /** Returns material, which is a mesh "subgroup". */ - MeshMaterial &getMaterial(const u32 &i) const { return obj->frames[0].getMaterial(i); }; + MeshMaterial &getMaterial(const u32 &i) const { return frames[0].getMaterial(i); }; /** * Returns material, which is a mesh "subgroup". * NULL if not found. */ - MeshMaterial *getMaterialById(const u32 &t_id) const { return obj->frames[0].getMaterialById(t_id); } + MeshMaterial *getMaterialById(const u32 &t_id) const { return frames[0].getMaterialById(t_id); } private: + Vector3 calc3Vectors[3]; void setupLodAndClut(); void setDefaultWrapSettings(texwrap_t &t_wrapSettings); u32 verticesCount; diff --git a/src/engine/include/models/obj_model.hpp b/src/engine/include/models/obj_model.hpp deleted file mode 100644 index c4acd58..0000000 --- a/src/engine/include/models/obj_model.hpp +++ /dev/null @@ -1,44 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#ifndef _TYRA_OBJ_MODEL_ -#define _TYRA_OBJ_MODEL_ - -#include -#include -#include "math/vector3.hpp" -#include "utils/debug.hpp" -#include "./anim_state.hpp" -#include "./mesh_material.hpp" -#include "./mesh_frame.hpp" - -/** Class which have common types for all 3D objects */ -class ObjModel -{ - -public: - /** File name without extension */ - char *filename; - u16 frameCount; - MeshFrame *frames; - AnimState animState; - - ObjModel(char *t_objFile); - ~ObjModel(); - void animate(); - u32 getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled); - u32 getFacesCount(); - u8 isMemoryAllocated; - -private: - Vector3 calc3Vectors[3]; -}; - -#endif diff --git a/src/engine/models/mesh.cpp b/src/engine/models/mesh.cpp index 89a14a2..24d83c6 100644 --- a/src/engine/models/mesh.cpp +++ b/src/engine/models/mesh.cpp @@ -34,14 +34,21 @@ Mesh::Mesh() isSpecInitialized = false; id = rand() % 1000000; scale = 1.0F; + animState.startFrame = 0; + animState.endFrame = 0; + animState.interpolation = 0.0F; + animState.animType = 0; + animState.currentFrame = 0; + animState.nextFrame = 0; + animState.speed = 0.1F; + // filename = String::createWithoutExtension(t_objFile); } Mesh::~Mesh() { + // TODO if (isMd2Loaded) delete md2; - if (isObjLoaded) - delete obj; if (isDffLoaded) delete dff; } @@ -50,6 +57,67 @@ Mesh::~Mesh() // Methods // ---- +u32 Mesh::getFacesCount() +{ + u32 result = 0; // TODO + for (u16 i = 0; i < frames[0].getMaterialsCount(); i++) + result += frames[0].getMaterial(i).getFacesCount(); + return result; +} + +void Mesh::animate() +{ + animState.interpolation += animState.speed; + if (animState.interpolation >= 1.0F) + { + animState.interpolation = 0.0F; + animState.currentFrame = animState.nextFrame; + if (++animState.nextFrame > animState.endFrame) + animState.nextFrame = animState.startFrame; + } +} + +u32 Mesh::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled) +{ +#define CURR_FRAME frames[animState.currentFrame] +#define NEXT_FRAME frames[animState.nextFrame] +#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) + { + for (u32 vertI = 0; vertI < 3; vertI++) + if (animState.startFrame == animState.endFrame) + calc3Vectors[vertI].set(CURR_VERT * t_scale); + else + calc3Vectors[vertI].setByLerp(CURR_VERT, NEXT_VERT, animState.interpolation, t_scale); + + if (!t_shouldBeBackfaceCulled || + Vector3::shouldBeBackfaceCulled(&t_cameraPos, &calc3Vectors[2], &calc3Vectors[1], &calc3Vectors[0])) + + for (u32 vertI = 0; vertI < 3; vertI++) + { + o_vertices[addedFaces][0] = calc3Vectors[vertI].x; + o_vertices[addedFaces][1] = calc3Vectors[vertI].y; + o_vertices[addedFaces][2] = calc3Vectors[vertI].z; + o_vertices[addedFaces][3] = 1.0F; + + 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.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; + } + } + return addedFaces; +} + void Mesh::loadDff(char *t_subfolder, char *t_dffFile, Vector3 &t_initPos, float t_scale) { createSpecIfNotCreated(); @@ -79,27 +147,26 @@ void Mesh::setDff(Vector3 &t_initPos, DffModel *t_dffModel) isDffLoaded = true; } -void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float t_scale, u16 framesAmount) +void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float t_scale, u16 t_framesCount) { createSpecIfNotCreated(); ObjLoader loader = ObjLoader(); - obj = new ObjModel(t_objFile); - obj->frameCount = framesAmount; - obj->frames = new MeshFrame[framesAmount]; + framesCount = t_framesCount; + + frames = new MeshFrame[framesCount]; char *part1 = String::createConcatenated(t_subfolder, t_objFile); // "folder/object" char *part2 = String::createConcatenated(part1, "_"); // "folder/object_" delete[] part1; - for (u16 i = 0; i < framesAmount; i++) + for (u16 i = 0; i < framesCount; i++) { - // obj->frames[i] = Frame(); char *part3 = String::createU32ToString(i + 1); // 0 -> "1" 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, false); + loader.load(&frames[i], finalPath, t_scale, false); delete[] part3; delete[] part4; delete[] part5; @@ -109,20 +176,20 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float delete[] part2; position = t_initPos; - setVerticesReference(obj->getFacesCount(), obj->frames[0].getVertices()); + setVerticesReference(getFacesCount(), frames[0].getVertices()); setDefaultColor(); isObjLoaded = true; } -void Mesh::setObj(Vector3 &t_initPos, ObjModel *t_objModel) -{ - position = t_initPos; - isSpecInitialized = true; - obj = t_objModel; - setVerticesReference(obj->getFacesCount(), obj->frames[0].getVertices()); - setDefaultColor(); - isObjLoaded = true; -} +// void Mesh::setObj(Vector3 &t_initPos, ObjModel *t_objModel) +// { +// position = t_initPos; +// isSpecInitialized = true; +// obj = t_objModel; +// setVerticesReference(getFacesCount(), frames[0].getVertices()); +// setDefaultColor(); +// isObjLoaded = true; +// } void Mesh::createSpecIfNotCreated() { @@ -147,7 +214,7 @@ u32 Mesh::getVertexCount() if (isMd2Loaded) return md2->trianglesCount * 3; else if (isObjLoaded) - return obj->getFacesCount(); + return getFacesCount(); else if (isDffLoaded) return dff->clump.geometryList.geometries[0].data.dataHeader.triangleCount * 3; else @@ -199,7 +266,7 @@ u32 Mesh::getDrawData(u32 splitIndex, VECTOR *t_vertices, VECTOR *t_normals, VEC if (isMd2Loaded) return md2->getCurrentFrameData(t_vertices, t_normals, t_coordinates, t_cameraPos, scale, shouldBeBackfaceCulled); else if (isObjLoaded) - return obj->getDrawData(splitIndex, t_vertices, t_normals, t_coordinates, t_cameraPos, scale, shouldBeBackfaceCulled); + return getDrawData(splitIndex, t_vertices, t_normals, t_coordinates, t_cameraPos, scale, shouldBeBackfaceCulled); else if (isDffLoaded) return dff->getDrawData(splitIndex, t_vertices, t_normals, t_coordinates, t_cameraPos, scale, shouldBeBackfaceCulled); PRINT_ERR("Can't get draw data, because no 3D model was loaded!"); @@ -273,8 +340,8 @@ void Mesh::playAnimation(u32 t_startFrame, u32 t_endFrame) } else if (isObjLoaded == 1) { - obj->animState.startFrame = t_startFrame; - obj->animState.endFrame = t_endFrame; + animState.startFrame = t_startFrame; + animState.endFrame = t_endFrame; } else PRINT_ERR("Animation is only supported in MD2/OBJ format!"); @@ -286,9 +353,9 @@ void Mesh::playAnimation(u32 t_startFrame, u32 t_endFrame) void Mesh::getFarthestVertex(Vector3 *o_result, int t_offset) { // TODO current frame - o_result->x = obj->frames[0].getBoundingBox(t_offset).x + position.x; - o_result->y = obj->frames[0].getBoundingBox(t_offset).y + position.y; - o_result->z = obj->frames[0].getBoundingBox(t_offset).z + position.z; + o_result->x = frames[animState.currentFrame].getBoundingBox(t_offset).x + position.x; + o_result->y = frames[animState.currentFrame].getBoundingBox(t_offset).y + position.y; + o_result->z = frames[animState.currentFrame].getBoundingBox(t_offset).z + position.z; } /** Sets texture level of details settings and CLUT settings */ diff --git a/src/engine/models/obj_model.cpp b/src/engine/models/obj_model.cpp deleted file mode 100644 index a809fc9..0000000 --- a/src/engine/models/obj_model.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#include "../include/models/obj_model.hpp" - -#include "../include/utils/debug.hpp" -#include "../include/utils/string.hpp" - -// ---- -// Constructors/Destructors -// ---- - -ObjModel::ObjModel(char *t_objFile) -{ - animState.startFrame = 0; - animState.endFrame = 0; - animState.interpolation = 0.0F; - animState.animType = 0; - animState.currentFrame = 0; - animState.nextFrame = 0; - animState.speed = 0.1F; - filename = String::createWithoutExtension(t_objFile); -} - -ObjModel::~ObjModel() -{ - if (isMemoryAllocated == true) - { - // TODO !!!! - // delete[] vertices; - // delete[] coordinates; - // delete[] normals; - // for (u16 i = 0; i < materialsCount; i++) - // { - // delete[] materials[i].verticeFaces; - // delete[] materials[i].coordinateFaces; - // delete[] materials[i].normalFaces; - // delete[] materials[i].materialName; - // } - // delete[] materials; - } -} - -// ---- -// Methods -// ---- - -u32 ObjModel::getFacesCount() -{ - u32 result = 0; // TODO - for (u16 i = 0; i < frames[0].getMaterialsCount(); i++) - result += frames[0].getMaterial(i).getFacesCount(); - return result; -} - -void ObjModel::animate() -{ - animState.interpolation += animState.speed; - if (animState.interpolation >= 1.0F) - { - animState.interpolation = 0.0F; - animState.currentFrame = animState.nextFrame; - if (++animState.nextFrame > animState.endFrame) - animState.nextFrame = animState.startFrame; - } -} - -u32 ObjModel::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled) -{ -#define CURR_FRAME frames[animState.currentFrame] -#define NEXT_FRAME frames[animState.nextFrame] -#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) - { - for (u32 vertI = 0; vertI < 3; vertI++) - if (animState.startFrame == animState.endFrame) - calc3Vectors[vertI].set(CURR_VERT * t_scale); - else - calc3Vectors[vertI].setByLerp(CURR_VERT, NEXT_VERT, animState.interpolation, t_scale); - - if (!t_shouldBeBackfaceCulled || - Vector3::shouldBeBackfaceCulled(&t_cameraPos, &calc3Vectors[2], &calc3Vectors[1], &calc3Vectors[0])) - - for (u32 vertI = 0; vertI < 3; vertI++) - { - o_vertices[addedFaces][0] = calc3Vectors[vertI].x; - o_vertices[addedFaces][1] = calc3Vectors[vertI].y; - o_vertices[addedFaces][2] = calc3Vectors[vertI].z; - o_vertices[addedFaces][3] = 1.0F; - - 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.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; - } - } - return addedFaces; -} diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index c33c9aa..e68820d 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -214,7 +214,7 @@ void Renderer::draw(Mesh *t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) VECTOR *coordinates = new VECTOR[vertCount]; if (t_mesh->isObjLoaded) { - t_mesh->obj->animate(); + t_mesh->animate(); for (u32 i = 0; i < t_mesh->getMaterialsCount(); i++) { changeTexture(t_mesh, t_mesh->getMaterial(i).getId()); diff --git a/src/samples/ari/Makefile b/src/samples/ari/Makefile index 64424d2..1aa811d 100644 --- a/src/samples/ari/Makefile +++ b/src/samples/ari/Makefile @@ -11,7 +11,6 @@ EE_OBJS = \ ../../engine/models/mesh_material.o \ ../../engine/models/mesh.o \ ../../engine/models/mesh_texture.o \ - ../../engine/models/obj_model.o \ ../../engine/modules/audio.o \ ../../engine/modules/camera_base.o \ ../../engine/modules/gif_sender.o \ diff --git a/src/samples/ari/ari.cpp b/src/samples/ari/ari.cpp index 19b7bc4..0bd72a6 100644 --- a/src/samples/ari/ari.cpp +++ b/src/samples/ari/ari.cpp @@ -102,6 +102,12 @@ void Ari::onInit() texRepo->removeById(textures->at(0)->getId()); test->playAnimation(0, 1); test->shouldBeBackfaceCulled = true; + + // TODO 1 - Merge dff/md2 + // TODO 2 - Refactor + // TODO 3 - Destructor + // TODO 4 - Mesh copy(); + // test->shouldBeFrustumCulled = true; // skybox = new Mesh();