I am looking to scale a node so that it will fill a certain position and size on the screen.
assume a camera with position 0,0,-100
how would I position and scale a cubescenenode so that it takes up a section of the screen in the rectangle 10,30,200,300?
scale node to correct size
Re: scale node to correct size
This should cover the math you need: http://en.wikipedia.org/wiki/Intercept_theorem
It's probably easiest to let Irrlicht give you spatial rays for your rectangle's screen-coordinates (there's a function for that somewhere) and do the math with those. The viewing field and angle don't matter then.
If you can calculate the height and width you need at a given distance, you just have to divide those by the node's bounding-box extents and use the result as your scaling-factor for the node.
Unless I'm wrong, that is
It's probably easiest to let Irrlicht give you spatial rays for your rectangle's screen-coordinates (there's a function for that somewhere) and do the math with those. The viewing field and angle don't matter then.
If you can calculate the height and width you need at a given distance, you just have to divide those by the node's bounding-box extents and use the result as your scaling-factor for the node.
Unless I'm wrong, that is
Re: scale node to correct size
researching now. thanks for the push
Re: scale node to correct size
I could use some more help 
I am trying to have a guielement that is at a particular screen point, and then move the scenenode so that it takes up exactly the same screen space as the guielement.
I know that i can render to texture and all that, but I have something unique in mind and need to do this.
I am using an ortho camera that never move, always at 0,0,-10
and always looking at 0,0,0
my code so far
but it doesnt work.
anyone able to help me out?
I am trying to have a guielement that is at a particular screen point, and then move the scenenode so that it takes up exactly the same screen space as the guielement.
I know that i can render to texture and all that, but I have something unique in mind and need to do this.
I am using an ortho camera that never move, always at 0,0,-10
and always looking at 0,0,0
my code so far
Code: Select all
// get the position of the screen element
vector2d<s32> upperLeftCorner = getAbsolutePosition().UpperLeftCorner;
vector2d<s32> lowerRightCorner = getAbsolutePosition().LowerRightCorner;
// get a pointer to the smgr
ISceneManager* smgr = getDesktop()->m_CSSkin->m_Smgr;
// create two rays that go from the camera, into world space,
// one at the topleft corner and one at the bottom right corner of the gui element
line3d<f32> upperLeftRay = getRayFromScreenCoordinates(smgr, upperLeftCorner);
line3d<f32> lowerRightRay = getRayFromScreenCoordinates(smgr, lowerRightCorner);
// create a plane that exists on the Z at 10 units
plane3df const planeXZ(vector3df(0,0,10), vector3df(0.0f, 0.0f, 1.0f));
vector3df intersectWithPlane;
// find where the upperleft ray hits the plane
if (planeXZ.getIntersectionWithLine(upperLeftRay.start, upperLeftRay.getVector(), intersectWithPlane))
upperLeftRay.end = intersectWithPlane;
// find where the lowerright ray hits the plane
if (planeXZ.getIntersectionWithLine(lowerRightRay.start, lowerRightRay.getVector(), intersectWithPlane))
lowerRightRay.end = intersectWithPlane;
// the node position will be where the upperleft ray hits the plane
// and at 10 units deep
vector3df pos(upperLeftRay.end.X, upperLeftRay.end.Y, 10);
// scale the node between the two rays
vector3df scale(lowerRightRay.end.X - upperLeftRay.end.X, lowerRightRay.end.Y - upperLeftRay.end.Y, 10);
// set the node position and scale
m_Node->m_Node->setPosition(pos);
m_Node->m_Node->setScale(scale);
anyone able to help me out?
Re: scale node to correct size
Well, what happens with that? The only things maybe wrong are that your scaling assumes the model is originally 1 unit long in X and Y, always orientated towards the camera, and that the origin is at the upper-left. Do all these assumptions hold?
edit: Also your Y scale seems inverted.
edit: Also your Y scale seems inverted.
Re: scale node to correct size
Yes those assumptions are true. You are right about the y inversion also so I will fix that. I have drawn it out on paper and it looks right. Will have to print out some variables to see what happens though because right now no node visible
Re: scale node to correct size
got it. thanks for the help.
posting code here so I don't lose it.
posting code here so I don't lose it.
Code: Select all
getDesktop()->m_CSSkin->m_Depth -= 0.1;
if (!m_Node) m_Node = getDesktop()->m_CSSkin->getNode();
// get the position of the screen element
vector2d<s32> upperLeftCorner = m_ClientRect.UpperLeftCorner;
vector2d<s32> lowerRightCorner = m_ClientRect.LowerRightCorner;
// get a pointer to the smgr
ISceneManager* smgr = getDesktop()->m_CSSkin->m_Smgr;
// create two rays that go from the camera, into world space,
// one at the topleft corner and one at the bottom right corner of the gui element
line3d<f32> upperLeftRay = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(upperLeftCorner);
line3d<f32> lowerRightRay = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(lowerRightCorner);
// create a plane that exists on the Z at 10 units
plane3df const planeXZ(vector3df(0, 0, 10), vector3df(0.0f, 0.0f, 1.0f));
vector3df intersectWithPlane;
// find where the upperleft ray hits the plane
if (planeXZ.getIntersectionWithLine(upperLeftRay.start, upperLeftRay.getVector(), intersectWithPlane))
upperLeftRay.end = intersectWithPlane;
// find where the lowerright ray hits the plane
if (planeXZ.getIntersectionWithLine(lowerRightRay.start, lowerRightRay.getVector(), intersectWithPlane))
lowerRightRay.end = intersectWithPlane;
// scale the node between the two rays
vector3df scale(lowerRightRay.end.X - upperLeftRay.end.X, lowerRightRay.end.Y - upperLeftRay.end.Y, 10);
// the node position will be where the upperleft ray hits the plane
// and at 10 units deep
vector3df pos(upperLeftRay.end.X + scale.X / 2, upperLeftRay.end.Y + scale.Y / 2, 10 - getDesktop()->m_CSSkin->m_Depth);
// set the node position and scale
m_Node->m_Node->setPosition(pos);
m_Node->m_Node->setScale(scale);