flat assembler
Message board for the users of flat assembler. Index > Heap > camera to view matrix
Author
 Thread  gens

Joined: 18 Feb 2013
Posts: 161
gens
i'm making a 3D engine in opengl and want to do all the calculations cpu side (and understand them more or less)

now the time has come to make the "camera"
i got the structure like "position" and "rotation" (bout vec3)

so the question is do i have to make the matrix then invert it
or can i when making it just apply minus position and opposite rotations ?
(edit: its 3x3 matrices for rotations and transformed coordinates for position)

has kind of a easy question vibe to it, but me brain is a bit slow these days
i ask here 'cuz i seen many know maths things here 18 Feb 2014, 18:00  tthsqe

Joined: 20 May 2009
Posts: 730
tthsqe
I like to keep the camera orientation as a unit quaternion.
Recall that the unit quaternions form a double cover of SO(3). (q and -q map to the same matrix)

The problem with maintaining the camera orientation directly as an orthogonal matrix M is that round off errors will occur when you multiply M by other rotations, and then it is non trivial to get M back to as close to orthogonal as possible.

However, it is trivial to renormalize a quternion, and matrix multiplication corresponds to quaternion multiplication.

I think it is elegant, and you can see how I do it in 'camera.inc' of my post in http://board.flatassembler.net/topic.php?t=15746&start=0 18 Feb 2014, 21:32  gens

Joined: 18 Feb 2013
Posts: 161
gens
i didn't learn quarterions at school (nor matrices, as far as i remember)
so that will take me time

the camera is for now as an object, absolute world placement
Code:
```camera_position:
dd 0f, 0f, 0f
camera_rotation:
dd 0f, 0f, 0f    ```

(for now)

where rotation is rotations around x,y,z in radians
then before rendering a frame i plan to calculate a matrix out of it and multiply it by object's matrices
(that are also calculated; now for every frame, later will be only when changed)

thx for the example
i see there is plenty theory there to learn

bdw i remember you wanting to render many triangles

now i mostly understand opengl buffers and rendering calls terminology well enough to make a quick example
one of my test meshes has 28208 triangles and its np for cpu/gpu (glDrawArrays)
i could also give the whole thing, but it's still work in progress (ugly at places) 18 Feb 2014, 22:40  revolution When all else fails, read the source Joined: 24 Aug 2004 Posts: 17352 Location: In your JS exploiting you and your system revolution gens wrote:where rotation is rotations around x,y,z in radians Do you mean the view direction from the origin to some point at (x,y,z)? For astronomy the usage of only azimuth and elevation is enough to point the telescope to any point. 18 Feb 2014, 22:45
gens

Joined: 18 Feb 2013
Posts: 161
gens
revolution wrote:
gens wrote:
where rotation is rotations around x,y,z in radians
Do you mean the view direction from the origin to some point at (x,y,z)? For astronomy the usage of only azimuth and elevation is enough to point the telescope to any point.

ye i imagined making it like that when starting, don't remember why it went this way
but i need tilting anyway so its good

i got 3 separate functions for making a rotational matrix around x,y and z
will make a different function that combines them, but they will still be useful for like two to two and a half dimensional games 18 Feb 2014, 22:53  tthsqe

Joined: 20 May 2009
Posts: 730
tthsqe
I have several objection to characterizing your camera orientation by three angles.
First, suppose you want to rotate your camera by an angle theta about the vector {x,y,z}. What are the new three angles? Nobody wants to work out those calculations, and they are literally 'transcendental' in difficulty.
Second, as the angles really only matter mod Pi or mod 2*Pi, the correspondence between angles and camera orientations is many-to-one.

With unit quaternions, the correspondence is only two-to-one. Also, the rotation problem can be solved my multiplying on the left by
Code:
`cos(theta)+sin(theta)*(x*I+y*J+z*K)/sqrt(x^2+y^2+z^2)    `
.
Very simple. 19 Feb 2014, 04:29  gens

Joined: 18 Feb 2013
Posts: 161
gens
tthsqe wrote:
First, suppose you want to rotate your camera by an angle theta about the vector {x,y,z}. What are the new three angles? Nobody wants to work out those calculations, and they are literally 'transcendental' in difficulty.

sounds fun tthsqe wrote:
Second, as the angles really only matter mod Pi or mod 2*Pi, the correspondence between angles and camera orientations is many-to-one.

rotation is sanitized when changed (if over 2pi, sub 2pi)
figured i don't want overflows, no matter how far fetched
tthsqe wrote:
With unit quaternions, the correspondence is only two-to-one. Also, the rotation problem can be solved my multiplying on the left by
Code:
`cos(theta)+sin(theta)*(x*I+y*J+z*K)/sqrt(x^2+y^2+z^2)    `
.
Very simple.

its back to the quarterion FAQ for me

thx for the tips
il' implement quarterions when i understand them
for now i'l limit the camera to two rotations so i can work on more fun parts (plan is a generic engine, so multiple types of objects, and now cameras, will be ok) 19 Feb 2014, 15:19  cl0ckw0rkz14

Joined: 18 Feb 2014
Posts: 21
cl0ckw0rkz14
I didn't get taught matrices at school either and have never really been confident at mathematics but this tutorial helped me loads when I was writing a 2d game for android. http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ 19 Feb 2014, 22:29  gens

Joined: 18 Feb 2013
Posts: 161
gens
yes it's a good tutorial but it is cpp with external libraries

http://www.cs.princeton.edu/~gewang/projects/darth/stuff/quat_faq.html
this one has lots of good maths stuff for 3D 20 Feb 2014, 23:04  tthsqe

Joined: 20 May 2009
Posts: 730
tthsqe
gens, is my example 'camera.inc' unreadable? These links seem to make a dog's dinner of the issues. My 'camera.inc' everything one could possibly want for a camera with free motion in all of its six dimensions.
Data structures:
CameraPos: {x,y,z}
CameraQuat: {x,y,z,w} with x^2+y^2+z^2+w^2=1
Given these two data structures, I have code to
- convert CameraPos and CameraQuat to a matrix for opengl modelview matrix
- rotate CameraQuat about a given vecotor by a given angle (3 dimensions for camera rotation)
- move CameraPos relative to a given vector (3 more dimensions)
I allow each of these six degrees of freedom 2 keys for a total of 12 key to control the camera.
You can avoid the lame-o c++ examples and all of their bloat.  20 Feb 2014, 23:42  gens

Joined: 18 Feb 2013
Posts: 161
gens
the example is good, i will surely use it as a reference

thing is i don't understand yet why quaterions work so i don't know their limitations/advantages
like when i come to physics where there will be lots of rotations and changes of same, i have to know what is going on
now i don't even know how to point at something using quaterions all functions are good to have
like many fps don't do roll on characters (maybe some on the camera but not things like bounding boxes, from what i know)
strategies... they many times don't even have a third dimension internally (sc2 has like fixed height plateaus (weird word that))

and ofc all code no libraries
other then 3 that i actually need (for window, for loading images and for GL ofc)

savagexr is one of the inspirations when planing
in that game teams can be 30v30 players and it still runs fine
simple physics, but you can swing around you with claws/swords and every hit is well timed over the network
it's a great game  21 Feb 2014, 04:47  tthsqe

Joined: 20 May 2009
Posts: 730
tthsqe
Quote:
thing is i don't understand yet why quaterions work so i don't know their limitations/advantages

A quaternion is a number of the form w + x I + y J + z K where {w,x,y,z} are real. Multiplication of a quaternion by real numbers is commutative. They are multiplied as in any ring with the equivalences
I*I = J*J = K*K = I*J*K = -1
With these rules, you can write down the four components of any product of quaternions.

Now, the useful thing is that to each unit quaternion (w + x I + y J + z K with w^2+x^2+y^2+z^2=1) we can associate the rotation matrix
Code:
```;     w^2+x^2-y^2-z^2    2 (x y-w z)        2 (w y+x z)
;     2 (x y+w z)        w^2-x^2+y^2-z^2    2 (y z-w x)
;     2 (x z-w y)        2 (w x+y z)        w^2-x^2-y^2+z^2    ```

As I mentioned before this gives a 2-to-1 covering of the rotation matrices. And matrix multiplication is compatible with quaternion multiplication, as you can check by tedious calculation.

To solve your 'look at' problem, lets assume that we are at a point {x,y,z} that is a distance of 1 away from the origin. What is quaternion associated with the matrix that will point us to the origin? It is simply
Code:
`(x*I + y*J + (z + 1)*K)/Sqrt[2 z + 2]    `

(you can also multiply this on the right by any quaternion of the form cos(t)+sin(t)*K to roll your camera)

You can verify this by computing the associated rotation matrix and finding out that the last column is {x,y,z} (assuming x^2+y^2+z^2=1), and this is exactly what my camera.inc says it needs to be. 21 Feb 2014, 08:03  gens

Joined: 18 Feb 2013
Posts: 161
gens
so i thought about it and slept on it

quaternion (i always get the name wrong) rotations is rotating around a normalized vector x,y,z by w radians ? 22 Feb 2014, 13:28  tthsqe

Joined: 20 May 2009
Posts: 730
tthsqe
gens,
almost.
The rotation matrix associated with the unit quaternion {w,x,y,z} is a rotation about the vector {x,y,z}, but the angle is 2*arccos(w). 22 Feb 2014, 18:23  gens

Joined: 18 Feb 2013
Posts: 161
gens
kind of makes sense now, il have to put some things on paper first

i'm more of a mechanics person then mathematics
from what i read the inventor of quaternions was also, so il look into hes writings later

thx 23 Feb 2014, 00:56  Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First  Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsCompiler InternalsIDE DevelopmentOS ConstructionNon-x86 architecturesHigh Level LanguagesProgramming Language DesignProjects and IdeasExamples and Tutorials Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou can attach files in this forumYou can download files in this forum