~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CSphereSceneNode.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 "CSphereSceneNode.h"
 
6
#include "IVideoDriver.h"
 
7
#include "ISceneManager.h"
 
8
#include "S3DVertex.h"
 
9
#include "os.h"
 
10
 
 
11
namespace irr
 
12
{
 
13
namespace scene
 
14
{
 
15
 
 
16
//! constructor
 
17
CSphereSceneNode::CSphereSceneNode(f32 radius, u32 polyCountX, u32 polyCountY, ISceneNode* parent, ISceneManager* mgr, s32 id,
 
18
                        const core::vector3df& position, const core::vector3df& rotation, const core::vector3df& scale)
 
19
: IMeshSceneNode(parent, mgr, id, position, rotation, scale), Mesh(0),
 
20
        Radius(radius), PolyCountX(polyCountX), PolyCountY(polyCountY)
 
21
{
 
22
        #ifdef _DEBUG
 
23
        setDebugName("CSphereSceneNode");
 
24
        #endif
 
25
 
 
26
        Mesh = SceneManager->getGeometryCreator()->createSphereMesh(radius, polyCountX, polyCountY);
 
27
}
 
28
 
 
29
 
 
30
 
 
31
//! destructor
 
32
CSphereSceneNode::~CSphereSceneNode()
 
33
{
 
34
        if (Mesh)
 
35
                Mesh->drop();
 
36
}
 
37
 
 
38
 
 
39
//! renders the node.
 
40
void CSphereSceneNode::render()
 
41
{
 
42
        video::IVideoDriver* driver = SceneManager->getVideoDriver();
 
43
 
 
44
        if (Mesh && driver)
 
45
        {
 
46
                driver->setMaterial(Mesh->getMeshBuffer(0)->getMaterial());
 
47
                driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
 
48
                driver->drawMeshBuffer(Mesh->getMeshBuffer(0));
 
49
                if ( DebugDataVisible & scene::EDS_BBOX )
 
50
                {
 
51
                        video::SMaterial m;
 
52
                        m.Lighting = false;
 
53
                        driver->setMaterial(m);
 
54
                        driver->draw3DBox(Mesh->getMeshBuffer(0)->getBoundingBox(), video::SColor(255,255,255,255));
 
55
                }
 
56
        }
 
57
}
 
58
 
 
59
 
 
60
 
 
61
//! returns the axis aligned bounding box of this node
 
62
const core::aabbox3d<f32>& CSphereSceneNode::getBoundingBox() const
 
63
{
 
64
        return Mesh ? Mesh->getBoundingBox() : Box;
 
65
}
 
66
 
 
67
 
 
68
void CSphereSceneNode::OnRegisterSceneNode()
 
69
{
 
70
        if (IsVisible)
 
71
                SceneManager->registerNodeForRendering(this);
 
72
 
 
73
        ISceneNode::OnRegisterSceneNode();
 
74
}
 
75
 
 
76
 
 
77
//! returns the material based on the zero based index i. To get the amount
 
78
//! of materials used by this scene node, use getMaterialCount().
 
79
//! This function is needed for inserting the node into the scene hirachy on a
 
80
//! optimal position for minimizing renderstate changes, but can also be used
 
81
//! to directly modify the material of a scene node.
 
82
video::SMaterial& CSphereSceneNode::getMaterial(u32 i)
 
83
{
 
84
        if (i>0 || !Mesh)
 
85
                return ISceneNode::getMaterial(i);
 
86
        else
 
87
                return Mesh->getMeshBuffer(i)->getMaterial();
 
88
}
 
89
 
 
90
 
 
91
//! returns amount of materials used by this scene node.
 
92
u32 CSphereSceneNode::getMaterialCount() const
 
93
{
 
94
        return 1;
 
95
}
 
96
 
 
97
 
 
98
//! Writes attributes of the scene node.
 
99
void CSphereSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
 
100
{
 
101
        ISceneNode::serializeAttributes(out, options);
 
102
 
 
103
        out->addFloat("Radius", Radius);
 
104
        out->addInt("PolyCountX", PolyCountX);
 
105
        out->addInt("PolyCountY", PolyCountY);
 
106
}
 
107
 
 
108
 
 
109
//! Reads attributes of the scene node.
 
110
void CSphereSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
 
111
{
 
112
        f32 oldRadius = Radius;
 
113
        u32 oldPolyCountX = PolyCountX;
 
114
        u32 oldPolyCountY = PolyCountY;
 
115
 
 
116
        Radius = in->getAttributeAsFloat("Radius");
 
117
        PolyCountX = in->getAttributeAsInt("PolyCountX");
 
118
        PolyCountY = in->getAttributeAsInt("PolyCountY");
 
119
        // legacy values read for compatibility with older versions
 
120
        u32 polyCount = in->getAttributeAsInt("PolyCount");
 
121
        if (PolyCountX ==0 && PolyCountY == 0)
 
122
                PolyCountX = PolyCountY = polyCount;
 
123
 
 
124
        Radius = core::max_(Radius, 0.0001f);
 
125
 
 
126
        if ( !core::equals(Radius, oldRadius) || PolyCountX != oldPolyCountX || PolyCountY != oldPolyCountY)
 
127
        {
 
128
                if (Mesh)
 
129
                        Mesh->drop();
 
130
                Mesh = SceneManager->getGeometryCreator()->createSphereMesh(Radius, PolyCountX, PolyCountY);
 
131
        }
 
132
 
 
133
        ISceneNode::deserializeAttributes(in, options);
 
134
}
 
135
 
 
136
//! Creates a clone of this scene node and its children.
 
137
ISceneNode* CSphereSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
 
138
{
 
139
        if (!newParent)
 
140
                newParent = Parent;
 
141
        if (!newManager)
 
142
                newManager = SceneManager;
 
143
 
 
144
        CSphereSceneNode* nb = new CSphereSceneNode(Radius, PolyCountX, PolyCountY, newParent,
 
145
                newManager, ID, RelativeTranslation);
 
146
 
 
147
        nb->cloneMembers(this, newManager);
 
148
        nb->getMaterial(0) = Mesh->getMeshBuffer(0)->getMaterial();
 
149
 
 
150
        if ( newParent )
 
151
                nb->drop();
 
152
        return nb;
 
153
}
 
154
 
 
155
} // end namespace scene
 
156
} // end namespace irr
 
157