From 138ab762badcf2d53759fef51b5d3f158f897775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Sobczy=C5=84ski?= Date: Sun, 1 Nov 2020 18:13:24 +0100 Subject: [PATCH] removed md2_model --- .../include/{utils => loaders}/anorms.hpp | 0 src/engine/include/loaders/md2_loader.hpp | 5 +- src/engine/include/loaders/obj_loader.hpp | 4 + src/engine/include/models/md2_model.hpp | 48 ------- src/engine/include/models/mesh.hpp | 16 +-- src/engine/include/utils/debug.hpp | 17 ++- src/engine/loaders/dff_loader.cpp | 4 +- src/engine/loaders/md2_loader.cpp | 114 +++++++++------- src/engine/loaders/obj_loader.cpp | 4 - src/engine/models/md2_model.cpp | 124 ------------------ src/engine/models/mesh.cpp | 122 ++++------------- src/engine/modules/renderer.cpp | 5 - src/samples/ari/Makefile | 3 +- src/samples/ari/ari.cpp | 46 ++++--- 14 files changed, 154 insertions(+), 358 deletions(-) rename src/engine/include/{utils => loaders}/anorms.hpp (100%) delete mode 100644 src/engine/include/models/md2_model.hpp delete mode 100644 src/engine/models/md2_model.cpp diff --git a/src/engine/include/utils/anorms.hpp b/src/engine/include/loaders/anorms.hpp similarity index 100% rename from src/engine/include/utils/anorms.hpp rename to src/engine/include/loaders/anorms.hpp diff --git a/src/engine/include/loaders/md2_loader.hpp b/src/engine/include/loaders/md2_loader.hpp index 9c04447..d84bbc4 100644 --- a/src/engine/include/loaders/md2_loader.hpp +++ b/src/engine/include/loaders/md2_loader.hpp @@ -11,7 +11,7 @@ #ifndef _TYRA_MD2_LOADER_ #define _TYRA_MD2_LOADER_ -#include "../models/md2_model.hpp" +#include "../models/mesh_frame.hpp" #include // magic number "IDP2" or 844121161 @@ -83,7 +83,8 @@ public: MD2Loader(); ~MD2Loader(); - void load(MD2Model *o_result, char *t_fileName, float t_scale); + /** Returns frames count */ + u32 load(MeshFrame *o_result, char *t_subpath, char *t_nameWithoutExtension, float t_scale, u8 t_invertT); private: }; diff --git a/src/engine/include/loaders/obj_loader.hpp b/src/engine/include/loaders/obj_loader.hpp index 96b889e..951f51a 100644 --- a/src/engine/include/loaders/obj_loader.hpp +++ b/src/engine/include/loaders/obj_loader.hpp @@ -22,6 +22,10 @@ public: ObjLoader(); ~ObjLoader(); + /** Parse .obj file, allocate output data and store it. Multitexture support + * Notice: At this moment textures names are MATERIAL names from .obj file! + * Notice 2: Faces MUST be triangulated (check out blender export settings). + */ void load(MeshFrame *o_result, char *t_fileName, float t_scale, u8 t_invertT); private: diff --git a/src/engine/include/models/md2_model.hpp b/src/engine/include/models/md2_model.hpp deleted file mode 100644 index 1d4cbda..0000000 --- a/src/engine/include/models/md2_model.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#ifndef _TYRA_MD2_MODEL_ -#define _TYRA_MD2_MODEL_ - -#include -#include -#include "math/vector3.hpp" -#include "math/point.hpp" -#include "./anim_state.hpp" - -typedef struct -{ - u16 verticeIndexes[3]; - u16 coordIndexes[3]; -} MD2Triangle; - -/** Class which have common types for all 3D objects */ -class MD2Model -{ - -public: - u32 verticesPerFrameCount, framesCount, coordinatesCount, trianglesCount; - Vector3 *vertices __attribute__((aligned(16))); - Point *coordinates; - u32 *normalIndexes; - MD2Triangle *triangles; - AnimState animState; - /** File name without extension */ - char *filename; - MD2Model(char *t_md2File); - ~MD2Model(); - u32 getCurrentFrameData(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled); - void allocateMemory(); - -private: - Vector3 calc3Vectors[3]; -}; - -#endif diff --git a/src/engine/include/models/mesh.hpp b/src/engine/include/models/mesh.hpp index 10f2abb..720347a 100644 --- a/src/engine/include/models/mesh.hpp +++ b/src/engine/include/models/mesh.hpp @@ -8,18 +8,18 @@ # Sandro Sobczyński */ -#ifndef _TYRA_OBJECT3D_ -#define _TYRA_OBJECT3D_ +#ifndef _TYRA_MESH_ +#define _TYRA_MESH_ #include "math/vector3.hpp" #include "math/plane.hpp" -#include "md2_model.hpp" #include "./mesh_texture.hpp" #include "./mesh_frame.hpp" #include #include #include #include +#include "./anim_state.hpp" /** Class which have common types for all 3D objects */ class Mesh @@ -29,7 +29,6 @@ public: Vector3 position, rotation; u8 shouldBeLighted, shouldBeBackfaceCulled, shouldBeFrustumCulled; - MD2Model *md2; float scale; color_t color; @@ -38,14 +37,13 @@ public: void loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float t_scale, u16 t_framesCount, u8 t_invertT); // void setObj(Vector3 &t_initPos); // TODO - void loadDff(char *t_subfolder, char *t_dffFile, Vector3 &t_initPos, float t_scale); + void loadDff(char *t_subfolder, char *t_dffFile, Vector3 &t_initPos, float t_scale, u8 t_invertT); // void setDff(Vector3 &t_initPos, DffModel *t_dffModel); - void loadMD2(char *t_subfolder, char *t_md2File, Vector3 &t_initPos, float t_scale); + void loadMD2(char *t_subfolder, char *t_md2File, Vector3 &t_initPos, float t_scale, u8 t_invertT); void getMinMax(Vector3 *t_min, Vector3 *t_max); void playAnimation(u32 t_startFrame, u32 t_endFrame); u32 getVertexCount(); void setAnimSpeed(float t_value); - u32 getDrawData(u32 splitIndex, VECTOR *t_vertices, VECTOR *t_normals, VECTOR *t_coordinates, Vector3 &t_cameraPos); u8 isInFrustum(Plane *t_frustumPlanes); u8 isMd2Loaded, isObjLoaded, isSpecInitialized; @@ -60,7 +58,7 @@ public: 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 getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos); u32 getFacesCount(); u8 isMemoryAllocated; @@ -84,9 +82,7 @@ private: void setDefaultWrapSettings(texwrap_t &t_wrapSettings); u32 verticesCount; Vector3 *vertices; - void loadTextures(char *t_subfolder, char *t_extension); void setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef); - void createSpecIfNotCreated(); void setDefaultColor(); void getFarthestVertex(Vector3 *o_result, int t_offset); }; diff --git a/src/engine/include/utils/debug.hpp b/src/engine/include/utils/debug.hpp index 106d75a..39074ba 100644 --- a/src/engine/include/utils/debug.hpp +++ b/src/engine/include/utils/debug.hpp @@ -15,7 +15,22 @@ #include #include +class Debug +{ +public: + static void errTrap(char *t_text, char *t_file) + { + printf("\n"); + printf("====================================\n"); + printf("| ERROR: %s\n", t_text); + printf("| File : %s\n", t_file); + printf("====================================\n\n"); + for (;;) + ; + } +}; + #define PRINT_LOG(TEXT) printf("LOG: " TEXT " (" __FILE__ ")\n") -#define PRINT_ERR(TEXT) printf("\n====================================\n| ERROR: " TEXT "\n| File : " __FILE__ "\n====================================\n\n") +#define PRINT_ERR(TEXT) Debug::errTrap(TEXT, __FILE__) #endif diff --git a/src/engine/loaders/dff_loader.cpp b/src/engine/loaders/dff_loader.cpp index 34a3e63..7aa9aa0 100644 --- a/src/engine/loaders/dff_loader.cpp +++ b/src/engine/loaders/dff_loader.cpp @@ -61,8 +61,8 @@ void DffLoader::convert(MeshFrame *frames, RwClump &t_clump, u8 t_invertT) MeshFrame *frame = &frames[0]; u32 vertNormalStCount = GEOMETRY.data.dataHeader.vertexCount; frame->allocateVertices(vertNormalStCount); - frame->allocateNormals(vertNormalStCount); - frame->allocateSTs(vertNormalStCount); + frame->allocateNormals(vertNormalStCount); // can be optimized + frame->allocateSTs(vertNormalStCount); // can be optimized Vector3 tempVec = Vector3(); Point tempPoint = Point(); diff --git a/src/engine/loaders/md2_loader.cpp b/src/engine/loaders/md2_loader.cpp index eb48565..f6db0d6 100644 --- a/src/engine/loaders/md2_loader.cpp +++ b/src/engine/loaders/md2_loader.cpp @@ -10,7 +10,9 @@ #include "../include/loaders/md2_loader.hpp" +#include "../include/utils/string.hpp" #include "../include/utils/debug.hpp" +#include "../include/loaders/anorms.hpp" #include "../include/models/math/vector3.hpp" #include "../include/models/math/point.hpp" #include @@ -29,100 +31,120 @@ MD2Loader::~MD2Loader() {} // ---- /** Load, parse .obj file and load data into given MeshSpec */ -void MD2Loader::load(MD2Model *o_result, char *t_filename, float t_scale) +u32 MD2Loader::load(MeshFrame *o_result, char *t_subpath, char *t_nameWithoutExtension, float t_scale, u8 t_invertT) { + PRINT_LOG("Loading new MD2 file"); + char *part1 = String::createConcatenated(t_subpath, t_nameWithoutExtension); + char *finalPath = String::createConcatenated(part1, ".md2"); // "folder/object.md2" + delete[] part1; + PRINT_LOG("Loading new MD2 file"); md2_t header; // md2 header + char *buffer; // buffer storing frame data - FILE *file = fopen(t_filename, "rb"); + FILE *file = fopen(finalPath, "rb"); if (file == NULL) + { PRINT_ERR("Failed to load .md2 file!"); + return 0; + } fread((char *)&header, sizeof(md2_t), 1, file); if ((header.ident != MD2_IDENT) && (header.version != MD2_VERSION)) + { PRINT_ERR("This MD2 file was not in correct format!"); + return 0; + } - o_result->framesCount = header.num_frames; - o_result->verticesPerFrameCount = header.num_xyz; - o_result->coordinatesCount = header.num_st; - o_result->trianglesCount = header.num_tris; + u32 framesCount = header.num_frames; + u32 vertexCount = header.num_xyz; + u32 stsCount = header.num_st; + u32 trianglesCount = header.num_tris; - printf("MD2 - Frames: %d\n", header.num_frames); - printf("MD2 - Vert: %d\n", header.num_xyz); - printf("MD2 - Tris: %d\n", header.num_tris); - printf("MD2 - STs: %d\n", header.num_st); - printf("MD2 - Skin W:%d H: %d\n", header.skinwidth, header.skinheight); + // o_result = new MeshFrame[framesCount]; - o_result->allocateMemory(); - - buffer = new char[o_result->framesCount * header.framesize]; + buffer = new char[framesCount * header.framesize]; fseek(file, header.ofs_frames, SEEK_SET); - fread((char *)buffer, o_result->framesCount * header.framesize, 1, file); + fread((char *)buffer, framesCount * header.framesize, 1, file); frame_t *frame; // temporary vars - Vector3 *ptrVerts; - u32 *ptrNormals; - // vertex array initialization - for (u32 j = 0; j < o_result->framesCount; j++) + Vector3 tempVec = Vector3(); + + for (u32 j = 0; j < framesCount; j++) { + o_result[j].allocateVertices(vertexCount); + o_result[j].allocateNormals(vertexCount); // can be optimized + o_result[j].allocateSTs(stsCount); + o_result[j].allocateMaterials(1); + printf("Materials count:%d\n", o_result[j].getMaterialsCount()); + o_result[j].getMaterial(0).allocateFaces(trianglesCount * 3); + o_result[j].getMaterial(0).setName(t_nameWithoutExtension); + frame = (frame_t *)&buffer[header.framesize * j]; - ptrVerts = &o_result->vertices[o_result->verticesPerFrameCount * j]; - ptrNormals = &o_result->normalIndexes[o_result->verticesPerFrameCount * j]; - for (u32 i = 0; i < o_result->verticesPerFrameCount; i++) + for (u32 i = 0; i < vertexCount; i++) { - ptrVerts[i].x = ((frame->verts[i].v[0] * frame->scale[0]) + frame->translate[0]) * t_scale; - ptrVerts[i].y = ((frame->verts[i].v[1] * frame->scale[1]) + frame->translate[1]) * t_scale; - ptrVerts[i].z = ((frame->verts[i].v[2] * frame->scale[2]) + frame->translate[2]) * t_scale; + tempVec.set( + ((frame->verts[i].v[0] * frame->scale[0]) + frame->translate[0]) * t_scale, + ((frame->verts[i].v[1] * frame->scale[1]) + frame->translate[1]) * t_scale, + ((frame->verts[i].v[2] * frame->scale[2]) + frame->translate[2]) * t_scale); + o_result[j].setVertex(i, tempVec); - ptrNormals[i] = frame->verts[i].lightnormalindex; + tempVec.set( + ANORMS[frame->verts[i].lightnormalindex][0], + ANORMS[frame->verts[i].lightnormalindex][1], + ANORMS[frame->verts[i].lightnormalindex][2]); + o_result[j].setNormal(i, tempVec); } - if (j == o_result->framesCount - 1) - printf("Last frame, last vert: X:%f Y:%f Z:%f\n", - ptrVerts[o_result->verticesPerFrameCount - 1].x, - ptrVerts[o_result->verticesPerFrameCount - 1].y, - ptrVerts[o_result->verticesPerFrameCount - 1].z); } delete[] buffer; - buffer = new char[o_result->coordinatesCount * sizeof(texCoord_t)]; + buffer = new char[stsCount * sizeof(texCoord_t)]; fseek(file, header.ofs_st, SEEK_SET); - fread((char *)buffer, o_result->coordinatesCount * sizeof(texCoord_t), 1, file); + fread((char *)buffer, stsCount * sizeof(texCoord_t), 1, file); + Point tempPoint = Point(); texCoord_t *texCoord; - for (u32 i = 0; i < o_result->coordinatesCount; i++) + for (u32 i = 0; i < stsCount; i++) { texCoord = (texCoord_t *)&buffer[sizeof(texCoord_t) * i]; - o_result->coordinates[i].x = (float)texCoord->s / header.skinwidth; - o_result->coordinates[i].y = (float)texCoord->t / header.skinheight; + tempPoint.set( + (float)texCoord->s / header.skinwidth, + (float)texCoord->t / header.skinheight); + if (t_invertT) + tempPoint.y = 1.0F - tempPoint.y; + for (u32 j = 0; j < framesCount; j++) + o_result[j].setST(i, tempPoint); } delete[] buffer; - printf("Last tex coord(%d): S:%f T:%f\n", - o_result->coordinatesCount, - o_result->coordinates[o_result->coordinatesCount - 1].x, - o_result->coordinates[o_result->coordinatesCount - 1].y); - - buffer = new char[o_result->trianglesCount * sizeof(triangle_t)]; + buffer = new char[trianglesCount * sizeof(triangle_t)]; fseek(file, header.ofs_tris, SEEK_SET); - fread((char *)buffer, o_result->trianglesCount * sizeof(triangle_t), 1, file); + fread((char *)buffer, trianglesCount * sizeof(triangle_t), 1, file); triangle_t *triangle; // temporary var // vertex array initialization - for (u32 i = 0; i < o_result->trianglesCount; i++) + for (u32 i = 0; i < trianglesCount; i++) { triangle = (triangle_t *)&buffer[sizeof(triangle_t) * i]; for (u8 j = 0; j < 3; j++) { - o_result->triangles[i].verticeIndexes[j] = triangle->index_xyz[j]; - o_result->triangles[i].coordIndexes[j] = triangle->index_st[j]; + for (u32 x = 0; x < framesCount; x++) + { + o_result[x].getMaterial(0).setVertexFace((i * 3) + j, triangle->index_xyz[j]); + o_result[x].getMaterial(0).setSTFace((i * 3) + j, triangle->index_st[j]); + o_result[x].getMaterial(0).setNormalFace((i * 3) + j, triangle->index_xyz[j]); + } } } delete[] buffer; fclose(file); PRINT_LOG("MD2 file loaded!"); + + delete[] finalPath; + return framesCount; } diff --git a/src/engine/loaders/obj_loader.cpp b/src/engine/loaders/obj_loader.cpp index f687241..73bc68b 100644 --- a/src/engine/loaders/obj_loader.cpp +++ b/src/engine/loaders/obj_loader.cpp @@ -26,10 +26,6 @@ ObjLoader::~ObjLoader() {} // Methods // ---- -/** Parse .obj file, allocate output data and store it. Multitexture support - * 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(MeshFrame *o_result, char *t_filename, float t_scale, u8 t_invertT) { FILE *file = fopen(t_filename, "rb"); diff --git a/src/engine/models/md2_model.cpp b/src/engine/models/md2_model.cpp deleted file mode 100644 index 505bbaf..0000000 --- a/src/engine/models/md2_model.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2020, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#include "../include/models/md2_model.hpp" - -#include "../include/utils/anorms.hpp" -#include "../include/utils/string.hpp" -#include "../include/utils/debug.hpp" - -// ---- -// Constructors/Destructors -// ---- - -MD2Model::MD2Model(char *t_md2File) -{ - filename = String::createWithoutExtension(t_md2File); - - verticesPerFrameCount = 0; - coordinatesCount = 0; - trianglesCount = 0; - framesCount = 0; - - animState.startFrame = 0; - animState.endFrame = 0; - animState.interpolation = 0.0F; - animState.animType = 0; - animState.currentFrame = 0; - animState.nextFrame = 0; - animState.speed = 0.1F; - - vertices = 0; - coordinates = 0; - normalIndexes = 0; - triangles = 0; -} - -MD2Model::~MD2Model() -{ - delete[] vertices; - delete[] normalIndexes; - delete[] coordinates; - delete[] triangles; -} - -// ---- -// Methods -// ---- - -u32 MD2Model::getCurrentFrameData(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled) -{ - u32 i = 0; - 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; - } - for (u32 iTri = 0; iTri < trianglesCount; iTri++) - { - for (u32 iVert = 0; iVert < 3; iVert++) - { - const u32 CURR_VERTICE = - triangles[iTri].verticeIndexes[iVert] + - (verticesPerFrameCount * animState.currentFrame); - - if (animState.startFrame == animState.endFrame) - { - calc3Vectors[iVert].x = vertices[CURR_VERTICE].x * t_scale; - calc3Vectors[iVert].y = vertices[CURR_VERTICE].y * t_scale; - calc3Vectors[iVert].z = vertices[CURR_VERTICE].z * t_scale; - } - else - { - const u32 NEXT_VERTICE = - triangles[iTri].verticeIndexes[iVert] + - (verticesPerFrameCount * animState.nextFrame); - calc3Vectors[iVert].setByLerp(vertices[CURR_VERTICE], vertices[NEXT_VERTICE], animState.interpolation, t_scale); - } - } - if (!t_shouldBeBackfaceCulled || Vector3::shouldBeBackfaceCulled(&t_cameraPos, &calc3Vectors[2], &calc3Vectors[1], &calc3Vectors[0]) == 0) - for (u32 iVert = 0; iVert < 3; iVert++) - { - const u32 CURR_VERTICE = - triangles[iTri].verticeIndexes[iVert] + - (verticesPerFrameCount * animState.currentFrame); - - const u32 CURR_COORD = triangles[iTri].coordIndexes[iVert]; - - o_vertices[i][0] = calc3Vectors[iVert].x; - o_vertices[i][1] = calc3Vectors[iVert].y; - o_vertices[i][2] = calc3Vectors[iVert].z; - o_vertices[i][3] = 1.0F; - - o_normals[i][0] = ANORMS[normalIndexes[CURR_VERTICE]][0]; - o_normals[i][1] = ANORMS[normalIndexes[CURR_VERTICE]][1]; - o_normals[i][2] = ANORMS[normalIndexes[CURR_VERTICE]][2]; - o_normals[i][3] = 1.0F; - - o_coordinates[i][0] = coordinates[CURR_COORD].x; - o_coordinates[i][1] = 1.0F - coordinates[CURR_COORD].y; - o_coordinates[i][2] = 1.0F; - o_coordinates[i][3] = 1.0F; - i++; - } - } - return i; -} - -void MD2Model::allocateMemory() -{ - vertices = new Vector3[verticesPerFrameCount * framesCount]; - normalIndexes = new u32[verticesPerFrameCount * framesCount]; - coordinates = new Point[coordinatesCount]; - triangles = new MD2Triangle[trianglesCount]; -} diff --git a/src/engine/models/mesh.cpp b/src/engine/models/mesh.cpp index b3d98a8..9c493fc 100644 --- a/src/engine/models/mesh.cpp +++ b/src/engine/models/mesh.cpp @@ -9,7 +9,6 @@ */ #include "../include/models/mesh.hpp" - #include "../include/loaders/obj_loader.hpp" #include "../include/loaders/md2_loader.hpp" #include "../include/loaders/dff_loader.hpp" @@ -33,6 +32,7 @@ Mesh::Mesh() isSpecInitialized = false; id = rand() % 1000000; scale = 1.0F; + framesCount = 0; animState.startFrame = 0; animState.endFrame = 0; animState.interpolation = 0.0F; @@ -46,8 +46,6 @@ Mesh::Mesh() Mesh::~Mesh() { // TODO - if (isMd2Loaded) - delete md2; } // ---- @@ -74,8 +72,10 @@ void Mesh::animate() } } -u32 Mesh::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled) +u32 Mesh::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, Vector3 &t_cameraPos) { + if (!frames) + PRINT_ERR("Can't draw, because no mesh data was loaded!"); #define CURR_FRAME frames[animState.currentFrame] #define NEXT_FRAME frames[animState.nextFrame] #define MATERIAL CURR_FRAME.getMaterial(t_materialIndex) @@ -87,9 +87,9 @@ u32 Mesh::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals { for (u32 vertI = 0; vertI < 3; vertI++) if (animState.startFrame != animState.endFrame) - calc3Vectors[vertI].setByLerp(CURR_VERT, NEXT_VERT, animState.interpolation, t_scale); + calc3Vectors[vertI].setByLerp(CURR_VERT, NEXT_VERT, animState.interpolation, scale); - if (!t_shouldBeBackfaceCulled || + if (!shouldBeBackfaceCulled || Vector3::shouldBeBackfaceCulled(&t_cameraPos, &calc3Vectors[2], &calc3Vectors[1], &calc3Vectors[0])) for (u32 vertI = 0; vertI < 3; vertI++) @@ -123,43 +123,22 @@ u32 Mesh::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals return addedFaces; } -void Mesh::loadDff(char *t_subfolder, char *t_dffFile, Vector3 &t_initPos, float t_scale) +void Mesh::loadDff(char *t_subfolder, char *t_dffFile, Vector3 &t_initPos, float t_scale, u8 t_invertT) { - createSpecIfNotCreated(); DffLoader loader = DffLoader(); char *dffPath = String::createConcatenated(t_subfolder, t_dffFile); framesCount = 1; frames = new MeshFrame[1]; - loader.load(frames, dffPath, t_scale, true); + loader.load(frames, dffPath, t_scale, t_invertT); delete[] dffPath; position = t_initPos; setVerticesReference(getFacesCount(), frames[0].getVertices()); setDefaultColor(); isObjLoaded = true; - // position = t_initPos; - // setVerticesReference( - // dff->clump.geometryList.geometries[0].data.dataHeader.vertexCount, - // dff->clump.geometryList.geometries[0].data.vertexInformation); - // setDefaultColor(); - // isDffLoaded = true; - // loadTextures(t_subfolder, ".bmp"); } -// void Mesh::setDff(Vector3 &t_initPos, DffModel *t_dffModel) -// { -// position = t_initPos; -// isSpecInitialized = 1; -// dff = t_dffModel; -// setVerticesReference( -// dff->clump.geometryList.geometries[0].data.dataHeader.vertexCount, -// dff->clump.geometryList.geometries[0].data.vertexInformation); -// setDefaultColor(); -// isDffLoaded = true; -// } - void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float t_scale, u16 t_framesCount, u8 t_invertT) { - createSpecIfNotCreated(); ObjLoader loader = ObjLoader(); framesCount = t_framesCount; frames = new MeshFrame[framesCount]; @@ -195,26 +174,9 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float 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() -{ -} - void Mesh::setAnimSpeed(float t_value) { - if (isMd2Loaded == 1) - md2->animState.speed = t_value; - else - PRINT_ERR("Animation speed set is not possible, because no md2 3D model was loaded!"); + animState.speed = t_value; } void Mesh::setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef) @@ -225,13 +187,11 @@ void Mesh::setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef) u32 Mesh::getVertexCount() { - if (isMd2Loaded) - return md2->trianglesCount * 3; - else if (isObjLoaded) + if (framesCount > 0) return getFacesCount(); else { - PRINT_ERR("Can't get vertex count, because no 3D model was loaded!"); + PRINT_ERR("Can't get vertex count, because mesh data was loaded!"); return 0; } } @@ -246,47 +206,18 @@ void Mesh::setDefaultWrapSettings(texwrap_t &t_wrapSettings) // t_wrapSettings.minv = 0; } -void Mesh::loadTextures(char *t_subfolder, char *t_extension) +/** Set object position and specification + * @param t_md2File without extension +*/ +void Mesh::loadMD2(char *t_subfolder, char *t_md2File, Vector3 &t_initPos, float t_scale, u8 t_invertT) { - BmpLoader bmpLoader = BmpLoader(); - if (isObjLoaded) - { - } - else if (isMd2Loaded) - { - textures = new MeshTexture[1]; - bmpLoader.load(textures[0], t_subfolder, md2->filename, t_extension); - // setDefaultWrapSettings(spec->textures[0].wrapSettings); - } - else - PRINT_ERR("Can't load textures, because no 3D model was loaded!"); -} - -// TODO refactor -u32 Mesh::getDrawData(u32 splitIndex, VECTOR *t_vertices, VECTOR *t_normals, VECTOR *t_coordinates, Vector3 &t_cameraPos) -{ - if (isMd2Loaded) - return md2->getCurrentFrameData(t_vertices, t_normals, t_coordinates, t_cameraPos, scale, shouldBeBackfaceCulled); - else if (isObjLoaded) - return 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!"); - return 0; -} - -/** Set object position and specification */ -void Mesh::loadMD2(char *t_subfolder, char *t_md2File, Vector3 &t_initPos, float t_scale) -{ - createSpecIfNotCreated(); - md2 = new MD2Model(t_md2File); MD2Loader loader = MD2Loader(); + frames = new MeshFrame[26]; + framesCount = loader.load(frames, t_subfolder, t_md2File, t_scale, t_invertT); position = t_initPos; - char *md2Path = String::createConcatenated(t_subfolder, t_md2File); - loader.load(md2, md2Path, t_scale); - delete[] md2Path; - setVerticesReference(md2->verticesPerFrameCount * md2->framesCount, md2->vertices); + setVerticesReference(getFacesCount(), frames[0].getVertices()); setDefaultColor(); - isMd2Loaded = true; - loadTextures(t_subfolder, ".bmp"); + isObjLoaded = true; } /** Set's default object color + no transparency */ @@ -333,18 +264,17 @@ void Mesh::getMinMax(Vector3 *t_min, Vector3 *t_max) void Mesh::playAnimation(u32 t_startFrame, u32 t_endFrame) { - if (isMd2Loaded == 1) - { - md2->animState.startFrame = t_startFrame; - md2->animState.endFrame = t_endFrame; - } - else if (isObjLoaded == 1) + if (framesCount > 1) { + if (t_endFrame >= framesCount) + PRINT_ERR("End frame value is too high. Valid range: (0, getFramesCount()-1)"); animState.startFrame = t_startFrame; animState.endFrame = t_endFrame; } - else - PRINT_ERR("Animation is only supported in MD2/OBJ format!"); + else if (framesCount == 0) + PRINT_ERR("Cant play animation, because no mesh data was loaded!"); + else if (framesCount == 1) + PRINT_ERR("Cant play animation, because this mesh have only one frame."); } /** Returns next farthest vertex of 3D object diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 3367242..cf5e2ff 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -201,8 +201,6 @@ void Renderer::draw(Mesh **t_meshes, u16 t_amount, LightBulb *t_bulbs, u16 t_bul draw(t_meshes[i], t_bulbs, t_bulbsCount); } -u8 once = true; - /** PATH1 Single + lighting */ void Renderer::draw(Mesh *t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) { @@ -221,12 +219,9 @@ void Renderer::draw(Mesh *t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) for (u32 i = 0; i < t_mesh->getMaterialsCount(); i++) { changeTexture(t_mesh, t_mesh->getMaterial(i).getId()); - if (once) - printf("Changing material to:%d\n", t_mesh->getMaterial(i).getId()); vertCount = t_mesh->getDrawData(i, vertices, normals, coordinates, *renderData.cameraPosition); vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount, &textureBuffer); } - once = false; } else if (t_mesh->isMd2Loaded) { diff --git a/src/samples/ari/Makefile b/src/samples/ari/Makefile index d2ef0c4..e2b12b3 100644 --- a/src/samples/ari/Makefile +++ b/src/samples/ari/Makefile @@ -5,7 +5,6 @@ EE_OBJS = \ ../../engine/models/math/plane.o \ ../../engine/models/math/point.o \ ../../engine/models/math/vector3.o \ - ../../engine/models/md2_model.o \ ../../engine/models/mesh_frame.o \ ../../engine/models/mesh_material.o \ ../../engine/models/mesh.o \ @@ -51,7 +50,7 @@ all: $(EE_BIN) $(EE_DVP) $< -o $@ clean: - rm -f $(EE_BIN) $(EE_OBJS) + rm -f $(EE_OBJS) include $(PS2SDK)/samples/Makefile.pref include $(PS2SDK)/samples/Makefile.eeglobal diff --git a/src/samples/ari/ari.cpp b/src/samples/ari/ari.cpp index 5eb04ea..c75316f 100644 --- a/src/samples/ari/ari.cpp +++ b/src/samples/ari/ari.cpp @@ -79,14 +79,23 @@ void Ari::onInit() texRepo = engine.renderer->getTextureRepository(); // engine.audio.play(); - Vector3 islandPos = Vector3(0.0F, 10.0F, 0.0F); - island = new Mesh(); - island->rotation.x = -1.6F; - island->loadDff("sunnyisl/", "sunnyisl.dff", islandPos, 0.1F); - texRepo->addByMesh("sunnyisl/", *island); - island->shouldBeFrustumCulled = false; + // Vector3 islandPos = Vector3(0.0F, 10.0F, 0.0F); + // island = new Mesh(); + // island->rotation.x = -1.6F; + // island->loadDff("sunnyisl/", "sunnyisl.dff", islandPos, 0.1F); + // texRepo->addByMesh("sunnyisl/", *island); + // island->shouldBeFrustumCulled = false; // island->shouldBeBackfaceCulled = true; + test = new Mesh(); + Vector3 initPos = Vector3(0.00F, 00.00F, 0.00F); + test->loadMD2("warrior/", "warrior", initPos, 0.2F, true); + texRepo->addByMesh("warrior/", *test); + test->rotation.x = -1.6F; + test->playAnimation(0, 25); + // test->shouldBeBackfaceCulled = false; + // test->shouldBeFrustumCulled = false; + // islandAddons = new Mesh(); // islandAddons->rotation.x = -1.6F; // islandAddons->loadDff("sunnyisl/", "sunnyisl3.dff", islandPos, 0.1F); @@ -104,18 +113,19 @@ void Ari::onInit() // test->playAnimation(0, 0); // test->shouldBeBackfaceCulled = true; - // TODO 1 - Merge dff/md2 - // TODO 2 - Refactor - // TODO 3 - Destructor - // TODO 4 - Mesh copy(); + // TODO 1 - Merge md2 + // TODO 2 - Refactor mesh, loaders + // TODO 3 - const & in parameters + // TODO 4 - Destructor + // TODO 5 - Mesh copy(); // test->shouldBeFrustumCulled = true; - skybox = new Mesh(); - Vector3 skyboxPos = Vector3(0.0F, 10.0F, 0.0F); - skybox->loadObj("skybox/", "skybox", skyboxPos, 80.0F, 1, false); - skybox->shouldBeFrustumCulled = true; - texRepo->addByMesh("skybox/", *skybox); + // skybox = new Mesh(); + // Vector3 skyboxPos = Vector3(0.0F, 10.0F, 0.0F); + // skybox->loadObj("skybox/", "skybox", skyboxPos, 80.0F, 1, false); + // skybox->shouldBeFrustumCulled = true; + // texRepo->addByMesh("skybox/", *skybox); // waterFloors = new Mesh *[WATER_TILES_COUNT]; // spirals = new Point[WATER_TILES_COUNT]; @@ -153,9 +163,9 @@ void Ari::onUpdate() if (engine.pad.isCrossClicked) printf("FPS:%f\n", engine.fps); camera->update(engine.pad, player->mesh); - // engine.renderer->draw(test); - engine.renderer->draw(skybox); - engine.renderer->draw(island); + engine.renderer->draw(test); + // engine.renderer->draw(skybox); + // engine.renderer->draw(island); // engine.renderer->draw(islandAddons); // // engine.renderer->draw(&player->mesh); // for (u8 i = 0; i < WATER_TILES_COUNT; i++)