removed md2_model

This commit is contained in:
Sandro Sobczyński
2020-11-01 18:13:24 +01:00
parent 200fa3e4d5
commit 138ab762ba
14 changed files with 154 additions and 358 deletions
+3 -2
View File
@@ -11,7 +11,7 @@
#ifndef _TYRA_MD2_LOADER_ #ifndef _TYRA_MD2_LOADER_
#define _TYRA_MD2_LOADER_ #define _TYRA_MD2_LOADER_
#include "../models/md2_model.hpp" #include "../models/mesh_frame.hpp"
#include <stdio.h> #include <stdio.h>
// magic number "IDP2" or 844121161 // magic number "IDP2" or 844121161
@@ -83,7 +83,8 @@ public:
MD2Loader(); MD2Loader();
~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: private:
}; };
@@ -22,6 +22,10 @@ public:
ObjLoader(); ObjLoader();
~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); void load(MeshFrame *o_result, char *t_fileName, float t_scale, u8 t_invertT);
private: private:
-48
View File
@@ -1,48 +0,0 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#ifndef _TYRA_MD2_MODEL_
#define _TYRA_MD2_MODEL_
#include <tamtypes.h>
#include <math3d.h>
#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
+6 -10
View File
@@ -8,18 +8,18 @@
# Sandro Sobczyński <sandro.sobczynski@gmail.com> # Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/ */
#ifndef _TYRA_OBJECT3D_ #ifndef _TYRA_MESH_
#define _TYRA_OBJECT3D_ #define _TYRA_MESH_
#include "math/vector3.hpp" #include "math/vector3.hpp"
#include "math/plane.hpp" #include "math/plane.hpp"
#include "md2_model.hpp"
#include "./mesh_texture.hpp" #include "./mesh_texture.hpp"
#include "./mesh_frame.hpp" #include "./mesh_frame.hpp"
#include <tamtypes.h> #include <tamtypes.h>
#include <draw_types.h> #include <draw_types.h>
#include <draw_buffers.h> #include <draw_buffers.h>
#include <draw_sampling.h> #include <draw_sampling.h>
#include "./anim_state.hpp"
/** Class which have common types for all 3D objects */ /** Class which have common types for all 3D objects */
class Mesh class Mesh
@@ -29,7 +29,6 @@ public:
Vector3 position, rotation; Vector3 position, rotation;
u8 shouldBeLighted, shouldBeBackfaceCulled, shouldBeFrustumCulled; u8 shouldBeLighted, shouldBeBackfaceCulled, shouldBeFrustumCulled;
MD2Model *md2;
float scale; float scale;
color_t color; 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 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 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 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 getMinMax(Vector3 *t_min, Vector3 *t_max);
void playAnimation(u32 t_startFrame, u32 t_endFrame); void playAnimation(u32 t_startFrame, u32 t_endFrame);
u32 getVertexCount(); u32 getVertexCount();
void setAnimSpeed(float t_value); 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 isInFrustum(Plane *t_frustumPlanes);
u8 isMd2Loaded, isObjLoaded, isSpecInitialized; u8 isMd2Loaded, isObjLoaded, isSpecInitialized;
@@ -60,7 +58,7 @@ public:
MeshFrame *frames; MeshFrame *frames;
AnimState animState; AnimState animState;
void animate(); 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(); u32 getFacesCount();
u8 isMemoryAllocated; u8 isMemoryAllocated;
@@ -84,9 +82,7 @@ private:
void setDefaultWrapSettings(texwrap_t &t_wrapSettings); void setDefaultWrapSettings(texwrap_t &t_wrapSettings);
u32 verticesCount; u32 verticesCount;
Vector3 *vertices; Vector3 *vertices;
void loadTextures(char *t_subfolder, char *t_extension);
void setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef); void setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef);
void createSpecIfNotCreated();
void setDefaultColor(); void setDefaultColor();
void getFarthestVertex(Vector3 *o_result, int t_offset); void getFarthestVertex(Vector3 *o_result, int t_offset);
}; };
+16 -1
View File
@@ -15,7 +15,22 @@
#include <math3d.h> #include <math3d.h>
#include <stdio.h> #include <stdio.h>
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_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 #endif
+2 -2
View File
@@ -61,8 +61,8 @@ void DffLoader::convert(MeshFrame *frames, RwClump &t_clump, u8 t_invertT)
MeshFrame *frame = &frames[0]; MeshFrame *frame = &frames[0];
u32 vertNormalStCount = GEOMETRY.data.dataHeader.vertexCount; u32 vertNormalStCount = GEOMETRY.data.dataHeader.vertexCount;
frame->allocateVertices(vertNormalStCount); frame->allocateVertices(vertNormalStCount);
frame->allocateNormals(vertNormalStCount); frame->allocateNormals(vertNormalStCount); // can be optimized
frame->allocateSTs(vertNormalStCount); frame->allocateSTs(vertNormalStCount); // can be optimized
Vector3 tempVec = Vector3(); Vector3 tempVec = Vector3();
Point tempPoint = Point(); Point tempPoint = Point();
+68 -46
View File
@@ -10,7 +10,9 @@
#include "../include/loaders/md2_loader.hpp" #include "../include/loaders/md2_loader.hpp"
#include "../include/utils/string.hpp"
#include "../include/utils/debug.hpp" #include "../include/utils/debug.hpp"
#include "../include/loaders/anorms.hpp"
#include "../include/models/math/vector3.hpp" #include "../include/models/math/vector3.hpp"
#include "../include/models/math/point.hpp" #include "../include/models/math/point.hpp"
#include <string.h> #include <string.h>
@@ -29,100 +31,120 @@ MD2Loader::~MD2Loader() {}
// ---- // ----
/** Load, parse .obj file and load data into given MeshSpec */ /** 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"); PRINT_LOG("Loading new MD2 file");
md2_t header; // md2 header md2_t header; // md2 header
char *buffer; // buffer storing frame data char *buffer; // buffer storing frame data
FILE *file = fopen(t_filename, "rb"); FILE *file = fopen(finalPath, "rb");
if (file == NULL) if (file == NULL)
{
PRINT_ERR("Failed to load .md2 file!"); PRINT_ERR("Failed to load .md2 file!");
return 0;
}
fread((char *)&header, sizeof(md2_t), 1, file); fread((char *)&header, sizeof(md2_t), 1, file);
if ((header.ident != MD2_IDENT) && (header.version != MD2_VERSION)) if ((header.ident != MD2_IDENT) && (header.version != MD2_VERSION))
{
PRINT_ERR("This MD2 file was not in correct format!"); PRINT_ERR("This MD2 file was not in correct format!");
return 0;
}
o_result->framesCount = header.num_frames; u32 framesCount = header.num_frames;
o_result->verticesPerFrameCount = header.num_xyz; u32 vertexCount = header.num_xyz;
o_result->coordinatesCount = header.num_st; u32 stsCount = header.num_st;
o_result->trianglesCount = header.num_tris; u32 trianglesCount = header.num_tris;
printf("MD2 - Frames: %d\n", header.num_frames); // o_result = new MeshFrame[framesCount];
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->allocateMemory(); buffer = new char[framesCount * header.framesize];
buffer = new char[o_result->framesCount * header.framesize];
fseek(file, header.ofs_frames, SEEK_SET); 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 frame_t *frame; // temporary vars
Vector3 *ptrVerts; Vector3 tempVec = Vector3();
u32 *ptrNormals;
// vertex array initialization for (u32 j = 0; j < framesCount; j++)
for (u32 j = 0; j < o_result->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]; 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; tempVec.set(
ptrVerts[i].y = ((frame->verts[i].v[1] * frame->scale[1]) + frame->translate[1]) * t_scale; ((frame->verts[i].v[0] * frame->scale[0]) + frame->translate[0]) * t_scale,
ptrVerts[i].z = ((frame->verts[i].v[2] * frame->scale[2]) + frame->translate[2]) * 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; 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); 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; 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]; texCoord = (texCoord_t *)&buffer[sizeof(texCoord_t) * i];
o_result->coordinates[i].x = (float)texCoord->s / header.skinwidth; tempPoint.set(
o_result->coordinates[i].y = (float)texCoord->t / header.skinheight; (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; delete[] buffer;
printf("Last tex coord(%d): S:%f T:%f\n", buffer = new char[trianglesCount * sizeof(triangle_t)];
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)];
fseek(file, header.ofs_tris, SEEK_SET); 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 triangle_t *triangle; // temporary var
// vertex array initialization // 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]; triangle = (triangle_t *)&buffer[sizeof(triangle_t) * i];
for (u8 j = 0; j < 3; j++) for (u8 j = 0; j < 3; j++)
{ {
o_result->triangles[i].verticeIndexes[j] = triangle->index_xyz[j]; for (u32 x = 0; x < framesCount; x++)
o_result->triangles[i].coordIndexes[j] = triangle->index_st[j]; {
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; delete[] buffer;
fclose(file); fclose(file);
PRINT_LOG("MD2 file loaded!"); PRINT_LOG("MD2 file loaded!");
delete[] finalPath;
return framesCount;
} }
-4
View File
@@ -26,10 +26,6 @@ ObjLoader::~ObjLoader() {}
// Methods // 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) void ObjLoader::load(MeshFrame *o_result, char *t_filename, float t_scale, u8 t_invertT)
{ {
FILE *file = fopen(t_filename, "rb"); FILE *file = fopen(t_filename, "rb");
-124
View File
@@ -1,124 +0,0 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#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];
}
+26 -96
View File
@@ -9,7 +9,6 @@
*/ */
#include "../include/models/mesh.hpp" #include "../include/models/mesh.hpp"
#include "../include/loaders/obj_loader.hpp" #include "../include/loaders/obj_loader.hpp"
#include "../include/loaders/md2_loader.hpp" #include "../include/loaders/md2_loader.hpp"
#include "../include/loaders/dff_loader.hpp" #include "../include/loaders/dff_loader.hpp"
@@ -33,6 +32,7 @@ Mesh::Mesh()
isSpecInitialized = false; isSpecInitialized = false;
id = rand() % 1000000; id = rand() % 1000000;
scale = 1.0F; scale = 1.0F;
framesCount = 0;
animState.startFrame = 0; animState.startFrame = 0;
animState.endFrame = 0; animState.endFrame = 0;
animState.interpolation = 0.0F; animState.interpolation = 0.0F;
@@ -46,8 +46,6 @@ Mesh::Mesh()
Mesh::~Mesh() Mesh::~Mesh()
{ {
// TODO // 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 CURR_FRAME frames[animState.currentFrame]
#define NEXT_FRAME frames[animState.nextFrame] #define NEXT_FRAME frames[animState.nextFrame]
#define MATERIAL CURR_FRAME.getMaterial(t_materialIndex) #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++) for (u32 vertI = 0; vertI < 3; vertI++)
if (animState.startFrame != animState.endFrame) 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])) Vector3::shouldBeBackfaceCulled(&t_cameraPos, &calc3Vectors[2], &calc3Vectors[1], &calc3Vectors[0]))
for (u32 vertI = 0; vertI < 3; vertI++) 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; 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(); DffLoader loader = DffLoader();
char *dffPath = String::createConcatenated(t_subfolder, t_dffFile); char *dffPath = String::createConcatenated(t_subfolder, t_dffFile);
framesCount = 1; framesCount = 1;
frames = new MeshFrame[1]; frames = new MeshFrame[1];
loader.load(frames, dffPath, t_scale, true); loader.load(frames, dffPath, t_scale, t_invertT);
delete[] dffPath; delete[] dffPath;
position = t_initPos; position = t_initPos;
setVerticesReference(getFacesCount(), frames[0].getVertices()); setVerticesReference(getFacesCount(), frames[0].getVertices());
setDefaultColor(); setDefaultColor();
isObjLoaded = true; 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) 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(); ObjLoader loader = ObjLoader();
framesCount = t_framesCount; framesCount = t_framesCount;
frames = new MeshFrame[framesCount]; frames = new MeshFrame[framesCount];
@@ -195,26 +174,9 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float
isObjLoaded = true; 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) void Mesh::setAnimSpeed(float t_value)
{ {
if (isMd2Loaded == 1) animState.speed = t_value;
md2->animState.speed = t_value;
else
PRINT_ERR("Animation speed set is not possible, because no md2 3D model was loaded!");
} }
void Mesh::setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef) 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() u32 Mesh::getVertexCount()
{ {
if (isMd2Loaded) if (framesCount > 0)
return md2->trianglesCount * 3;
else if (isObjLoaded)
return getFacesCount(); return getFacesCount();
else 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; return 0;
} }
} }
@@ -246,47 +206,18 @@ void Mesh::setDefaultWrapSettings(texwrap_t &t_wrapSettings)
// t_wrapSettings.minv = 0; // 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(); MD2Loader loader = MD2Loader();
frames = new MeshFrame[26];
framesCount = loader.load(frames, t_subfolder, t_md2File, t_scale, t_invertT);
position = t_initPos; position = t_initPos;
char *md2Path = String::createConcatenated(t_subfolder, t_md2File); setVerticesReference(getFacesCount(), frames[0].getVertices());
loader.load(md2, md2Path, t_scale);
delete[] md2Path;
setVerticesReference(md2->verticesPerFrameCount * md2->framesCount, md2->vertices);
setDefaultColor(); setDefaultColor();
isMd2Loaded = true; isObjLoaded = true;
loadTextures(t_subfolder, ".bmp");
} }
/** Set's default object color + no transparency */ /** 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) void Mesh::playAnimation(u32 t_startFrame, u32 t_endFrame)
{ {
if (isMd2Loaded == 1) if (framesCount > 1)
{
md2->animState.startFrame = t_startFrame;
md2->animState.endFrame = t_endFrame;
}
else if (isObjLoaded == 1)
{ {
if (t_endFrame >= framesCount)
PRINT_ERR("End frame value is too high. Valid range: (0, getFramesCount()-1)");
animState.startFrame = t_startFrame; animState.startFrame = t_startFrame;
animState.endFrame = t_endFrame; animState.endFrame = t_endFrame;
} }
else else if (framesCount == 0)
PRINT_ERR("Animation is only supported in MD2/OBJ format!"); 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 /** Returns next farthest vertex of 3D object
-5
View File
@@ -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); draw(t_meshes[i], t_bulbs, t_bulbsCount);
} }
u8 once = true;
/** PATH1 Single + lighting */ /** PATH1 Single + lighting */
void Renderer::draw(Mesh *t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) 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++) for (u32 i = 0; i < t_mesh->getMaterialsCount(); i++)
{ {
changeTexture(t_mesh, t_mesh->getMaterial(i).getId()); 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); 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); vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, t_mesh, t_bulbs, t_bulbsCount, &textureBuffer);
} }
once = false;
} }
else if (t_mesh->isMd2Loaded) else if (t_mesh->isMd2Loaded)
{ {
+1 -2
View File
@@ -5,7 +5,6 @@ EE_OBJS = \
../../engine/models/math/plane.o \ ../../engine/models/math/plane.o \
../../engine/models/math/point.o \ ../../engine/models/math/point.o \
../../engine/models/math/vector3.o \ ../../engine/models/math/vector3.o \
../../engine/models/md2_model.o \
../../engine/models/mesh_frame.o \ ../../engine/models/mesh_frame.o \
../../engine/models/mesh_material.o \ ../../engine/models/mesh_material.o \
../../engine/models/mesh.o \ ../../engine/models/mesh.o \
@@ -51,7 +50,7 @@ all: $(EE_BIN)
$(EE_DVP) $< -o $@ $(EE_DVP) $< -o $@
clean: clean:
rm -f $(EE_BIN) $(EE_OBJS) rm -f $(EE_OBJS)
include $(PS2SDK)/samples/Makefile.pref include $(PS2SDK)/samples/Makefile.pref
include $(PS2SDK)/samples/Makefile.eeglobal include $(PS2SDK)/samples/Makefile.eeglobal
+28 -18
View File
@@ -79,14 +79,23 @@ void Ari::onInit()
texRepo = engine.renderer->getTextureRepository(); texRepo = engine.renderer->getTextureRepository();
// engine.audio.play(); // engine.audio.play();
Vector3 islandPos = Vector3(0.0F, 10.0F, 0.0F); // Vector3 islandPos = Vector3(0.0F, 10.0F, 0.0F);
island = new Mesh(); // island = new Mesh();
island->rotation.x = -1.6F; // island->rotation.x = -1.6F;
island->loadDff("sunnyisl/", "sunnyisl.dff", islandPos, 0.1F); // island->loadDff("sunnyisl/", "sunnyisl.dff", islandPos, 0.1F);
texRepo->addByMesh("sunnyisl/", *island); // texRepo->addByMesh("sunnyisl/", *island);
island->shouldBeFrustumCulled = false; // island->shouldBeFrustumCulled = false;
// island->shouldBeBackfaceCulled = true; // 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 = new Mesh();
// islandAddons->rotation.x = -1.6F; // islandAddons->rotation.x = -1.6F;
// islandAddons->loadDff("sunnyisl/", "sunnyisl3.dff", islandPos, 0.1F); // islandAddons->loadDff("sunnyisl/", "sunnyisl3.dff", islandPos, 0.1F);
@@ -104,18 +113,19 @@ void Ari::onInit()
// test->playAnimation(0, 0); // test->playAnimation(0, 0);
// test->shouldBeBackfaceCulled = true; // test->shouldBeBackfaceCulled = true;
// TODO 1 - Merge dff/md2 // TODO 1 - Merge md2
// TODO 2 - Refactor // TODO 2 - Refactor mesh, loaders
// TODO 3 - Destructor // TODO 3 - const & in parameters
// TODO 4 - Mesh copy(); // TODO 4 - Destructor
// TODO 5 - Mesh copy();
// test->shouldBeFrustumCulled = true; // test->shouldBeFrustumCulled = true;
skybox = new Mesh(); // skybox = new Mesh();
Vector3 skyboxPos = Vector3(0.0F, 10.0F, 0.0F); // Vector3 skyboxPos = Vector3(0.0F, 10.0F, 0.0F);
skybox->loadObj("skybox/", "skybox", skyboxPos, 80.0F, 1, false); // skybox->loadObj("skybox/", "skybox", skyboxPos, 80.0F, 1, false);
skybox->shouldBeFrustumCulled = true; // skybox->shouldBeFrustumCulled = true;
texRepo->addByMesh("skybox/", *skybox); // texRepo->addByMesh("skybox/", *skybox);
// waterFloors = new Mesh *[WATER_TILES_COUNT]; // waterFloors = new Mesh *[WATER_TILES_COUNT];
// spirals = new Point[WATER_TILES_COUNT]; // spirals = new Point[WATER_TILES_COUNT];
@@ -153,9 +163,9 @@ void Ari::onUpdate()
if (engine.pad.isCrossClicked) if (engine.pad.isCrossClicked)
printf("FPS:%f\n", engine.fps); printf("FPS:%f\n", engine.fps);
camera->update(engine.pad, player->mesh); camera->update(engine.pad, player->mesh);
// engine.renderer->draw(test); engine.renderer->draw(test);
engine.renderer->draw(skybox); // engine.renderer->draw(skybox);
engine.renderer->draw(island); // engine.renderer->draw(island);
// engine.renderer->draw(islandAddons); // engine.renderer->draw(islandAddons);
// // engine.renderer->draw(&player->mesh); // // engine.renderer->draw(&player->mesh);
// for (u8 i = 0; i < WATER_TILES_COUNT; i++) // for (u8 i = 0; i < WATER_TILES_COUNT; i++)