Implemented KRMeshBinding, mesh lod functionality is now explicit in KRScene format and api

This commit is contained in:
2025-11-11 23:07:04 -08:00
parent 8c8aa1893c
commit 82019987e6
22 changed files with 268 additions and 180 deletions

View File

@@ -27,6 +27,7 @@ add_source_and_header(resources/bundle/KRBundleManager)
add_source_and_header(resources/material/KRMaterial)
add_source_and_header(resources/material/KRMaterialManager)
add_source_and_header(resources/mesh/KRMesh)
add_source_and_header(resources/mesh/KRMeshBinding)
add_source_and_header(resources/mesh/KRMeshCube)
add_source_and_header(resources/mesh/KRMeshManager)
add_source_and_header(resources/mesh/KRMeshQuad)

View File

@@ -260,7 +260,7 @@ std::vector<KRResource*> KRContext::getResources()
for (unordered_map<std::string, KRMaterial*>::iterator itr = m_pMaterialManager->getMaterials().begin(); itr != m_pMaterialManager->getMaterials().end(); itr++) {
resources.push_back((*itr).second);
}
for (unordered_multimap<std::string, KRMesh*>::iterator itr = m_pMeshManager->getModels().begin(); itr != m_pMeshManager->getModels().end(); itr++) {
for (unordered_map<std::string, KRMesh*>::iterator itr = m_pMeshManager->getMeshes().begin(); itr != m_pMeshManager->getMeshes().end(); itr++) {
resources.push_back((*itr).second);
}
for (unordered_map<std::string, KRAnimation*>::iterator itr = m_pAnimationManager->getAnimations().begin(); itr != m_pAnimationManager->getAnimations().end(); itr++) {
@@ -309,7 +309,7 @@ KRResource* KRContext::loadResource(const std::string& file_name, Block* data)
if (extension.compare("krbundle") == 0) {
resource = m_pBundleManager->loadBundle(name.c_str(), data);
} else if (extension.compare("krmesh") == 0) {
resource = m_pMeshManager->loadModel(name.c_str(), data);
resource = m_pMeshManager->loadMesh(name.c_str(), data);
} else if (extension.compare("krscene") == 0) {
resource = m_pSceneManager->loadScene(name.c_str(), data);
} else if (extension.compare("kranimation") == 0) {

View File

@@ -44,6 +44,7 @@ using namespace kraken;
#include "siren.h"
#include <stdint.h>
#include <array>
#include <vector>
#include <string>
#include <set>

View File

@@ -130,7 +130,7 @@ void KRAmbientZone::render(RenderInfo& ri)
bool bVisualize = ri.camera->settings.debug_display == KRRenderSettings::KRENGINE_DEBUG_DISPLAY_SIREN_AMBIENT_ZONES;
if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT && bVisualize) {
KRMesh* sphereModel = getContext().getMeshManager()->getMaxLODModel("__sphere");
KRMesh* sphereModel = getContext().getMeshManager()->getMesh("__sphere");
if (sphereModel) {
Matrix4 sphereModelMatrix = getModelMatrix();

View File

@@ -200,7 +200,7 @@ void KRAudioSource::render(RenderInfo& ri)
bool bVisualize = false;
if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT && bVisualize) {
KRMesh* sphereModel = getContext().getMeshManager()->getMaxLODModel("__sphere");
KRMesh* sphereModel = getContext().getMeshManager()->getMesh("__sphere");
if (sphereModel) {
Matrix4 sphereModelMatrix = getModelMatrix();

View File

@@ -82,7 +82,7 @@ void KRBone::render(RenderInfo& ri)
bool bVisualize = ri.camera->settings.debug_display == KRRenderSettings::KRENGINE_DEBUG_DISPLAY_BONES;
if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT && bVisualize) {
KRMesh* sphereModel = getContext().getMeshManager()->getMaxLODModel("__sphere");
KRMesh* sphereModel = getContext().getMeshManager()->getMesh("__sphere");
if (sphereModel) {
Matrix4 sphereModelMatrix = getModelMatrix();

View File

@@ -103,7 +103,7 @@ void KRCollider::loadXML(tinyxml2::XMLElement* e)
void KRCollider::loadModel()
{
if (m_model == nullptr) {
m_model = m_pContext->getMeshManager()->getMaxLODModel(m_model_name.c_str());
m_model = m_pContext->getMeshManager()->getMesh(m_model_name.c_str());
if (m_model) {
getScene().notify_sceneGraphModify(this);
}

View File

@@ -344,7 +344,7 @@ void KRLight::render(RenderInfo& ri)
if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_PARTICLE_OCCLUSION) {
if (m_flareTexture.isSet() && m_flareSize > 0.0f) {
KRMesh* sphereModel = getContext().getMeshManager()->getMaxLODModel("__sphere");
KRMesh* sphereModel = getContext().getMeshManager()->getMesh("__sphere");
if (sphereModel) {
Matrix4 occlusion_test_sphere_matrix = Matrix4();

View File

@@ -46,7 +46,9 @@ void KRModel::InitNodeInfo(KrNodeInfo* nodeInfo)
nodeInfo->model.faces_camera = false;
nodeInfo->model.light_map_texture = KR_NULL_HANDLE;
nodeInfo->model.lod_min_coverage = 0.0f;
nodeInfo->model.mesh = KR_NULL_HANDLE;
for (int lod = 0; lod < kMeshLODCount; lod++) {
nodeInfo->model.mesh[lod] = KR_NULL_HANDLE;
}
nodeInfo->model.receives_shadow = true;
nodeInfo->model.rim_color = Vector3::Zero();
nodeInfo->model.rim_power = 0.0f;
@@ -78,11 +80,13 @@ KRModel::KRModel(KRScene& scene, std::string name)
m_boundsCachedMat.c[15] = -1.0f;
}
KRModel::KRModel(KRScene& scene, std::string instance_name, std::string model_name, std::string light_map, float lod_min_coverage, bool receives_shadow, bool faces_camera, Vector3 rim_color, float rim_power)
KRModel::KRModel(KRScene& scene, std::string instance_name, std::string model_name[kMeshLODCount], std::string light_map, float lod_min_coverage, bool receives_shadow, bool faces_camera, Vector3 rim_color, float rim_power)
: KRNode(scene, instance_name)
{
m_lightMap.setName(light_map);
m_model_name = model_name;
for (int lod = 0; lod < kMeshLODCount; lod++) {
m_meshes[lod].setName(model_name[lod]);
}
m_min_lod_coverage = lod_min_coverage;
m_receivesShadow = receives_shadow;
m_faces_camera = faces_camera;
@@ -133,19 +137,15 @@ KrResult KRModel::update(const KrNodeInfo* nodeInfo)
}
m_lightMap.set(light_map_texture);
for (int lod = 0; lod < kMeshLODCount; lod++) {
KRMesh* mesh = nullptr;
if (nodeInfo->model.mesh != KR_NULL_HANDLE) {
res = m_pContext->getMappedResource<KRMesh>(nodeInfo->model.mesh, &mesh);
if (nodeInfo->model.mesh[lod] != KR_NULL_HANDLE) {
res = m_pContext->getMappedResource<KRMesh>(nodeInfo->model.mesh[lod], &mesh);
if (res != KR_SUCCESS) {
return res;
}
}
if (mesh != nullptr) {
m_models.clear();
m_model_name = mesh->getName();
} else {
m_models.clear();
m_model_name = "";
m_meshes[lod].set(mesh);
}
return KR_SUCCESS;
@@ -159,7 +159,12 @@ std::string KRModel::getElementName()
tinyxml2::XMLElement* KRModel::saveXML(tinyxml2::XMLNode* parent)
{
tinyxml2::XMLElement* e = KRNode::saveXML(parent);
e->SetAttribute("mesh", m_model_name.c_str());
e->SetAttribute("mesh", m_meshes[0].getName().c_str());
for (int lod = 1; lod < kMeshLODCount; lod++) {
char attribName[8];
snprintf(attribName, 8, "mesh%i", lod);
e->SetAttribute(attribName, m_meshes[lod].getName().c_str());
}
e->SetAttribute("light_map", m_lightMap.getName().c_str());
e->SetAttribute("lod_min_coverage", m_min_lod_coverage);
e->SetAttribute("receives_shadow", m_receivesShadow ? "true" : "false");
@@ -201,15 +206,19 @@ std::string KRModel::getLightMap()
void KRModel::loadModel()
{
if (m_models.size() == 0) {
std::vector<KRMesh*> models = m_pContext->getMeshManager()->getModel(m_model_name.c_str()); // The model manager returns the LOD levels in sorted order, with the highest detail first
unordered_map<KRMesh*, std::vector<KRBone*> > bones;
if (models.size() > 0) {
bool all_bones_found = true;
for (std::vector<KRMesh*>::iterator model_itr = models.begin(); model_itr != models.end(); model_itr++) {
KRMesh* model = *model_itr;
bool meshChanged = false;
for (int lod = 0; lod < kMeshLODCount; lod++) {
KRMesh* prevMesh = nullptr;
prevMesh = m_meshes[lod].get();
m_meshes[lod].load(&getContext());
if (m_meshes[lod].get() != prevMesh) {
meshChanged = true;
}
if (m_meshes[lod].isLoaded()) {
KRMesh* model = m_meshes[lod].get();
std::vector<KRBone*> model_bones;
int bone_count = model->getBoneCount();
bool all_bones_found = true;
for (int bone_index = 0; bone_index < bone_count; bone_index++) {
KRBone* matching_bone = dynamic_cast<KRBone*>(getScene().getRootNode()->find<KRNode>(model->getBoneName(bone_index)));
if (matching_bone) {
@@ -218,17 +227,29 @@ void KRModel::loadModel()
all_bones_found = false; // Reject when there are any missing bones or multiple matches
}
}
bones[model] = model_bones;
}
if (all_bones_found) {
m_models = models;
m_bones = bones;
getScene().notify_sceneGraphModify(this);
if (m_bones[lod] != model_bones) {
m_bones[lod] = model_bones;
meshChanged = true;
}
} else {
if (!m_bones[lod].empty()) {
m_bones[lod].clear();
meshChanged = true;
}
}
} else {
if (!m_bones[lod].empty()) {
m_bones[lod].clear();
meshChanged = true;
}
}
}
if (meshChanged) {
getScene().notify_sceneGraphModify(this);
invalidateBounds();
}
}
}
void KRModel::render(KRNode::RenderInfo& ri)
@@ -243,6 +264,7 @@ void KRModel::render(KRNode::RenderInfo& ri)
KRNode::render(ri);
// Don't render meshes on second pass of the deferred lighting renderer, as only lights will be applied
if (ri.renderPass->getType() != RenderPassType::RENDER_PASS_DEFERRED_LIGHTS
&& ri.renderPass->getType() != RenderPassType::RENDER_PASS_ADDITIVE_PARTICLES
&& ri.renderPass->getType() != RenderPassType::RENDER_PASS_PARTICLE_OCCLUSION
@@ -251,9 +273,6 @@ void KRModel::render(KRNode::RenderInfo& ri)
&& ri.renderPass->getType() != RenderPassType::RENDER_PASS_PRESTREAM) {
loadModel();
if (m_models.size() > 0) {
// Don't render meshes on second pass of the deferred lighting renderer, as only lights will be applied
/*
float lod_coverage = 0.0f;
if(m_models.size() > 1) {
@@ -264,19 +283,21 @@ void KRModel::render(KRNode::RenderInfo& ri)
*/
float lod_coverage = ri.viewport->coverage(getBounds()); // This also checks the view frustrum culling
if (lod_coverage > m_min_lod_coverage) {
// ---===--- Select the best LOD model based on screen coverage ---===---
std::vector<KRMesh*>::iterator itr = m_models.begin();
KRMesh* pModel = *itr++;
int bestLOD = -1;
KRMesh* pModel = nullptr;
for (int lod = 0; lod < kMeshLODCount; lod++) {
if (m_meshes[lod].isLoaded()) {
KRMesh* pLODModel = m_meshes[lod].get();
while (itr != m_models.end()) {
KRMesh* pLODModel = *itr++;
if ((float)pLODModel->getLODCoverage() / 100.0f > lod_coverage && pLODModel->getLODCoverage() < pModel->getLODCoverage()) {
if ((float)pLODModel->getLODCoverage() / 100.0f > lod_coverage) {
if(bestLOD == -1 || pLODModel->getLODCoverage() < pModel->getLODCoverage()) {
pModel = pLODModel;
} else {
break;
bestLOD = lod;
continue;
}
}
}
}
@@ -288,6 +309,7 @@ void KRModel::render(KRNode::RenderInfo& ri)
// m_pContext->getTextureManager()->selectTexture(5, m_pLightMap, lod_coverage, KRTexture::TEXTURE_USAGE_LIGHT_MAP);
}
if (pModel) {
Matrix4 matModel = getModelMatrix();
if (m_faces_camera) {
Vector3 model_center = Matrix4::Dot(matModel, Vector3::Zero());
@@ -295,7 +317,7 @@ void KRModel::render(KRNode::RenderInfo& ri)
matModel = Quaternion::Create(Vector3::Forward(), Vector3::Normalize(camera_pos - model_center)).rotationMatrix() * matModel;
}
pModel->render(ri, getName(), matModel, m_lightMap.get(), m_bones[pModel], lod_coverage);
pModel->render(ri, getName(), matModel, m_lightMap.get(), m_bones[bestLOD], lod_coverage);
}
}
}
@@ -308,8 +330,10 @@ void KRModel::preStream(const KRViewport& viewport)
loadModel();
float lod_coverage = viewport.coverage(getBounds());
for (auto itr = m_models.begin(); itr != m_models.end(); itr++) {
(*itr)->preStream(lod_coverage);
for (int i = 0; i < kMeshLODCount; i++) {
if (m_meshes[i].isLoaded()) {
m_meshes[i].get()->preStream(lod_coverage);
}
}
m_lightMap.load(&getContext());
@@ -326,8 +350,10 @@ kraken_stream_level KRModel::getStreamLevel(const KRViewport& viewport)
loadModel();
for (auto itr = m_models.begin(); itr != m_models.end(); itr++) {
stream_level = KRMIN(stream_level, (*itr)->getStreamLevel());
for (int lod = 0; lod < kMeshLODCount; lod++) {
if (m_meshes[lod].isLoaded()) {
stream_level = KRMIN(stream_level, m_meshes[lod].get()->getStreamLevel());
}
}
return stream_level;
@@ -336,22 +362,28 @@ kraken_stream_level KRModel::getStreamLevel(const KRViewport& viewport)
AABB KRModel::getBounds()
{
loadModel();
if (m_models.size() > 0) {
// Get the bounds of the lowest lod mesh
for(int lod=0; lod<kMeshLODCount; lod++) {
if (!m_meshes[lod].isLoaded()) {
continue;
}
KRMesh* mesh = m_meshes[lod].get();
if (m_faces_camera) {
AABB normal_bounds = AABB::Create(m_models[0]->getMinPoint(), m_models[0]->getMaxPoint(), getModelMatrix());
AABB normal_bounds = AABB::Create(mesh->getMinPoint(), mesh->getMaxPoint(), getModelMatrix());
float max_dimension = normal_bounds.longest_radius();
return AABB::Create(normal_bounds.center() - Vector3::Create(max_dimension), normal_bounds.center() + Vector3::Create(max_dimension));
} else {
if (!(m_boundsCachedMat == getModelMatrix())) {
m_boundsCachedMat = getModelMatrix();
m_boundsCached = AABB::Create(m_models[0]->getMinPoint(), m_models[0]->getMaxPoint(), getModelMatrix());
m_boundsCached = AABB::Create(mesh->getMinPoint(), mesh->getMaxPoint(), getModelMatrix());
}
return m_boundsCached;
}
} else {
return AABB::Infinite();
}
// No models loaded
return AABB::Infinite();
}

View File

@@ -40,6 +40,7 @@
#include "KRNode.h"
#include "KRContext.h"
#include "resources/mesh/KRMesh.h"
#include "resources/mesh/KRMeshBinding.h"
#include "resources/texture/KRTexture.h"
#include "resources/texture/KRTextureBinding.h"
#include "KRBone.h"
@@ -48,10 +49,12 @@ class KRModel : public KRNode
{
public:
static const int kMeshLODCount = 8;
static void InitNodeInfo(KrNodeInfo* nodeInfo);
KRModel(KRScene& scene, std::string name);
KRModel(KRScene& scene, std::string instance_name, std::string model_name, std::string light_map, float lod_min_coverage, bool receives_shadow, bool faces_camera, hydra::Vector3 rim_color = hydra::Vector3::Zero(), float rim_power = 0.0f);
KRModel(KRScene& scene, std::string instance_name, std::string model_name[kMeshLODCount], std::string light_map, float lod_min_coverage, bool receives_shadow, bool faces_camera, hydra::Vector3 rim_color = hydra::Vector3::Zero(), float rim_power = 0.0f);
virtual ~KRModel();
KrResult update(const KrNodeInfo* nodeInfo) override;
@@ -76,10 +79,9 @@ public:
private:
void preStream(const KRViewport& viewport);
std::vector<KRMesh*> m_models;
unordered_map<KRMesh*, std::vector<KRBone*> > m_bones; // Outer std::map connects model to set of bones
std::array<KRMeshBinding, kMeshLODCount> m_meshes;
std::array<std::vector<KRBone*>, kMeshLODCount> m_bones; // Connects model to set of bones
KRTextureBinding m_lightMap;
std::string m_model_name;
float m_min_lod_coverage;

View File

@@ -653,7 +653,14 @@ KRNode* KRNode::LoadXML(KRScene& scene, tinyxml2::XMLElement* e)
}
Vector3 rim_color = Vector3::Zero();
rim_color = kraken::getXMLAttribute("rim_color", e, Vector3::Zero());
new_node = new KRModel(scene, szName, e->Attribute("mesh"), e->Attribute("light_map"), lod_min_coverage, receives_shadow, faces_camera, rim_color, rim_power);
std::string meshNames[KRModel::kMeshLODCount];
meshNames[0] = e->Attribute("mesh");
for (int lod = 1; lod < KRModel::kMeshLODCount; lod++) {
char attribName[8];
snprintf(attribName, 8, "mesh%i", lod);
meshNames[lod] = e->Attribute(attribName);
}
new_node = new KRModel(scene, szName, meshNames, e->Attribute("light_map"), lod_min_coverage, receives_shadow, faces_camera, rim_color, rim_power);
} else if (strcmp(szElementName, "collider") == 0) {
new_node = new KRCollider(scene, szName, e->Attribute("mesh"), 65535, 1.0f);
} else if (strcmp(szElementName, "bone") == 0) {

View File

@@ -128,7 +128,7 @@ void KRReverbZone::render(RenderInfo& ri)
bool bVisualize = ri.camera->settings.debug_display == KRRenderSettings::KRENGINE_DEBUG_DISPLAY_SIREN_REVERB_ZONES;
if (ri.renderPass->getType()== RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT && bVisualize) {
KRMesh* sphereModel = getContext().getMeshManager()->getMaxLODModel("__sphere");
KRMesh* sphereModel = getContext().getMeshManager()->getMesh("__sphere");
if (sphereModel) {
Matrix4 sphereModelMatrix = getModelMatrix();
PipelineInfo info{};

View File

@@ -301,7 +301,7 @@ typedef struct
bool faces_camera;
float rim_power;
hydra::Vector3 rim_color;
KrResourceMapIndex mesh;
KrResourceMapIndex mesh[8];
KrResourceMapIndex light_map_texture;
} model;
struct

View File

@@ -1556,7 +1556,7 @@ void LoadMesh(KRContext& context, FbxScene* pFbxScene, FbxGeometryConverter* pGe
KRMesh* new_mesh = new KRMesh(context, pMesh->GetNode()->GetName());
new_mesh->LoadData(mi, true, need_tangents);
context.getMeshManager()->addModel(new_mesh);
context.getMeshManager()->addMesh(new_mesh);
}
KRNode* LoadMesh(KRNode* parent_node, FbxScene* pFbxScene, FbxGeometryConverter* pGeometryConverter, FbxNode* pNode)

View File

@@ -67,31 +67,8 @@ KRMesh::KRMesh(KRContext& context, std::string name, Block* data) : KRResource(c
loadPack(data);
}
void KRMesh::parseName(const std::string& name, std::string& lodBaseName, int& lodCoverage)
{
lodCoverage = 100;
lodBaseName = name;
size_t last_underscore_pos = name.find_last_of('_');
if (last_underscore_pos != std::string::npos) {
// Found an underscore
std::string suffix = name.substr(last_underscore_pos + 1);
if (suffix.find("lod") == 0) {
std::string lod_level_string = suffix.substr(3);
char* end = NULL;
int c = (int)strtol(lod_level_string.c_str(), &end, 10);
if (c >= 0 && c <= 100 && *end == '\0') {
lodCoverage = c;
lodBaseName = name.substr(0, last_underscore_pos);
}
}
}
}
void KRMesh::setName(const std::string name)
{
parseName(name, m_lodBaseName, m_lodCoverage);
m_lodCoverage = 100;
m_lodBaseName = name;
}

View File

@@ -70,8 +70,6 @@ class KRMesh : public KRResource
{
public:
static void parseName(const std::string& name, std::string& lodBaseName, int& lodCoverage);
KRMesh(KRContext& context, std::string name, mimir::Block* data);
KRMesh(KRContext& context, std::string name);
virtual ~KRMesh();

View File

@@ -0,0 +1,53 @@
//
// KRMeshBinding.cpp
// Kraken Engine
//
// Copyright 2025 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#include "KREngine-common.h"
#include "KRContext.h"
#include "KRMesh.h"
#include "KRMeshBinding.h"
KRMesh* KRMeshBinding::get()
{
return static_cast<KRMesh*>(m_resource);
}
bool KRMeshBinding::load(KRContext* context)
{
if (m_name.size() == 0) {
return true;
}
if (m_resource != nullptr) {
return true;
}
m_resource = context->getMeshManager()->getMesh(m_name.c_str());
return (m_resource != nullptr);
}

View File

@@ -0,0 +1,46 @@
//
// KRMeshBinding.h
// Kraken Engine
//
// Copyright 2025 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#pragma once
#include "KREngine-common.h"
#include "resources/KRResourceBinding.h"
class KRMesh;
class KRMeshBinding : public KRResourceBinding
{
public:
KRMesh* get();
bool load(KRContext* context) override final;
private:
};

View File

@@ -55,9 +55,9 @@ KRMeshManager::KRMeshManager(KRContext& context)
void KRMeshManager::init()
{
addModel(new KRMeshCube(*m_pContext));
addModel(new KRMeshQuad(*m_pContext));
addModel(new KRMeshSphere(*m_pContext));
addMesh(new KRMeshCube(*m_pContext));
addMesh(new KRMeshQuad(*m_pContext));
addMesh(new KRMeshSphere(*m_pContext));
// ---- Initialize stock models ----
static const float _KRENGINE_VBO_3D_CUBE_VERTEX_DATA[] = {
@@ -114,87 +114,59 @@ void KRMeshManager::init()
KRMeshManager::~KRMeshManager()
{
for (unordered_multimap<std::string, KRMesh*>::iterator itr = m_models.begin(); itr != m_models.end(); ++itr) {
for (unordered_map<std::string, KRMesh*>::iterator itr = m_meshes.begin(); itr != m_meshes.end(); ++itr) {
delete (*itr).second;
}
m_models.clear();
m_meshes.clear();
}
KRResource* KRMeshManager::loadResource(const std::string& name, const std::string& extension, Block* data)
{
if (extension.compare("krmesh") == 0) {
return loadModel(name.c_str(), data);
return loadMesh(name.c_str(), data);
}
return nullptr;
}
KRResource* KRMeshManager::getResource(const std::string& name, const std::string& extension)
{
if (extension.compare("krmesh") == 0) {
std::string lodBaseName;
int lodCoverage;
KRMesh::parseName(name, lodBaseName, lodCoverage);
std::vector<KRMesh*> models = getModel(lodBaseName.c_str());
for (KRMesh* mesh : models) {
if (mesh->getLODCoverage() == lodCoverage) {
return getMesh(name.c_str());
}
return nullptr;
}
KRMesh* KRMeshManager::loadMesh(const char* szName, Block* pData)
{
KRMesh* mesh = new KRMesh(*m_pContext, szName, pData);
addMesh(mesh);
return mesh;
}
}
}
return nullptr;
}
KRMesh* KRMeshManager::loadModel(const char* szName, Block* pData)
void KRMeshManager::addMesh(KRMesh* mesh)
{
KRMesh* pModel = new KRMesh(*m_pContext, szName, pData);
addModel(pModel);
return pModel;
}
void KRMeshManager::addModel(KRMesh* model)
{
std::string lowerName = model->getLODBaseName();
std::string lowerName = mesh->getLODBaseName();
std::transform(lowerName.begin(), lowerName.end(),
lowerName.begin(), ::tolower);
m_models.insert(std::pair<std::string, KRMesh*>(lowerName, model));
m_meshes[lowerName] = mesh;
}
KRMesh* KRMeshManager::getMaxLODModel(const char* szName)
KRMesh* KRMeshManager::getMesh(const char* szName)
{
std::vector<KRMesh*> models = getModel(szName);
// models are always in order of highest LOD first
if (models.size()) {
return models[0];
}
std::string lower_name = szName;
std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower);
unordered_map<std::string, KRMesh*>::iterator itr = m_meshes.find(lower_name);
if (itr == m_meshes.end()) {
KRContext::Log(KRContext::LOG_LEVEL_INFORMATION, "Model not found: %s", lower_name.c_str());
return nullptr;
}
return itr->second;
}
std::vector<KRMesh*> KRMeshManager::getModel(const char* szName)
unordered_map<std::string, KRMesh*>& KRMeshManager::getMeshes()
{
std::string lowerName = szName;
std::transform(lowerName.begin(), lowerName.end(),
lowerName.begin(), ::tolower);
std::vector<KRMesh*> matching_models;
std::pair<unordered_multimap<std::string, KRMesh*>::iterator, unordered_multimap<std::string, KRMesh*>::iterator> range = m_models.equal_range(lowerName);
for (unordered_multimap<std::string, KRMesh*>::iterator itr_match = range.first; itr_match != range.second; itr_match++) {
matching_models.push_back(itr_match->second);
}
std::sort(matching_models.begin(), matching_models.end(), KRMesh::lod_sort_predicate);
if (matching_models.size() == 0) {
KRContext::Log(KRContext::LOG_LEVEL_INFORMATION, "Model not found: %s", lowerName.c_str());
}
return matching_models;
}
unordered_multimap<std::string, KRMesh*>& KRMeshManager::getModels()
{
return m_models;
return m_meshes;
}
void KRMeshManager::bindVBO(VkCommandBuffer& commandBuffer, KRVBOData* vbo_data, float lodCoverage)

View File

@@ -58,13 +58,12 @@ public:
void startFrame(float deltaTime);
void endFrame(float deltaTime);
KRMesh* loadModel(const char* szName, mimir::Block* pData);
std::vector<KRMesh*> getModel(const char* szName);
KRMesh* getMaxLODModel(const char* szName);
void addModel(KRMesh* model);
KRMesh* loadMesh(const char* szName, mimir::Block* pData);
KRMesh* getMesh(const char* szName);
void addMesh(KRMesh* mesh);
std::vector<std::string> getModelNames();
unordered_multimap<std::string, KRMesh*>& getModels();
std::vector<std::string> getMeshNames();
unordered_map<std::string, KRMesh*>& getMeshes();
class KRVBOData
{
@@ -217,7 +216,7 @@ private:
mimir::Block KRENGINE_VBO_2D_SQUARE_VERTICES;
__int32_t KRENGINE_VBO_2D_SQUARE_ATTRIBS;
unordered_multimap<std::string, KRMesh*> m_models; // Multiple models with the same name/key may be inserted, representing multiple LOD levels of the model
unordered_map<std::string, KRMesh*> m_meshes;
long m_vboMemUsed;
KRVBOData* m_currentVBO;

View File

@@ -107,7 +107,7 @@ void KRScene::render(KRNode::RenderInfo& ri)
// ---------- Start: Vulkan Debug Code ----------
/*
if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_FORWARD_OPAQUE) {
KRMesh* sphereMesh = getContext().getMeshManager()->getMaxLODModel("__sphere");
KRMesh* sphereMesh = getContext().getMeshManager()->getMesh("__sphere");
if (sphereMesh && sphereMesh->isReady()) {
PipelineInfo info{};
std::string shader_name("vulkan_test");

View File

@@ -127,7 +127,7 @@ bool smoke_load()
create_cube_info.newNodeHandle = kCubeNodeHandle;
create_cube_info.sceneHandle = kSceneResourceHandle;
create_cube_info.node.pName = "my_cube";
create_cube_info.node.model.mesh = kCubeMeshResourceHandle;
create_cube_info.node.model.mesh[1] = kCubeMeshResourceHandle;
res = KrCreateNode(&create_cube_info);
assert(res == KR_SUCCESS);