# OpenGL Unexpected results when calculating with Projection Matrix

## Recommended Posts

HolyFish    127
Hi, I'm doing my first steps in OpenGL and I'm trying to understand the way the ModelView and Projection matrices work. I, more or less, know what their use is but I'm getting unexpected results when doing some test-calculations. As I see it: The Projection Matrix is a simple matrix that defines the Viewing-frustum of your "camera" and holds the information about how OpenGL should transform the 3D-vertices in your scene to equivalent 2D-coordinates on your screen. So far, so good, but now I wanted to test this myself. If I multiply a random vertex with the projection matrix I should get the projected value (as I would see it on screen) right? Here is my code:
Vector3* v = new Vector3(16.0f, 7.0f,10.0f);

float buffer[16];
glGetFloat(GL_PROJECTION_MATRIX, buffer);

Vector3* proj = new Vector3(v.x*buffer[0] + v.y*buffer[1] + v.z*buffer[2],
v.x*buffer[3] + v.y*buffer[4] + v.z*buffer[5],
v.x*buffer[6] + v.y*buffer[7] + v.z*buffer[8]);

The camera is in position (0, 0, 6) and the result I get here is: (20.784609, 17.320507, 0.0). The Z-coordinate is 0, that seems right, but in what coordinate-system are the X and Y values? Am I making a mistake? Thank you.

##### Share on other sites
haegarr    7372
(1) The matrices in OpenGL are given in "column major order", what means that the indices are not as you assume (you assume "row major order"). So the first correction would be:
Vector3* proj = new Vector3(v.x*buffer[0] + v.y*buffer[4] + v.z*buffer[8],                            v.x*buffer[1] + v.y*buffer[5] + v.z*buffer[9],                            v.x*buffer[2] + v.y*buffer[6] + v.z*buffer[10]);

(Notice that this "major order" thingy comes from the fact that the 2D matrix is accessed by a 1D indexing.)

(2) You use a vertex, and a vertex is a "position vector". But you use it as a "direction vector". Look at the homogeneous co-ordinate w for the difference: w is 1 for a (normalized) position vector, but 0 for a direction vector! So the second correction would be:
Vector3* proj = new Vector3(v.x*buffer[0] + v.y*buffer[4] + v.z*buffer[8] + buffer[12],                            v.x*buffer[1] + v.y*buffer[5] + v.z*buffer[9] + buffer[13],                            v.x*buffer[2] + v.y*buffer[6] + v.z*buffer[10] + buffer[14]);

(3) The projection matrix is special in that it does _not_ provide an affine transformation. So you have to take also the lowest row into account, since it stores the perspective stuff if you do a perspective projection (as far as I remember; maybe I'm wrong here).
Vector3* proj = new Vector3(v.x*buffer[0] + v.y*buffer[4] + v.z*buffer[8] + buffer[12],                            v.x*buffer[1] + v.y*buffer[5] + v.z*buffer[9] + buffer[13],                            v.x*buffer[2] + v.y*buffer[6] + v.z*buffer[10] + buffer[14]);float proj_w = v.x*buffer[3] + v.y*buffer[7] + v.z*buffer[11] + v.z*buffer[15];proj /= proj_w; // normalize

[Edited by - haegarr on November 23, 2005 4:39:49 AM]

##### Share on other sites
Eric Lengyel    3461
Vertices go through multiple transformations to get from camera space to the screen. The following code mirrors what OpenGL does. First, the projection matrix transforms a camera-space vertex into homogeneous clip-space coordinates (hence the 4-dimensional vector 'clip'). Then, the clip-space position is divided by its w-coordinate to obtain 3D normalized device coordinates. In NDC, the view frustum has been mapped to the unit cube extending from -1 to 1 in each direction. Finally, the viewport transformation is applied to map positions from NDC to the viewport. The z-coordinate in viewport space is the depth that's written to the depth buffer.

Vector3 v(16.0F, 7.0F, 10.0F);// Calculate clip-space coordinatesfloat buffer[16];glGetFloat(GL_PROJECTION_MATRIX, buffer);Vector4 clip(buffer[0]*v.x + buffer[4]*v.y + buffer[8]*v.z + buffer[12],             buffer[1]*v.x + buffer[5]*v.y + buffer[9]*v.z + buffer[13],             buffer[2]*v.x + buffer[6]*v.y + buffer[10]*v.z + buffer[14],             buffer[3]*v.x + buffer[7]*v.y + buffer[11]*v.z + buffer[15]);// Calculate normalized device coordinatesfloat invW = 1.0F / clip.w;Vector3 ndc(clip.x * invW, clip.y * invW, clip.z * invW);// Calculate (x,y) viewport coordinatesglGetFloat(GL_VIEWPORT, buffer);float w = buffer[2] * 0.5F;float h = buffer[3] * 0.5F;float x = (ndc.x + 1.0F) * w + buffer[0];float y = (ndc.y + 1.0F) * h + buffer[1];// Calculate z depthglGetFloat(GL_DEPTH_RANGE, buffer);float d = (buffer[1] - buffer[0]) * 0.5F;float z = (ndc.z + 1.0F) * d + buffer[0];

##### Share on other sites
HolyFish    127
Thank you both. Things are clearing up now :-)

## Create an account

Register a new account

• ### Similar Content

• I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.

• There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window.
I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.

Thanks
• By cebugdev
hi all,

i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
thank you, and looking forward to positive replies.

• I have a few beginner questions about tesselation that I really have no clue.
The opengl wiki doesn't seem to talk anything about the details.

What is the relationship between TCS layout out and TES layout in?
How does the tesselator know how control points are organized?
e.g. If TES input requests triangles, but TCS can output N vertices.
What happens in this case?
http://www.informit.com/articles/article.aspx?p=2120983
the isoline example TCS out=4, but TES in=isoline.
And gl_TessCoord is only a single one.
So which ones are the control points?
How are tesselator building primitives?
• By Orella
I've been developing a 2D Engine using SFML + ImGui.
Here you can see an image
The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine.
I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui.
3D Editor preview
But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
If you can provide code will be better. And if you want me to provide any specific code tell me.
Thanks!

• 14
• 20
• 35
• 13
• 39