Code: Select all
/*
Copyright (C) 2010 Daniel Sudmann
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Daniel Sudmann suddani@googlemail.com
*/
#ifndef CBOLTSCENENODE_H
#define CBOLTSCENENODE_H
#include <irrlicht.h>
namespace irr
{
namespace scene
{
class CBoltSceneNode : public ISceneNode
{
public:
CBoltSceneNode( scene::ISceneNode* parent, scene::ISceneManager *mgr, s32 id);
virtual ~CBoltSceneNode(void);
virtual void OnRegisterSceneNode(void);
virtual void OnAnimate(u32 timeMs);
virtual void render(void);
virtual const core::aabbox3d<f32>& getBoundingBox() const;
virtual u32 getMaterialCount() const;
virtual video::SMaterial& getMaterial(u32 i);
void setLine(core::vector3df start, core::vector3df end, u32 updateTime = 300, u32 height = 10, u32 parts = 10, u32 bolts = 1, bool steddyend = true, video::SColor color = video::SColor(255,255,0,0));
protected:
struct Bolt
{
u32 m_height;
u32 m_updateTime;
bool m_end;
Bolt(u32 height, u32 updateTime, bool end)
{
m_height = height;
m_updateTime = updateTime;
time = 0;
startTime = 0;
m_end = end;
}
irr::core::vector3df normal;
core::array<f32> delta;
core::array<irr::core::vector3df> points;
u32 time;
u32 startTime;
void Update(u32 timeMs)
{
time = timeMs;
if (startTime == 0)
startTime = timeMs;
}
void draw(video::IVideoDriver* driver, video::SColor color)
{
u32 h = m_height*2;
for (u32 i=0;i<points.size()-1;i++)
{
driver->draw3DLine(points[i]+normal*delta[i], points[i+1]+normal*delta[i+1], color);
if (time-startTime >= m_updateTime)
{
if (i == 0)
{
delta[i] = 0.0f;
}
else
{
delta[i] = rand()%h;
delta[i] -= (f32)m_height;
}
}
}
if (!m_end && time-startTime >= m_updateTime)
{
delta[delta.size()-1] = rand()%h;
delta[delta.size()-1] -= (f32)m_height;
}
if (time-startTime >= m_updateTime)
startTime = 0;
}
};
// The beam material.
video::SMaterial material;
// The bolts
core::array<Bolt> boltarray;
// Bounding Box
core::aabbox3d<f32> Box;
core::vector3df m_start;
core::vector3df m_end;
u32 m_parts;
private:
};
}
}
#endif // CBOLTSCENENODE_H
Code: Select all
/*
Copyright (C) 2010 Daniel Sudmann
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Daniel Sudmann suddani@googlemail.com
*/
#include "CBoltSceneNode.h"
namespace irr
{
namespace scene
{
CBoltSceneNode::CBoltSceneNode( scene::ISceneNode* parent, scene::ISceneManager *mgr, s32 id) : ISceneNode( parent, mgr, id )
{
//ctor
material.Wireframe = false;
material.Lighting = false;
AutomaticCullingState = EAC_FRUSTUM_SPHERE;
}
CBoltSceneNode::~CBoltSceneNode()
{
//dtor
}
void CBoltSceneNode::OnRegisterSceneNode(void)
{
if ( IsVisible )
{
SceneManager->registerNodeForRendering( this );
}
}
void CBoltSceneNode::OnAnimate(u32 timeMs)
{
if ( IsVisible )
{
for (u32 i=0;i<boltarray.size();i++)
{
boltarray[i].Update(timeMs);
}
}
ISceneNode::OnAnimate(timeMs);
}
void CBoltSceneNode::setLine(core::vector3df start, core::vector3df end, u32 updateTime, u32 height, u32 parts, u32 bolts, bool steddyend, video::SColor color)
{
setPosition(start);
srand(parts);
boltarray.clear();
m_start = core::vector3df(0,0,0);
m_end = end-start;
m_parts = parts;
material.DiffuseColor = color;
core::vector3df dir = end-start;
dir/=(f32)parts;
for (u32 i=0;i<bolts;i++)
{
//printf("add Bolt\n");
boltarray.push_back(Bolt(height, updateTime, steddyend));
for (u32 a=0;a<parts;a++)
{
//printf("add Part[%i] ", a);
boltarray[i].points.push_back(m_start+dir*a);
if (a == parts-1 || a == 0)
boltarray[i].delta.push_back(0.0f);
else
{
u32 h = height*2;
f32 d = (rand()%h);
d-=(f32)height;
//printf("Delta: %f", d);
boltarray[i].delta.push_back(d);
}
//printf("\n");
}
u32 vec = rand()%2;
//printf("vec: %i\n", vec);
u32 cord = rand()%10+1;
if (dir.X != 0)
{
if (vec == 0)
boltarray[i].normal = core::vector3df(-dir.Y/dir.X*cord,cord,0);
else
boltarray[i].normal = core::vector3df(-dir.Z/dir.X*cord,0,cord);
}
else if (dir.Y != 0)
{
if (vec == 0)
boltarray[i].normal = core::vector3df(cord,-dir.X/dir.Y*cord,0);
else
boltarray[i].normal = core::vector3df(0,-dir.Z/dir.Y*cord,cord);
}
else if (dir.Z != 0)
{
if (vec == 0)
boltarray[i].normal = core::vector3df(cord,0,-dir.X/dir.Z*cord);
else
boltarray[i].normal = core::vector3df(0,cord,-dir.Y/dir.Z*cord);
}
boltarray[i].normal.normalize();
}
}
void CBoltSceneNode::render(void)
{
video::IVideoDriver* driver = SceneManager->getVideoDriver();
driver->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);
driver->setMaterial(material);
for (u32 i=0;i<boltarray.size();i++)
{
boltarray[i].draw(driver, material.DiffuseColor);
}
}
const core::aabbox3d<f32>& CBoltSceneNode::getBoundingBox() const
{
return Box;
}
u32 CBoltSceneNode::getMaterialCount() const
{
return 1;
}
video::SMaterial& CBoltSceneNode::getMaterial(u32 i)
{
return material;
}
}
}
Code: Select all
irr::scene::CBoltSceneNode* beam = new irr::scene::CBoltSceneNode(smgr->getRootSceneNode(), smgr, -1);
beam->setLine(irr::core::vector3df(0,0,0), irr::core::vector3df(100,100,100), 50, 10, 10, 3, false, irr::video::SColor(255,255,0,0));
beam->drop();

PS: btw looks way better/cooler in action






