~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CMeshSceneNode.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
 
2
// This file is part of the "Irrlicht Engine".
 
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
 
4
 
 
5
#include "CMeshSceneNode.h"
 
6
#include "IVideoDriver.h"
 
7
#include "ISceneManager.h"
 
8
#include "S3DVertex.h"
 
9
#include "ICameraSceneNode.h"
 
10
#include "IMeshCache.h"
 
11
#include "IAnimatedMesh.h"
 
12
#include "IMaterialRenderer.h"
 
13
#include "IFileSystem.h"
 
14
 
 
15
namespace irr
 
16
{
 
17
namespace scene
 
18
{
 
19
 
 
20
 
 
21
 
 
22
//! constructor
 
23
CMeshSceneNode::CMeshSceneNode(IMesh* mesh, ISceneNode* parent, ISceneManager* mgr, s32 id,
 
24
                        const core::vector3df& position, const core::vector3df& rotation,
 
25
                        const core::vector3df& scale)
 
26
: IMeshSceneNode(parent, mgr, id, position, rotation, scale), Mesh(0), PassCount(0),
 
27
        ReadOnlyMaterials(false)
 
28
{
 
29
        #ifdef _DEBUG
 
30
        setDebugName("CMeshSceneNode");
 
31
        #endif
 
32
 
 
33
        setMesh(mesh);
 
34
}
 
35
 
 
36
 
 
37
//! destructor
 
38
CMeshSceneNode::~CMeshSceneNode()
 
39
{
 
40
        if (Mesh)
 
41
                Mesh->drop();
 
42
}
 
43
 
 
44
 
 
45
//! frame
 
46
void CMeshSceneNode::OnRegisterSceneNode()
 
47
{
 
48
        if (IsVisible)
 
49
        {
 
50
                // because this node supports rendering of mixed mode meshes consisting of
 
51
                // transparent and solid material at the same time, we need to go through all
 
52
                // materials, check of what type they are and register this node for the right
 
53
                // render pass according to that.
 
54
 
 
55
                video::IVideoDriver* driver = SceneManager->getVideoDriver();
 
56
 
 
57
                PassCount = 0;
 
58
                int transparentCount = 0;
 
59
                int solidCount = 0;
 
60
 
 
61
                // count transparent and solid materials in this scene node
 
62
                if (ReadOnlyMaterials && Mesh)
 
63
                {
 
64
                        // count mesh materials
 
65
 
 
66
                        for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
 
67
                        {
 
68
                                scene::IMeshBuffer* mb = Mesh->getMeshBuffer(i);
 
69
                                video::IMaterialRenderer* rnd = mb ? driver->getMaterialRenderer(mb->getMaterial().MaterialType) : 0;
 
70
 
 
71
                                if (rnd && rnd->isTransparent())
 
72
                                        ++transparentCount;
 
73
                                else
 
74
                                        ++solidCount;
 
75
 
 
76
                                if (solidCount && transparentCount)
 
77
                                        break;
 
78
                        }
 
79
                }
 
80
                else
 
81
                {
 
82
                        // count copied materials
 
83
 
 
84
                        for (u32 i=0; i<Materials.size(); ++i)
 
85
                        {
 
86
                                video::IMaterialRenderer* rnd =
 
87
                                        driver->getMaterialRenderer(Materials[i].MaterialType);
 
88
 
 
89
                                if (rnd && rnd->isTransparent())
 
90
                                        ++transparentCount;
 
91
                                else
 
92
                                        ++solidCount;
 
93
 
 
94
                                if (solidCount && transparentCount)
 
95
                                        break;
 
96
                        }
 
97
                }
 
98
 
 
99
                // register according to material types counted
 
100
 
 
101
                if (solidCount)
 
102
                        SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
 
103
 
 
104
                if (transparentCount)
 
105
                        SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
 
106
 
 
107
                ISceneNode::OnRegisterSceneNode();
 
108
        }
 
109
}
 
110
 
 
111
 
 
112
//! renders the node.
 
113
void CMeshSceneNode::render()
 
114
{
 
115
        video::IVideoDriver* driver = SceneManager->getVideoDriver();
 
116
 
 
117
        if (!Mesh || !driver)
 
118
                return;
 
119
 
 
120
        bool isTransparentPass =
 
121
                SceneManager->getSceneNodeRenderPass() == scene::ESNRP_TRANSPARENT;
 
122
 
 
123
        ++PassCount;
 
124
 
 
125
        driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
 
126
        Box = Mesh->getBoundingBox();
 
127
 
 
128
        // for debug purposes only:
 
129
 
 
130
        bool renderMeshes = true;
 
131
        video::SMaterial mat;
 
132
        if (DebugDataVisible && PassCount==1)
 
133
        {
 
134
                // overwrite half transparency
 
135
                if (DebugDataVisible & scene::EDS_HALF_TRANSPARENCY)
 
136
                {
 
137
                        for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
 
138
                        {
 
139
                                mat = Materials[g];
 
140
                                mat.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
 
141
                                driver->setMaterial(mat);
 
142
                                driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
 
143
                        }
 
144
                        renderMeshes = false;
 
145
                }
 
146
        }
 
147
 
 
148
        // render original meshes
 
149
        if (renderMeshes)
 
150
        {
 
151
                for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
 
152
                {
 
153
                        scene::IMeshBuffer* mb = Mesh->getMeshBuffer(i);
 
154
                        if (mb)
 
155
                        {
 
156
                                const video::SMaterial& material = ReadOnlyMaterials ? mb->getMaterial() : Materials[i];
 
157
 
 
158
                                video::IMaterialRenderer* rnd = driver->getMaterialRenderer(material.MaterialType);
 
159
                                bool transparent = (rnd && rnd->isTransparent());
 
160
 
 
161
                                // only render transparent buffer if this is the transparent render pass
 
162
                                // and solid only in solid pass
 
163
                                if (transparent == isTransparentPass)
 
164
                                {
 
165
                                        driver->setMaterial(material);
 
166
                                        driver->drawMeshBuffer(mb);
 
167
                                }
 
168
                        }
 
169
                }
 
170
        }
 
171
 
 
172
        driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
 
173
 
 
174
        // for debug purposes only:
 
175
        if (DebugDataVisible && PassCount==1)
 
176
        {
 
177
                video::SMaterial m;
 
178
                m.Lighting = false;
 
179
                m.AntiAliasing=0;
 
180
                driver->setMaterial(m);
 
181
 
 
182
                if (DebugDataVisible & scene::EDS_BBOX)
 
183
                {
 
184
                        driver->draw3DBox(Box, video::SColor(255,255,255,255));
 
185
                }
 
186
                if (DebugDataVisible & scene::EDS_BBOX_BUFFERS)
 
187
                {
 
188
                        for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
 
189
                        {
 
190
                                driver->draw3DBox(
 
191
                                        Mesh->getMeshBuffer(g)->getBoundingBox(),
 
192
                                        video::SColor(255,190,128,128));
 
193
                        }
 
194
                }
 
195
 
 
196
                if (DebugDataVisible & scene::EDS_NORMALS)
 
197
                {
 
198
 
 
199
                        // draw normals
 
200
                        core::vector3df normalizedNormal;
 
201
                        const f32 DebugNormalLength = SceneManager->getParameters()->getAttributeAsFloat(DEBUG_NORMAL_LENGTH);
 
202
                        const video::SColor DebugNormalColor = SceneManager->getParameters()->getAttributeAsColor(DEBUG_NORMAL_COLOR);
 
203
 
 
204
                        for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
 
205
                        {
 
206
                                const scene::IMeshBuffer* mb = Mesh->getMeshBuffer(g);
 
207
                                const u32 vSize = video::getVertexPitchFromType(mb->getVertexType());
 
208
                                const video::S3DVertex* v = ( const video::S3DVertex*)mb->getVertices();
 
209
                                const bool normalize = mb->getMaterial().NormalizeNormals;
 
210
 
 
211
                                for (u32 i=0; i != mb->getVertexCount(); ++i)
 
212
                                {
 
213
                                        normalizedNormal = v->Normal;
 
214
                                        if (normalize)
 
215
                                                normalizedNormal.normalize();
 
216
 
 
217
                                        driver->draw3DLine(v->Pos, v->Pos + (normalizedNormal * DebugNormalLength), DebugNormalColor);
 
218
 
 
219
                                        v = (const video::S3DVertex*) ( (u8*) v+vSize );
 
220
                                }
 
221
                        }
 
222
                        driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
 
223
                }
 
224
 
 
225
                // show mesh
 
226
                if (DebugDataVisible & scene::EDS_MESH_WIRE_OVERLAY)
 
227
                {
 
228
                        m.Wireframe = true;
 
229
                        driver->setMaterial(m);
 
230
 
 
231
                        for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
 
232
                        {
 
233
                                driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
 
234
                        }
 
235
                }
 
236
        }
 
237
}
 
238
 
 
239
 
 
240
//! returns the axis aligned bounding box of this node
 
241
const core::aabbox3d<f32>& CMeshSceneNode::getBoundingBox() const
 
242
{
 
243
        return Mesh ? Mesh->getBoundingBox() : Box;
 
244
}
 
245
 
 
246
 
 
247
//! returns the material based on the zero based index i. To get the amount
 
248
//! of materials used by this scene node, use getMaterialCount().
 
249
//! This function is needed for inserting the node into the scene hierarchy on a
 
250
//! optimal position for minimizing renderstate changes, but can also be used
 
251
//! to directly modify the material of a scene node.
 
252
video::SMaterial& CMeshSceneNode::getMaterial(u32 i)
 
253
{
 
254
        if (Mesh && ReadOnlyMaterials && i<Mesh->getMeshBufferCount())
 
255
        {
 
256
                ReadOnlyMaterial = Mesh->getMeshBuffer(i)->getMaterial();
 
257
                return ReadOnlyMaterial;
 
258
        }
 
259
 
 
260
        if (i >= Materials.size())
 
261
                return ISceneNode::getMaterial(i);
 
262
 
 
263
        return Materials[i];
 
264
}
 
265
 
 
266
 
 
267
//! returns amount of materials used by this scene node.
 
268
u32 CMeshSceneNode::getMaterialCount() const
 
269
{
 
270
        if (Mesh && ReadOnlyMaterials)
 
271
                return Mesh->getMeshBufferCount();
 
272
 
 
273
        return Materials.size();
 
274
}
 
275
 
 
276
 
 
277
//! Sets a new mesh
 
278
void CMeshSceneNode::setMesh(IMesh* mesh)
 
279
{
 
280
        if (mesh)
 
281
        {
 
282
                mesh->grab();
 
283
                if (Mesh)
 
284
                        Mesh->drop();
 
285
 
 
286
                Mesh = mesh;
 
287
                copyMaterials();
 
288
        }
 
289
}
 
290
 
 
291
 
 
292
void CMeshSceneNode::copyMaterials()
 
293
{
 
294
        Materials.clear();
 
295
 
 
296
        if (Mesh)
 
297
        {
 
298
                video::SMaterial mat;
 
299
 
 
300
                for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
 
301
                {
 
302
                        IMeshBuffer* mb = Mesh->getMeshBuffer(i);
 
303
                        if (mb)
 
304
                                mat = mb->getMaterial();
 
305
 
 
306
                        Materials.push_back(mat);
 
307
                }
 
308
        }
 
309
}
 
310
 
 
311
 
 
312
//! Writes attributes of the scene node.
 
313
void CMeshSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
 
314
{
 
315
        IMeshSceneNode::serializeAttributes(out, options);
 
316
 
 
317
        if (options && (options->Flags&io::EARWF_USE_RELATIVE_PATHS) && options->Filename)
 
318
        {
 
319
                const io::path path = SceneManager->getFileSystem()->getRelativeFilename(
 
320
                                SceneManager->getFileSystem()->getAbsolutePath(SceneManager->getMeshCache()->getMeshName(Mesh).getPath()),
 
321
                                options->Filename);
 
322
                out->addString("Mesh", path.c_str());
 
323
        }
 
324
        else
 
325
                out->addString("Mesh", SceneManager->getMeshCache()->getMeshName(Mesh).getPath().c_str());
 
326
        out->addBool("ReadOnlyMaterials", ReadOnlyMaterials);
 
327
}
 
328
 
 
329
 
 
330
//! Reads attributes of the scene node.
 
331
void CMeshSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
 
332
{
 
333
        io::path oldMeshStr = SceneManager->getMeshCache()->getMeshName(Mesh);
 
334
        io::path newMeshStr = in->getAttributeAsString("Mesh");
 
335
        ReadOnlyMaterials = in->getAttributeAsBool("ReadOnlyMaterials");
 
336
 
 
337
        if (newMeshStr != "" && oldMeshStr != newMeshStr)
 
338
        {
 
339
                IMesh* newMesh = 0;
 
340
                IAnimatedMesh* newAnimatedMesh = SceneManager->getMesh(newMeshStr.c_str());
 
341
 
 
342
                if (newAnimatedMesh)
 
343
                        newMesh = newAnimatedMesh->getMesh(0);
 
344
 
 
345
                if (newMesh)
 
346
                        setMesh(newMesh);
 
347
        }
 
348
 
 
349
        // optional attribute to assign the hint to the whole mesh
 
350
        if (in->existsAttribute("HardwareMappingHint") &&
 
351
                in->existsAttribute("HardwareMappingBufferType"))
 
352
        {
 
353
                scene::E_HARDWARE_MAPPING mapping = scene::EHM_NEVER;
 
354
                scene::E_BUFFER_TYPE bufferType = scene::EBT_NONE;
 
355
 
 
356
                core::stringc smapping = in->getAttributeAsString("HardwareMappingHint");
 
357
                if (smapping.equals_ignore_case("static"))
 
358
                        mapping = scene::EHM_STATIC;
 
359
                else if (smapping.equals_ignore_case("dynamic"))
 
360
                        mapping = scene::EHM_DYNAMIC;
 
361
                else if (smapping.equals_ignore_case("stream"))
 
362
                        mapping = scene::EHM_STREAM;
 
363
 
 
364
                core::stringc sbufferType = in->getAttributeAsString("HardwareMappingBufferType");
 
365
                if (sbufferType.equals_ignore_case("vertex"))
 
366
                        bufferType = scene::EBT_VERTEX;
 
367
                else if (sbufferType.equals_ignore_case("index"))
 
368
                        bufferType = scene::EBT_INDEX;
 
369
                else if (sbufferType.equals_ignore_case("vertexindex"))
 
370
                        bufferType = scene::EBT_VERTEX_AND_INDEX;
 
371
 
 
372
                IMesh* mesh = getMesh();
 
373
                if (mesh)
 
374
                        mesh->setHardwareMappingHint(mapping, bufferType);
 
375
        }
 
376
 
 
377
        IMeshSceneNode::deserializeAttributes(in, options);
 
378
}
 
379
 
 
380
 
 
381
//! Sets if the scene node should not copy the materials of the mesh but use them in a read only style.
 
382
/* In this way it is possible to change the materials a mesh causing all mesh scene nodes
 
383
referencing this mesh to change too. */
 
384
void CMeshSceneNode::setReadOnlyMaterials(bool readonly)
 
385
{
 
386
        ReadOnlyMaterials = readonly;
 
387
}
 
388
 
 
389
 
 
390
//! Returns if the scene node should not copy the materials of the mesh but use them in a read only style
 
391
bool CMeshSceneNode::isReadOnlyMaterials() const
 
392
{
 
393
        return ReadOnlyMaterials;
 
394
}
 
395
 
 
396
 
 
397
//! Creates a clone of this scene node and its children.
 
398
ISceneNode* CMeshSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
 
399
{
 
400
        if (!newParent)
 
401
                newParent = Parent;
 
402
        if (!newManager)
 
403
                newManager = SceneManager;
 
404
 
 
405
        CMeshSceneNode* nb = new CMeshSceneNode(Mesh, newParent,
 
406
                newManager, ID, RelativeTranslation, RelativeRotation, RelativeScale);
 
407
 
 
408
        nb->cloneMembers(this, newManager);
 
409
        nb->ReadOnlyMaterials = ReadOnlyMaterials;
 
410
        nb->Materials = Materials;
 
411
 
 
412
        if (newParent)
 
413
                nb->drop();
 
414
        return nb;
 
415
}
 
416
 
 
417
 
 
418
} // end namespace scene
 
419
} // end namespace irr
 
420