#include "CMeshSceneNode.h"
#include "IVideoDriver.h"
#include "ISceneManager.h"
#include "S3DVertex.h"
#include "ICameraSceneNode.h"
#include "IMeshCache.h"
#include "IAnimatedMesh.h"
#include "IMaterialRenderer.h"
namespace irr
{
namespace scene
{
CMeshSceneNode::CMeshSceneNode(IMesh* mesh, ISceneNode* parent, ISceneManager* mgr, s32 id,
const core::vector3df& position, const core::vector3df& rotation,
const core::vector3df& scale)
: IMeshSceneNode(parent, mgr, id, position, rotation, scale), Mesh(0), PassCount(0),
ReadOnlyMaterials(false)
{
#ifdef _DEBUG
setDebugName("CMeshSceneNode");
#endif
setMesh(mesh);
}
CMeshSceneNode::~CMeshSceneNode()
{
if (Mesh)
Mesh->drop();
}
void CMeshSceneNode::OnRegisterSceneNode()
{
if (IsVisible)
{
video::IVideoDriver* driver = SceneManager->getVideoDriver();
PassCount = 0;
int transparentCount = 0;
int solidCount = 0;
if (ReadOnlyMaterials && Mesh)
{
for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
{
scene::IMeshBuffer* mb = Mesh->getMeshBuffer(i);
video::IMaterialRenderer* rnd = mb ? driver->getMaterialRenderer(mb->getMaterial().MaterialType) : 0;
if (rnd && rnd->isTransparent())
++transparentCount;
else
++solidCount;
if (solidCount && transparentCount)
break;
}
}
else
{
for (u32 i=0; i<Materials.size(); ++i)
{
video::IMaterialRenderer* rnd =
driver->getMaterialRenderer(Materials[i].MaterialType);
if (rnd && rnd->isTransparent())
++transparentCount;
else
++solidCount;
if (solidCount && transparentCount)
break;
}
}
if (solidCount)
SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
if (transparentCount)
SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
ISceneNode::OnRegisterSceneNode();
}
}
void CMeshSceneNode::render()
{
video::IVideoDriver* driver = SceneManager->getVideoDriver();
if (!Mesh || !driver)
return;
bool isTransparentPass =
SceneManager->getSceneNodeRenderPass() == scene::ESNRP_TRANSPARENT;
++PassCount;
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
Box = Mesh->getBoundingBox();
bool renderMeshes = true;
video::SMaterial mat;
if (DebugDataVisible && PassCount==1)
{
if (DebugDataVisible & scene::EDS_HALF_TRANSPARENCY)
{
for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
{
mat = Materials[g];
mat.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
driver->setMaterial(mat);
driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
}
renderMeshes = false;
}
}
if (renderMeshes)
{
for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
{
scene::IMeshBuffer* mb = Mesh->getMeshBuffer(i);
if (mb)
{
const video::SMaterial& material = ReadOnlyMaterials ? mb->getMaterial() : Materials[i];
video::IMaterialRenderer* rnd = driver->getMaterialRenderer(material.MaterialType);
bool transparent = (rnd && rnd->isTransparent());
if (transparent == isTransparentPass)
{
driver->setMaterial(material);
driver->drawMeshBuffer(mb);
}
}
}
}
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
if (DebugDataVisible && PassCount==1)
{
video::SMaterial m;
m.Lighting = false;
m.AntiAliasing=0;
driver->setMaterial(m);
if (DebugDataVisible & scene::EDS_BBOX)
{
driver->draw3DBox(Box, video::SColor(255,255,255,255));
}
if (DebugDataVisible & scene::EDS_BBOX_BUFFERS)
{
for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
{
driver->draw3DBox(
Mesh->getMeshBuffer(g)->getBoundingBox(),
video::SColor(255,190,128,128));
}
}
if (DebugDataVisible & scene::EDS_NORMALS)
{
core::vector3df normalizedNormal;
const f32 DebugNormalLength = SceneManager->getParameters()->getAttributeAsFloat(DEBUG_NORMAL_LENGTH);
const video::SColor DebugNormalColor = SceneManager->getParameters()->getAttributeAsColor(DEBUG_NORMAL_COLOR);
for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
{
const scene::IMeshBuffer* mb = Mesh->getMeshBuffer(g);
const u32 vSize = video::getVertexPitchFromType(mb->getVertexType());
const video::S3DVertex* v = ( const video::S3DVertex*)mb->getVertices();
const bool normalize = mb->getMaterial().NormalizeNormals;
for (u32 i=0; i != mb->getVertexCount(); ++i)
{
normalizedNormal = v->Normal;
if (normalize)
normalizedNormal.normalize();
driver->draw3DLine(v->Pos, v->Pos + (normalizedNormal * DebugNormalLength), DebugNormalColor);
v = (const video::S3DVertex*) ( (u8*) v+vSize );
}
}
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
}
if (DebugDataVisible & scene::EDS_MESH_WIRE_OVERLAY)
{
m.Wireframe = true;
driver->setMaterial(m);
for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
{
driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
}
}
}
}
const core::aabbox3d<f32>& CMeshSceneNode::getBoundingBox() const
{
return Mesh ? Mesh->getBoundingBox() : Box;
}
video::SMaterial& CMeshSceneNode::getMaterial(u32 i)
{
if (Mesh && ReadOnlyMaterials && i<Mesh->getMeshBufferCount())
{
ReadOnlyMaterial = Mesh->getMeshBuffer(i)->getMaterial();
return ReadOnlyMaterial;
}
if (i >= Materials.size())
return ISceneNode::getMaterial(i);
return Materials[i];
}
u32 CMeshSceneNode::getMaterialCount() const
{
if (Mesh && ReadOnlyMaterials)
return Mesh->getMeshBufferCount();
return Materials.size();
}
void CMeshSceneNode::setMesh(IMesh* mesh)
{
if (mesh)
{
mesh->grab();
if (Mesh)
Mesh->drop();
Mesh = mesh;
copyMaterials();
}
}
void CMeshSceneNode::copyMaterials()
{
Materials.clear();
if (Mesh)
{
video::SMaterial mat;
for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
{
IMeshBuffer* mb = Mesh->getMeshBuffer(i);
if (mb)
mat = mb->getMaterial();
Materials.push_back(mat);
}
}
}
void CMeshSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
{
IMeshSceneNode::serializeAttributes(out, options);
out->addString("Mesh", SceneManager->getMeshCache()->getMeshName(Mesh).getPath().c_str());
out->addBool("ReadOnlyMaterials", ReadOnlyMaterials);
}
void CMeshSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
io::path oldMeshStr = SceneManager->getMeshCache()->getMeshName(Mesh);
io::path newMeshStr = in->getAttributeAsString("Mesh");
ReadOnlyMaterials = in->getAttributeAsBool("ReadOnlyMaterials");
if (newMeshStr != "" && oldMeshStr != newMeshStr)
{
IMesh* newMesh = 0;
IAnimatedMesh* newAnimatedMesh = SceneManager->getMesh(newMeshStr.c_str());
if (newAnimatedMesh)
newMesh = newAnimatedMesh->getMesh(0);
if (newMesh)
setMesh(newMesh);
}
if (in->existsAttribute("HardwareMappingHint") &&
in->existsAttribute("HardwareMappingBufferType"))
{
scene::E_HARDWARE_MAPPING mapping = scene::EHM_NEVER;
scene::E_BUFFER_TYPE bufferType = scene::EBT_NONE;
core::stringc smapping = in->getAttributeAsString("HardwareMappingHint");
if (smapping.equals_ignore_case("static"))
mapping = scene::EHM_STATIC;
else if (smapping.equals_ignore_case("dynamic"))
mapping = scene::EHM_DYNAMIC;
else if (smapping.equals_ignore_case("stream"))
mapping = scene::EHM_STREAM;
core::stringc sbufferType = in->getAttributeAsString("HardwareMappingBufferType");
if (sbufferType.equals_ignore_case("vertex"))
bufferType = scene::EBT_VERTEX;
else if (sbufferType.equals_ignore_case("index"))
bufferType = scene::EBT_INDEX;
else if (sbufferType.equals_ignore_case("vertexindex"))
bufferType = scene::EBT_VERTEX_AND_INDEX;
IMesh* mesh = getMesh();
if (mesh)
mesh->setHardwareMappingHint(mapping, bufferType);
}
IMeshSceneNode::deserializeAttributes(in, options);
}
void CMeshSceneNode::setReadOnlyMaterials(bool readonly)
{
ReadOnlyMaterials = readonly;
}
bool CMeshSceneNode::isReadOnlyMaterials() const
{
return ReadOnlyMaterials;
}
ISceneNode* CMeshSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
{
if (!newParent)
newParent = Parent;
if (!newManager)
newManager = SceneManager;
CMeshSceneNode* nb = new CMeshSceneNode(Mesh, newParent,
newManager, ID, RelativeTranslation, RelativeRotation, RelativeScale);
nb->cloneMembers(this, newManager);
nb->ReadOnlyMaterials = ReadOnlyMaterials;
nb->Materials = Materials;
if (newParent)
nb->drop();
return nb;
}
}
}