• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

# View matrix forming

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1glad  Members   -  Reputation: 134

Like
0Likes
Like

Posted 11 September 2012 - 09:37 AM

Hello everyone!

I'm a new member of this forum despite the fact that I was registered here long time ago.

So..

All matrix will be OpenGL matrices (not DX)

I would like to tell a little about the View matrix. The model-view matrix consists of Model matrix and View. So I need the second one.
And the view matrix is represented like this:
ux uy uz dx
vx vy vz dy
nx ny nz dz
0 0 0 1

where u, v, n - are the normalized vectors of the camera that are representing the coordinate system of the camera.
d - is the translation vector

So I understand that we need to multiply model matrix to a View Matrix to translate World coordinates of some object into Camera coordinates.
But..
I can't understand why u, v, n is in rows, not in columns? I mean that the matrix should be:
ux vx nx dx
uy vy ny dy
uz vz nz dz
0 0 0 1

Why am I think so ?
Let imagine that we have some point that have it own transformation matrix. This point P will be:
P = v + P*i + P*j + P*k;
v - origin,
i,j,k - are orthogonal vectors-columns.

i = (1, 0, 0, 0)T
j = (0, 1, 0, 0)T
k = (0, 0, 1, 0)T
v - (vx, vy, vz, 1)T

The last number tells us what is it: vector or point.. if it equals to 0 hence it vector (because it is haven't origin), if 1 then it is a point.
So the final matrix would be:

1 0 0 vx
0 1 0 vy
0 0 1 vz
0 0 0 1

And this is logically ok.

But, looking at the view matrix I can't see any logic..

Could onyone help me to understand what is wrong about my argumentation ?

Thank you.

Pardon for my English. It is not perfect, but I'm working on it. =)

Edited by glad, 12 September 2012 - 01:29 AM.

### #2oggs91  Members   -  Reputation: 289

Like
0Likes
Like

Posted 11 September 2012 - 06:47 PM

it's not that simple ^^ i don't understand it in detail too, but anyway i try to help

the view matrix consits of two matrizes, a rotation matrix and a translation matrix ... multiplied they give the view matrix
the translation can be easily calculated, we only have problems with the rotation part

sometime ago i tried to calculate a rotation matrix from an orientationvector which is the exact same thing as a view matrix
and i think this was pretty hard to proof

i just looked it up in my mathbook, but it doesnt make any sence to me now ^^

... search for orthogonal matrizes, and vectors

edit: i think i got it

first we build a 3x3 rotation matrix from the u,v,w vectors, then we invert it, because we don't want to transform the camera it self, we want to transform the whole world relative to the camera.

Rotation Matrix:
(1 0 0) u
(0 1 0) v
(0 0 1) w


because this is a orthogonal matrix we can invert it by transposing it. now expand the rotation matrix to 4x4 matrix and multiply it with the inverse translation ... voila

Edited by oggs91, 12 September 2012 - 01:23 AM.

### #3Lauris Kaplinski  Members   -  Reputation: 841

Like
1Likes
Like

Posted 12 September 2012 - 12:32 PM

Think of all matrices as a transformation from one coordinate system to another one. In case of view matrix you have always keep in mind, which transformation you are talking about - world to camera or camera to world.

Your view matrix above transforms from world coordinate system to camera coordinate system.

Now u,v and w are camera axes represented in world coordinate system. Thus in your view matrix they form rows, not columns.
On the other hand the world axes represented in camera coordinate system do form the rows of view matrix.

Edited by Lauris Kaplinski, 12 September 2012 - 12:33 PM.

Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

### #4Kaptein  Prime Members   -  Reputation: 2180

Like
1Likes
Like

Posted 12 September 2012 - 03:38 PM

the modelview matrix is usually a combination of a basic rotation matrix and a translation matrix
most times they are multiplied together in a specific order, depending on what type of game you are making
space games are more complex, first person games are simpler (usually), since you only have xrot and yrot for camera
in my case, and hopefully yours (i didnt see the details), you can just rotate and then translate it
where the translation is simply an identity matrix with tx, ty and tz set to whatever

if you have an object out in the world, however, youll need to translate it back to its rotational origin BEFORE you can rotate it!
that's more complex, but doable if you can visualize it in your head
say you have a cube at 64,64,64, and you wish to rotate it around itself, where the cube is 1.0 in size
well, then the center of the cube is 0.5, so youll need to translate it back to -0.5,-0.5,-0.5, rotate it there, and translate it back
thats 3 matrix multiplications! remember the order matters, and its backwards!

the guy above me posted about coordinate systems, and thats extremely important too, since opengl makes vast use of it, and for good reasons too
don't hesitate to ask about anything, such as creating perspectives andwhat not

### #5Trienco  Crossbones+   -  Reputation: 2224

Like
1Likes
Like

Posted 12 September 2012 - 10:37 PM

The way I consider safe to a persons sanity is to handle the "camera" like any other object. Since scaling the camera doesn't make much sense, all you got are rotation (in the top left 3x3 "sub matrix" and translation in the last column -corresponding to the last 4 floats in the memory layout used for OpenGL-).
This means the first three columns are basically the cameras x,y,z (typically "right", "up" and "forward") axes expressed in world coordinates with the last column being it's position. It's convenient and easy to read.

To use it as view matrix, you obviously need to move everything "the opposite way", so you need the inverse of your matrix. Since you didn't scale, your matrix can be inverted in a simple way. The rotation part is transposed (which explains why it's rows, not columns) and the translation is negated along the original axes (which is better explained by showing the result.

So if your camera matrix (using r,u,f,p as right, up, forward, position instead of uvn) is:

r.x u.x f.x p.x
r.y u.y f.y p.y
r.z u.z f.z p.z
0 0 0 1

Your view matrix will be (created from the camera matrix once per frame):

r.x r.y r.z -(p dot r)
u.x u.y u.z -(p dot u)
f.x f.y f.z -(p dot f)
0 0 0 1

So: keep your camera matrix around, don't recalculate from scratch every frame and for heavens sake, stay as far away from Euler angles as possible (ego shooter style camera is about the only thing they can handle without a lot of head ache).

I'm not sure why Kaptein makes rotating an object around itself so complicated. If you want the camera to rotate around itself, you use camera_matrix * rotation_matrix. If you want it to rotate around the origin you use rotation_matrix * camera_matrix (or the other way around). If you are in fact feeling lazy, you can abuse OpenGL to do the matrix math. Translation and rotation are always applied in object coordinates, so objects already rotate around themselves (using their current right/up/forward axes), no matter where they are and always translate along their local axes as well.

Point is: OpenGL is not doing anything "backwards" unless you insist on thinking in world coordinates instead of object coordinates.

Examples:

-Strafe right is always glTranslate(1,0,0)
What people often do: calculating some "right" vector (instead of 1:1 extracting it from the objects transformation matrix) and then going to the trouble of translating along that vector in global coordinates by forcing things to be done "backwards".

-"Pitch around self" is always glRotate(angle, 1,0,0)
What people often do: calculate some "right" vector (again), then do stuff backwards (including translating stuff to the origin and back)

If you want to mix things, like rotate around "global up" but around your current position, you still don't need to start translating. You just need to adjust your axis by multiplying the "global" axis with the transposed rotation part of the object (basically you cancel out the existing rotations, so your objects "up" is the same as the worlds "up"):

To rotate around global (0,1,0):

axis.x = 0*r.x + 1*r.y + 0*r.z
axis.y = 0*u.x + 1*u.y + 0*u.z
axis.z = 0*f.x + 1*f.y + 0*f.z

ie: glRotate(angle, r.y, u.y, f.y)

Bottom line: think forward and think local, it will make things much easier and more intuitive

Edited by Trienco, 12 September 2012 - 10:50 PM.

f@dzhttp://festini.device-zero.de

### #6glad  Members   -  Reputation: 134

Like
0Likes
Like

Posted 18 September 2012 - 03:54 PM

Hi!
Sorry, I can't get to the computer for a long time.

First of all thank you all! Your information is very usefull for me. To say the truth it is difficult to think in 3D space, in those calculations..

So, let's go

To all
I want to show you an example from the book.

So, we look from eye(4,4,4) point to look(0,1,0) point. Up vector is (0,1,0).
So if we form the view matrix we will get something like that:
0.7071 0.0000 -0.7071 0.0000
-0.3313 0.8835 -0.3313 -0.8835
0.6247 0.4685 0.6247 -6.8716
0.0000 0.0000 0.0000 1.0000

if we invert this matrix the new matrix will be:
0.7071 -0.3313 0.6247 4.0000
0.0000 0.8835 0.4685 4.0000
-0.7071 -0.3313 0.6247 4.0000
0.0000 0.0000 0.0000 1.0000

So the second matrix is understandable(I don't know if it correct English word ) for me, because eye is the last column vector and the 3 axes are columns too. And everything is great!

To use it as view matrix, you obviously need to move everything "the opposite way", so you need the inverse of your matrix

May be you have any mathematical explanation ? For example, if we take an example of model matrix the inversing of the matrix means that we transit to the previous state. And this is mathematically correct and clear M*M-1=E. The question is: why do we do inverse of the camera matrix ? If I am corect the invertion of camera object matrix is the view matrix. Am I right?

So.. Going further..
From the book..
First of all the picture:

№1, №2, №3 - are some coordinate systems(picture above have Russian words), that is got through sequence of matrix transformations. We can examine some matrix transfornation with 2 different views:
1. We have one coordinate system and our point/points transform within this coordinate system
2. Each transformation creates new coordinate frame.

So.. for 2D space as in the picture above:
(a,b,1)T=M1*M2*(e,f,1)T
If I am corect the coordinate system(CS) #1 is the World coordinate system, and #2 or #3 can be Camera CS.. or vice versa ?
So if, for example, we have some matrix M that translates CS#1 into CS#2 (World CS into Camera CS) then
(a,b,1)T=M*(c,d,1)T
So if we know (a,b) how can we get (c,d) ? Should we multiply it by M-1? May be that is the answer ?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS