Advertisement

Camera

Started by April 12, 2003 09:22 PM
8 comments, last by TheProx 21 years, 10 months ago
I am working on a demo, and it seems that I will need somesort of advanced camera model. Something or someway to change the aspect ratio and to move better than just simply "moving the world," As its not very effient. Does anyone have any ideas??? Out - The Prox
NARF - Pinky and the Brain
What do you mean "moving the world"?

Any real game or engine does rendering by calculating the world-to-eye transform, and then combining that with each object''s model-to-world transform to generate the model-view transform for each object.

Also, the field-of-view is set up in your perspective transform, which you can set up for pretty much no cost once per frame, at the start of the frame.

This is easiest done by representing each model and the camera with a postion and orientation. Then, the modelview transform M for object O and camera C is:

M.pos = O.pos-C.pos;
M.ori = O.ori-C.ori;

This is especially simple to write if you''re using quaternions. Once you have M.pos and M.ori, plug these two into the quaternion+point -> matrix conversion function, and call LoadMatrix() into your modelview transform, and render the model.

There are optimizations you can make for special kinds of geometry, where particle systems, portals, and other systems will intrude themselves into this simple model, but that''s basically how you do it.
Advertisement
That actually made sense, thank you very much! but I am assuming O and M are 3D vertices of the object or are they the center?

Out - The Prox
NARF - Pinky and the Brain
everything in GL is done by matrices.
a 4x4 matrix is 16 floats in a 4x4 sized grid (ie, in a 4x4 matrix)

in the simplest form, what a matrix does is when a vector is multiplied by a matrix, the exact same effect will occur no matter the vector. This effect may be rotating by a certain number of radians, scaling, or offsetting.
the video card multiplies vertices by the modelview matrix for you, so it does this instantly, so you have no need to worry about moving or rotataing your vertices 'by hand'...

You can also multiply multiple matrices themselves to mix more effects together into a single matrix...
this way you can have a matrix representing the camera position, a matrix representing the position of an object, and do the following:

this is a simple scene graph. It is a very powerful way to represent a game world.


glLoadMatrixf(camera)

for (each object)
{

glPushMatirx() - saves the current matrix (camera) to the top of a stack

glMultMatrixf(objectMatrix)

glDrawElements(...)

glPopMatrix() - return to the matrix on the top of the stack, ie the camera in this case

}


the camera matrix is the tricky one for people to understand though.
it's easy to understand an objects matrix... it usually would rotate some amount, and translate to the objects position. (the idea being that your vertices are so that 0,0,0 is the centre of the model, centre of rotation, etc)

the camera on the other hand has to do things as an inverse.

say your camera is at the position (10,14,-2). looking in the direction 0,0,1,
what you want is that an object drawn at (10,14,0) will appear slightly in front of you.. ie, at (0,0,2). This is where the opposite comes in, in order to do that, you need to subtract the cameras position, not add it. ie, (10,14,0)-(10,14,-2) = (0,0,2).

the same is true for rotations, for which there is a nice trick, that being if you don't scale your matrix, you can simply transpose it and you have the inverse.

so, to set a camera, have a matrix, ie:

Matrix camera;

camera.rotate(....);
camera.translate( 10 * -1 , 14 * -1 , -2 * -1);

camera.transpose();
(note only transpose the top left 3x3 block of the matrix)

glLoadMatrixf(camera);

there, you have a camera.

for info on things like transposing a matrix, read up - highly reccommend you go through this thougroughly.

| - Project-X - my mega project.. getting warmer - | - adDeath - an ad blocker I made - | - email me - |

[edited by - RipTorn on April 12, 2003 11:08:43 PM]
thnx a lot riptorn ... from what i see, you know a lot about this kinda stuff, even making your own engine with a lot of planning. about how far are you into actually coding it?

Out - The Prox
NARF - Pinky and the Brain
quote:
Original post by TheProx about how far are you into actually coding it?


some of the code in my engine is over 1 and a half years old... so it''s been one of those projects that just keeps evolving (although most of the code is less than 8 months old). as for how complete it is, something like this is never complete so I''d dare not say as I''d almost certainly be wrong.



| - Project-X - my mega project.. getting warmer - | - adDeath - an ad blocker I made - | - email me - |
Advertisement
ALRIGHT! I got my cam to work, just now, i am wondering(after reading tht nifty matrix-faq) is the method of using the rotation matrix suspectable to gimble-lock?

Out - The Prox
NARF - Pinky and the Brain
if you store the matrix in memory, and apply small delta rotations to it, then there won''t be any problems at all. it''s when you try and store the angles, then rotate to them every frame, that you get problems.
there are some issues if you are using large delta rotations, but for most cases you won''t do this, so it should be fine.
You can use quats if you really want to, but it isn''t vital.

| - Project-X - my mega project.. getting warmer - | - adDeath - an ad blocker I made - | - email me - |
btw. I did make a small mistake in my first larger post..
when it comes to converting a standard matrix into a 'camera' matrix...
I said to transpose (or invert) the 3x3 portion of the matrix, and then negate the positional components.. this isn't actually correct...

the inversion of the top left 3x3 portion of the matrix is still correct, but the positional bit isn't...

instead of negating, you have to replace them by the negation multiplied by the inverted 3x3 matrix... this is a small, but significatnt difference... and is best done by a function...

ie: (this is modified from my own code, so I guarentee it works)


    bool Matrix::cameraInvert(){	if (!invert3x3())  // or transpose if the matrix hasn't been scaled		return false;	Vector3 v(matrix[12],matrix[13],matrix[14]);	matrix[12]=matrix[13]=matrix[14]=0;	translate(v*-1);	return true;};     where...void Matrix::translate(Vector3 & v){	matrix[12]+=v.x*matrix[0]+		v.y*matrix[4]+		v.z*matrix[8];	matrix[13]+=v.x*matrix[1]+		v.y*matrix[5]+		v.z*matrix[9];	matrix[14]+=v.x*matrix[2]+		v.y*matrix[6]+		v.z*matrix[10];};    



| - Project-X - my mega project.. getting warmer - | - adDeath - an ad blocker I made - | - email me - |

[edited by - RipTorn on April 15, 2003 10:54:06 PM]
Thanks a lot RipTorn, I never got to see the ''messed up'' way anyway... i seem to have a bug somewhere in my matrix code...some kind of loop that keeps going until the stack is full...o well, i think i fixed it, and then this camera fix, thank God I didnt have to deal with this. If it would just move around unexpectadly...i woulda flipped... thanks a LOT!

Out - The Prox
NARF - Pinky and the Brain

This topic is closed to new replies.

Advertisement