# what are matrices?

In the manual I see word “matrix” here and there: Inverse Transposed Projection Matrix, Modelview Matrix, matrix this, matrix that… But what are all these matrices? What are they for? How and when to use them?

in most cases you dont need to use them at all. for the simple reason. matrices are sorta abstract and hard to imagine.

usualy matrices are like collums and rows filled with data. in most cases float-values.
they are hardware-friendly math to do rotations,scale and transformation in general.

if you dont exactly know what they are then you most likely wont need them. panda provides many easy to use high-level functions so you dont have to mess with matrices directly.

i know thats not a direct answer on what matrices are. but at least you know now that you dont have to break your head about it.

I wish you was right… But I need to know what they are because I want to learn Cg. I see them being used everywhere in shader codes. The manual doesn’t give exhaustive information about them. For example, it gives:
uniform float4x4 mat_modelproj : Composed Modelview/Projection Matrix
What do I know after reading it? Not more then before, as if it’s written in Chinese…

Basically, all you need to do is multiply the mat_modelproj with the vtx_position to get the l_position. You wouldn’t need much more when just writing simple shaders. See this manual page for more information about coordinate spaces.

If you really want to know more about matrix calculations, there are hundreds of tutorials and explanations on the internet. Just google and you will find plenty.

"I wish you was right… But I need to know what they are because I want to learn Cg. "

That’s going to hurt if you are not inclined to maths and linear algebra, however the subject is not that hard and can even be interesting with a good book. I also want to learn Cg and i’m a bit stuck with the maths. My advice is don’t waste time with internet tutorials that explain matrices and the other cg math in a few pages. There’s no easy ride for this stuff. You will need to know about computer graphics math to use shaders and this takes time to learn. Someone has recommended me Computer Graphics Using OpenGL from F.S.Hill Jr.

In short a matrix is a way to store a linear transformation. This is a function that transforms coordinates (x,y,z) in the case of tridimensional space into coordinates (x’,y’,z’). It’s said to be linear because the formula to calculate the transformed coordinates is something like this:

``````x' = a*x+b*y+c*z
y' = d*x+e*y+f*z
z' = g*x+h*y+i*z``````

This is stored in a matrix for convenience. Computing the transformed vector is just a matter of pre-multiplying/post-multiplying row/column vector with the matrix. For example the result of this operation:

``````|a b c| * | x |
|d e f|   | y |
|g h i|   | z |``````

is the same as this:

``````|x y z| * |a d g|
|b e h|
|c f i|``````

and is exactly the right side of the system of equations above.

There’s much more to know that is essential about transformations and matrices. Reading a good computer graphics book is essential if you want to make more than a simple demo.

I like this website for a basic overview of many 3d maths concept: euclideanspace.com/maths/alg … /index.htm

Matrix math can be tricky for the newcomer. But don’t let it frustrate you, because there are many built in functions to build them for you. I’d like to take a moment to briefly describe my knowledge of them.

You have 3 basic types that affect objects:
Scaling (increase the size)
Rotation (rotate the object/vertex)
Translation (moves the objects)

These 3 together when multiplied create what’s called your world matrix. Don’t let the name fool you, it doesn’t affect all objects, only the ones you apply it to. This will set the position, orientation and size of your renderable objects. Very neat, huh?

But you also have 3 basic types that affect rendering:
World Matrix (described above)
Projection Matrix (projects to a 3D space)
View Matrix (renders to a viewport)

Think of view matrix as a window that you look out of. Think of projection matrix as how your eye perceives it. By playing with the projection matrix values you can change the field of view to be narrow, normal or more like a fish-eye lens. Also very neat.

In almost all games, every object reuses the same projection and view matrix since you want all objects to appear the way you would look at them. But each will have it’s own world matrix, otherwise every object would appear in the same spot, even if according to your positioning, they are in completely different areas. To get the final result of how an object appears, all 3 (world, view and projection) are multiplied together against every vertex on the mesh to give it the exact position on your screen.

You can find a ton of resources on the web to find out what each number in matrix represents. But just be mindful that changing them explicitly does not always have the desired results. They are generated by a series of dot product and cross product calculations to get the number that will get the result you want. Chances are, if you’re just playing with Cg, you’re not actually changing them at all because they are generated by the engine and then passed into the shader for rendering. So really nothing to worry about.

Hope that helps.