Skip to content

Commit

Permalink
Merge pull request #136 from h4570/135-obj-get-texture-names-from-mtl…
Browse files Browse the repository at this point in the history
…-file-instead-of-material-name

Fix for obj loader and dynamic pipeline
  • Loading branch information
h4570 authored Aug 29, 2022
2 parents 13f4b5e + e359bcb commit 1fe1296
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 153 deletions.
2 changes: 1 addition & 1 deletion demo/src/states/game/enemy/enemy_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ EnemyManager::EnemyManager(Engine* engine, const Heightmap& heightmap) {

auto data = loader.load(FileUtils::fromCwd("game/models/zombie/zombie.obj"),
objOptions);
data->normalsEnabled = false;
data->loadNormals = false;
motherMesh = new DynamicMesh(data.get());

bodyTexture = textureRepo->add(
Expand Down
2 changes: 1 addition & 1 deletion demo/src/states/game/player/weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Weapon::Weapon(Engine* engine) {
auto* repo = &engine->renderer.core.texture.repository;
auto data =
loader.load(FileUtils::fromCwd("game/models/ak47/ak47.obj"), objOptions);
data->normalsEnabled = false;
data->loadNormals = false;
mesh = new StaticMesh(data.get());

mesh->translation.translateX(2.85F);
Expand Down
2 changes: 1 addition & 1 deletion demo/src/states/game/skybox/skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Skybox::Skybox(TextureRepository* repo) {

auto data = loader.load(FileUtils::fromCwd("game/models/skybox/skybox.obj"),
objOptions);
data->normalsEnabled = false;
data->loadNormals = false;
mesh = new StaticMesh(data.get());

repo->addByMesh(mesh, FileUtils::fromCwd("game/models/skybox/"), "png");
Expand Down
2 changes: 1 addition & 1 deletion demo/src/states/game/terrain/terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Terrain::Terrain(TextureRepository* repo)

auto data = loader.load(FileUtils::fromCwd("game/models/terrain/terrain.obj"),
objOptions);
data->normalsEnabled = false;
data->loadNormals = false;
mesh = new StaticMesh(data.get());

repo->addByMesh(mesh, FileUtils::fromCwd("game/models/terrain/"), "png");
Expand Down
5 changes: 5 additions & 0 deletions engine/inc/file/file_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class FileUtils {
static std::string fromCwd(const std::string& relativePath);
static std::string fromCwd(const char* relativePath);

static std::string getFilenameFromPath(const std::string& path);
static std::string getPathFromFilename(const std::string& path);
static std::string getFilenameWithoutExtension(const std::string& filename);
static std::string getExtensionOfFilename(const std::string& filename);

const char* getElfName() const { return elfName; };
const char* getElfPath() const { return elfPath; };

Expand Down
2 changes: 1 addition & 1 deletion engine/inc/loaders/3d/builder/mesh_builder_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MeshBuilderData {

std::vector<MeshBuilderMaterialData*> materials;

bool textureCoordsEnabled, normalsEnabled, lightMapEnabled;
bool loadNormals, loadLightmap;
};

} // namespace Tyra
2 changes: 2 additions & 0 deletions engine/inc/loaders/3d/builder/mesh_builder_material_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <vector>
#include <string>
#include <optional>
#include "./mesh_builder_material_frame_data.hpp"
#include "renderer/models/color.hpp"

Expand All @@ -25,6 +26,7 @@ class MeshBuilderMaterialData {

std::vector<MeshBuilderMaterialFrameData*> frames;
std::string name;
std::optional<std::string> texturePath;
Color ambient;
};

Expand Down
3 changes: 1 addition & 2 deletions engine/inc/loaders/3d/md2_loader/md2_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#pragma once

#include "../../loader.hpp"
#include "../builder/mesh_builder_data.hpp"
#include <string>
#include <memory>
Expand All @@ -23,7 +22,7 @@ struct MD2LoaderOptions {
};

/** Class responsible for loading & parsing Quake's II ".md2" 3D files */
class MD2Loader : public Loader {
class MD2Loader {
public:
static std::unique_ptr<MeshBuilderData> load(const char* fullpath);
static std::unique_ptr<MeshBuilderData> load(const char* fullpath,
Expand Down
4 changes: 2 additions & 2 deletions engine/inc/loaders/3d/obj_loader/obj_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

#pragma once

#include "../../loader.hpp"
#include "../builder/mesh_builder_data.hpp"
#include <string>
#include "renderer/models/color.hpp"
#include "loaders/3d/obj_loader/tiny_obj_loader.hpp"
#include <memory.h>
#include <optional>

namespace Tyra {

Expand All @@ -36,7 +36,7 @@ struct MaterialVertexCount {
};

/** Class responsible for loading & parsing obj files */
class ObjLoader : public Loader {
class ObjLoader {
public:
static std::unique_ptr<MeshBuilderData> load(const char* fullpath);
static std::unique_ptr<MeshBuilderData> load(const char* fullpath,
Expand Down
25 changes: 0 additions & 25 deletions engine/inc/loaders/loader.hpp

This file was deleted.

3 changes: 1 addition & 2 deletions engine/inc/loaders/texture/base/texture_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@

#pragma once

#include "loaders/loader.hpp"
#include "loaders/texture/builder/texture_builder_data.hpp"
#include "renderer/core/texture/models/texture_bpp.hpp"
#include <string>

namespace Tyra {
class TextureLoader : public Loader {
class TextureLoader {
public:
/**
* @brief Loads texture data from file.
Expand Down
5 changes: 2 additions & 3 deletions engine/inc/renderer/3d/mesh/mesh_material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "debug/debug.hpp"
#include "./mesh_material_frame.hpp"
#include "loaders/3d/builder/mesh_builder_data.hpp"
#include <optional>

namespace Tyra {

Expand All @@ -23,11 +24,9 @@ class MeshMaterial {
~MeshMaterial();

bool isMother, lightmapFlag;

u32 id;

std::string name;

std::optional<std::string> textureName;
Color ambient;

std::vector<MeshMaterialFrame*> frames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class DynPipRenderer {
void uploadPrograms();
void setDoubleBuffer();

void addBufferDataToPacket(DynPipVU1Program* program, DynPipBag** bags,
const u32& count);
void addBufferDataToPacket(DynPipBag** bags, const u32& count);
void sendPacket();

prim_t* prim;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DynamicPipeline : public Renderer3DPipeline {

void setBuffer(DynPipBag* buffers, DynPipBag* buffer, u16* bufferIndex);

void sendRestOfBuffers(DynPipBag* buffers, u16* bufferIndex);
void sendRestOfBuffers(DynPipBag* buffers, const u16& bufferIndex);

void addVertices(const MeshMaterialFrame* materialFrameFrom,
const MeshMaterialFrame* materialFrameTo, DynPipBag* bag,
Expand Down
28 changes: 28 additions & 0 deletions engine/src/file/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,32 @@ std::string FileUtils::fromCwd(const char* file) {
auto cwd = getCwd();
return cwd + file;
}

std::string FileUtils::getFilenameFromPath(const std::string& path) {
std::string filename = path.substr(path.find_last_of("/\\") + 1);
if (filename.size() == path.size()) {
filename = path.substr(path.find_last_of(":\\") + 1);
}
return filename;
}

std::string FileUtils::getPathFromFilename(const std::string& path) {
std::string basepath = path.substr(0, path.find_last_of("/\\"));
if (basepath.size() == path.size()) {
basepath = path.substr(0, path.find_last_of(":\\"));
}
return basepath;
}

std::string FileUtils::getFilenameWithoutExtension(
const std::string& filename) {
auto lastindex = filename.find_last_of(".");
return filename.substr(0, lastindex);
}

std::string FileUtils::getExtensionOfFilename(const std::string& filename) {
auto lastindex = filename.find_last_of(".");
return filename.substr(lastindex + 1);
}

} // namespace Tyra
5 changes: 2 additions & 3 deletions engine/src/loaders/3d/builder/mesh_builder_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
namespace Tyra {

MeshBuilderData::MeshBuilderData() {
textureCoordsEnabled = false;
normalsEnabled = false;
lightMapEnabled = false;
loadNormals = false;
loadLightmap = false;
}

MeshBuilderData::~MeshBuilderData() {
Expand Down
13 changes: 7 additions & 6 deletions engine/src/loaders/3d/md2_loader/md2_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <string>
#include "debug/debug.hpp"
#include "loaders/3d/md2_loader/anorms.hpp"
#include "loaders/loader.hpp"
#include "file/file_utils.hpp"

namespace Tyra {

Expand Down Expand Up @@ -88,7 +88,7 @@ std::unique_ptr<MeshBuilderData> MD2Loader::load(const char* fullpath,
std::string path = fullpath;
TYRA_ASSERT(!path.empty(), "Provided path is empty!");

auto filename = getFilenameFromPath(path);
auto filename = FileUtils::getFilenameFromPath(path);

FILE* file = fopen(fullpath, "rb");
TYRA_ASSERT(file != nullptr, "Failed to load: ", filename);
Expand Down Expand Up @@ -122,12 +122,13 @@ std::unique_ptr<MeshBuilderData> MD2Loader::load(const char* fullpath,
auto result = std::make_unique<MeshBuilderData>();

auto* material = new MeshBuilderMaterialData();
material->name = getFilenameWithoutExtension(filename);
material->name = FileUtils::getFilenameWithoutExtension(filename);
material->texturePath = material->name;
material->texturePath.value().append(".png");

result->materials.push_back(material);
result->normalsEnabled = true;
result->textureCoordsEnabled = true;
result->lightMapEnabled = false;
result->loadNormals = true;
result->loadLightmap = false;

Vec4** tempVertices = new Vec4*[framesCount];
Vec4** tempNormals = new Vec4*[framesCount];
Expand Down
Loading

0 comments on commit 1fe1296

Please sign in to comment.