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
5
#include "CBillboardSceneNode.h"
6
#include "IVideoDriver.h"
7
#include "ISceneManager.h"
8
#include "ICameraSceneNode.h"
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)
23
setDebugName("CBillboardSceneNode");
35
vertices[0].TCoords.set(1.0f, 1.0f);
36
vertices[0].Color = colorBottom;
38
vertices[1].TCoords.set(1.0f, 0.0f);
39
vertices[1].Color = colorTop;
41
vertices[2].TCoords.set(0.0f, 0.0f);
42
vertices[2].Color = colorTop;
44
vertices[3].TCoords.set(0.0f, 1.0f);
45
vertices[3].Color = colorBottom;
50
void CBillboardSceneNode::OnRegisterSceneNode()
53
SceneManager->registerNodeForRendering(this);
55
ISceneNode::OnRegisterSceneNode();
60
void CBillboardSceneNode::render()
62
video::IVideoDriver* driver = SceneManager->getVideoDriver();
63
ICameraSceneNode* camera = SceneManager->getActiveCamera();
65
if (!camera || !driver)
68
// make billboard look to camera
70
core::vector3df pos = getAbsolutePosition();
72
core::vector3df campos = camera->getAbsolutePosition();
73
core::vector3df target = camera->getTarget();
74
core::vector3df up = camera->getUpVector();
75
core::vector3df view = target - campos;
78
core::vector3df horizontal = up.crossProduct(view);
79
if ( horizontal.getLength() == 0 )
81
horizontal.set(up.Y,up.X,up.Z);
83
horizontal.normalize();
84
horizontal *= 0.5f * Size.Width;
86
core::vector3df vertical = horizontal.crossProduct(view);
88
vertical *= 0.5f * Size.Height;
92
for (s32 i=0; i<4; ++i)
93
vertices[i].Normal = view;
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;
102
if ( DebugDataVisible & scene::EDS_BBOX )
104
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
107
driver->setMaterial(m);
108
driver->draw3DBox(BBox, video::SColor(0,208,195,152));
111
driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
113
driver->setMaterial(Material);
115
driver->drawIndexedTriangleList(vertices, 4, indices, 2);
119
//! returns the axis aligned bounding box of this node
120
const core::aabbox3d<f32>& CBillboardSceneNode::getBoundingBox() const
126
//! sets the size of the billboard
127
void CBillboardSceneNode::setSize(const core::dimension2d<f32>& size)
131
if (Size.Width == 0.0f)
134
if (Size.Height == 0.0f )
137
f32 avg = (size.Width + size.Height)/6;
138
BBox.MinEdge.set(-avg,-avg,-avg);
139
BBox.MaxEdge.set(avg,avg,avg);
143
video::SMaterial& CBillboardSceneNode::getMaterial(u32 i)
149
//! returns amount of materials used by this scene node.
150
u32 CBillboardSceneNode::getMaterialCount() const
156
//! gets the size of the billboard
157
const core::dimension2d<f32>& CBillboardSceneNode::getSize() const
163
//! Writes attributes of the scene node.
164
void CBillboardSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
166
IBillboardSceneNode::serializeAttributes(out, options);
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 );
175
//! Reads attributes of the scene node.
176
void CBillboardSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
178
IBillboardSceneNode::deserializeAttributes(in, options);
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;
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)
195
for(u32 vertex = 0; vertex < 4; ++vertex)
196
vertices[vertex].Color = overallColor;
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)
205
vertices[0].Color = bottomColor;
206
vertices[1].Color = topColor;
207
vertices[2].Color = topColor;
208
vertices[3].Color = bottomColor;
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
217
bottomColor = vertices[0].Color;
218
topColor = vertices[1].Color;
222
//! Creates a clone of this scene node and its children.
223
ISceneNode* CBillboardSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
228
newManager = SceneManager;
230
CBillboardSceneNode* nb = new CBillboardSceneNode(newParent,
231
newManager, ID, RelativeTranslation, Size);
233
nb->cloneMembers(this, newManager);
234
nb->Material = Material;
242
} // end namespace scene
243
} // end namespace irr