1 // Copyright (C) 2002-2010 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 "MyBillboardSceneNode.h"
\r
6 #include "IVideoDriver.h"
\r
7 #include "ISceneManager.h"
\r
8 #include "ICameraSceneNode.h"
\r
16 MyBillboardSceneNode::MyBillboardSceneNode(ISceneNode* parent,
\r
17 ISceneManager* mgr, s32 id,
\r
18 const core::vector3df& position, const core::dimension2d<f32>& size)
\r
19 : IBillboardSceneNode(parent, mgr, id, position)
\r
22 setDebugName("MyBillboardSceneNode");
\r
34 video::SColor colorTop = video::SColor(0xFFFFFFFF);
\r
35 video::SColor colorBottom = video::SColor(0xFFFFFFFF);
\r
37 vertices[0].TCoords.set(1.0f, 1.0f);
\r
38 vertices[0].Color = colorBottom;
\r
40 vertices[1].TCoords.set(1.0f, 0.0f);
\r
41 vertices[1].Color = colorTop;
\r
43 vertices[2].TCoords.set(0.0f, 0.0f);
\r
44 vertices[2].Color = colorTop;
\r
46 vertices[3].TCoords.set(0.0f, 1.0f);
\r
47 vertices[3].Color = colorBottom;
\r
51 //! pre render event
\r
52 void MyBillboardSceneNode::OnRegisterSceneNode()
\r
55 SceneManager->registerNodeForRendering(this);
\r
57 ISceneNode::OnRegisterSceneNode();
\r
62 void MyBillboardSceneNode::render()
\r
64 video::IVideoDriver* driver = SceneManager->getVideoDriver();
\r
65 ICameraSceneNode* camera = SceneManager->getActiveCamera();
\r
67 if (!camera || !driver)
\r
70 // make billboard look to camera
\r
72 core::vector3df pos = getAbsolutePosition();
\r
74 core::vector3df campos = camera->getAbsolutePosition();
\r
75 core::vector3df target = camera->getTarget();
\r
76 core::vector3df up = camera->getUpVector();
\r
77 core::vector3df view = target - campos;
\r
80 core::vector3df horizontal = up.crossProduct(view);
\r
81 if ( horizontal.getLength() == 0 )
\r
83 horizontal.set(up.Y,up.X,up.Z);
\r
85 horizontal.normalize();
\r
86 horizontal *= 0.5f * Size.Width;
\r
88 core::vector3df vertical = horizontal.crossProduct(view);
\r
89 vertical.normalize();
\r
90 vertical *= 0.5f * Size.Height;
\r
94 for (s32 i=0; i<4; ++i)
\r
95 vertices[i].Normal = view;
\r
97 vertices[0].Pos = pos + horizontal + vertical;
\r
98 vertices[1].Pos = pos + horizontal - vertical;
\r
99 vertices[2].Pos = pos - horizontal - vertical;
\r
100 vertices[3].Pos = pos - horizontal + vertical;
\r
104 if ( DebugDataVisible & scene::EDS_BBOX )
\r
106 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
\r
107 video::SMaterial m;
\r
108 m.Lighting = false;
\r
109 driver->setMaterial(m);
\r
110 driver->draw3DBox(BBox, video::SColor(0,208,195,152));
\r
113 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
\r
115 driver->setMaterial(Material);
\r
117 driver->drawIndexedTriangleList(vertices, 4, indices, 2);
\r
121 //! returns the axis aligned bounding box of this node
\r
122 const core::aabbox3d<f32>& MyBillboardSceneNode::getBoundingBox() const
\r
128 //! sets the size of the billboard
\r
129 void MyBillboardSceneNode::setSize(const core::dimension2d<f32>& size)
\r
133 if (Size.Width == 0.0f)
\r
136 if (Size.Height == 0.0f )
\r
137 Size.Height = 1.0f;
\r
139 f32 avg = (size.Width + size.Height)/6;
\r
140 BBox.MinEdge.set(-avg,-avg,-avg);
\r
141 BBox.MaxEdge.set(avg,avg,avg);
\r
145 video::SMaterial& MyBillboardSceneNode::getMaterial(u32 i)
\r
151 //! returns amount of materials used by this scene node.
\r
152 u32 MyBillboardSceneNode::getMaterialCount() const
\r
158 //! gets the size of the billboard
\r
159 const core::dimension2d<f32>& MyBillboardSceneNode::getSize() const
\r
165 //! Set the color of all vertices of the billboard
\r
166 //! \param overallColor: the color to set
\r
167 void MyBillboardSceneNode::setColor(const video::SColor & overallColor)
\r
169 for(u32 vertex = 0; vertex < 4; ++vertex)
\r
170 vertices[vertex].Color = overallColor;
\r
174 //! Set the color of the top and bottom vertices of the billboard
\r
175 //! \param topColor: the color to set the top vertices
\r
176 //! \param bottomColor: the color to set the bottom vertices
\r
177 void MyBillboardSceneNode::setColor(const video::SColor & topColor, const video::SColor & bottomColor)
\r
179 vertices[0].Color = bottomColor;
\r
180 vertices[1].Color = topColor;
\r
181 vertices[2].Color = topColor;
\r
182 vertices[3].Color = bottomColor;
\r
186 //! Gets the color of the top and bottom vertices of the billboard
\r
187 //! \param[out] topColor: stores the color of the top vertices
\r
188 //! \param[out] bottomColor: stores the color of the bottom vertices
\r
189 void MyBillboardSceneNode::getColor(video::SColor & topColor, video::SColor & bottomColor) const
\r
191 bottomColor = vertices[0].Color;
\r
192 topColor = vertices[1].Color;
\r
195 void MyBillboardSceneNode::setTCoords(u32 i, core::vector2d<f32> c)
\r
197 vertices[i].TCoords = c;
\r
200 } // end namespace scene
\r
201 } // end namespace irr
\r