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 "CBillboardSceneNode.h"
\r
6 #include "IVideoDriver.h"
\r
7 #include "ISceneManager.h"
\r
8 #include "ICameraSceneNode.h"
\r
17 CBillboardSceneNode::CBillboardSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id,
\r
18 const core::vector3df& position, const core::dimension2d<f32>& size,
\r
19 video::SColor colorTop, video::SColor colorBottom)
\r
20 : IBillboardSceneNode(parent, mgr, id, position)
\r
21 , Buffer(new SMeshBuffer())
\r
24 setDebugName("CBillboardSceneNode");
\r
29 Buffer->Vertices.set_used(4);
\r
30 Buffer->Indices.set_used(6);
\r
32 Buffer->Indices[0] = 0;
\r
33 Buffer->Indices[1] = 2;
\r
34 Buffer->Indices[2] = 1;
\r
35 Buffer->Indices[3] = 0;
\r
36 Buffer->Indices[4] = 3;
\r
37 Buffer->Indices[5] = 2;
\r
39 Buffer->Vertices[0].TCoords.set(1.0f, 1.0f);
\r
40 Buffer->Vertices[0].Color = colorBottom;
\r
42 Buffer->Vertices[1].TCoords.set(1.0f, 0.0f);
\r
43 Buffer->Vertices[1].Color = colorTop;
\r
45 Buffer->Vertices[2].TCoords.set(0.0f, 0.0f);
\r
46 Buffer->Vertices[2].Color = colorTop;
\r
48 Buffer->Vertices[3].TCoords.set(0.0f, 1.0f);
\r
49 Buffer->Vertices[3].Color = colorBottom;
\r
52 CBillboardSceneNode::~CBillboardSceneNode()
\r
57 //! pre render event
\r
58 void CBillboardSceneNode::OnRegisterSceneNode()
\r
61 SceneManager->registerNodeForRendering(this);
\r
63 ISceneNode::OnRegisterSceneNode();
\r
68 void CBillboardSceneNode::render()
\r
70 video::IVideoDriver* driver = SceneManager->getVideoDriver();
\r
71 ICameraSceneNode* camera = SceneManager->getActiveCamera();
\r
73 if (!camera || !driver)
\r
76 // make billboard look to camera
\r
79 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
\r
80 driver->setMaterial(Buffer->Material);
\r
81 driver->drawMeshBuffer(Buffer);
\r
83 if (DebugDataVisible & scene::EDS_BBOX)
\r
85 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
\r
88 driver->setMaterial(m);
\r
89 driver->draw3DBox(BBoxSafe, video::SColor(0,208,195,152));
\r
93 void CBillboardSceneNode::updateMesh(const irr::scene::ICameraSceneNode* camera)
\r
95 // billboard looks toward camera
\r
96 core::vector3df pos = getAbsolutePosition();
\r
98 core::vector3df campos = camera->getAbsolutePosition();
\r
99 core::vector3df target = camera->getTarget();
\r
100 core::vector3df up = camera->getUpVector();
\r
101 core::vector3df view = target - campos;
\r
104 core::vector3df horizontal = up.crossProduct(view);
\r
105 if ( horizontal.getLength() == 0 )
\r
107 horizontal.set(up.Y,up.X,up.Z);
\r
109 horizontal.normalize();
\r
110 core::vector3df topHorizontal = horizontal * 0.5f * TopEdgeWidth;
\r
111 horizontal *= 0.5f * Size.Width;
\r
114 core::vector3df vertical = horizontal.crossProduct(view);
\r
115 vertical.normalize();
\r
116 vertical *= 0.5f * Size.Height;
\r
120 core::array<video::S3DVertex>& vertices = Buffer->Vertices;
\r
122 for (s32 i=0; i<4; ++i)
\r
123 vertices[i].Normal = view;
\r
131 vertices[0].Pos = pos + horizontal + vertical;
\r
132 vertices[1].Pos = pos + topHorizontal - vertical;
\r
133 vertices[2].Pos = pos - topHorizontal - vertical;
\r
134 vertices[3].Pos = pos - horizontal + vertical;
\r
136 Buffer->setDirty(EBT_VERTEX);
\r
137 Buffer->recalculateBoundingBox();
\r
141 //! returns the axis aligned bounding box of this node
\r
142 const core::aabbox3d<f32>& CBillboardSceneNode::getBoundingBox() const
\r
144 // Really wrong when scaled.
\r
148 const core::aabbox3d<f32>& CBillboardSceneNode::getTransformedBillboardBoundingBox(const irr::scene::ICameraSceneNode* camera)
\r
150 updateMesh(camera);
\r
151 return Buffer->BoundingBox;
\r
154 void CBillboardSceneNode::setSize(const core::dimension2d<f32>& size)
\r
158 if (core::equals(Size.Width, 0.0f))
\r
160 TopEdgeWidth = Size.Width;
\r
162 if (core::equals(Size.Height, 0.0f))
\r
163 Size.Height = 1.0f;
\r
165 const f32 avg = (Size.Width + Size.Height)/6;
\r
166 BBoxSafe.MinEdge.set(-avg,-avg,-avg);
\r
167 BBoxSafe.MaxEdge.set(avg,avg,avg);
\r
171 void CBillboardSceneNode::setSize(f32 height, f32 bottomEdgeWidth, f32 topEdgeWidth)
\r
173 Size.set(bottomEdgeWidth, height);
\r
174 TopEdgeWidth = topEdgeWidth;
\r
176 if (core::equals(Size.Height, 0.0f))
\r
177 Size.Height = 1.0f;
\r
179 if (core::equals(Size.Width, 0.f) && core::equals(TopEdgeWidth, 0.f))
\r
182 TopEdgeWidth = 1.0f;
\r
185 const f32 avg = (core::max_(Size.Width,TopEdgeWidth) + Size.Height)/6;
\r
186 BBoxSafe.MinEdge.set(-avg,-avg,-avg);
\r
187 BBoxSafe.MaxEdge.set(avg,avg,avg);
\r
191 video::SMaterial& CBillboardSceneNode::getMaterial(u32 i)
\r
193 return Buffer->Material;
\r
197 //! returns amount of materials used by this scene node.
\r
198 u32 CBillboardSceneNode::getMaterialCount() const
\r
204 //! gets the size of the billboard
\r
205 const core::dimension2d<f32>& CBillboardSceneNode::getSize() const
\r
211 //! Gets the widths of the top and bottom edges of the billboard.
\r
212 void CBillboardSceneNode::getSize(f32& height, f32& bottomEdgeWidth,
\r
213 f32& topEdgeWidth) const
\r
215 height = Size.Height;
\r
216 bottomEdgeWidth = Size.Width;
\r
217 topEdgeWidth = TopEdgeWidth;
\r
221 //! Set the color of all vertices of the billboard
\r
222 //! \param overallColor: the color to set
\r
223 void CBillboardSceneNode::setColor(const video::SColor& overallColor)
\r
225 for(u32 vertex = 0; vertex < 4; ++vertex)
\r
226 Buffer->Vertices[vertex].Color = overallColor;
\r
230 //! Set the color of the top and bottom vertices of the billboard
\r
231 //! \param topColor: the color to set the top vertices
\r
232 //! \param bottomColor: the color to set the bottom vertices
\r
233 void CBillboardSceneNode::setColor(const video::SColor& topColor,
\r
234 const video::SColor& bottomColor)
\r
236 Buffer->Vertices[0].Color = bottomColor;
\r
237 Buffer->Vertices[1].Color = topColor;
\r
238 Buffer->Vertices[2].Color = topColor;
\r
239 Buffer->Vertices[3].Color = bottomColor;
\r
243 //! Gets the color of the top and bottom vertices of the billboard
\r
244 //! \param[out] topColor: stores the color of the top vertices
\r
245 //! \param[out] bottomColor: stores the color of the bottom vertices
\r
246 void CBillboardSceneNode::getColor(video::SColor& topColor,
\r
247 video::SColor& bottomColor) const
\r
249 bottomColor = Buffer->Vertices[0].Color;
\r
250 topColor = Buffer->Vertices[1].Color;
\r
254 //! Creates a clone of this scene node and its children.
\r
255 ISceneNode* CBillboardSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
\r
258 newParent = Parent;
\r
260 newManager = SceneManager;
\r
262 CBillboardSceneNode* nb = new CBillboardSceneNode(newParent,
\r
263 newManager, ID, RelativeTranslation, Size);
\r
265 nb->cloneMembers(this, newManager);
\r
266 nb->Buffer->Material = Buffer->Material;
\r
268 nb->TopEdgeWidth = this->TopEdgeWidth;
\r
270 video::SColor topColor,bottomColor;
\r
271 getColor(topColor,bottomColor);
\r
272 nb->setColor(topColor,bottomColor);
\r
280 } // end namespace scene
\r
281 } // end namespace irr
\r