Followers 0

# OpenGL and 3D Math

## 4 posts in this topic

I have read several sources saying along the lines "this has been explained at many other places already so I won't bother with it" and then they continue to explain what every place explains.

Anyways there are a few things I would like to get a better understanding of and that's the opengl transformation matrices and how they are derived. I have found sources where they kinda explain it but they often skip quite a few details.

Another subject I want to look into is quaternions. I have searched google for some of these already and I have found some places but none that really jump out at me as a [b]good[/b] source for learning the subject in a decent amount of detail.

So any references would be awesome. Edited by Homunculi
0

##### Share on other sites
Both of those are covered in collegiate linear algebra classes.

3D mathematics is simply linear algebra.

Actually, linear algebra covers more than three dimensions, but the coursework generally focuses on 3D because it is easier for people to understand.

If you haven't had a course on it yet, you can try your luck with online tutorials that pick-and-choose what they teach and hope it covers the material you need, or you can get a book on linear algebra and treat it like a self-taught mathematics class. I recommend the actual course with an instructor first, if not possible then a self-taught course on the subject. If you try to cobble it together from online tutorials you won't understand everything you need to know and you will have some serious gaps in your knowledge that will likely make things more difficult than they should be.
0

##### Share on other sites
I highly recommend Khan academy for most or all your math needs.

Here's a link to linear [url="http://www.khanacademy.org/math/linear-algebra"]algebra lectures[/url], and I'd suggest starting with the multiplication of matrices and vectors. He also shows how to inverse a matrix, which is handy for reversing a transformation/projection matrix which you may need for screen to world coordinate conversions. Granted, they don't cover strictly "3D math", but the essential basics is never a bad thing to know. It's a lot easier to understand what the 3D math people talk about if you know the matrix basics first.

Quaternions are multidimensional complex numbers. Not really sure how they're implemented, but I'm sure that atleast some basic grasp of [url="http://www.khanacademy.org/math/algebra/complex-numbers"]complex numbers[/url] is necessary before delving into that.

There's also a practice section of the site to test your math skills that you learn in the lectures. Have fun! Edited by Zoomulator
0

##### Share on other sites
Well good news. I do know a bit of linear algebra already. I am familiar with linear transformations, basis, dot product, matrix multiplication, transpose, determinants, inverse matrices etc. There is a lot of terminology and the whole opengl graphics pipeline that I need to get sorted out. I'll say a few things and if someone can correct me where I am wrong and I'll ask some questions and what not.

Let me see if I have this right so far:

The opengl projection matrix is about defining the viewing frustum and perspective or orthopraphic projection... so... are the clipped coordinates the coordinates that lay in the viewing frustum? Which is definied by the projection matrix? That would explain why to get the clipped coordinate one has to multiply the projection matrix and the eye coordinate.

The modelview matrix is a combinaton of the model and the view matrix. It is used for doing transformations to a given vertex (scale, rotate and position). OpenGL doesn't use a "camera" or it can be thought of as the camera always being at (0,0,0) looking down the -Z axis. In order to change one's view, they need to transform all the objects positions so it's as if the world is moving around the "camera" not the "camera" moving around the world.

songho is the best website I have found for the formulation of the projection matrix and modelview matrix. I still want to read it a bit more before I continue.

What I understand I need to do is create the projection matrix and the modelview matrix. I can pass these into my vertex shader which I can then do matrix multiplication on them and my given vertex to get clipped coordinates. These coordinates will be passed down the pipeline eventually turned into window coordinates. The rasterization comes next in the pipeline right? The rasterization is the creation of the pixels to be rendered based on the vertices given if I understand correctly.

This is where fragment shaders are used to control how the pixels are formed? Also known as "pixel shader"?

Btw, khan academy is amazing =) Edited by Homunculi
0

