0
votes

I'm new to OpenGL and was wondering what do we use matrices for, if someone could explain me in abstract intuitive way, because when reading references or any tutorial, all of these take matrices as known mechanism. I've learned matrices in maths, but as English is not my native language, it's hard to figure out what does some stuff mean.

I found good example at www.learnopengl.com which says:

The model matrix. This matrix is used to place a model somewhere in the “world”. For example, if you have a model of a car and you want it located 1000 meters to the east, you will use the model matrix to do this.
The view matrix. This matrix represents the camera. If we want to view our car which is 1000 meters to the east, we’ll have to move ourselves 1000 meters to the east as well (another way of thinking about it is that we remain stationary, and the rest of the world moves 1000 meters to the west). We use the view matrix to do this.
The projection matrix. Since our screens are flat, we need to do a final transformation to “project” our view onto our screen and get that nice 3D perspective. This is what the projection matrix is used for.

This explains it pretty good. But, how do we build them? How large are they?

Also, I've read in this question: What does glLoadIdentity() do in OpenGL?

that:

glMatrixMode(GL_PROJECTION) deals with the matrices used by perspective transformation or orthogonal transformation.

glMatrixMode(GL_MODELVIEW) deals with matrices used by model-view transformation. That is, to transform your object (aka model) to the view coordinate space (or camera space).

What those transformation mean and how do they get calculated?

I know that here are many question, but I'm trying to make better conception of all of these to get better view on OpenGL. That's why I need some abstract explanation to dive into all details with understanding of conception beyond.

2
They are used for manipulating vertices, translation, rotation and scaling. Read up on matrix algebra if you want to understand more.FreelanceConsultant
Your question is a bit like asking "why to I need to learn physics to build a rocket which will fly as I want it to". Sometimes you just have to research and learn things. (PS: I'm not the one who downvoted this question)FreelanceConsultant
It's ok, I'm not here for reputation. What I really needed is someone like jh314 just to tell 2 sentences about glMatrixMode which would make whole stuff much understandable. Little piece to connect conceptions of OpenGL.Tommz
That is a good attitude to have - I once asked what the purpose of the modes was, I will link you to the question, some people provided good answers.FreelanceConsultant
@EdwardBird Thanks :)Tommz

2 Answers

5
votes

Translation, rotation, and scaling are all affine transforms, which can be implemented using matrix multiplication and addition. Actually, by augmenting the vector with a w element that's always one, the affine transform in 3 dimensions becomes a linear transformation in 4 dimensions and all you need is a matrix multiply.

Doing it with a matrix multiply is very nice because (1) it's fast and (2) you don't need special logic for any of the operations -- you can even compose as many of these affine operations as you want and still represent it with a single matrix.

Having multiple matrix modes is useful when composing. If you had only one matrix, you could add new operations at either end but not in the middle. By having 3 matrices multiplied together later, you can insert new operations at four different points in the order.

The matrix stack is also very useful because it allows you to do an operation for a few primitives and then remove it. If you tried to undo the operation by doing the inverse operation, eventually rounding errors would get out of control. But by remembering the previous matrix, you can just get it back as if the rotation or whatever never happened.

1
votes

OpenGL is nice in that rather than working with matrices directly, you can call functions that will manipulate them.

So under the hood (what really happens), is that there are several matrices that transform your objects (a model-view matrix that transforms object to camera space, and projection matrix for perspective / orthogonal transformations).

glMatrixMode is like a switch that allows you to choose which type of matrix to use and manipulate, and you specify using the arguments. So glMatrixMode(GL_PROJECTION) means that you will be manipulating the projection matrix.