View matrix forming

This topic is 2196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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:
u[sub]x[/sub] u[sub]y[/sub] u[sub]z[/sub] d[sub]x[/sub]
v[sub]x[/sub] v[sub]y[/sub] v[sub]z[/sub] d[sub]y[/sub]
n[sub]x[/sub] n[sub]y[/sub] n[sub]z[/sub] d[sub]z[/sub]
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:
u[sub]x[/sub] v[sub]x[/sub] n[sub]x[/sub] d[sub]x[/sub]
u[sub]y[/sub] v[sub]y[/sub] n[sub]y[/sub] d[sub]y[/sub]
u[sub]z[/sub] v[sub]z[/sub] n[sub]z[/sub] d[sub]z[/sub]
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)[sup]T[/sup]
j = (0, 1, 0, 0)[sup]T[/sup]
k = (0, 0, 1, 0)[sup]T[/sup]
v - (v[sub]x, [/sub]v[sub]y, [/sub]v[sub]z[/sub], 1)[sup]T[/sup]

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 v[sub]x[/sub]
0 1 0 v[sub]y[/sub]
0 0 1 v[sub]z[/sub]
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

Share on other sites
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

Share on other sites
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

Share on other sites
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

Share on other sites
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:

[font=courier new,courier,monospace]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[/font]

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

[font=courier new,courier,monospace]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[/font]

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):

[font=courier new,courier,monospace]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[/font]
[font=courier new,courier,monospace]ie: glRotate(angle, r.y, u.y, f.y)[/font]

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

Share on other sites
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[sup]-1[/sup]=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)[sup]T[/sup]=M[sub]1[/sub]*M[sub]2[/sub]*(e,f,1)[sup]T[/sup]
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)[sup]T[/sup]=M*(c,d,1)[sup]T[/sup]
So if we know (a,b) how can we get (c,d) ? Should we multiply it by M[sup]-1[/sup]? May be that is the answer ?

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
14

• 14
• 10
• 23
• 9
• 33
• Forum Statistics

• Total Topics
632631
• Total Posts
3007536
• Who's Online (See full list)

There are no registered users currently online

×