##### Share on other sites
[quote name='Homunculi' timestamp='1337995684' post='4943389']
The opengl projection matrix is about defining the viewing frustum and perspective or orthopraphic projection... so... are the clipped coordinates the coordinates that lay in the viewing frustum? Which is definied by the projection matrix? That would explain why to get the clipped coordinate one has to multiply the projection matrix and the eye coordinate.

The modelview matrix is a combinaton of the model and the view matrix. It is used for doing transformations to a given vertex (scale, rotate and position). OpenGL doesn't use a "camera" or it can be thought of as the camera always being at (0,0,0) looking down the -Z axis. In order to change one's view, they need to transform all the objects positions so it's as if the world is moving around the "camera" not the "camera" moving around the world.
[/quote]
You have a viewing volume in general. Before normalization it is a "frustum" in case of perspective projection but a cuboid in case of orthographic projection. The projection matrix is responsible to squeeze the viewing volume into the normalized viewing volume which is in fact a cube ranging from -1 to +1 in each dimension. However, you need more to described than how to squeeze the volume: you need to describe where in the space the volume is located and how it is orientated. The both standard projection matrices are defined in a way that the viewing volume has an origin at [b]0[/b] and OpenGL's standard viewing orientation. I other words, the projection expects to be applied in a local space that is called the viewing space.

You can relate a space to another space by giving a transformation matrix. (In this sense also the projection transforms from one space into another one.) There isn't really a dominant space; each space is in principle as good as another one, but some tasks are more convenient to be done in the one than in the other space. The viewing space, for example, is convenient for doing projection. However, it isn't convenient for placing models. This is because when wandering around we would need to alter each model's co-ordinates. Instead we define a common space where we relate both the models as well as the viewing volume. We usually call this space the global space or "world".

When we relate a model to the world, we'll see that defining a model's shape (by its vertices, of course) in global space isn't convenient, because we have to adapt each and every vertex when we want to shift or rotate the model. It is more convenient to have a space where the vertices have fixed co-ordinates regardless how the model is placed in the world. Such a space is called a local space or "model space", and the transformation that relates the model space to the global space is called the MODEL matrix in OpenGL's nomenclature. It is a transformation that computes the vertex co-ordinates given in model space now in global space, and hence is a local-to-global transformation.

We've said that the viewing volume is related to the global space as well. Hence the belonging matrix "as is" describes also a local-to-global transformation. In analogy to "model" we call the belonging object in the world the "camera" also it is even more "virtual" than a model, because we never see the camera itself but only the subspace it picks out of the world.

Now we can relate the models to the world and the viewing volume (or camera) to the world as well. But to apply the projection (and further steps) we need to work the models in the view space. Transformation from model space to global space to view space hence requires to concatenate the model's local-to-global transformation and the camera's global-to-local-transformation, where the latter one is obviously the inverse of the camera's local-to-global transformation. This inverse one is called the VIEW transformation in OpenGL's nomenclature, and the concatenation logically the MODELVIEW transformation.

Mathematically we speak of
[b]P [/b]*[b] V[/b] * [b]M[/b] = [b]P[/b] * [b]C[/b][sup]-1[/sup] * [b]M[/b]
where [b]P[/b] means the projection matrix, [b]V[/b] the VIEW matrix (global-to-local), [b]M[/b] the MODEL matrix (local-to-global), and [b]C[/b] the camera (local-to-global). Due to the way OpenGL expects the matrices (at least legacy OpenGL), it computes
[b]P [/b]* ([b] V[/b] * [b]M[/b] )
but this is mathematically equivalent to
[b]P [/b]* ([b] V[/b] * [b]M[/b] ) = ( [b]P [/b]*[b] V[/b] ) * [b]M[/b]

That said, the concept of a camera [i]is[/i] actually existing in OpenGL, but mathematically it plays no role whether you move models or a camera around. It is just a question of efficiency: It is more efficient to let the GPU transform the vertices and deal with dynamic models plus the camera by the CPU instead of letting the CPU transform all vertices.
1

## Create an account

Register a new account