~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CBillboardSceneNode.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 "CBillboardSceneNode.h"
 
6
#include "IVideoDriver.h"
 
7
#include "ISceneManager.h"
 
8
#include "ICameraSceneNode.h"
 
9
#include "os.h"
 
10
 
 
11
namespace irr
 
12
{
 
13
namespace scene
 
14
{
 
15
 
 
16
//! constructor
 
17
CBillboardSceneNode::CBillboardSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id,
 
18
                        const core::vector3df& position, const core::dimension2d<f32>& size,
 
19
                        video::SColor colorTop, video::SColor colorBottom)
 
20
        : IBillboardSceneNode(parent, mgr, id, position)
 
21
{
 
22
        #ifdef _DEBUG
 
23
        setDebugName("CBillboardSceneNode");
 
24
        #endif
 
25
 
 
26
        setSize(size);
 
27
 
 
28
        indices[0] = 0;
 
29
        indices[1] = 2;
 
30
        indices[2] = 1;
 
31
        indices[3] = 0;
 
32
        indices[4] = 3;
 
33
        indices[5] = 2;
 
34
 
 
35
        vertices[0].TCoords.set(1.0f, 1.0f);
 
36
        vertices[0].Color = colorBottom;
 
37
 
 
38
        vertices[1].TCoords.set(1.0f, 0.0f);
 
39
        vertices[1].Color = colorTop;
 
40
 
 
41
        vertices[2].TCoords.set(0.0f, 0.0f);
 
42
        vertices[2].Color = colorTop;
 
43
 
 
44
        vertices[3].TCoords.set(0.0f, 1.0f);
 
45
        vertices[3].Color = colorBottom;
 
46
}
 
47
 
 
48
 
 
49
//! pre render event
 
50
void CBillboardSceneNode::OnRegisterSceneNode()
 
51
{
 
52
        if (IsVisible)
 
53
                SceneManager->registerNodeForRendering(this);
 
54
 
 
55
        ISceneNode::OnRegisterSceneNode();
 
56
}
 
57
 
 
58
 
 
59
//! render
 
60
void CBillboardSceneNode::render()
 
61
{
 
62
        video::IVideoDriver* driver = SceneManager->getVideoDriver();
 
63
        ICameraSceneNode* camera = SceneManager->getActiveCamera();
 
64
 
 
65
        if (!camera || !driver)
 
66
                return;
 
67
 
 
68
        // make billboard look to camera
 
69
 
 
70
        core::vector3df pos = getAbsolutePosition();
 
71
 
 
72
        core::vector3df campos = camera->getAbsolutePosition();
 
73
        core::vector3df target = camera->getTarget();
 
74
        core::vector3df up = camera->getUpVector();
 
75
        core::vector3df view = target - campos;
 
76
        view.normalize();
 
77
 
 
78
        core::vector3df horizontal = up.crossProduct(view);
 
79
        if ( horizontal.getLength() == 0 )
 
80
        {
 
81
                horizontal.set(up.Y,up.X,up.Z);
 
82
        }
 
83
        horizontal.normalize();
 
84
        horizontal *= 0.5f * Size.Width;
 
85
 
 
86
        core::vector3df vertical = horizontal.crossProduct(view);
 
87
        vertical.normalize();
 
88
        vertical *= 0.5f * Size.Height;
 
89
 
 
90
        view *= -1.0f;
 
91
 
 
92
        for (s32 i=0; i<4; ++i)
 
93
                vertices[i].Normal = view;
 
94
 
 
95
        vertices[0].Pos = pos + horizontal + vertical;
 
96
        vertices[1].Pos = pos + horizontal - vertical;
 
97
        vertices[2].Pos = pos - horizontal - vertical;
 
98
        vertices[3].Pos = pos - horizontal + vertical;
 
99
 
 
100
        // draw
 
101
 
 
102
        if ( DebugDataVisible & scene::EDS_BBOX )
 
103
        {
 
104
                driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
 
105
                video::SMaterial m;
 
106
                m.Lighting = false;
 
107
                driver->setMaterial(m);
 
108
                driver->draw3DBox(BBox, video::SColor(0,208,195,152));
 
109
        }
 
110
 
 
111
        driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
 
112
 
 
113
        driver->setMaterial(Material);
 
114
 
 
115
        driver->drawIndexedTriangleList(vertices, 4, indices, 2);
 
116
}
 
117
 
 
118
 
 
119
//! returns the axis aligned bounding box of this node
 
120
const core::aabbox3d<f32>& CBillboardSceneNode::getBoundingBox() const
 
121
{
 
122
        return BBox;
 
123
}
 
124
 
 
125
 
 
126
//! sets the size of the billboard
 
127
void CBillboardSceneNode::setSize(const core::dimension2d<f32>& size)
 
128
{
 
129
        Size = size;
 
130
 
 
131
        if (Size.Width == 0.0f)
 
132
                Size.Width = 1.0f;
 
133
 
 
134
        if (Size.Height == 0.0f )
 
135
                Size.Height = 1.0f;
 
136
 
 
137
        f32 avg = (size.Width + size.Height)/6;
 
138
        BBox.MinEdge.set(-avg,-avg,-avg);
 
139
        BBox.MaxEdge.set(avg,avg,avg);
 
140
}
 
141
 
 
142
 
 
143
video::SMaterial& CBillboardSceneNode::getMaterial(u32 i)
 
144
{
 
145
        return Material;
 
146
}
 
147
 
 
148
 
 
149
//! returns amount of materials used by this scene node.
 
150
u32 CBillboardSceneNode::getMaterialCount() const
 
151
{
 
152
        return 1;
 
153
}
 
154
 
 
155
 
 
156
//! gets the size of the billboard
 
157
const core::dimension2d<f32>& CBillboardSceneNode::getSize() const
 
158
{
 
159
        return Size;
 
160
}
 
161
 
 
162
 
 
163
//! Writes attributes of the scene node.
 
164
void CBillboardSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
 
165
{
 
166
        IBillboardSceneNode::serializeAttributes(out, options);
 
167
 
 
168
        out->addFloat("Width", Size.Width);
 
169
        out->addFloat("Height", Size.Height);
 
170
        out->addColor ("Shade_Top", vertices[1].Color );
 
171
        out->addColor ("Shade_Down", vertices[0].Color );
 
172
}
 
173
 
 
174
 
 
175
//! Reads attributes of the scene node.
 
176
void CBillboardSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
 
177
{
 
178
        IBillboardSceneNode::deserializeAttributes(in, options);
 
179
 
 
180
        Size.Width = in->getAttributeAsFloat("Width");
 
181
        Size.Height = in->getAttributeAsFloat("Height");
 
182
        vertices[1].Color = in->getAttributeAsColor ( "Shade_Top" );
 
183
        vertices[0].Color = in->getAttributeAsColor ( "Shade_Down" );
 
184
        vertices[2].Color = vertices[1].Color;
 
185
        vertices[3].Color = vertices[0].Color;
 
186
 
 
187
        setSize(Size);
 
188
}
 
189
 
 
190
 
 
191
//! Set the color of all vertices of the billboard
 
192
//! \param overallColor: the color to set
 
193
void CBillboardSceneNode::setColor(const video::SColor & overallColor)
 
194
{
 
195
        for(u32 vertex = 0; vertex < 4; ++vertex)
 
196
                vertices[vertex].Color = overallColor;
 
197
}
 
198
 
 
199
 
 
200
//! Set the color of the top and bottom vertices of the billboard
 
201
//! \param topColor: the color to set the top vertices
 
202
//! \param bottomColor: the color to set the bottom vertices
 
203
void CBillboardSceneNode::setColor(const video::SColor & topColor, const video::SColor & bottomColor)
 
204
{
 
205
        vertices[0].Color = bottomColor;
 
206
        vertices[1].Color = topColor;
 
207
        vertices[2].Color = topColor;
 
208
        vertices[3].Color = bottomColor;
 
209
}
 
210
 
 
211
 
 
212
//! Gets the color of the top and bottom vertices of the billboard
 
213
//! \param[out] topColor: stores the color of the top vertices
 
214
//! \param[out] bottomColor: stores the color of the bottom vertices
 
215
void CBillboardSceneNode::getColor(video::SColor & topColor, video::SColor & bottomColor) const
 
216
{
 
217
        bottomColor = vertices[0].Color;
 
218
        topColor = vertices[1].Color;
 
219
}
 
220
 
 
221
 
 
222
//! Creates a clone of this scene node and its children.
 
223
ISceneNode* CBillboardSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
 
224
{
 
225
        if (!newParent)
 
226
                newParent = Parent;
 
227
        if (!newManager)
 
228
                newManager = SceneManager;
 
229
 
 
230
        CBillboardSceneNode* nb = new CBillboardSceneNode(newParent,
 
231
                newManager, ID, RelativeTranslation, Size);
 
232
 
 
233
        nb->cloneMembers(this, newManager);
 
234
        nb->Material = Material;
 
235
 
 
236
        if ( newParent )
 
237
                nb->drop();
 
238
        return nb;
 
239
}
 
240
 
 
241
 
 
242
} // end namespace scene
 
243
} // end namespace irr
 
244