2008-05-22 04:51:37 -07:00
|
|
|
// Copyright (C) 2002-2008 Nikolaus Gebhardt
|
2007-05-20 11:03:49 -07:00
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CBillboardSceneNode.h"
|
|
|
|
#include "IVideoDriver.h"
|
|
|
|
#include "ISceneManager.h"
|
|
|
|
#include "ICameraSceneNode.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
//! constructor
|
|
|
|
CBillboardSceneNode::CBillboardSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id,
|
2008-02-12 08:01:26 -08:00
|
|
|
const core::vector3df& position, const core::dimension2d<f32>& size,
|
|
|
|
video::SColor shade_top, video::SColor shade_down)
|
|
|
|
: ISceneNode(parent, mgr, id, position), IBillboardSceneNode(parent, mgr, id, position)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
setDebugName("CBillboardSceneNode");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
setSize(size);
|
|
|
|
|
|
|
|
indices[0] = 0;
|
|
|
|
indices[1] = 2;
|
|
|
|
indices[2] = 1;
|
|
|
|
indices[3] = 0;
|
|
|
|
indices[4] = 3;
|
|
|
|
indices[5] = 2;
|
|
|
|
|
|
|
|
vertices[0].TCoords.set(1.0f, 1.0f);
|
|
|
|
vertices[0].Color = shade_down;
|
|
|
|
|
|
|
|
vertices[1].TCoords.set(1.0f, 0.0f);
|
|
|
|
vertices[1].Color = shade_top;
|
|
|
|
|
|
|
|
vertices[2].TCoords.set(0.0f, 0.0f);
|
|
|
|
vertices[2].Color = shade_top;
|
|
|
|
|
|
|
|
vertices[3].TCoords.set(0.0f, 1.0f);
|
|
|
|
vertices[3].Color = shade_down;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! pre render event
|
|
|
|
void CBillboardSceneNode::OnRegisterSceneNode()
|
|
|
|
{
|
|
|
|
if (IsVisible)
|
|
|
|
SceneManager->registerNodeForRendering(this);
|
2007-11-13 06:36:17 -08:00
|
|
|
|
|
|
|
ISceneNode::OnRegisterSceneNode();
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! render
|
|
|
|
void CBillboardSceneNode::render()
|
|
|
|
{
|
|
|
|
video::IVideoDriver* driver = SceneManager->getVideoDriver();
|
|
|
|
ICameraSceneNode* camera = SceneManager->getActiveCamera();
|
|
|
|
|
|
|
|
if (!camera || !driver)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// make billboard look to camera
|
|
|
|
|
|
|
|
core::vector3df pos = getAbsolutePosition();
|
|
|
|
|
|
|
|
core::vector3df campos = camera->getAbsolutePosition();
|
|
|
|
core::vector3df target = camera->getTarget();
|
|
|
|
core::vector3df up = camera->getUpVector();
|
|
|
|
core::vector3df view = target - campos;
|
|
|
|
view.normalize();
|
|
|
|
|
|
|
|
core::vector3df horizontal = up.crossProduct(view);
|
|
|
|
if ( horizontal.getLength() == 0 )
|
|
|
|
{
|
|
|
|
horizontal.set(up.Y,up.X,up.Z);
|
|
|
|
}
|
|
|
|
horizontal.normalize();
|
|
|
|
horizontal *= 0.5f * Size.Width;
|
|
|
|
|
|
|
|
core::vector3df vertical = horizontal.crossProduct(view);
|
|
|
|
vertical.normalize();
|
|
|
|
vertical *= 0.5f * Size.Height;
|
|
|
|
|
|
|
|
view *= -1.0f;
|
|
|
|
|
|
|
|
for (s32 i=0; i<4; ++i)
|
|
|
|
vertices[i].Normal = view;
|
|
|
|
|
|
|
|
vertices[0].Pos = pos + horizontal + vertical;
|
|
|
|
vertices[1].Pos = pos + horizontal - vertical;
|
|
|
|
vertices[2].Pos = pos - horizontal - vertical;
|
|
|
|
vertices[3].Pos = pos - horizontal + vertical;
|
|
|
|
|
|
|
|
// draw
|
|
|
|
|
|
|
|
if ( DebugDataVisible & scene::EDS_BBOX )
|
|
|
|
{
|
|
|
|
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
|
|
|
|
video::SMaterial m;
|
|
|
|
m.Lighting = false;
|
|
|
|
driver->setMaterial(m);
|
|
|
|
driver->draw3DBox(BBox, video::SColor(0,208,195,152));
|
|
|
|
}
|
|
|
|
|
2007-12-02 15:57:20 -08:00
|
|
|
driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
driver->setMaterial(Material);
|
|
|
|
|
|
|
|
driver->drawIndexedTriangleList(vertices, 4, indices, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! returns the axis aligned bounding box of this node
|
|
|
|
const core::aabbox3d<f32>& CBillboardSceneNode::getBoundingBox() const
|
|
|
|
{
|
|
|
|
return BBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! sets the size of the billboard
|
|
|
|
void CBillboardSceneNode::setSize(const core::dimension2d<f32>& size)
|
|
|
|
{
|
|
|
|
Size = size;
|
|
|
|
|
|
|
|
if (Size.Width == 0.0f)
|
|
|
|
Size.Width = 1.0f;
|
|
|
|
|
|
|
|
if (Size.Height == 0.0f )
|
|
|
|
Size.Height = 1.0f;
|
|
|
|
|
|
|
|
f32 avg = (size.Width + size.Height)/6;
|
|
|
|
BBox.MinEdge.set(-avg,-avg,-avg);
|
|
|
|
BBox.MaxEdge.set(avg,avg,avg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
video::SMaterial& CBillboardSceneNode::getMaterial(u32 i)
|
|
|
|
{
|
|
|
|
return Material;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! returns amount of materials used by this scene node.
|
2007-09-16 16:41:55 -07:00
|
|
|
u32 CBillboardSceneNode::getMaterialCount() const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! gets the size of the billboard
|
2007-09-16 16:41:55 -07:00
|
|
|
const core::dimension2d<f32>& CBillboardSceneNode::getSize() const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Writes attributes of the scene node.
|
2007-09-14 15:25:59 -07:00
|
|
|
void CBillboardSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
IBillboardSceneNode::serializeAttributes(out, options);
|
|
|
|
|
|
|
|
out->addFloat("Width", Size.Width);
|
|
|
|
out->addFloat("Height", Size.Height);
|
|
|
|
out->addColor ("Shade_Top", vertices[1].Color );
|
|
|
|
out->addColor ("Shade_Down", vertices[0].Color );
|
|
|
|
}
|
|
|
|
|
2007-09-16 16:41:55 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Reads attributes of the scene node.
|
|
|
|
void CBillboardSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
|
|
|
|
{
|
|
|
|
IBillboardSceneNode::deserializeAttributes(in, options);
|
|
|
|
|
|
|
|
Size.Width = in->getAttributeAsFloat("Width");
|
|
|
|
Size.Height = in->getAttributeAsFloat("Height");
|
|
|
|
vertices[1].Color = in->getAttributeAsColor ( "Shade_Top" );
|
|
|
|
vertices[0].Color = in->getAttributeAsColor ( "Shade_Down" );
|
|
|
|
|
|
|
|
setSize(Size);
|
|
|
|
}
|
|
|
|
|
2007-09-16 16:41:55 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Set the color of all vertices of the billboard
|
|
|
|
//! \param overallColor: the color to set
|
|
|
|
void CBillboardSceneNode::setColor(const video::SColor & overallColor)
|
|
|
|
{
|
|
|
|
for(u32 vertex = 0; vertex < 4; ++vertex)
|
2008-03-30 16:19:12 -07:00
|
|
|
vertices[vertex].Color = overallColor;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
2007-09-16 16:41:55 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Set the color of the top and bottom vertices of the billboard
|
|
|
|
//! \param topColor: the color to set the top vertices
|
|
|
|
//! \param bottomColor: the color to set the bottom vertices
|
|
|
|
void CBillboardSceneNode::setColor(const video::SColor & topColor, const video::SColor & bottomColor)
|
|
|
|
{
|
|
|
|
vertices[0].Color = bottomColor;
|
|
|
|
vertices[1].Color = topColor;
|
|
|
|
vertices[2].Color = topColor;
|
|
|
|
vertices[3].Color = bottomColor;
|
|
|
|
}
|
|
|
|
|
2007-09-16 16:41:55 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Gets the color of the top and bottom vertices of the billboard
|
|
|
|
//! \param[out] topColor: stores the color of the top vertices
|
|
|
|
//! \param[out] bottomColor: stores the color of the bottom vertices
|
2007-09-16 16:41:55 -07:00
|
|
|
void CBillboardSceneNode::getColor(video::SColor & topColor, video::SColor & bottomColor) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
bottomColor = vertices[0].Color;
|
|
|
|
topColor = vertices[1].Color;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Creates a clone of this scene node and its children.
|
|
|
|
ISceneNode* CBillboardSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
|
|
|
|
{
|
2007-09-16 16:41:55 -07:00
|
|
|
if (!newParent)
|
|
|
|
newParent = Parent;
|
|
|
|
if (!newManager)
|
|
|
|
newManager = SceneManager;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
CBillboardSceneNode* nb = new CBillboardSceneNode(newParent,
|
|
|
|
newManager, ID, RelativeTranslation, Size);
|
|
|
|
|
|
|
|
nb->cloneMembers(this, newManager);
|
|
|
|
nb->Material = Material;
|
|
|
|
|
|
|
|
nb->drop();
|
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|
|
|
|
|