View Full Version : Algorithm for gluProject

04-30-2001, 10:56 AM
I need to know if anyone has an algorithm for converting a 3D location to 2D coordinates. I tried to implement my own, but I couldn't get it to work. gluProject works fine, but is rather slow. I have the matrix from the Object I wish to draw. There has to be a way to do this, anyone have an algorithm?

04-30-2001, 11:30 AM
A simple algorithm

x' = xfactor * x/(z + bias)
y' = yfactor * y/(z + bias)

xfactor/yfactor create an aspect ratio

04-30-2001, 12:05 PM
errrm, are xfactor,yfactor,bias trial and error values or is there some relationship to the FOVY and the Viewport. I guess I don't understand how to utilize that.

04-30-2001, 02:42 PM
xfactor and yfactor represent a distance from the eye to the focus. There's some more math needed to get a FOVY equivalent for them. bias is supposed to be a fairly small number to prevent a division by zero.

tan(FOVY/2) * (focus length) should get you your max abs(y') denoted by [-h,h].

y' = yfactor * (y/z), in this sense will get you a value in [-h,h] if visible. Same goes with x'

Of course, this is all assuming you translated first and scale your (x',y') from [-h,h] to your actual window coords afterwards.

04-30-2001, 03:18 PM
I'll quote the OpenGL Reference Manual v1.1:

To compute the coordinates, let v=(objX,objY,objZ,1.0) represented as a matrix with 4 rows and 1 column. Then gluProject computes v' as follows:

v' = P x M x v

where P is the current projection matrix proj, M is the current modelview matrix model (both represented as 4x4 matrices in column-major order) and 'x' represents matrix multiplication.

(Then v' = v' / v'(3) is performed, where v'(3) is the fourth coordinate - w - of v'. This is missing in the text, but AFAIK is needed for a correct implementation. ET.)

The window coordinates are the computer as follows:

winX = view(0) + view(2) * (v'(0) + 1) / 2
winY = view(1) + view(3) * (v'(1) + 1) / 2
winZ = (v'(2) + 1) / 2

(I agree with you that glProject is slow. I implemented my own projection in Java, and it was faster. The implementation was based on the above description, with the correction I mentioned.)

[This message has been edited by ET3D (edited 04-30-2001).]

05-02-2001, 08:31 PM
Looks complicated but thank you. I'll try to get that working somehow. http://www.opengl.org/discussion_boards/ubb/smile.gif

I guess I thought there might be an easier was of doing it.

05-03-2001, 01:09 AM
It's not that complicated. You just have to be able to do matrix/vector multiplication (and matrix/matrix multiplication, but you should do this only once, not for every vertex you project).

09-12-2003, 07:34 AM
winX = view(0) + view(2) * (v'(0) + 1) / 2

What is 'view' here please?

09-12-2003, 08:36 AM
NeHe has just posted a new article. I think it explains exactly the thing you are asking for, but iīm not sure, havenīt read it, yet.


09-12-2003, 09:01 AM
Where is the article please?

09-12-2003, 12:09 PM
Ah, iīm sorry. I havenīt had it correctly in mind. The article is about gluUnproject.
Itīs under "Articles" -> "Article 13".

Well, just forget what i said, it doesnīt look appropriate for your purpose.


09-12-2003, 05:15 PM
You can get the source to GLU from Mesa CVS.