1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "CMeshSceneNode.h"
\r
6 #include "IVideoDriver.h"
\r
7 #include "ISceneManager.h"
\r
8 #include "S3DVertex.h"
\r
9 #include "ICameraSceneNode.h"
\r
10 #include "IMeshCache.h"
\r
11 #include "IAnimatedMesh.h"
\r
12 #include "IMaterialRenderer.h"
\r
13 #include "IFileSystem.h"
\r
14 #ifdef _IRR_COMPILE_WITH_SHADOW_VOLUME_SCENENODE_
\r
15 #include "CShadowVolumeSceneNode.h"
\r
17 #include "IShadowVolumeSceneNode.h"
\r
18 #endif // _IRR_COMPILE_WITH_SHADOW_VOLUME_SCENENODE_
\r
28 CMeshSceneNode::CMeshSceneNode(IMesh* mesh, ISceneNode* parent, ISceneManager* mgr, s32 id,
\r
29 const core::vector3df& position, const core::vector3df& rotation,
\r
30 const core::vector3df& scale)
\r
31 : IMeshSceneNode(parent, mgr, id, position, rotation, scale), Mesh(0), Shadow(0),
\r
32 PassCount(0), ReadOnlyMaterials(false)
\r
35 setDebugName("CMeshSceneNode");
\r
43 CMeshSceneNode::~CMeshSceneNode()
\r
53 void CMeshSceneNode::OnRegisterSceneNode()
\r
55 if (IsVisible && Mesh)
\r
57 // because this node supports rendering of mixed mode meshes consisting of
\r
58 // transparent and solid material at the same time, we need to go through all
\r
59 // materials, check of what type they are and register this node for the right
\r
60 // render pass according to that.
\r
62 video::IVideoDriver* driver = SceneManager->getVideoDriver();
\r
65 int transparentCount = 0;
\r
68 // count transparent and solid materials in this scene node
\r
69 const u32 numMaterials = ReadOnlyMaterials ? Mesh->getMeshBufferCount() : Materials.size();
\r
70 for (u32 i=0; i<numMaterials; ++i)
\r
72 const video::SMaterial& material = ReadOnlyMaterials ? Mesh->getMeshBuffer(i)->getMaterial() : Materials[i];
\r
74 if ( driver->needsTransparentRenderPass(material) )
\r
79 if (solidCount && transparentCount)
\r
83 // register according to material types counted
\r
86 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
\r
88 if (transparentCount)
\r
89 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
\r
91 ISceneNode::OnRegisterSceneNode();
\r
96 //! renders the node.
\r
97 void CMeshSceneNode::render()
\r
99 video::IVideoDriver* driver = SceneManager->getVideoDriver();
\r
101 if (!Mesh || !driver)
\r
104 const bool isTransparentPass =
\r
105 SceneManager->getSceneNodeRenderPass() == scene::ESNRP_TRANSPARENT;
\r
109 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
\r
110 Box = Mesh->getBoundingBox();
\r
112 if (Shadow && PassCount==1)
\r
113 Shadow->updateShadowVolumes();
\r
115 // for debug purposes only:
\r
117 bool renderMeshes = true;
\r
118 video::SMaterial mat;
\r
119 if (DebugDataVisible && PassCount==1)
\r
121 // overwrite half transparency
\r
122 if (DebugDataVisible & scene::EDS_HALF_TRANSPARENCY)
\r
124 for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
\r
126 mat = Materials[g];
\r
127 mat.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
\r
128 driver->setMaterial(mat);
\r
129 driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
\r
131 renderMeshes = false;
\r
135 // render original meshes
\r
138 for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
\r
140 scene::IMeshBuffer* mb = Mesh->getMeshBuffer(i);
\r
143 const video::SMaterial& material = ReadOnlyMaterials ? mb->getMaterial() : Materials[i];
\r
145 const bool transparent = driver->needsTransparentRenderPass(material);
\r
147 // only render transparent buffer if this is the transparent render pass
\r
148 // and solid only in solid pass
\r
149 if (transparent == isTransparentPass)
\r
151 driver->setMaterial(material);
\r
152 driver->drawMeshBuffer(mb);
\r
158 // for debug purposes only:
\r
159 if (DebugDataVisible && PassCount==1)
\r
161 video::SMaterial m;
\r
162 m.Lighting = false;
\r
164 driver->setMaterial(m);
\r
166 if (DebugDataVisible & scene::EDS_BBOX)
\r
168 driver->draw3DBox(Box, video::SColor(255,255,255,255));
\r
170 if (DebugDataVisible & scene::EDS_BBOX_BUFFERS)
\r
172 for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
\r
175 Mesh->getMeshBuffer(g)->getBoundingBox(),
\r
176 video::SColor(255,190,128,128));
\r
180 if (DebugDataVisible & scene::EDS_NORMALS)
\r
183 const f32 debugNormalLength = SceneManager->getParameters()->getAttributeAsFloat(DEBUG_NORMAL_LENGTH);
\r
184 const video::SColor debugNormalColor = SceneManager->getParameters()->getAttributeAsColor(DEBUG_NORMAL_COLOR);
\r
185 const u32 count = Mesh->getMeshBufferCount();
\r
187 for (u32 i=0; i != count; ++i)
\r
189 driver->drawMeshBufferNormals(Mesh->getMeshBuffer(i), debugNormalLength, debugNormalColor);
\r
194 if (DebugDataVisible & scene::EDS_MESH_WIRE_OVERLAY)
\r
196 m.Wireframe = true;
\r
197 driver->setMaterial(m);
\r
199 for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
\r
201 driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
\r
208 //! Removes a child from this scene node.
\r
209 //! Implemented here, to be able to remove the shadow properly, if there is one,
\r
210 //! or to remove attached childs.
\r
211 bool CMeshSceneNode::removeChild(ISceneNode* child)
\r
213 if (child && Shadow == child)
\r
219 return ISceneNode::removeChild(child);
\r
223 //! returns the axis aligned bounding box of this node
\r
224 const core::aabbox3d<f32>& CMeshSceneNode::getBoundingBox() const
\r
226 return Mesh ? Mesh->getBoundingBox() : Box;
\r
230 //! returns the material based on the zero based index i. To get the amount
\r
231 //! of materials used by this scene node, use getMaterialCount().
\r
232 //! This function is needed for inserting the node into the scene hierarchy on a
\r
233 //! optimal position for minimizing renderstate changes, but can also be used
\r
234 //! to directly modify the material of a scene node.
\r
235 video::SMaterial& CMeshSceneNode::getMaterial(u32 i)
\r
237 if (Mesh && ReadOnlyMaterials && i<Mesh->getMeshBufferCount())
\r
239 ReadOnlyMaterial = Mesh->getMeshBuffer(i)->getMaterial();
\r
240 return ReadOnlyMaterial;
\r
243 if (i >= Materials.size())
\r
244 return ISceneNode::getMaterial(i);
\r
246 return Materials[i];
\r
250 //! returns amount of materials used by this scene node.
\r
251 u32 CMeshSceneNode::getMaterialCount() const
\r
253 if (Mesh && ReadOnlyMaterials)
\r
254 return Mesh->getMeshBufferCount();
\r
256 return Materials.size();
\r
260 //! Sets a new mesh
\r
261 void CMeshSceneNode::setMesh(IMesh* mesh)
\r
275 //! Creates shadow volume scene node as child of this node
\r
276 //! and returns a pointer to it.
\r
277 IShadowVolumeSceneNode* CMeshSceneNode::addShadowVolumeSceneNode(
\r
278 const IMesh* shadowMesh, s32 id, bool zfailmethod, f32 infinity)
\r
280 #ifdef _IRR_COMPILE_WITH_SHADOW_VOLUME_SCENENODE_
\r
281 if (!SceneManager->getVideoDriver()->queryFeature(video::EVDF_STENCIL_BUFFER))
\r
285 shadowMesh = Mesh; // if null is given, use the mesh of node
\r
290 Shadow = new CShadowVolumeSceneNode(shadowMesh, this, SceneManager, id, zfailmethod, infinity);
\r
298 void CMeshSceneNode::copyMaterials()
\r
304 video::SMaterial mat;
\r
306 for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
\r
308 IMeshBuffer* mb = Mesh->getMeshBuffer(i);
\r
310 mat = mb->getMaterial();
\r
312 Materials.push_back(mat);
\r
318 //! Writes attributes of the scene node.
\r
319 void CMeshSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
\r
321 IMeshSceneNode::serializeAttributes(out, options);
\r
323 if (options && (options->Flags&io::EARWF_USE_RELATIVE_PATHS) && options->Filename)
\r
325 const io::path path = SceneManager->getFileSystem()->getRelativeFilename(
\r
326 SceneManager->getFileSystem()->getAbsolutePath(SceneManager->getMeshCache()->getMeshName(Mesh).getPath()),
\r
327 options->Filename);
\r
328 out->addString("Mesh", path.c_str());
\r
331 out->addString("Mesh", SceneManager->getMeshCache()->getMeshName(Mesh).getPath().c_str());
\r
332 out->addBool("ReadOnlyMaterials", ReadOnlyMaterials);
\r
336 //! Reads attributes of the scene node.
\r
337 void CMeshSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
\r
339 io::path oldMeshStr = SceneManager->getMeshCache()->getMeshName(Mesh);
\r
340 io::path newMeshStr = in->getAttributeAsString("Mesh");
\r
341 ReadOnlyMaterials = in->getAttributeAsBool("ReadOnlyMaterials");
\r
343 if (newMeshStr != "" && oldMeshStr != newMeshStr)
\r
345 IMesh* newMesh = 0;
\r
346 IAnimatedMesh* newAnimatedMesh = SceneManager->getMesh(newMeshStr.c_str());
\r
348 if (newAnimatedMesh)
\r
349 newMesh = newAnimatedMesh->getMesh(0);
\r
355 // optional attribute to assign the hint to the whole mesh
\r
356 if (in->existsAttribute("HardwareMappingHint") &&
\r
357 in->existsAttribute("HardwareMappingBufferType"))
\r
359 scene::E_HARDWARE_MAPPING mapping = scene::EHM_NEVER;
\r
360 scene::E_BUFFER_TYPE bufferType = scene::EBT_NONE;
\r
362 core::stringc smapping = in->getAttributeAsString("HardwareMappingHint");
\r
363 if (smapping.equals_ignore_case("static"))
\r
364 mapping = scene::EHM_STATIC;
\r
365 else if (smapping.equals_ignore_case("dynamic"))
\r
366 mapping = scene::EHM_DYNAMIC;
\r
367 else if (smapping.equals_ignore_case("stream"))
\r
368 mapping = scene::EHM_STREAM;
\r
370 core::stringc sbufferType = in->getAttributeAsString("HardwareMappingBufferType");
\r
371 if (sbufferType.equals_ignore_case("vertex"))
\r
372 bufferType = scene::EBT_VERTEX;
\r
373 else if (sbufferType.equals_ignore_case("index"))
\r
374 bufferType = scene::EBT_INDEX;
\r
375 else if (sbufferType.equals_ignore_case("vertexindex"))
\r
376 bufferType = scene::EBT_VERTEX_AND_INDEX;
\r
378 IMesh* mesh = getMesh();
\r
380 mesh->setHardwareMappingHint(mapping, bufferType);
\r
383 IMeshSceneNode::deserializeAttributes(in, options);
\r
387 //! Sets if the scene node should not copy the materials of the mesh but use them in a read only style.
\r
388 /* In this way it is possible to change the materials a mesh causing all mesh scene nodes
\r
389 referencing this mesh to change too. */
\r
390 void CMeshSceneNode::setReadOnlyMaterials(bool readonly)
\r
392 ReadOnlyMaterials = readonly;
\r
396 //! Returns if the scene node should not copy the materials of the mesh but use them in a read only style
\r
397 bool CMeshSceneNode::isReadOnlyMaterials() const
\r
399 return ReadOnlyMaterials;
\r
403 //! Creates a clone of this scene node and its children.
\r
404 ISceneNode* CMeshSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
\r
407 newParent = Parent;
\r
409 newManager = SceneManager;
\r
411 CMeshSceneNode* nb = new CMeshSceneNode(Mesh, newParent,
\r
412 newManager, ID, RelativeTranslation, RelativeRotation, RelativeScale);
\r
414 nb->cloneMembers(this, newManager);
\r
415 nb->ReadOnlyMaterials = ReadOnlyMaterials;
\r
416 nb->Materials = Materials;
\r
417 nb->Shadow = Shadow;
\r
419 nb->Shadow->grab();
\r
427 } // end namespace scene
\r
428 } // end namespace irr
\r