Skip to content

Commit

Permalink
Added fbx import to model loader
Browse files Browse the repository at this point in the history
  • Loading branch information
EPICGameGuy committed Nov 26, 2023
1 parent 9969386 commit 4e4fa8a
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 5,302 deletions.
1,850 changes: 0 additions & 1,850 deletions assets/models/ket.obj

This file was deleted.

Binary file added assets/models/kettle.fbx
Binary file not shown.
3,354 changes: 0 additions & 3,354 deletions assets/models/kettle.obj

This file was deleted.

Binary file modified assets/models/testmap.fbx
Binary file not shown.
File renamed without changes.
18 changes: 10 additions & 8 deletions src/Makefile.iso
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@ sounds: $(SOUND_ISO_FILES)

# Model compilation
MODEL_CC = egg-ps2-mesh-converter
MODEL_SRC_FILES = $(wildcard assets/*.obj) $(wildcard assets/**/*.obj)
MODEL_FILES := $(MODEL_SRC_FILES:%.obj=$(ISO_FOLDER_DIR)/%.mdl)
OBJ_MODEL_SRC_FILES = $(wildcard assets/*.obj) $(wildcard assets/**/*.obj)
OBJ_MODEL_FILES := $(OBJ_MODEL_SRC_FILES:%.obj=$(ISO_FOLDER_DIR)/%.mdl)

$(MODEL_FILES): $(ISO_FOLDER_DIR)/assets/%.mdl: assets/%.obj
$(OBJ_MODEL_FILES): $(ISO_FOLDER_DIR)/assets/%.mdl: assets/%.obj
$(MKDIR) -p $(dir $@)
$(MODEL_CC) $< $@

debug:
@echo working directory: ${PWD}
@echo .obj input files: $(MODEL_SRC_FILES)
@echo .ps2_model files: $(MODEL_FILES)
FBX_MODEL_SRC_FILES = $(wildcard assets/*.fbx) $(wildcard assets/**/*.fbx)
FBX_MODEL_FILES := $(FBX_MODEL_SRC_FILES:%.fbx=$(ISO_FOLDER_DIR)/%.mdl)

$(FBX_MODEL_FILES): $(ISO_FOLDER_DIR)/assets/%.mdl: assets/%.fbx
$(MKDIR) -p $(dir $@)
$(MODEL_CC) $< $@

.PHONY: models
models: $(MODEL_FILES)
models: $(OBJ_MODEL_FILES) $(FBX_MODEL_FILES)

.PHONY: isoassets
isoassets: $(ISO_FOLDER_DIR)/SYSTEM.CNF copy_libs $(SOUND_ISO_FILES) models
Expand Down
25 changes: 5 additions & 20 deletions src/renderer/gs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,18 @@ static GSState _gs_state;
static void init_lights()
{
// lighting
GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};

GLfloat ambient[] = {0.0, 0.0, 0.0, 0.0};
GLfloat l0_diffuse[] = {1.0f, 1.0f, 1.0f, 0};

// GLfloat l1_position[] = {0, -1, 1, 0.0};
GLfloat l1_diffuse[] = {.6f, .6f, .6f, 0.0f};

GLfloat black[] = {0, 0, 0, 0};

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//glEnable(GL_LIGHT1);

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, l0_diffuse);
//glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
//glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse);

// glLightfv(GL_LIGHT1, GL_AMBIENT, black);
// glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse);
Expand All @@ -71,15 +67,6 @@ static void init_lights()
// glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.0f);
}

static void set_light_positions()
{
GLfloat l0_position[] = {0, 1.0, 1.0, 0.0};
GLfloat l1_position[] = {0.0, -20.0, -80.0, 1.0};

glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
//glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
}

//-----------------------------------------------------------------------------
static void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
Expand Down Expand Up @@ -109,13 +96,13 @@ static void init_renderer()
//glEnable(GL_RESCALE_NORMAL);
//glEnable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glEnable(GL_COLOR_MATERIAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
pglEnable(PGL_CLIPPING);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);


glViewport(0, 0, screen_width, screen_height);
Expand Down Expand Up @@ -252,8 +239,6 @@ static int gs_render()
glMatrixMode(GL_MODELVIEW);
//glLoadMatrixf(_gs_state.world_view);

set_light_positions();

pglBeginGeometry();

