This commit is contained in:
h4570
2022-08-29 19:00:47 +02:00
parent 2ae1ccd761
commit e359bcbe2b
30 changed files with 174 additions and 152 deletions
+28
View File
@@ -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
@@ -14,9 +14,8 @@
namespace Tyra {
MeshBuilderData::MeshBuilderData() {
textureCoordsEnabled = false;
normalsEnabled = false;
lightMapEnabled = false;
loadNormals = false;
loadLightmap = false;
}
MeshBuilderData::~MeshBuilderData() {
@@ -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 {
@@ -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);
@@ -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];
+41 -17
View File
@@ -10,13 +10,13 @@
#include <cstring>
#include <stdio.h>
#include <string>
#include <vector>
#include "math/vec2.hpp"
#include "math/vec4.hpp"
#include "renderer/models/color.hpp"
#include "debug/debug.hpp"
#include "loaders/3d/obj_loader/obj_loader.hpp"
#include "file/file_utils.hpp"
#define TINYOBJLOADER_USE_MAPBOX_EARCUT
#define TINYOBJLOADER_IMPLEMENTATION
@@ -40,12 +40,12 @@ std::unique_ptr<MeshBuilderData> ObjLoader::load(
std::unique_ptr<MeshBuilderData> ObjLoader::load(
const char* fullpath, const ObjLoaderOptions& options) {
std::string path = fullpath;
std::string basePath = getPathFromFilename(path);
std::string basePath = FileUtils::getPathFromFilename(path);
TYRA_ASSERT(!path.empty(), "Provided path is empty!");
auto rawFilename = getFilenameWithoutExtension(path);
auto extension = getExtensionOfFilename(path);
auto rawFilename = FileUtils::getFilenameWithoutExtension(path);
auto extension = FileUtils::getExtensionOfFilename(path);
auto result = std::make_unique<MeshBuilderData>();
tinyobj::ObjReaderConfig readerConfig;
@@ -131,9 +131,7 @@ void ObjLoader::addOutputMaterialsAndFrames(
const std::vector<tinyobj::shape_t>& shapes,
const std::vector<tinyobj::material_t>& materials, const u16& framesCount,
std::vector<MaterialVertexCount>& materialVertexCounts) {
if (attrib.texcoords.size()) output->textureCoordsEnabled = true;
if (attrib.normals.size()) output->normalsEnabled = true;
if (attrib.normals.size()) output->loadNormals = true;
TYRA_ASSERT(materials.size() > 0,
"No material data found! Please add .mtl "
@@ -145,6 +143,10 @@ void ObjLoader::addOutputMaterialsAndFrames(
material->name = materials[i].name;
if (!materials[i].diffuse_texname.empty()) {
material->texturePath = materials[i].diffuse_texname;
}
material->ambient.set(materials[i].diffuse[0] * 128.0F,
materials[i].diffuse[1] * 128.0F,
materials[i].diffuse[2] * 128.0F, 128.0F);
@@ -157,10 +159,11 @@ void ObjLoader::addOutputMaterialsAndFrames(
frame->count = counter.count;
frame->vertices = new Vec4[counter.count];
if (output->textureCoordsEnabled)
if (material->texturePath.has_value()) {
frame->textureCoords = new Vec4[counter.count];
}
if (output->normalsEnabled) frame->normals = new Vec4[counter.count];
if (output->loadNormals) frame->normals = new Vec4[counter.count];
material->frames.push_back(frame);
}
@@ -186,6 +189,8 @@ void ObjLoader::importFrame(MeshBuilderData* output,
materialInsertControls.push_back(control);
}
std::vector<u32> materialIdsWithTextureWarning;
// Loop over shapes
for (size_t s = 0; s < shapes.size(); s++) {
const auto& mesh = shapes[s].mesh;
@@ -197,7 +202,8 @@ void ObjLoader::importFrame(MeshBuilderData* output,
auto& materialInsertControl = materialInsertControls[materialId];
size_t vertCountPerFace = size_t(mesh.num_face_vertices[f]);
auto* outFrame = output->materials[materialId]->frames[frameIndex];
auto* outMaterial = output->materials[materialId];
auto* outFrame = outMaterial->frames[frameIndex];
TYRA_ASSERT(count == 1 || vertCountPerFace == 3,
"Please triangulate obj files if you are animating!",
@@ -232,14 +238,32 @@ void ObjLoader::importFrame(MeshBuilderData* output,
// Check if `texcoord_index` is zero or positive. negative =
// notexcoord data
if (idx.texcoord_index >= 0) {
tinyobj::real_t tx =
attrib.texcoords[2 * size_t(idx.texcoord_index) + 0];
tinyobj::real_t ty =
attrib.texcoords[2 * size_t(idx.texcoord_index) + 1];
if (outMaterial->texturePath.has_value()) {
tinyobj::real_t tx =
attrib.texcoords[2 * size_t(idx.texcoord_index) + 0];
tinyobj::real_t ty =
attrib.texcoords[2 * size_t(idx.texcoord_index) + 1];
auto finalY = invertY ? 1.0F - ty : ty;
outFrame->textureCoords[materialInsertControl.inserted].set(
tx, finalY, 1.0F, 0.0F);
auto finalY = invertY ? 1.0F - ty : ty;
outFrame->textureCoords[materialInsertControl.inserted].set(
tx, finalY, 1.0F, 0.0F);
} else {
auto warningWasNotPrinted =
std::find(materialIdsWithTextureWarning.begin(),
materialIdsWithTextureWarning.end(),
materialId) == materialIdsWithTextureWarning.end();
if (frameIndex == 0 && warningWasNotPrinted) {
materialIdsWithTextureWarning.push_back(materialId);
TYRA_WARN(
"Material \"", outMaterial->name,
"\" has texture coordinates, but texture path was not found "
"in .mtl file! ",
"Please consider adding texture path to map_Kd field in .mtl "
"file!");
}
}
}
materialInsertControl.inserted++;
-42
View File
@@ -1,42 +0,0 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include "loaders/loader.hpp"
namespace Tyra {
std::string Loader::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 Loader::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 Loader::getFilenameWithoutExtension(const std::string& filename) {
auto lastindex = filename.find_last_of(".");
return filename.substr(0, lastindex);
}
std::string Loader::getExtensionOfFilename(const std::string& filename) {
auto lastindex = filename.find_last_of(".");
return filename.substr(lastindex + 1);
}
} // namespace Tyra
+2 -1
View File
@@ -16,6 +16,7 @@
#include <string>
#include <draw_buffers.h>
#include "loaders/texture/png_loader.hpp"
#include "file/file_utils.hpp"
namespace Tyra {
@@ -32,7 +33,7 @@ TextureBuilderData* PngLoader::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 ", fullPath);
+18 -5
View File
@@ -14,6 +14,7 @@
#include <cstdlib>
#include <iomanip>
#include "renderer/3d/mesh/mesh_material.hpp"
#include "file/file_utils.hpp"
namespace Tyra {
@@ -29,7 +30,7 @@ MeshMaterial::MeshMaterial(const MeshBuilderData& data,
id = rand() % 1000000;
if (data.lightMapEnabled) {
if (data.loadLightmap) {
lightmapFlag = true;
TYRA_ASSERT(material->frames[0]->colors != nullptr,
"Colors faces are required");
@@ -38,8 +39,15 @@ MeshMaterial::MeshMaterial(const MeshBuilderData& data,
}
ambient.set(material->ambient);
name = material->name;
if (material->texturePath.has_value()) {
auto textureFilename =
FileUtils::getFilenameFromPath(material->texturePath.value());
textureName = FileUtils::getFilenameWithoutExtension(textureFilename);
}
TYRA_ASSERT(name.length() > 0, "MeshMaterial name cannot be empty");
u32 lastVertexCount = 0;
@@ -71,7 +79,7 @@ MeshMaterial::MeshMaterial(const MeshMaterial& mesh) {
lightmapFlag = mesh.lightmapFlag;
name = mesh.name;
textureName = mesh.textureName;
ambient.set(128.0F, 128.0F, 128.0F, 128.0F);
for (u32 i = 0; i < mesh.frames.size(); i++) {
@@ -112,9 +120,14 @@ std::string MeshMaterial::getPrint(const char* name) const {
res << std::endl;
res << std::fixed << std::setprecision(2);
res << "Id: " << id << ", " << std::endl;
res << "Name: " << name << ", " << std::endl;
res << "Name: " << this->name << ", " << std::endl;
if (textureName.has_value()) {
res << "Texture path: " << textureName.value() << ", " << std::endl;
}
res << "Frames count: " << frames.size() << ", " << std::endl;
res << "Single color?: " << static_cast<u32>(lightmapFlag) << std::endl;
res << "Lightmap?: " << static_cast<u32>(lightmapFlag) << std::endl;
res << ")";
return res.str();
@@ -36,11 +36,12 @@ MeshMaterialFrame::MeshMaterialFrame(const MeshBuilderData& data,
TYRA_ASSERT(frame->count > 0, "Vertex count must be greater than 0",
"Material name: ", material->name);
TYRA_ASSERT(frame->vertices != nullptr, "Vertex array can't be null");
TYRA_ASSERT(!data.normalsEnabled || frame->normals != nullptr,
TYRA_ASSERT(!data.loadNormals || frame->normals != nullptr,
"Normal array can't be null");
TYRA_ASSERT(!data.textureCoordsEnabled || frame->textureCoords != nullptr,
"Texture coordinate array can't be null");
TYRA_ASSERT(!data.lightMapEnabled || frame->colors != nullptr,
TYRA_ASSERT(
!material->texturePath.has_value() || frame->textureCoords != nullptr,
"Texture coordinate array can't be null");
TYRA_ASSERT(!data.loadLightmap || frame->colors != nullptr,
"Color array can't be null");
bbox = new BBox(frame->vertices, frame->count);
@@ -48,7 +49,7 @@ MeshMaterialFrame::MeshMaterialFrame(const MeshBuilderData& data,
count = frame->count;
vertices = frame->vertices;
if (data.normalsEnabled) {
if (data.loadNormals) {
normals = frame->normals;
} else {
if (frame->normals != nullptr) delete[] frame->normals;
@@ -56,7 +57,7 @@ MeshMaterialFrame::MeshMaterialFrame(const MeshBuilderData& data,
normals = nullptr;
}
if (data.textureCoordsEnabled) {
if (material->texturePath.has_value()) {
textureCoords = frame->textureCoords;
} else {
if (frame->textureCoords != nullptr) delete[] frame->textureCoords;
@@ -64,7 +65,7 @@ MeshMaterialFrame::MeshMaterialFrame(const MeshBuilderData& data,
textureCoords = nullptr;
}
if (data.lightMapEnabled) {
if (data.loadLightmap) {
colors = frame->colors;
} else {
if (frame->colors != nullptr) delete[] frame->colors;
@@ -160,19 +160,19 @@ void DynPipRenderer::sendObjectData(
void DynPipRenderer::render(DynPipBag** bags, const u32& count) {
if (count <= 0) return;
auto* program = programsRepo->getProgramByBag(bags[0]);
addBufferDataToPacket(program, bags, count);
addBufferDataToPacket(bags, count);
sendPacket();
}
void DynPipRenderer::addBufferDataToPacket(DynPipVU1Program* program,
DynPipBag** bags, const u32& count) {
void DynPipRenderer::addBufferDataToPacket(DynPipBag** bags, const u32& count) {
currentPacket = packets[context];
packet2_reset(currentPacket, false);
for (u32 i = 0; i < count; i++) {
if (bags[i]->count <= 0) continue;
auto* program = programsRepo->getProgramByBag(bags[i]);
program->addBufferDataToPacket(currentPacket, bags[i], prim);
if (lastProgramName != program->getName()) {
@@ -35,6 +35,11 @@ void DynamicPipeline::onUse() {
buffers = new DynPipBag[buffersCount];
for (u32 i = 0; i < buffersCount; i++) {
buffers[i].texture = nullptr;
buffers[i].lighting = nullptr;
}
auto packetSize =
buffersCount * 5.1F; // 5.1 = packet2_get_qw_count / buffersCount
@@ -96,11 +101,12 @@ void DynamicPipeline::render(const DynamicMesh* mesh,
for (u32 i = 0; i < mesh->materials.size(); i++) {
auto* material = mesh->materials[i];
auto* frameFrom = material->frames[mesh->animation.getState().currentFrame];
auto* frameTo = material->frames[mesh->animation.getState().nextFrame];
auto partSize = core.getMaxVertCountByParams(
options && options->lighting, material->frames[0]->textureCoords);
options && options->lighting, material->textureName.has_value());
u32 partsCount = ceil(frameFrom->count / static_cast<float>(partSize));
@@ -109,13 +115,17 @@ void DynamicPipeline::render(const DynamicMesh* mesh,
setBuffersColorBag(buffers, colorBag);
u8 isPartInitialized = false;
auto* texture =
rendererCore->texture.repository.getByMeshMaterialId(material->id);
Texture* texture = nullptr;
TYRA_ASSERT(texture, "Texture for material: ", material->name,
"Id: ", material->id,
"Was not found in texture repository! Did you forget to add "
"texture or disable texture coords loading in mesh?");
if (material->textureName.has_value()) {
texture =
rendererCore->texture.repository.getByMeshMaterialId(material->id);
TYRA_ASSERT(texture, "Texture for material: ", material->name,
"Id: ", material->id,
"Was not found in texture repository! Did you forget to add "
"texture or disable texture coords loading in mesh?");
}
for (u32 k = 0; k < partsCount; k++) {
auto& buffer = buffers[bufferIndex];
@@ -127,6 +137,7 @@ void DynamicPipeline::render(const DynamicMesh* mesh,
u32 startIndex = k * partSize;
addVertices(frameFrom, frameTo, &buffer, startIndex);
buffer.texture = getTextureBag(texture, frameFrom, frameTo, startIndex);
buffer.lighting = getLightingBag(frameFrom, frameTo, &model, options,
dirLights, startIndex);
@@ -139,11 +150,13 @@ void DynamicPipeline::render(const DynamicMesh* mesh,
setBuffer(buffers, &buffer, &bufferIndex);
}
sendRestOfBuffers(buffers, bufferIndex);
bufferIndex = 0;
core.begin(infoBag);
delete colorBag;
}
sendRestOfBuffers(buffers, &bufferIndex);
if (dirLights) {
delete dirLights;
}
@@ -177,11 +190,12 @@ void DynamicPipeline::setBuffer(DynPipBag* buffers, DynPipBag* buffer,
*bufferIndex += 1;
}
void DynamicPipeline::sendRestOfBuffers(DynPipBag* buffers, u16* bufferIndex) {
auto isEndOf1stDBuffer = *bufferIndex <= halfBuffersCount - 1;
void DynamicPipeline::sendRestOfBuffers(DynPipBag* buffers,
const u16& bufferIndex) {
auto isEndOf1stDBuffer = bufferIndex <= halfBuffersCount - 1;
u32 offset = isEndOf1stDBuffer ? 0 : halfBuffersCount;
u32 size = *bufferIndex - offset;
u32 size = bufferIndex - offset;
if (size <= 0) return;
@@ -258,7 +272,7 @@ DynPipColorBag* DynamicPipeline::getColorBag(
DynPipTextureBag* DynamicPipeline::getTextureBag(
Texture* texture, const MeshMaterialFrame* materialFrameFrom,
const MeshMaterialFrame* materialFrameTo, const u32& startIndex) {
if (!materialFrameFrom->textureCoords) return nullptr;
if (!materialFrameFrom->textureCoords || texture == nullptr) return nullptr;
auto* result = new DynPipTextureBag();
@@ -65,6 +65,7 @@ void StaticPipeline::render(const StaticMesh* mesh,
!(options->frustumCulling != PipelineFrustumCulling_Precise &&
infoBag->fullClipChecks == true),
"Full clip checks are only supported with frustum culling == Precise!");
TYRA_ASSERT(options->transformationType == TyraMVP ||
(!options->fullClipChecks &&
options->frustumCulling == PipelineFrustumCulling_None),
@@ -146,7 +147,8 @@ StaPipColorBag* StaticPipeline::getColorBag(
StaPipTextureBag* StaticPipeline::getTextureBag(
const MeshMaterial* material, const MeshMaterialFrame* materialFrame) {
if (!materialFrame->textureCoords) return nullptr;
if (!materialFrame->textureCoords || !material->textureName.has_value())
return nullptr;
auto* result = new StaPipTextureBag();
@@ -116,8 +116,12 @@ void TextureRepository::addByMesh(const Mesh* mesh, const char* directory,
if (dirFixed.back() != '/' && dirFixed.back() != ':') dirFixed += "/";
for (u32 i = 0; i < mesh->materials.size(); i++) {
if (!mesh->materials[i]->textureName.has_value()) {
continue;
}
std::string fullPath =
dirFixed + mesh->materials[i]->name + "." + extension;
dirFixed + mesh->materials[i]->textureName.value() + "." + extension;
auto* data = loader.load(fullPath.c_str());
Texture* texture = new Texture(data);