Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exactly which matrices does OpenGL multiply by in the pipeline?

For the last while I've been putting together an OpenGL program, and I've reached the point where I'm coding in my transformation matrices (the model transform, camera transform, and perspective transform). So far, I've been computing my model transform and sending to a uniform and multiplying it in vertex shader. Recently I added the camera and perspective transforms to the matrix that is sent to the uniform in the shader.

But now, a lot of things aren't working and there's a few things I can't understand:

Firstly, this webpage says that OpenGL automatically divides everything by the Z component of position (for use with the perspective transform), but I can't figure out where this happens, and secondly:

There are a number of resources that mention OpenGL functions such as glFrustumf(), glMatrixMode(), glPushMatrix(), glPopMatrix(), glLoadIdentity(), gRotatef() etc. All these functions that seem to pass and modify matrices inside OpenGL, where I wouldn't need to bother with them in the vertex shader at all.

So now I'm thoroughly lost as to how transformations are done in OpenGL. Do we compute them ourselves and multiply them in the vertex shader, or do we send parameters to OpenGL and let it do all the work behind the API, or something else completely different?

Thanks

like image 834
Marco Merlini Avatar asked Feb 12 '23 11:02

Marco Merlini


1 Answers

Perspective Divison

Once you have transformed your vertices by the ModelViewProjection matrix, they are in clipping space. Without dividing the x, y, and z components by the w component the values are within the range -w to w. Everything outside that range is clipped.

Perspective division (dividing by the w component) brings that range to -1 to 1, known as "normalized device coordinates". This is done automatically by OpenGL on the vertex shader output gl_Position. This (if I recall correctly) is done for the hardware so that all subsequent calculations do not have to account for variable ranges.

Fixed vs. Programmable Pipeline

If you are using uniforms to pass your own transformations to vertex shaders then you are using the modern OpenGL pipeline (yay!). This gives you full control over the transformations of your vertices.

If you are using deprecated functions such as glFrustumf(), glMatrixMode(), glPushMatrix(), glPopMatrix(), glLoadIdentity(), gRotatef(), etc, then you are using the older fixed function pipeline.

like image 111
kbirk Avatar answered Feb 14 '23 23:02

kbirk