clear_screen();
Expand Down
11 changes: 11 additions & 0 deletions src/renderer/mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ void Mesh::compile()
check(num_lists <= 4096);
glNewList(list, GL_COMPILE);
{
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
//glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
//static float material_diff_amb[] = {0.5f, 0.5f, 0.5f, 0};
//glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material_diff_amb);
//glShadeModel(GL_SMOOTH);
glCullFace(GL_BACK);
//glDisable(GL_LIGHT0);

//float material[] = {.5f, .5f, .5f, .5f};
//glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material);
//glFrontFace(GL_CCW);

check((uintptr_t)mesh->pos.get_ptr() % 16 == 0);
Expand All @@ -76,6 +86,7 @@ void Mesh::compile()

if (mesh->colors.offset > 0)
{
printf("Mesh: %d has colors\n", list);
glColorPointer(4, GL_FLOAT, 0, mesh->colors.get_ptr());
}

Expand Down
2 changes: 1 addition & 1 deletion src/world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class WorldMesh: public MeshObject

virtual void render(const GS::GSState& gs_state) override
{
for (int i = 0; i < 7; ++i)
for (int i = 0; i < 1; ++i)
{
const Matrix local_world = transforms[i];
ScopedMatrix sm(local_world); // * gs_state.world_view);
Expand Down
2 changes: 1 addition & 1 deletion tools/ps2-mesh-converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ add_executable(egg-ps2-mesh-converter ${Source})

target_include_directories(egg-ps2-mesh-converter PRIVATE include)
target_include_directories(egg-ps2-mesh-converter PRIVATE "${PS2SDK_DIR}/ports/include")
target_link_libraries(egg-ps2-mesh-converter PUBLIC meshoptimizer::meshoptimizer)
target_link_libraries(egg-ps2-mesh-converter PUBLIC meshoptimizer::meshoptimizer assimp)

set_property(TARGET egg-ps2-mesh-converter PROPERTY CXX_STANDARD 20)

Expand Down
13 changes: 9 additions & 4 deletions tools/ps2-mesh-converter/include/model_exporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
#include "egg/math_types.hpp"
#include <vector>

std::vector<std::byte> serialize_mesh(const std::vector<Vector>& positions,
const std::vector<Vector>& normals,
const std::vector<Vector2>& texture_coords,
const std::vector<Vector>& colors);
struct MeshStrip
{
std::vector<Vector> positions;
std::vector<Vector> normals;
std::vector<Vector2> texture_coords;
std::vector<Vector> colors;
};

std::vector<std::byte> serialize_meshes(const std::vector<MeshStrip>& strips);
5 changes: 3 additions & 2 deletions tools/ps2-mesh-converter/include/model_importer.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "types.hpp"
#include <assimp/Importer.hpp>

bool load_mesh(Mesh& mesh, std::string_view path);
bool load_mesh(std::vector<Mesh>& out_meshes, std::string_view path);

bool parseFbx(std::string_view path, std::vector<Mesh>& meshes);
17 changes: 16 additions & 1 deletion tools/ps2-mesh-converter/include/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ static bool isMeshValid(const Mesh& mesh)

union Triangle
{
Triangle()
{
}
Vertex v[3];
char data[sizeof(Vertex) * 3];
};
};

static bool ichar_equals(char a, char b)
{
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
}

// Case insensitive string compare
static bool iequals(const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end(), ichar_equals);
}
119 changes: 119 additions & 0 deletions tools/ps2-mesh-converter/src/fbx_importer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "model_importer.hpp"
#include "types.hpp"

#include <filesystem>

#include <assimp/Importer.hpp>
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags

static Mesh fbxProcessMesh(aiMesh* mesh, const aiScene* scene)
{
Mesh out;

printf("Processing fbx mesh \"%s\"\n", mesh->mName.C_Str());
if (mesh->HasVertexColors(0))
{
printf("Mesh \"%s\" has colors!\n", mesh->mName.C_Str());
}

out.vertices.reserve(mesh->mNumVertices);

for (size_t i = 0; i < mesh->mNumVertices; ++i)
{
Vertex& v = out.vertices.emplace_back();
v.px = mesh->mVertices[i].x;
v.py = mesh->mVertices[i].y;
v.pz = mesh->mVertices[i].z;

std::swap(v.py, v.pz);

assert(mesh->HasNormals());
if (mesh->HasNormals())
{
v.nx = mesh->mNormals[i].x;
v.ny = mesh->mNormals[i].y;
v.nz = mesh->mNormals[i].z;
std::swap(v.ny, v.nz);
}

if (mesh->HasTextureCoords(0))
{
v.tx = mesh->mTextureCoords[0][i].x;
v.ty = mesh->mTextureCoords[0][i].y;
}
else
{
v.tx = 0.f;
v.ty = 0.f;
}

if (mesh->HasVertexColors(0))
{
v.r = mesh->mColors[0][i].r;
v.g = mesh->mColors[0][i].g;
v.b = mesh->mColors[0][i].b;
v.a = mesh->mColors[0][i].a;
}
else
{
v.r = 1.f;
v.g = 1.f;
v.b = 1.f;
v.a = 1.f;
}
}

out.indices.reserve(mesh->mNumFaces * 3);
for (size_t i = 0; i < mesh->mNumFaces; ++i)
{
assert(mesh->mFaces[i].mNumIndices == 3);
out.indices.push_back(mesh->mFaces[i].mIndices[0]);
out.indices.push_back(mesh->mFaces[i].mIndices[1]);
out.indices.push_back(mesh->mFaces[i].mIndices[2]);
}

return out;
}

static void fbxProcessNode(aiNode* node, const aiScene* scene, std::vector<Mesh>& meshes)
{
// process all the node's meshes (if any)
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(fbxProcessMesh(mesh, scene));
}

// then do the same for each of its children
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
fbxProcessNode(node->mChildren[i], scene, meshes);
}
}

bool parseFbx(std::string_view path, std::vector<Mesh>& meshes)
{
Assimp::Importer importer;

// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// probably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(path.data(),
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType |
aiProcess_PreTransformVertices);

// If the import failed, report it
if (scene == nullptr)
{
fprintf(stderr, "%s\n", importer.GetErrorString());
return false;
}

fbxProcessNode(scene->mRootNode, scene, meshes);

return true;
}
Loading

0 comments on commit 4e4fa8a

Please sign in to comment.