Add convertIrrlichtMeshToSPM

This commit is contained in:
Benau 2022-10-21 08:40:23 +08:00
parent 243f7f7008
commit 9cd217acfe
3 changed files with 60 additions and 39 deletions

View File

@ -8,6 +8,14 @@
#include <string>
#include <unordered_set>
namespace irr
{
namespace scene
{
class IMesh; class IAnimatedMesh;
}
}
namespace GE
{
class GEVulkanDriver;
@ -47,6 +55,7 @@ inline int get4x4CompressedTextureSize(int width, int height)
int blocksize = 4 * 4;
return blockcount * blocksize;
}
irr::scene::IAnimatedMesh* convertIrrlichtMeshToSPM(irr::scene::IMesh* mesh);
}
#endif

View File

@ -1,6 +1,14 @@
#include "ge_main.hpp"
#include "ge_spm.hpp"
#include "ge_spm_buffer.hpp"
#include "ge_vulkan_driver.hpp"
#include "mini_glm.hpp"
#include "IMesh.h"
#include "IMeshBuffer.h"
#include "S3DVertex.h"
#include <algorithm>
#include <chrono>
namespace GE
@ -124,4 +132,44 @@ void mathPlaneFrustumf(float* out, const irr::core::matrix4& pvm)
mathPlaneNormf(&out[5 * 4]);
}
irr::scene::IAnimatedMesh* convertIrrlichtMeshToSPM(irr::scene::IMesh* mesh)
{
GESPM* spm = new GESPM();
for (unsigned i = 0; i < mesh->getMeshBufferCount(); i++)
{
std::vector<video::S3DVertexSkinnedMesh> vertices;
scene::IMeshBuffer* mb = mesh->getMeshBuffer(i);
if (!mb)
continue;
GESPMBuffer* spm_mb = new GESPMBuffer();
assert(mb->getVertexType() == video::EVT_STANDARD);
video::S3DVertex* v_ptr = (video::S3DVertex*)mb->getVertices();
for (unsigned j = 0; j < mb->getVertexCount(); j++)
{
video::S3DVertexSkinnedMesh sp;
sp.m_position = v_ptr[j].Pos;
sp.m_normal = MiniGLM::compressVector3(v_ptr[j].Normal);
video::SColorf orig(v_ptr[j].Color);
video::SColorf diffuse(mb->getMaterial().DiffuseColor);
orig.r = orig.r * diffuse.r;
orig.g = orig.g * diffuse.g;
orig.b = orig.b * diffuse.b;
orig.a = orig.a * diffuse.a;
sp.m_color = orig.toSColor();
sp.m_all_uvs[0] = MiniGLM::toFloat16(v_ptr[j].TCoords.X);
sp.m_all_uvs[1] = MiniGLM::toFloat16(v_ptr[j].TCoords.Y);
spm_mb->getVerticesVector().push_back(sp);
}
uint16_t* idx_ptr = mb->getIndices();
std::vector<uint16_t> indices(idx_ptr, idx_ptr + mb->getIndexCount());
std::swap(spm_mb->getIndicesVector(), indices);
spm_mb->getMaterial() = mb->getMaterial();
spm_mb->recalculateBoundingBox();
spm->addMeshBuffer(spm_mb);
}
spm->finalize();
return spm;
}
}

View File

@ -3,8 +3,6 @@
#include "../source/Irrlicht/os.h"
#include "ge_main.hpp"
#include "ge_spm.hpp"
#include "ge_spm_buffer.hpp"
#include "ge_vulkan_animated_mesh_scene_node.hpp"
#include "ge_vulkan_camera_scene_node.hpp"
#include "ge_vulkan_command_loader.hpp"
@ -16,7 +14,6 @@
#include "ge_vulkan_skybox_renderer.hpp"
#include "ge_vulkan_texture_descriptor.hpp"
#include "mini_glm.hpp"
#include "IBillboardSceneNode.h"
#include <sstream>
@ -77,7 +74,8 @@ irr::scene::IAnimatedMeshSceneNode* GEVulkanSceneManager::addAnimatedMeshSceneNo
const irr::core::vector3df& scale,
bool alsoAddIfMeshPointerZero)
{
if (!alsoAddIfMeshPointerZero && (!mesh || !dynamic_cast<GESPM*>(mesh)))
if (!alsoAddIfMeshPointerZero && (!mesh ||
mesh->getMeshType() != irr::scene::EAMT_SPM))
return NULL;
if (!parent)
@ -131,41 +129,7 @@ irr::scene::IMeshSceneNode* GEVulkanSceneManager::addMeshSceneNode(
if (convert_irrlicht_mesh)
{
GESPM* spm = new GESPM();
for (unsigned i = 0; i < mesh->getMeshBufferCount(); i++)
{
std::vector<video::S3DVertexSkinnedMesh> vertices;
scene::IMeshBuffer* mb = mesh->getMeshBuffer(i);
if (!mb)
continue;
GESPMBuffer* spm_mb = new GESPMBuffer();
assert(mb->getVertexType() == video::EVT_STANDARD);
video::S3DVertex* v_ptr = (video::S3DVertex*)mb->getVertices();
for (unsigned j = 0; j < mb->getVertexCount(); j++)
{
video::S3DVertexSkinnedMesh sp;
sp.m_position = v_ptr[j].Pos;
sp.m_normal = MiniGLM::compressVector3(v_ptr[j].Normal);
video::SColorf orig(v_ptr[j].Color);
video::SColorf diffuse(mb->getMaterial().DiffuseColor);
orig.r = orig.r * diffuse.r;
orig.g = orig.g * diffuse.g;
orig.b = orig.b * diffuse.b;
orig.a = orig.a * diffuse.a;
sp.m_color = orig.toSColor();
sp.m_all_uvs[0] = MiniGLM::toFloat16(v_ptr[j].TCoords.X);
sp.m_all_uvs[1] = MiniGLM::toFloat16(v_ptr[j].TCoords.Y);
spm_mb->getVerticesVector().push_back(sp);
}
uint16_t* idx_ptr = mb->getIndices();
std::vector<uint16_t> indices(idx_ptr, idx_ptr + mb->getIndexCount());
std::swap(spm_mb->getIndicesVector(), indices);
spm_mb->getMaterial() = mb->getMaterial();
spm_mb->recalculateBoundingBox();
spm->addMeshBuffer(spm_mb);
}
spm->finalize();
irr::scene::IAnimatedMesh* spm = convertIrrlichtMeshToSPM(mesh);
std::stringstream oss;
oss << (uint64_t)spm;
getMeshCache()->addMesh(oss.str().c_str(), spm);