node picking from a user defined window

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

node picking from a user defined window

Post by Seven »

I have designed a GUI system that allows me to render the level into a gui element. This element does not necessarily render into the center of the screen, but could be a rectangle anywhere on the screen. In the node selection code, I am getting an offset of the ray in the world, and I think it is from the render area not being centered in the screen. (i also had trouble with the FPS camera because it wanted to reset the cursor to the center of the screen, I was able to fix that)

in this code from the collisionmanager, i try to get a ray that goes from the camera, through the screen and picks nodes.
as i said, it doesnt work if the render window is not centered on the screen, but I cannot for the life of me figure out how to fix it.
any help would be appreciated.

to render to any portion of the screen, i set the viewport to the desired window and then render.

Code: Select all

 
    //! Returns a 3d ray which would go through the 2d screen coodinates.
    core::line3d<f32> CSLevel::getRayFromScreenCoordinates(const core::position2d<s32> & pos, ICameraSceneNode* camera)
    {
        core::line3d<f32> ln(0, 0, 0, 0, 0, 0);
 
        if (!getSmgr()) return ln;
 
        if (!camera)
            camera = getSmgr()->getActiveCamera();
 
        if (!camera)
            return ln;
 
        const scene::SViewFrustum* f = camera->getViewFrustum();
 
        core::vector3df farLeftUp = f->getFarLeftUp();
        core::vector3df lefttoright = f->getFarRightUp() - farLeftUp;
        core::vector3df uptodown = f->getFarLeftDown() - farLeftUp;
 
        const core::rect<s32>& viewPort = getDriver()->getViewPort();
        core::dimension2d<u32> screenSize(viewPort.getWidth(), viewPort.getHeight());
 
        f32 dx = pos.X / (f32)screenSize.Width;
        f32 dy = pos.Y / (f32)screenSize.Height;
 
        if (camera->isOrthogonal())
            ln.start = f->cameraPosition + (lefttoright * (dx - 0.5f)) + (uptodown * (dy - 0.5f));
        else
            ln.start = f->cameraPosition;
 
        ln.end = farLeftUp + (lefttoright * dx) + (uptodown * dy);
 
        return ln;
    }
 
any thoughts on how to use this to create a ray that does what i need?
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: node picking from a user defined window

Post by thanhle »

Hi,
If you draw into a sub-window or a gui element. You might want to do the following, without having to change the ray collision code like you did.

Code: Select all

 
//Update size if size has change
device->getVideoDriver()->OnResize(widgetSize);
 
//Update mouse position to new rectangle global position.
e.g. Inside a Qt widget mdi window.
 
      RenderForm *frm = (RenderForm *)mdiArea->activeSubWindow();
     if (frm == 0) return;
           QWidget *renderWidget = frm->renderWidget;
       QPoint p = (((QWidget *)mdiArea))->mapToGlobal(renderForm->pos() + renderWidget->pos());
           core::rect<s32> rec = core::rect<s32>(p.x(), p.y(), p.x() + renderWidget->width(), p.y() + renderWidget->height());
           device->getCursorControl()->setReferenceRect(&rec);
 
   //Set aspect ratio
    cam->setAspectRatio((float)widgetSize.Width / (float)widgetSize.Height);
 
Perform those three steps should readjust the mouse position.

I use Qt now. But the method is the same for any gui including .net.

Regards
thanh
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: node picking from a user defined window

Post by Seven »

will give it a try. thanks for the help!
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: node picking from a user defined window

Post by Seven »

got it. thanks again!
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: node picking from a user defined window

Post by thanhle »

Great work :).

Regards
thanh
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: node picking from a user defined window

Post by Vectrotek »

Nice! Very useful.
Post Reply