Sign in to follow this  
Majinr

GluProject Odd Problem

Recommended Posts

Majinr    100
Hey all,

Basically I am trying to create a radar which has a 2D shape showing where the object is in 3D coordinates. a bit like here [url="http://www.4gamer.net/patch/demo/freelancer.jpg"]http://www.4gamer.net/patch/demo/freelancer.jpg[/url], with the red triangles.

Now it works fine until I turn my camera about 180 Degrees where I am not facing the object anymore where the 2D shape will appear even though the object is behind the camera, in the same position.

The video below will show what I mean (Quality is not good but its the green square shape , that will appear with the big space ship[supposed to happen] and when I turn it will appear again[not supposed to happen]) - [url="http://www.youtube.com/watch?v=GkT4lXoC0co&feature=youtu.be"]http://www.youtube.com/watch?v=GkT4lXoC0co&feature=youtu.be[/url]

The code I am using is here below for gluProject:



glPushMatrix();

double modelview[16];
double proj[16];
int viewport[4];
double winx, winy, winz;

glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, viewport);

if(gluProject(m_enemyPos.x, m_enemyPos.y, m_enemyPos.z, modelview, proj, viewport, &winx, &winy, &winz) == GL_TRUE)
{
if(winx > WINDOW_WIDTH)
winx = WINDOW_WIDTH - 10;
if(winx <0)
winx = 0;
if(winy > WINDOW_HEIGHT)
winy = WINDOW_HEIGHT;
if(winy < 0)
winy = 0;

screenTest.SetPosition(winx, winy);
screenTest.Render();
}

glPopMatrix();

"screenTest" is the little square that is being rendered to show where the object is.

In this specific project I am using my own camera functions - so manipulating my own matrices . I thought this would be a problem and tried the same thing in another project where I am using gluLookAt - and suprise surprise , it is the same problem.

So I am not really sure how to fix this or what I am doing wrong, so any help is appreciated.

Thanks

Romesh

Share this post


Link to post
Share on other sites
Brother Bob    10344
Think of the projection as a line connecting the view point and the point of interest; the point of interest can be moved anywhere along this line without changing its projected location on the screen. The point on the projection plane (think of the it as the near clip plane if you want to visualize it) where the line intersects the plane is the point on the screen that you get from gluProject. If the point is behind the the view point, the line is still intersecting the projection plane, but at a "negative" distance along the line.

The simple solution is to just not project it if it's behind the view point, or even in front of the near clip plane to avoid potential problems when the eye-space Z-coordinate approaches zero. I'm not sure the winz-coordinate is useful to detect this. If it's not, it's not so much trouble projecting the point onto the forward-vector to determine the point's distance from the view point.

Share this post


Link to post
Share on other sites
Majinr    100
[quote name='Brother Bob' timestamp='1329337920' post='4913455']
If it's not, it's not so much trouble projecting the point onto the forward-vector to determine the point's distance from the view point.
[/quote]

Thanks for the reply Bob, but I am not sure if I understand you correctly here, how would I go about doing that?

I mean i have come up with a quick and dirty solution so far - its not perfect


Vec3 direction = m_playerPos - m_enemyPos; // work out the direction from current position
direction.Normalize(); // normalize
float dot = m_playerLookAt.Dot(direction); // Obtain the angle between the direction and Current Forward vector of the player
float radianAngle = static_cast<float>(acos(dot)); // part of above
radianAngle = RAD2DEG*radianAngle; // Turn from radians into degrees

then I check if the angle is above a certain amount and reverse the value


if(radianAngle > 95)
winx *= -1;

now this works , but isn't perfect as there is some sort of switching occurring at a certain point during the camera rotation where the green point jumps from one end to another.

Again Brother Bob I am not sure if I understand you solution well lol, still scratching my head on how I would approach it in general

Thanks

Romesh

Share this post


Link to post
Share on other sites
Brother Bob    10344
You are pretty much there with your solution, although some tweaks will help. If you leave the direction vector unnormalized (but I assume that the the look-at direction is normalized), then the interpretation of the dot product is [i]the length of the direction vector as projected into the look-at vector[/i]. Or, in the context of your application, how far away the object is from the view point along the look-at direction.

So all you need to do is check if the dot product result is less than zero. That will tell you it is in front of or behind the view point. But I would probably compare the dot product result against the near clip plane instead, or some small value close to zero, to avoid problems with projecting points very close to the view point

Share this post


Link to post
Share on other sites
Majinr    100
[quote name='Brother Bob' timestamp='1329340446' post='4913466']
If you leave the direction vector unnormalized
[/quote]

[quote name='Brother Bob' timestamp='1329340446' post='4913466']
So all you need to do is check if the dot product result is less than zero
[/quote]

That was it!

The little tweak I needed , thanks for that, I really appreciate it, seems to work a lot better now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this