Skip to content

Commit

Permalink
Merge pull request #3 from wagrenier/Tri2TextureFormat
Browse files Browse the repository at this point in the history
* Refactor code, move logic to its own files
* Handle Tri2 sections, missing `PSMCT32`
* Fix wrong textures being associated with a mesh
* Basic mesh exportation
  • Loading branch information
wagrenier authored Apr 14, 2023
2 parents 8768cf2 + 47a68bd commit c8406b8
Show file tree
Hide file tree
Showing 129 changed files with 27,024 additions and 438 deletions.
16 changes: 13 additions & 3 deletions ModelConverter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ igl_include(glfw)
igl_include(imgui)
igl_include(opengl)

add_subdirectory(third-party)

include_directories(./)

add_executable(ModelConverter
Expand All @@ -19,8 +21,8 @@ add_executable(ModelConverter
render/tim2.h
render/render.h
render/render.cpp
render/logging.cpp
render/logging.h
utils/logging.cpp
utils/logging.h
render/sgdMdl.cpp
render/sgdMdl.h
render/tim2.cpp
Expand All @@ -34,8 +36,16 @@ add_executable(ModelConverter
game/packfile.h
game/packfile.cpp
stb_write_image.h
game/Model.cpp
game/Model.h
math/linalg.cpp
math/linalg.h
utils/utility.cpp
utils/utility.h
game/Exporter.cpp
game/Exporter.h
)

target_link_libraries(ModelConverter PRIVATE igl::glfw igl::imgui igl::opengl assimp)
target_link_libraries(ModelConverter PRIVATE spdlog igl::glfw igl::imgui igl::opengl assimp)


3 changes: 2 additions & 1 deletion ModelConverter/cmake/assimp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "Disable assimp tests" FORCE)
# No importers will be needed for this project
set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF CACHE BOOL "Disable all importers" FORCE)
set(ASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT OFF CACHE BOOL "Disable all exporters" FORCE)
set(ASSIMP_BUILD_FBX_EXPORTER ON CACHE BOOL "Enable FBX exporter" FORCE)
set(ASSIMP_BUILD_OBJ_EXPORTER ON CACHE BOOL "Enable OBJ exporter" FORCE)
set(ASSIMP_BUILD_FBX_EXPORTER OFF CACHE BOOL "DISABLE FBX exporter" FORCE)
set(ASSIMP_BUILD_ASSIMP_VIEW OFF CACHE BOOL "Disable assimp view" FORCE)

