Crosshairs flat to the screen

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
isfk
Posts: 24
Joined: Tue Jul 17, 2007 8:12 pm

Crosshairs flat to the screen

Post by isfk »

I created a simple crosshair for my project, but I decided to make it into a scene node. As a scene node I need to add it to the scene manager, but I'm not sure what I should add it as. Now that it's a scene node, how do I keep it flat to the screen?

Before I was simply drawing it manually after the scene manager was done drawing.

Thanks.
psychophoniac
Posts: 101
Joined: Wed Dec 03, 2008 5:33 pm
Location: ger

Post by psychophoniac »

just load an ITexture and then add an gui image
(don't forget the guienv->drawAll() in the mainloop.)

this is how i do it.

Code: Select all

video::ITexture* CrosshairTex = driver->getTexture("../../media/Crosshair.png");
guienv->addImage(CrosshairTex, core::position2d<s32>(driver->getScreenSize().Width / 2 - (CrosshairTex->getSize().Width/2), driver->getScreenSize().Height / 2 - (CrosshairTex->getSize().Height/2)) , true);
i think this is what you mean...(?!)
i love skateboarding!
isfk
Posts: 24
Joined: Tue Jul 17, 2007 8:12 pm

Post by isfk »

Thanks for the reply, but I am rendering it in the render method as an inherited ISceneNode. I have the scene node ready, but I don't know how to add it to the scene manager. I tried adding it as a bill board, but I can't keep it stick to the front of the camera no matter how I move the camera.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Code: Select all

class CCrossHair : public irr::gui::IGUIElement
{
public:
    CCrossHair(irr::gui::IGUIEnvironment *  environment, irr::u32 id = -1) : irr::gui::IGUIElement  (  irr::gui::EGUIET_ELEMENT, environment, environment->getRootGUIElement(), id, irr::core::rect< irr::s32 >(0,0,0,0))
    {
        Texture = 0;
        Color = irr::video::SColor(255,255,255,0);
        Size = 10;
    }

    virtual ~CCrossHair(void)
    {
        if (Texture)
            Texture->drop();
    }

    void draw(void)
    {
        irr::video::IVideoDriver* driver = Environment->getVideoDriver();

        irr::u32 screenW = driver->getScreenSize().Width;
        irr::u32 screenH = driver->getScreenSize().Height;
        irr::u32 cenW = screenW / 2;
        irr::u32 cenH = screenH / 2;

        if (!Texture)
        {
            //Draw crosshair
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW-1,cenH-Size,cenW+1,cenH-4) ); //above
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW+4,cenH-1,cenW+Size,cenH+1) ); //right
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW-1,cenH+4,cenW+1,cenH+Size) ); //down
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW-Size,cenH-1,cenW-4,cenH+1) ); //left
            driver->draw2DRectangle( Color, irr::core::rect<irr::s32>(cenW-1,cenH-1,cenW+1,cenH+1) ); //center of screen
        }
        else
        {
            irr::video::SColor Colors [] = {Color, Color, Color, Color};
            driver->draw2DImage(Texture, irr::core::rect< irr::s32 >(cenW-Size, cenH-Size, cenW+Size, cenH+Size), irr::core::rect< irr::s32 >(0, 0, Texture->getOriginalSize().Width, Texture->getOriginalSize().Height), 0, Colors, true);
        }

    }

    void setTexture(irr::video::ITexture* texture)
    {
        if (Texture)
            Texture->drop();

        Texture = texture;

        if (Texture)
            Texture->grab();
    }

    void setSize(irr::u32 size)
    {
        Size = size;
    }

    void setColor(irr::video::SColor color)
    {
        Color = color;
    }
protected:
    irr::video::ITexture* Texture;
    irr::u32 Size;
    irr::video::SColor Color;
}; 
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.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

Code: Select all

crosshair = smgr->addBillboardSceneNode(camera, irr::core::dimension2df(4, 4), irr::core::vector3df(0.0f, 0.0f, 50.0f));
crosshair->setMaterialType(irr::video::EMT_TRANSPARENT_ADD_COLOR);
crosshair->setMaterialTexture(0, videoDriver->getTexture("media/images/crosshair/cross.bmp"));
crosshair->setMaterialFlag(irr::video::EMF_LIGHTING, false);
crosshair->setMaterialFlag(irr::video::EMF_ZBUFFER, false);
Last edited by randomMesh on Sun Jan 11, 2009 12:40 am, edited 1 time in total.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

isfk wrote:I am rendering it in the render method as an inherited ISceneNode.
So you have your own scene node that you've written? i.e., you wrote a class that inherits from ISceneNode.
I have the scene node ready, but I don't know how to add it to the scene manager.
I'm not sure what you mean. Are you trying to figure out how to add a function to the ISceneManager interface for creating your new scene node (like smgr->addCrosshairSceneNode()), or are you just trying to figure out how to put one of the nodes into the scene so it will render?
I tried adding it as a bill board, but I can't keep it stick to the front of the camera no matter how I move the camera.
Now i'm even more confused. Are you just saying that you tried to use a IBillboardSceneNode but didn't like it because it wouldn't automatically stay in the right place?

Travis
isfk
Posts: 24
Joined: Tue Jul 17, 2007 8:12 pm

Post by isfk »

randomMesh wrote:

Code: Select all

crosshair = smgr->addBillboardSceneNode(camera, irr::core::dimension2df(4, 4), irr::core::vector3df(0.0f, 0.0f, 50.0f));
crosshair->setMaterialType(irr::video::EMT_TRANSPARENT_ADD_COLOR);
crosshair->setMaterialTexture(0, videoDriver()->getTexture("media/images/crosshair/cross.bmp"));
crosshair->setMaterialFlag(irr::video::EMF_LIGHTING, false);
crosshair->setMaterialFlag(irr::video::EMF_ZBUFFER, false);
That is what I was after, I needed to use the camera as the node to attach it to. Thanks for your help.
isfk
Posts: 24
Joined: Tue Jul 17, 2007 8:12 pm

Post by isfk »

No, I'm sorry, that didn't work. The billboard does not stay attached to the screen. I guess I will have to just make it a GUI control. But I wanted to make it a scene node, because I feel you have more flexiblity when adding animations, etc.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

try cam->bindTargetAndRotation(true);

Or use the collision manager to get the 3d coordinates of the centre of the screen, like in the collision example.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Post Reply