BeamSceneNode

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

BeamSceneNode

Post by sudi »

I thought this migt be a better place for this code. So u guys won't loose it

Header:

Code: Select all

#ifndef CBEAMSCENENODE_H
#define CBEAMSCENENODE_H
#include <irrlicht.h>
namespace irr
{
    namespace scene
    {
        class CBeamNode : public ISceneNode
        {
        public:
        private:
            // The beam material.
            video::SMaterial material;
            video::SMaterial material2;

            // Bounding Box
            core::aabbox3d<f32> Box;

            core::vector3df m_start;
            core::vector3df m_end;
            f32 m_thickness;

        public:

            CBeamNode( scene::ISceneNode* parent, scene::ISceneManager *mgr, s32 id, char* szBeam , char* szBeamFront );

            ~CBeamNode(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, f32 thickness);
        };
    }
}
#endif
Source:

Code: Select all

#include "CBeamSceneNode.h"
namespace irr
{
    namespace scene
    {
        CBeamNode::CBeamNode( ISceneNode* parent, ISceneManager *mgr, s32 id, char* szBeam , char* szBeamFront ) : ISceneNode( parent, mgr, id )
        {
            // Setup the beam material
            material.Wireframe = false;
            material.Lighting = false;
            material.ZWriteEnable = false;

            material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
            material.setTexture(0, mgr->getVideoDriver( )->getTexture( szBeam ));

            material2.Wireframe = false;
            material2.Lighting = false;
            material2.ZWriteEnable = false;

            material2.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
            material2.setTexture(0, mgr->getVideoDriver( )->getTexture( szBeamFront ));
            material2.BackfaceCulling = false;

            m_thickness = 1.0f;

           AutomaticCullingState = EAC_FRUSTUM_SPHERE;
        }

        CBeamNode::~CBeamNode(void)
        {

        }

        void CBeamNode::OnRegisterSceneNode(void)
        {
            if (IsVisible)
            {
                SceneManager->registerNodeForRendering(this, irr::scene::ESNRP_TRANSPARENT);
                ISceneNode::OnRegisterSceneNode();
            }
        }

        void CBeamNode::OnAnimate(u32 timeMs)
        {
            ISceneNode::OnAnimate(timeMs);
        }

        void CBeamNode::setLine(core::vector3df start, core::vector3df end, f32 thickness)
        {
            setPosition(start);
            m_start = core::vector3df(0,0,0);
            m_end = end;
            m_thickness = thickness;
        }

        void CBeamNode::render(void)
        {
            video::IVideoDriver* driver = SceneManager->getVideoDriver();
            driver->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);

            core::vector3df direction = m_end-m_start;
            direction.normalize();
            core::vector3df savesatrt = m_start;
            AbsoluteTransformation.transformVect(savesatrt);
            core::vector3df lookdir = savesatrt-SceneManager->getActiveCamera()->getAbsolutePosition();
            if(lookdir.getLength() < 20)
                return;
            lookdir.normalize();

            core::vector3df view(SceneManager->getActiveCamera()->getTarget() - SceneManager->getActiveCamera()->getAbsolutePosition());
            view.normalize();

            f32 angle = lookdir.dotProduct(direction)/*/(lookdir.getLength()*direction.getLength())*/;

            if(angle < 0.9999f && angle > -0.9999f)
            {
                core::vector3df updown = direction.crossProduct(lookdir);
                updown.normalize();
                core::vector3df normal = direction.crossProduct(updown);
                video::S3DVertex vertices[4];
                u16 indices[] = {0,1,2,1,3,2};

                vertices[0] = video::S3DVertex(m_end-updown*m_thickness*0.5f, normal, video::SColor(255,255,255,255), core::vector2d<f32>(0,0));
                vertices[1] = video::S3DVertex(m_end+updown*m_thickness*0.5f, normal, video::SColor(255,255,255,255), core::vector2d<f32>(0,1));
                vertices[2] = video::S3DVertex(m_start-updown*m_thickness*0.5f, normal, video::SColor(255,255,255,255), core::vector2d<f32>(1,0));
                vertices[3] = video::S3DVertex(m_start+updown*m_thickness*0.5f, normal, video::SColor(255,255,255,255), core::vector2d<f32>(1,1));

                driver->setMaterial(material);
                driver->drawIndexedTriangleList(&vertices[0], 4, &indices[0], 2);
                //driver->draw3DLine(m_start, m_end, video::SColor(255,255,0,0));
            }
            else
            {
                //printf("daw dot\n");

                core::vector3df horizontal = SceneManager->getActiveCamera()->getUpVector().crossProduct(view);
                horizontal.normalize();
                horizontal *= 0.5f * m_thickness;

                core::vector3df vertical = horizontal.crossProduct(view);
                vertical.normalize();
                vertical *= 0.5f * m_thickness;
                view *= -1.0f;

                video::S3DVertex vertices[4];
                u16 indices[] = {0,1,2,1,3,2};

                //decide to use end or start
                core::vector3df point;

				point = m_end;

                vertices[0] = video::S3DVertex(point - horizontal - vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(0,0));
                vertices[1] = video::S3DVertex(point + horizontal - vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(0,1));
                vertices[2] = video::S3DVertex(point - horizontal + vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(1,0));
                vertices[3] = video::S3DVertex(point + horizontal + vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(1,1));

                driver->setMaterial(material2);
                driver->drawIndexedTriangleList(&vertices[0], 4, &indices[0], 2);

                point = m_start;

                vertices[0] = video::S3DVertex(point - horizontal - vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(0,0));
                vertices[1] = video::S3DVertex(point + horizontal - vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(0,1));
                vertices[2] = video::S3DVertex(point - horizontal + vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(1,0));
                vertices[3] = video::S3DVertex(point + horizontal + vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(1,1));

                driver->setMaterial(material2);
                driver->drawIndexedTriangleList(&vertices[0], 4, &indices[0], 2);
            }
        }

