OpenGL ViewPort Matrix

Started by
21 comments, last by _Silence_ 4 years, 6 months ago

I am also confused. Some of the functions are OpenGL 1 or 2, it seems, in which case i can't help anyway.

 

OpenGL knows a viewport (a rectangle in screen pixels) via glViewport( x,y,sizex,sizey ) or you can glGetIntegerv() with the parameter GL_VIEWPORT the current viewport. Aside from that, it has a view matrix to transform vertices from world to view space. Both concepts, the view matrix and the viewport, are very distinct from each other.

The view matrix is not retrieved with a call to opengl, but set by the programmer, e.g. with a lookat-matrix, for example glm::lookat( from, direction, up ) or any other linear algebra home brew.

Maybe it would help if we knew what @Cacks wants to achieve ? Is it about resizing the window ? This is usually done with a callback function from the windowing api, the callback delivers the new size on change. The viewport must then be set to the new values (and framebuffers resized in modern versions) but the view matrix usually stays untouched in these cases as camera position and direction don't change.

But maybe i simply haven't understood ... it so happens far too often ... ?

Advertisement

@Green_Baron

after the Perspective Transformation there is a Viewport Transformation using a 4x4 matrix to map the coords to screen coords

The reason I want the 4x4 Viewport matrix is because I need it to convert world coords to screen coords

GL_VIEWPORT  just gives the viewport position & size not the 4x4 matrix

Reject the basic asumption of civialisation especially the importance of material possessions

Never heard of a viewport matrix. Anybody else maybe ?

The model matrix converts from local to world space.

The view matrix converts from world to view space.

The projection matrix from view to clip space.

Transformation from clipspace to screen coords is done by setting the viewport which is a rectangle, not a matrix. The viewport is set automagically by the windowing api to the size of the window in window mode. Else or if you want a different area you can set the viewport to different sizes with glViewport() and you can query the size with glGetIntegerv( GL_V... ).

@Green_Baron

good point, there might not be a matrix which holds this info, the windowing api might do this behind the scenes

Reject the basic asumption of civialisation especially the importance of material possessions
2 minutes ago, Green_Baron said:

Never heard of a viewport matrix. Anybody else maybe ?

The viewport transform can be expressed in matrix form, but unless things have changed I don't think OpenGL exposes that to the user.

14 minutes ago, Cacks said:

@Green_Baron

after the Perspective Transformation there is a Viewport Transformation using a 4x4 matrix to map the coords to screen coords

The reason I want the 4x4 Viewport matrix is because I need it to convert world coords to screen coords

GL_VIEWPORT  just gives the viewport position & size not the 4x4 matrix

I don't know if modern OpenGL provides a way to 'feed back' screen coordinates - I'll leave it to others to comment on that.

But, you can do it yourself. Everything OpenGL does is just math, ultimately, and can be recreated on the CPU or wherever if needed. Getting positions into clip space is fairly straightforward, as you just need to transform by the same matrices you're submitting to OpenGL (or using in a vertex program or whatever). After that you need to get the coordinates into NDC space, and then transform to screen space.

I've implemented all this in my own code before, although I don't have that code handy at the moment. I'll also mention that math libraries sometimes offer this functionality in the form of e.g. a 'project point' function. GLM, for example, might include this (I don't know if it does - it just seems like it might).

Also, depending on what you need the screen space coordinates for, you might be able to accomplish the same thing via other means. For example, if you want to know whether a point is visible to the camera (not counting occlusion), that can be done via other means that don't involve transforming to screen space.

You're right, i was very superficial. Thanks for pointing it out.

Yes, glm has project and unproject functions for these cases.

The glm functions claim to be compatible to the old glut functions ...

I'm using gluProject & gluUnproject atm but it would simplify my code if I could use the correct matrices

Reject the basic asumption of civialisation especially the importance of material possessions
On 10/16/2019 at 12:52 PM, Green_Baron said:

Never heard of a viewport matrix. Anybody else maybe ?

 

On 10/16/2019 at 1:01 PM, Cacks said:

there might not be a matrix which holds this info

There is no such matrix available in the API. However all OpenGL implementation must follow the same equations that are depicted here for example.

 

To OP, what exactly are you trying to achieve ?

@_Silence_

I want to convert my selected game object world position to screen position

I then want to add my mouse movement coords to that position

Project the updated screen position into the world

Then make a ray

Reject the basic asumption of civialisation especially the importance of material possessions
21 hours ago, Cacks said:

I want to convert my selected game object world position to screen position

I then want to add my mouse movement coords to that position

Project the updated screen position into the world

Then make a ray

If I understand you well, you want to do picking with casting a ray from where the camera is, up to the object which is under the cursor location. There are many tutorials about this. I found this one.

There might have easier alternatives depending on what you exactly want to do with the picked object, which looks unclear to me: normally you first start by the ray, until you find the selected object.

This topic is closed to new replies.

Advertisement