added skybox and multitexture feature in .obj
This commit is contained in:
@@ -25,7 +25,7 @@ public:
|
||||
void load(ObjModel *o_result, char *t_fileName, float t_scale);
|
||||
|
||||
private:
|
||||
void setObjDataQuantities(FILE *t_file, ObjModel *t_result);
|
||||
void allocateObjMemory(FILE *t_file, ObjModel *t_result);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -15,28 +15,35 @@
|
||||
#include <math3d.h>
|
||||
#include "math/vector3.hpp"
|
||||
|
||||
struct ObjMaterial
|
||||
{
|
||||
char *materialName;
|
||||
u32 facesCount;
|
||||
u32 *verticeFaces, *coordinateFaces, *normalFaces;
|
||||
};
|
||||
|
||||
/** Class which have common types for all 3D objects */
|
||||
class ObjModel
|
||||
{
|
||||
|
||||
public:
|
||||
u32 verticesCount, coordinatesCount, normalsCount, facesCount;
|
||||
u32 verticesCount, coordinatesCount, normalsCount, materialsCount;
|
||||
Vector3 *vertices __attribute__((aligned(16))),
|
||||
*coordinates __attribute__((aligned(16))),
|
||||
*normals __attribute__((aligned(16)));
|
||||
u32 *verticeFaces, *coordinateFaces, *normalFaces;
|
||||
|
||||
/** File name without extension */
|
||||
char *filename;
|
||||
ObjMaterial *materials;
|
||||
|
||||
ObjModel(char *t_objFile);
|
||||
~ObjModel();
|
||||
u32 getDrawData(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled);
|
||||
void allocateMemory();
|
||||
u32 getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled);
|
||||
u32 getFacesCount();
|
||||
u8 isMemoryAllocated;
|
||||
|
||||
private:
|
||||
void fillNextFace(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, u32 t_getI, u32 t_setI);
|
||||
u8 isMemoryAllocated;
|
||||
void fillNextFace(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, u32 t_materialI, u32 t_getI, u32 t_setI);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -51,6 +51,8 @@ void BmpLoader::load(Texture &o_texture, char *t_subfolder, char *t_name, char *
|
||||
o_texture.height = height;
|
||||
o_texture.data = new unsigned char[width * height * 3];
|
||||
printf("BMPLoader - width: %d | height: %d\n", width, height);
|
||||
if (width > 128 || height > 128)
|
||||
PRINT_ERR("Given texture is big for PS2. Please strict to 128x128 max");
|
||||
|
||||
u64 rowPadded = (width * 3 + 3) & (~3);
|
||||
|
||||
@@ -76,4 +78,5 @@ void BmpLoader::load(Texture &o_texture, char *t_subfolder, char *t_name, char *
|
||||
}
|
||||
delete[] t_path;
|
||||
delete[] data;
|
||||
// fclose(file); // wtf?
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "../include/loaders/obj_loader.hpp"
|
||||
|
||||
#include "../include/utils/debug.hpp"
|
||||
#include "../include/utils/string.hpp"
|
||||
#include <string.h>
|
||||
|
||||
// ----
|
||||
@@ -25,15 +26,18 @@ ObjLoader::~ObjLoader() {}
|
||||
// Methods
|
||||
// ----
|
||||
|
||||
/** Load, parse .obj file and load data into given MeshSpec */
|
||||
/** Parse .obj file, allocate output data and store it. Multitexture support
|
||||
* Notice: At this moment textures names are MATERIAL names from .obj file!
|
||||
*/
|
||||
void ObjLoader::load(ObjModel *o_result, char *t_filename, float t_scale)
|
||||
{
|
||||
FILE *file = fopen(t_filename, "rb");
|
||||
if (file == NULL)
|
||||
PRINT_ERR("Failed to load .obj file!");
|
||||
setObjDataQuantities(file, o_result);
|
||||
o_result->allocateMemory();
|
||||
u32 verticesI = 0, cordsI = 0, normalsI = 0, i = 0, vertexIndex[3], coordIndex[3], normalIndex[3];
|
||||
allocateObjMemory(file, o_result);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
u32 verticesI = 0, cordsI = 0, normalsI = 0, faceI = 0, vertexIndex[3], coordIndex[3], normalIndex[3];
|
||||
s16 materialsI = -1;
|
||||
while (1)
|
||||
{
|
||||
char lineHeader[128]; // read the first word of the line
|
||||
@@ -58,6 +62,13 @@ void ObjLoader::load(ObjModel *o_result, char *t_filename, float t_scale)
|
||||
fscanf(file, "%f %f %f\n", &vector.x, &vector.y, &vector.z);
|
||||
o_result->normals[normalsI++] = vector;
|
||||
}
|
||||
else if (strcmp(lineHeader, "usemtl") == 0)
|
||||
{
|
||||
char temp[30];
|
||||
fscanf(file, "%s\n", temp);
|
||||
o_result->materials[++materialsI].materialName = String::createCopy(temp);
|
||||
faceI = 0;
|
||||
}
|
||||
else if (strcmp(lineHeader, "f") == 0)
|
||||
{
|
||||
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n",
|
||||
@@ -68,18 +79,18 @@ void ObjLoader::load(ObjModel *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->verticeFaces[i] = vertexIndex[0] - 1;
|
||||
o_result->verticeFaces[i + 1] = vertexIndex[1] - 1;
|
||||
o_result->verticeFaces[i + 2] = vertexIndex[2] - 1;
|
||||
o_result->materials[materialsI].verticeFaces[faceI] = vertexIndex[0] - 1;
|
||||
o_result->materials[materialsI].verticeFaces[faceI + 1] = vertexIndex[1] - 1;
|
||||
o_result->materials[materialsI].verticeFaces[faceI + 2] = vertexIndex[2] - 1;
|
||||
|
||||
o_result->coordinateFaces[i] = coordIndex[0] - 1;
|
||||
o_result->coordinateFaces[i + 1] = coordIndex[1] - 1;
|
||||
o_result->coordinateFaces[i + 2] = coordIndex[2] - 1;
|
||||
o_result->materials[materialsI].coordinateFaces[faceI] = coordIndex[0] - 1;
|
||||
o_result->materials[materialsI].coordinateFaces[faceI + 1] = coordIndex[1] - 1;
|
||||
o_result->materials[materialsI].coordinateFaces[faceI + 2] = coordIndex[2] - 1;
|
||||
|
||||
o_result->normalFaces[i] = normalIndex[0] - 1;
|
||||
o_result->normalFaces[i + 1] = normalIndex[1] - 1;
|
||||
o_result->normalFaces[i + 2] = normalIndex[2] - 1;
|
||||
i += 3;
|
||||
o_result->materials[materialsI].normalFaces[faceI] = normalIndex[0] - 1;
|
||||
o_result->materials[materialsI].normalFaces[faceI + 1] = normalIndex[1] - 1;
|
||||
o_result->materials[materialsI].normalFaces[faceI + 2] = normalIndex[2] - 1;
|
||||
faceI += 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,15 +100,14 @@ void ObjLoader::load(ObjModel *o_result, char *t_filename, float t_scale)
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
/** Calculate how many vertices(v), coordinates(vt), normals(vn) and faces(f) have .obj file
|
||||
* When done, file read offset is resetted to the begginning of file
|
||||
*/
|
||||
void ObjLoader::setObjDataQuantities(FILE *t_file, ObjModel *o_result)
|
||||
/** Calculate how many vertices(v), coordinates(vt), normals(vn) and faces(f) have .obj file */
|
||||
void ObjLoader::allocateObjMemory(FILE *t_file, ObjModel *o_result)
|
||||
{
|
||||
o_result->verticesCount = 0;
|
||||
o_result->coordinatesCount = 0;
|
||||
o_result->normalsCount = 0;
|
||||
o_result->facesCount = 0;
|
||||
o_result->materialsCount = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
char lineHeader[128];
|
||||
@@ -110,11 +120,41 @@ void ObjLoader::setObjDataQuantities(FILE *t_file, ObjModel *o_result)
|
||||
o_result->coordinatesCount += 1;
|
||||
else if (strcmp(lineHeader, "vn") == 0)
|
||||
o_result->normalsCount += 1;
|
||||
else if (strcmp(lineHeader, "f") == 0)
|
||||
o_result->facesCount += 3;
|
||||
else if (strcmp(lineHeader, "usemtl") == 0)
|
||||
o_result->materialsCount += 1;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
o_result->materials = new ObjMaterial[o_result->materialsCount];
|
||||
s16 currentMatI = -1;
|
||||
|
||||
fseek(t_file, 0, SEEK_SET);
|
||||
|
||||
while (1)
|
||||
{
|
||||
char lineHeader[128];
|
||||
int res = fscanf(t_file, "%s", lineHeader);
|
||||
if (res != EOF)
|
||||
{
|
||||
if (strcmp(lineHeader, "usemtl") == 0)
|
||||
o_result->materials[++currentMatI].facesCount = 0;
|
||||
else if (strcmp(lineHeader, "f") == 0)
|
||||
o_result->materials[currentMatI].facesCount += 3;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
for (u16 i = 0; i < o_result->materialsCount; i++)
|
||||
{
|
||||
o_result->materials[i].coordinateFaces = new u32[o_result->materials[i].facesCount];
|
||||
o_result->materials[i].normalFaces = new u32[o_result->materials[i].facesCount];
|
||||
o_result->materials[i].verticeFaces = new u32[o_result->materials[i].facesCount];
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float
|
||||
loader.load(obj, objPath, t_scale);
|
||||
delete[] objPath;
|
||||
position = t_initPos;
|
||||
setVerticesReference(obj->facesCount, obj->vertices);
|
||||
setVerticesReference(obj->getFacesCount(), obj->vertices);
|
||||
setDefaultColor();
|
||||
isObjLoaded = true;
|
||||
loadTextures(t_subfolder, ".bmp");
|
||||
@@ -100,7 +100,7 @@ void Mesh::setObj(Vector3 &t_initPos, ObjModel *t_objModel, MeshSpec *t_spec)
|
||||
spec = t_spec;
|
||||
isSpecInitialized = true;
|
||||
obj = t_objModel;
|
||||
setVerticesReference(obj->facesCount, obj->vertices);
|
||||
setVerticesReference(obj->getFacesCount(), obj->vertices);
|
||||
setDefaultColor();
|
||||
isObjLoaded = true;
|
||||
}
|
||||
@@ -134,7 +134,7 @@ u32 Mesh::getVertexCount()
|
||||
if (isMd2Loaded)
|
||||
return md2->trianglesCount * 3;
|
||||
else if (isObjLoaded)
|
||||
return obj->facesCount;
|
||||
return obj->getFacesCount();
|
||||
else if (isDffLoaded)
|
||||
return dff->clump.geometryList.geometries[0].data.dataHeader.triangleCount * 3;
|
||||
else
|
||||
@@ -157,13 +157,19 @@ void Mesh::setDefaultWrapSettings(texwrap_t &t_wrapSettings)
|
||||
void Mesh::loadTextures(char *t_subfolder, char *t_extension)
|
||||
{
|
||||
BmpLoader bmpLoader = BmpLoader();
|
||||
if (isMd2Loaded || isObjLoaded)
|
||||
if (isObjLoaded)
|
||||
{
|
||||
spec->textures = new Texture[obj->materialsCount];
|
||||
for (u8 i = 0; i < obj->materialsCount; i++)
|
||||
{
|
||||
bmpLoader.load(spec->textures[i], t_subfolder, obj->materials[i].materialName, t_extension);
|
||||
setDefaultWrapSettings(spec->textures[i].wrapSettings);
|
||||
}
|
||||
}
|
||||
else if (isMd2Loaded)
|
||||
{
|
||||
spec->textures = new Texture[1];
|
||||
if (isMd2Loaded)
|
||||
bmpLoader.load(spec->textures[0], t_subfolder, md2->filename, t_extension);
|
||||
if (isObjLoaded)
|
||||
bmpLoader.load(spec->textures[0], t_subfolder, obj->filename, t_extension);
|
||||
bmpLoader.load(spec->textures[0], t_subfolder, md2->filename, t_extension);
|
||||
setDefaultWrapSettings(spec->textures[0].wrapSettings);
|
||||
}
|
||||
else if (isDffLoaded)
|
||||
@@ -186,7 +192,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_colors, t_cameraPos, scale, shouldBeBackfaceCulled);
|
||||
else if (isObjLoaded)
|
||||
return obj->getDrawData(t_vertices, t_normals, t_coordinates, t_colors, t_cameraPos, scale, shouldBeBackfaceCulled);
|
||||
return obj->getDrawData(splitIndex, t_vertices, t_normals, t_coordinates, t_colors, t_cameraPos, scale, shouldBeBackfaceCulled);
|
||||
else if (isDffLoaded)
|
||||
return dff->getDrawData(splitIndex, t_vertices, t_normals, t_coordinates, t_colors, t_cameraPos, scale, shouldBeBackfaceCulled);
|
||||
PRINT_ERR("Can't get draw data, because no 3D model was loaded!");
|
||||
|
||||
@@ -29,10 +29,14 @@ ObjModel::~ObjModel()
|
||||
delete[] vertices;
|
||||
delete[] coordinates;
|
||||
delete[] normals;
|
||||
|
||||
delete[] verticeFaces;
|
||||
delete[] coordinateFaces;
|
||||
delete[] normalFaces;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,55 +44,45 @@ ObjModel::~ObjModel()
|
||||
// Methods
|
||||
// ----
|
||||
|
||||
u32 ObjModel::getDrawData(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled)
|
||||
u32 ObjModel::getFacesCount()
|
||||
{
|
||||
u32 result = 0;
|
||||
for (u16 i = 0; i < materialsCount; i++)
|
||||
result += materials[i].facesCount;
|
||||
return result;
|
||||
}
|
||||
|
||||
u32 ObjModel::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled)
|
||||
{
|
||||
u32 addedFaces = 0;
|
||||
for (u32 i = 0; i < facesCount; i += 3)
|
||||
for (u32 j = 0; j < materials[t_materialIndex].facesCount; j += 3)
|
||||
if (!t_shouldBeBackfaceCulled || Vector3::shouldBeBackfaceCulled(
|
||||
&t_cameraPos,
|
||||
&vertices[verticeFaces[i]],
|
||||
&vertices[verticeFaces[i + 1]],
|
||||
&vertices[verticeFaces[i + 2]]) == 0)
|
||||
&vertices[materials[t_materialIndex].verticeFaces[j]],
|
||||
&vertices[materials[t_materialIndex].verticeFaces[j + 1]],
|
||||
&vertices[materials[t_materialIndex].verticeFaces[j + 2]]) == 0)
|
||||
{
|
||||
fillNextFace(o_vertices, o_normals, o_coordinates, o_colors, i, addedFaces++);
|
||||
fillNextFace(o_vertices, o_normals, o_coordinates, o_colors, i + 1, addedFaces++);
|
||||
fillNextFace(o_vertices, o_normals, o_coordinates, o_colors, i + 2, addedFaces++);
|
||||
fillNextFace(o_vertices, o_normals, o_coordinates, o_colors, t_materialIndex, j, addedFaces++);
|
||||
fillNextFace(o_vertices, o_normals, o_coordinates, o_colors, t_materialIndex, j + 1, addedFaces++);
|
||||
fillNextFace(o_vertices, o_normals, o_coordinates, o_colors, t_materialIndex, j + 2, addedFaces++);
|
||||
}
|
||||
|
||||
return addedFaces;
|
||||
}
|
||||
|
||||
/** Allocate memory for vertices,coords etc.
|
||||
* Called by ObjLoader
|
||||
*/
|
||||
void ObjModel::allocateMemory()
|
||||
void ObjModel::fillNextFace(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, u32 t_materialI, u32 t_getI, u32 t_setI)
|
||||
{
|
||||
PRINT_LOG("Allocating 3D object specification memory");
|
||||
vertices = new Vector3[verticesCount];
|
||||
coordinates = new Vector3[coordinatesCount];
|
||||
normals = new Vector3[normalsCount];
|
||||
|
||||
verticeFaces = new u32[facesCount];
|
||||
coordinateFaces = new u32[facesCount];
|
||||
normalFaces = new u32[facesCount];
|
||||
isMemoryAllocated = true;
|
||||
PRINT_LOG("3D object specification memory allocated!");
|
||||
}
|
||||
|
||||
void ObjModel::fillNextFace(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, u32 t_getI, u32 t_setI)
|
||||
{
|
||||
o_vertices[t_setI][0] = vertices[verticeFaces[t_getI]].x;
|
||||
o_vertices[t_setI][1] = vertices[verticeFaces[t_getI]].y;
|
||||
o_vertices[t_setI][2] = vertices[verticeFaces[t_getI]].z;
|
||||
o_vertices[t_setI][0] = vertices[materials[t_materialI].verticeFaces[t_getI]].x;
|
||||
o_vertices[t_setI][1] = vertices[materials[t_materialI].verticeFaces[t_getI]].y;
|
||||
o_vertices[t_setI][2] = vertices[materials[t_materialI].verticeFaces[t_getI]].z;
|
||||
o_vertices[t_setI][3] = 1.0F;
|
||||
|
||||
o_normals[t_setI][0] = normals[normalFaces[t_getI]].x;
|
||||
o_normals[t_setI][1] = normals[normalFaces[t_getI]].y;
|
||||
o_normals[t_setI][2] = normals[normalFaces[t_getI]].z;
|
||||
o_normals[t_setI][0] = normals[materials[t_materialI].normalFaces[t_getI]].x;
|
||||
o_normals[t_setI][1] = normals[materials[t_materialI].normalFaces[t_getI]].y;
|
||||
o_normals[t_setI][2] = normals[materials[t_materialI].normalFaces[t_getI]].z;
|
||||
o_normals[t_setI][3] = 1.0F;
|
||||
|
||||
o_coordinates[t_setI][0] = coordinates[coordinateFaces[t_getI]].x;
|
||||
o_coordinates[t_setI][1] = coordinates[coordinateFaces[t_getI]].y;
|
||||
o_coordinates[t_setI][0] = coordinates[materials[t_materialI].coordinateFaces[t_getI]].x;
|
||||
o_coordinates[t_setI][1] = coordinates[materials[t_materialI].coordinateFaces[t_getI]].y;
|
||||
o_coordinates[t_setI][2] = 1.0F;
|
||||
o_coordinates[t_setI][3] = 1.0F;
|
||||
|
||||
|
||||
@@ -180,7 +180,14 @@ void Renderer::draw(Mesh *t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount)
|
||||
VECTOR *normals = new VECTOR[vertCount];
|
||||
VECTOR *coordinates = new VECTOR[vertCount];
|
||||
VECTOR *colors = new VECTOR[vertCount];
|
||||
if (t_mesh->isObjLoaded || t_mesh->isMd2Loaded)
|
||||
if (t_mesh->isObjLoaded)
|
||||
for (u32 i = 0; i < t_mesh->obj->materialsCount; i++)
|
||||
{
|
||||
changeTexture(t_mesh, i);
|
||||
vertCount = t_mesh->getDrawData(i, vertices, normals, coordinates, colors, *renderData.cameraPosition);
|
||||
vifSender->drawMesh(&renderData, perspective, vertCount, vertices, normals, coordinates, colors, t_mesh, t_bulbs, t_bulbsCount);
|
||||
}
|
||||
else if (t_mesh->isMd2Loaded)
|
||||
{
|
||||
changeTexture(t_mesh, 0);
|
||||
vertCount = t_mesh->getDrawData(0, vertices, normals, coordinates, colors, *renderData.cameraPosition);
|
||||
|
||||
Reference in New Issue
Block a user