added skybox and multitexture feature in .obj

This commit is contained in:
Sandro Sobczyński
2020-10-28 16:19:24 +01:00
parent bde819903b
commit 18e00f8e17
9 changed files with 142 additions and 87 deletions
+3
View File
@@ -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?
}
+61 -21
View File
@@ -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;
}