FetchContent_Declare(
Expand Down
113 changes: 113 additions & 0 deletions ModelConverter/game/Exporter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "Exporter.h"
#include "assimp/scene.h"
#include "assimp/Exporter.hpp"
#include "utils/logging.h"

void ExportMesh(const std::vector<Mesh>& meshes, const std::filesystem::path& path) {
programLogger->info("Exporting mesh to {}", path.string());

//Setup scene and root node
auto scene = new aiScene();
scene->mRootNode = new aiNode();
scene->mRootNode->mMeshes = new unsigned int[meshes.size()];
scene->mRootNode->mNumMeshes = meshes.size();

// Setup arrays
scene->mNumMeshes = meshes.size();
scene->mMeshes = new aiMesh*[meshes.size()];

scene->mNumMaterials = meshes.size();
scene->mMaterials = new aiMaterial*[meshes.size()];

scene->mNumTextures = meshes.size();
scene->mTextures = new aiTexture*[meshes.size()];

auto m = 0;

for(auto mesh : meshes)
{
programLogger->info("Converting mesh: {}", mesh.mesh_name[0]);

// Setup meshes
scene->mRootNode->mMeshes[m] = m;
scene->mMeshes[m] = new aiMesh();
scene->mMeshes[m]->mName = mesh.mesh_name[0];
scene->mMeshes[m]->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;

scene->mMeshes[m]->mNumVertices = mesh.vertices.size();
scene->mMeshes[m]->mVertices = new aiVector3D[mesh.vertices.size()];

for (auto i = 0; i < mesh.vertices.size(); i++)
{
scene->mMeshes[m]->mVertices[i] = aiVector3D(mesh.vertices[i].x, mesh.vertices[i].y, mesh.vertices[i].z);
}

scene->mMeshes[m]->mNormals = new aiVector3D[mesh.vertex_normals.size()];

for (auto i = 0; i < mesh.vertex_normals.size(); i++)
{
scene->mMeshes[m]->mNormals[i] = aiVector3D(mesh.vertex_normals[i].x, mesh.vertex_normals[i].y, mesh.vertex_normals[i].z);
}

// Setting up the faces
scene->mMeshes[m]->mNumFaces = mesh.triangles.size();
scene->mMeshes[m]->mFaces = new aiFace[mesh.triangles.size()];

for (auto i = 0; i < mesh.triangles.size(); i++)
{
aiFace face;
face.mIndices = new unsigned int[3];
face.mNumIndices = 3;

face.mIndices[0] = mesh.triangles[i].x;
face.mIndices[1] = mesh.triangles[i].y;
face.mIndices[2] = mesh.triangles[i].z;

scene->mMeshes[m]->mFaces[i] = face;
}

// Setting up the UVs
scene->mMeshes[m]->mNumUVComponents[0] = mesh.uv.size();
scene->mMeshes[m]->mTextureCoords[0] = new aiVector3D[mesh.uv.size()];

for (auto i = 0; i < mesh.uv.size(); i++)
{
scene->mMeshes[m]->mTextureCoords[0][i] = {mesh.uv[i].x, mesh.uv[i].y, 0.0f};
}

// Setting up the textures
scene->mTextures[m] = new aiTexture();

if (!mesh.textures.empty())
{
scene->mTextures[m]->mHeight = mesh.textures[0]->GetHeight();
scene->mTextures[m]->mWidth = mesh.textures[0]->GetWidth();

scene->mTextures[m]->pcData = new aiTexel[mesh.textures[0]->GetHeight() * mesh.textures[0]->GetWidth()];

for (auto i = 0; i < mesh.textures[0]->GetHeight(); i++)
{
for (auto j = 0; j < mesh.textures[0]->GetWidth(); j++)
{
unsigned int pixel = mesh.textures[0]->GetPixel(i, j);
auto p = (aiTexel*) &pixel;
scene->mTextures[m]->pcData[i * mesh.textures[0]->GetWidth() + j] = {p->b, p->g, p->r, p->a};
}
}
}

// Setting up the materials
scene->mMeshes[m]->mMaterialIndex = m;
scene->mMaterials[m] = new aiMaterial();
scene->mMaterials[m]->AddProperty(&mesh.ambient[0], 1, AI_MATKEY_COLOR_AMBIENT);
scene->mMaterials[m]->AddProperty(&mesh.diffuse[0], 1, AI_MATKEY_COLOR_DIFFUSE);
scene->mMaterials[m]->AddProperty(&mesh.specular[0], 1, AI_MATKEY_COLOR_SPECULAR);

m++;
}

Assimp::Exporter exporter;
auto result = exporter.Export(scene, "obj", path.string());

programLogger->info("Status of the export: {}", result == aiReturn_SUCCESS ? "Success" : "Failure");
}
9 changes: 9 additions & 0 deletions ModelConverter/game/Exporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef OBSCURA_EXPORTER_H
#define OBSCURA_EXPORTER_H

#include <filesystem>
#include "Mesh.h"

void ExportMesh(const std::vector<Mesh>& meshes, const std::filesystem::path& path);

#endif //OBSCURA_EXPORTER_H
1 change: 1 addition & 0 deletions ModelConverter/game/Model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Model.h"
22 changes: 22 additions & 0 deletions ModelConverter/game/Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef OBSCURA_MODEL_H
#define OBSCURA_MODEL_H

#include <vector>
#include <map>
#include "Texture.h"
#include "Mesh.h"

class Model {
private:
std::vector<Texture *> textures;
std::map<std::string, Mesh> meshes;
std::vector<Mesh> vectorMeshes;
SGDFILEHEADER *sgdTop;
SGDFILEHEADER *sgdCurr;
SGDPROCUNITHEADER *s_ppuhVUVN;
SGDCOORDINATEDESC *sgdCoordintate;
SGDVUMATERIALDESC *sgdMaterial;
};


#endif //OBSCURA_MODEL_H
18 changes: 17 additions & 1 deletion ModelConverter/game/Texture.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "Texture.h"

Texture *CreateTextureFromRawData(int width, int height, void *data)
Texture *CreateTextureFromRawData(int width, int height, void *data, int address)
{
if (width <= 0 || height <= 0 || data == nullptr)
{
return nullptr;
}

auto texture = new Texture(width, height, data);
texture->SetAddress(address);

auto rawPixel = (unsigned int*) data;

struct RGBA
Expand All @@ -23,6 +25,7 @@ Texture *CreateTextureFromRawData(int width, int height, void *data)
for (auto k = 0; k < width; k++)
{
auto pixel = (RGBA*) &rawPixel[(i * width + k)];
pixel->a = (char) (255.0f * (pixel->a / 128.0f));
texture->AddPixel(k, height - 1 - i, pixel->r, pixel->g, pixel->b, pixel->a);
}
}
Expand Down Expand Up @@ -88,3 +91,16 @@ void *Texture::GetRawData()
{
return this->RawData;
}

int Texture::GetAddress() {
return this->Address;
}

int Texture::SetAddress(int address) {
return this->Address = address;
}

unsigned int Texture::GetPixel(int i, int j) {
auto rawPixel = (unsigned int*) this->RawData;
return (unsigned int) rawPixel[(i) + (j * this->Width)];
}
9 changes: 7 additions & 2 deletions ModelConverter/game/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ class Texture{
void AddPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
int GetWidth();
int GetHeight();
int GetAddress();
int SetAddress(int address);
void* GetRawData();
Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> GetRed();
Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> GetGreen();
Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> GetBlue();
Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> GetAlpha();
private:
unsigned int GetPixel(int i, int j);

private:
int Width;
int Height;
int Address = 0;
void* RawData;
Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> R, G, B, A;
void SetImageDimensions();
};

Texture* CreateTextureFromRawData(int width, int height, void* data);
Texture* CreateTextureFromRawData(int width, int height, void* data, int address = 0);
1 change: 1 addition & 0 deletions ModelConverter/game/packfile.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "packfile.h"
#include "render/sgdMdl.h"
#include "utils/utility.h"

int64_t PakAlign128(int64_t address)
{
Expand Down
67 changes: 15 additions & 52 deletions ModelConverter/game/sgd_types.h
Original file line number Diff line number Diff line change
@@ -1,53 +1,16 @@
#pragma once
#include "vif.h"
#include "math/linalg.h"
#include <string>

#include <stdint.h>
#include <cstdint>
#include <cstring>

typedef unsigned int uint32;
typedef unsigned char uint8;
typedef unsigned int uint;
typedef unsigned long long uint64;
typedef unsigned long long sceGsLoadImage;
typedef unsigned long long u8l;

struct Vector2i {
int x;
int y;
};

struct Vector2 {
float x;
float y;
};

struct Vector3 {
float x;
float y;
float z;
};

struct Vector3i {
int x;
int y;
int z;
};

struct Vector4 {
float x;
float y;
float z;
float w;
};

struct Matrix4x4 {
Vector4 row1;
Vector4 row2;
Vector4 row3;
Vector4 row4;
};

typedef Vector4 sceVu0FVECTOR;

enum G3DLIGHTTYPE {
Expand Down Expand Up @@ -101,7 +64,7 @@ enum ProcUnitType : int {
};

struct SGDVUMESHDATA {
qword qwVif1Code;
G3DVIF1CODE qwVif1Code[4];
sceGifTag GifTag;
};

Expand Down Expand Up @@ -148,7 +111,7 @@ struct SGDMESHVERTEXDATA_TYPE2F {
};

struct SGDVUVNDATA_PRESET {
unsigned int aui[10];
G3DVIF1CODE aui[10];
union {
SGDMESHVERTEXDATA_TYPE2 avt2[1];
SGDMESHVERTEXDATA_TYPE2F vt2f;
Expand All @@ -162,19 +125,19 @@ struct _SGDVUMESHCOLORDATA
};

struct TRI2SIZEDATA {
/* 0 */ unsigned int uiMaxAddress;
/* 4 */ unsigned int uiMinAddress;
/* 8 */ unsigned int uiVRAMTexSize;
/* c */ unsigned int uiMaxTbp;
/* 10 */ unsigned int uiPageSize;
unsigned int uiMaxAddress;
unsigned int uiMinAddress;
unsigned int uiVRAMTexSize;
unsigned int uiMaxTbp;
unsigned int uiPageSize;
};

struct SGDTRI2FILEHEADER {
/* 0 */ unsigned int uiVif1Code_NOP0;
/* 4 */ unsigned int uiVif1Code_NOP1;
/* 8 */ unsigned int uiVif1Code_FLUSH;
/* c */ G3DVIF1CODE_DIRECT uiVif1Code_DIRECT;
/* 10 */ sceGsLoadImage gsli;
unsigned int uiVif1Code_NOP0;
unsigned int uiVif1Code_NOP1;
unsigned int uiVif1Code_FLUSH;
G3DVIF1CODE_DIRECT uiVif1Code_DIRECT;
sceGsLoadImage gsli;
};

struct SGDGSIMAGEDATA
Expand Down Expand Up @@ -301,7 +264,7 @@ struct SGDVUMESHPOINTNUM {
};

struct SGDVUMESHSTREGSET {
unsigned int auiVifCode[3];
G3DVIF1CODE auiVifCode[3];
};

struct SGDVUMESHTYPE {
Expand Down
Loading

0 comments on commit c8406b8

Please sign in to comment.