Obj file export basic

This commit is contained in:
JackCarterSmith 2022-07-27 19:58:55 +02:00
parent e0c9f2c7d8
commit 068fdff8fd
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
3 changed files with 65 additions and 10 deletions

View File

@ -19,7 +19,7 @@ unsigned char parseHOBFile(const char* fileName, T_HOB* hob_struct) {
long fileSize; long fileSize;
FILE* fStream = NULL; FILE* fStream = NULL;
char* memFile = NULL; char* memFile = NULL;
int i,j,k; unsigned int i,j,k;
unsigned int facesExtraOffset; unsigned int facesExtraOffset;
int* offset_index = NULL; int* offset_index = NULL;

View File

@ -16,30 +16,78 @@
unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path) { unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path) {
char export_path[128]; char objExport_path[128];
char mtlExport_path[128];
obj* objConstruct = NULL; obj* objConstruct = NULL;
unsigned int i,j;
int surfID = 0, materialID = 0, tmpVertex = 0, tmpIndex = 0;
float vertexBuff[3] = {0}, textureBuff[2] = {0};
int indicesBuff[3] = {0};
if (hob_objects == NULL || out_path == NULL) if (hob_objects == NULL || out_path == NULL)
return ERROR_ARGS_NULL; return ERROR_ARGS_NULL;
if (_options & OUTPUT_DIR) { if (_options & OUTPUT_DIR) {
strcpy(export_path, out_path); strcpy(objExport_path, out_path);
#ifdef _WIN32 #ifdef _WIN32
strcat(export_path, "-out\\"); strcat(objExport_path, "-out\\");
#else #else
strcat(export_path, "-out/"); strcat(objExport_path, "-out/");
#endif #endif
strcat(export_path, hob_objects->name); strcat(objExport_path, hob_objects->name);
} else { } else {
strcpy(export_path, hob_objects->name); strcpy(objExport_path, hob_objects->name);
} }
strcat(export_path, ".obj"); strcpy(mtlExport_path, objExport_path);
strcat(objExport_path, ".obj");
strcat(mtlExport_path, ".mtl");
objConstruct = obj_create(NULL); objConstruct = obj_create(NULL);
//TODO: Write datas to obj file! // Build face/surface material group
for ( i = 0; i < hob_objects->face_group_count; i++) {
surfID = obj_add_surf(objConstruct);
materialID = obj_add_mtrl(objConstruct);
obj_write(objConstruct, hob_objects->name, export_path, 8); // Build vertex container
for ( j = 0; j < hob_objects->face_groups[i].vertex_count; j++ ) {
tmpVertex = obj_add_vert(objConstruct);
vertexBuff[0] = ((float)1/1024) * -hob_objects->face_groups[i].vertices[j].x; // Invert X to fix mirror display
vertexBuff[1] = ((float)1/1024) * -hob_objects->face_groups[i].vertices[j].y; // Invert Y to render upside up
vertexBuff[2] = ((float)1/1024) * hob_objects->face_groups[i].vertices[j].z;
obj_set_vert_v(objConstruct, tmpVertex, vertexBuff);
//textureBuff[0] = ((float)1/1) * hob_objects->face_groups[i].
//obj_set_vert_t(objConstruct, tmpVertex, textureBuff);
}
// Build indices container
for ( j = 0; j < hob_objects->face_groups[i].face_count; j++ ) {
tmpIndex = obj_add_poly(objConstruct, surfID);
indicesBuff[0] = (int)hob_objects->face_groups[i].faces[j].indices[0];
indicesBuff[1] = (int)hob_objects->face_groups[i].faces[j].indices[1];
indicesBuff[2] = (int)hob_objects->face_groups[i].faces[j].indices[2];
obj_set_poly(objConstruct, surfID, tmpIndex, indicesBuff);
// Process 2 triangles if face is Quad
if (hob_objects->face_groups[i].faces[j].flags_bits.fIsQuad) {
tmpIndex = obj_add_poly(objConstruct, surfID);
indicesBuff[0] = (int)hob_objects->face_groups[i].faces[j].indices[0];
indicesBuff[1] = (int)hob_objects->face_groups[i].faces[j].indices[2];
indicesBuff[2] = (int)hob_objects->face_groups[i].faces[j].indices[3];
obj_set_poly(objConstruct, surfID, tmpIndex, indicesBuff);
}
}
}
obj_write(objConstruct, objExport_path, NULL, 8);
obj_delete(objConstruct); obj_delete(objConstruct);
return NO_ERROR; return NO_ERROR;

View File

@ -8,6 +8,13 @@
#ifndef SRC_OBJ_EXPORTER_H_ #ifndef SRC_OBJ_EXPORTER_H_
#define SRC_OBJ_EXPORTER_H_ #define SRC_OBJ_EXPORTER_H_
typedef struct t_material {
unsigned short hasTexture;
unsigned short bpp;
unsigned int gl_tex_id;
} T_MATERIAL;
unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path); unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path);
#endif /* SRC_OBJ_EXPORTER_H_ */ #endif /* SRC_OBJ_EXPORTER_H_ */