View Full Version : Screen -> World: Works only with orthographic

Glex1

09-20-2008, 09:27 PM

I'm writing a routine to intersect the line from eye-point to the point on the screen with a plane (a plane is given in the world's coordinate system).

Here's the pseudocode:

Vector3d

intersect(windowX, windowY, plane) {

Line line = new Line(

project(modelview, windowX, windowY, 3),

project(modelview, windowX, windowY, 10)

);

return line.intersect(plane);

}

The code that implements this pseudocode works correctly with orthographic projection, but returns incorrect results with perspective projection.

Here's the pseudocode of project routine:

Vector3d

project(modelview, winX, winY, winZ) {

double[] pos = new double[3];

gluUnProject(winX, viewport[3] - winY, winZ, modelview,

projection, viewport, pos);

return pos;

}

Please, can anyone explain what I am doing wrong?

trinitrotoluene

09-21-2008, 08:04 AM

The second parameter for gluUnProject should be viewport[3] - winY - 1, but this is just a one pixel distance error.

I notice that you pass only the modelview matrix for your project function. I think you don't need to pass it because you can call getDoublev(GL_MODELVIEW_MATRIX,...) inside your function.

In my unProject function I do something like this

GLdouble proj[16];

GLdouble model[16];

GLint view[4];

glGetDoublev(GL_MODELVIEW_MATRIX,model);

glGetDoublev(GL_PROJECTION_MATRIX,proj);

glGetIntegerv(GL_VIEWPORT,view);

y = height - 1 - y;

gluUnProject((GLdouble)x,(GLdouble)y,z,model,proj, view,

&objx,&objy,&objz);

Glex1

09-21-2008, 03:51 PM

1) Are you sure about this -1 thing? I changed it, but it didn't fix the problem

2) The current modelview matrix is simply not the one I need (there were some pushs and pops during rendering)

3) Everything works fine without perspective, it gets worse only when I turn on the perspective..

trinitrotoluene

09-22-2008, 09:30 AM

1) You need to subtract -1 because if the height of your viewport is 480, the range of y is [0 479].

2) I think you need the most recent modelview and projection matrix when you unproject a screen coord to object coord.

3) My guess is when you are in orthographic projection, if you get the modelview matrix and then you translate forward or backward, the result is correct because all line are parallel. But this is not the case in perspective view, so the first point of the line is very important.

Edit:

project(modelview, windowX, windowY, 3),

project(modelview, windowX, windowY, 10)

vs

project(modelview, windowX, windowY, 0),

project(modelview, windowX, windowY, 10)

will have probably the same result in orthographic view,

but not the same in perspective view.

Glex1

10-01-2008, 02:16 AM

trinitrotoluene

1) Yes, you are right :) Thanks

2) No, you need to send the modelview matrix with which the object was drawn

3)

Yes, the problem is actually that

Line (

project(modelview, windowX, windowY, 0),

project(modelview, windowX, windowY, 10)

)

and

Line (

project(modelview, windowX, windowY, 3),

project(modelview, windowX, windowY, 10)

)

are different :(

Does anyone by any chance has an idea on how to solve this problem?

project(modelview, windowX, windowY, 3),

project(modelview, windowX, windowY, 10)

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.