Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL - ortho Projection matrix, glViewport

I'm having trouble wrapping my head around how these work. First, in a 2d game the projection matrix should be set up as ortho with left, right, top, bottom matching the window, right? But when the window resizes, should I just change glViewport, not the projection matrix? And how do I keep the aspect ratio?

Could someone explain the purposes of these two things, in 2d orthographical game, so that I can understand it better?

It feels like OpenGL is doing a lot of useless stuff in a 2d setup. Rasterizing and calculating fragments when the images are already there, converting vertex coordinates to NDC only to be converted back to what they already where by glViewport.

Also, how come in legacy free OpenGL we have to make our own matrices, but not our own calculations that glViewport does?

Thanks.

like image 422
mk12 Avatar asked Sep 01 '10 15:09

mk12


People also ask

What is glViewport?

The glViewport function specifies the affine transformation of x and y from normalized device coordinates to window coordinates. Let (xnd , ynd ) be normalized device coordinates.

How do you find the orthographic projection matrix?

The Orthographic Projection Matrix Translate the volume defined by the createOrthographic parameters so that it is centered at the origin, then. Scale the volume to be a 2 unit wide cube using an appropriate scale factor for each axis, and then. Flip the z axis to match the clipping space's coordinate system.

What is orthographic projection OpenGL?

An orthographic projection matrix defines a cube-like frustum box that defines the clipping space where each vertex outside this box is clipped. When creating an orthographic projection matrix we specify the width, height and length of the visible frustum.

What is gluOrtho2D in OpenGL?

The gluOrtho2D function sets up a two-dimensional orthographic viewing region. This is equivalent to calling glOrtho with zNear = -1 and zFar = 1.


1 Answers

Don't confuse what you input to GL and what it outputs to.

the parameters you use to compute the Ortho matrix are the ones that correlate to your input. The output of a projection matrix is always the [-1:1]x[-1:1]x[-1:1] cube.

the viewport translates that cube to your render target coordinates. So that's typically what you want to change to match your new window size (well, that and the framebuffer itself).

Yes, GL does a lot of useless stuff for a 2D rendering path. It's a 3D API after all...

I'll finish by saying that you don't have to build matrices to do 2D transforms, so long as your vertex shader outputs in the cube I mentioned earlier. If you want to write the upper right corner of the viewport, you can always pass your vertices directly as (0,0) (0,1) (1,0) (1,1) and simply output that.

like image 54
Bahbar Avatar answered Oct 13 '22 22:10

Bahbar