        const core::aabbox3d<f32>& CBeamNode::getBoundingBox() const
        {
            return Box;
        }

        u32 CBeamNode::getMaterialCount() const
        {
            return 2;
        }

        video::SMaterial& CBeamNode::getMaterial(u32 i)
        {
            if(i==0)
                return material;
            else if(i==1)
                return material2;
        }
    }
}
Usage:

Code: Select all

scene::CBeamSceneNode* beam = new scene::CBeamNode(parent, smgr, -1, "The texture the beam should have when looking at it from the side , "The texture when looking at the cap" );

//now set the beam
beam->setLine(core::vector3df(0,0,0), core::vector3df(10,10,10), 5.0f);

beam->drop();
8) 8) 8) 8) 8) 8) :lol: :P :oops: :P :lol:

Image
Last edited by sudi on Sun Sep 07, 2008 4:38 pm, edited 9 times in total.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
pera
Posts: 460
Joined: Wed May 14, 2008 1:05 pm
Location: Novi Sad, Serbia
Contact:

Post by pera »

Picture maybe?
trivtn
Posts: 132
Joined: Tue Jan 17, 2006 12:30 pm
Location: Viet Nam
Contact:

Post by trivtn »

Thanks!
There's something is fantastic, there's nothing is absolute.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Hey is this code from Blaze: The Proving Ground, or did you write it yourself?
If you wrote it, can we have it for IrrExt please? Same goes for CBoltSceneNode :)
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Both SceneNodes are written entirely by me. If u want them please give like 3 days to clean up the stuff. i was going to to that anyway but right now i don't have so much time right now. Oh btw i'm really proud that u want my scenenodes. :D
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
D-Eagle
Posts: 25
Joined: Fri Jan 25, 2008 11:03 pm

Post by D-Eagle »

Image
The transparency seems to work correctly only with the skybox.. How can i fix that?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Looks like the node renders in the solid pass.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

well that is a problem with ur texture and the transparent type of the material. but actually i only encounter this problem when using opengl and this problem also exists wit normal billboards and particles. Its a problem with the Transparent_add_color material. simply change it to a Transparent_alpha_channel (ofcourse only when ur texture has a alpha channel otherwise u r lost....
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Oh, use Material.ZWriteEnable=false. Also, the material is already TRANSPARENT_ALPHA_CHANNEL...
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

hybrid wrote:Oh, use Material.ZWriteEnable=false. Also, the material is already TRANSPARENT_ALPHA_CHANNEL...
ha... i already changed that in the version i posted...
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
D-Eagle
Posts: 25
Joined: Fri Jan 25, 2008 11:03 pm

Post by D-Eagle »

The transparency looks fine when drawing stuff like this:

Code: Select all

m_Driver->beginScene(true,true,SColor(255,60,60,50));
{
	beam->setVisible(false);
	m_Smgr->drawAll();
	beam->setVisible(true);
	beam->render();
} 
m_Driver->endScene();
But i'd really like to make it work with just drawAll..
I tried Material.ZWriteEnable=false but it didn't help.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

What kind of image are u using? bmp? jpg? tga? png?
bmp and jpg don't have a alpha channel so these textures won't work.
As u see in my screenshot it works perfectly for me. i really dunno what ur doing wrong besides textures maybe.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
D-Eagle
Posts: 25
Joined: Fri Jan 25, 2008 11:03 pm

Post by D-Eagle »

I'm using a png texture.
Image
And it works perfectly when doing the drawing like i did in the last post.
But it just doesn't work with drawAll();
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Ok, my first analysis was correct. The node is rendered in the SOLID pass, because the materials are not recognized. Irrlicht 1.4 has the getMaterialCount const'ified, so the present version of the node has virtually no material :wink:
Should work once you set ZBufferWrite to disable and change the method signature.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Nope Hybrid....i already fixed though...the error was that the node was registering in OnAnimate and in OnRegisterSceneNode.....now it works.

PS: also fixed the const thingy
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply