OpenGL Camera Concept

Started by
8 comments, last by Trienco 9 years, 10 months ago

Hi Everyone,

Does anybody know where I could find a good tutorial for understanding how to program a Camera in OpenGL.

Meaning that I don't want tthe world to move according to Input, but have the world be rendered in fonction how the camera Moves through the World.

Thank guys I really Appreciate it.

Cydriic

Advertisement

I don't know of any tutorials, but basically all you need to do is give your camera a transform matrix just like any other object in your world. When it comes time to render the scene, invert the Camera's matrix to get the view matrix. At the end of the day, your verts need to be in camera-relative space before doing the projection transformation. So, this last step of inverting the camera matrix to get the view matrix, and then multiplying your world-space verts with this view matrix, cannot be avoided. However, at least with this method, you can think in terms of the camera's position until the very end.

Hey Cydriic,

I'm currently working on a large OpenGL tutorial set and recently launched a prototype version of the first tutorial set, including a large section about an OpenGL FPS camera and its OpenGL transformations (note that these tutorials are only useful if you want to create a first person camera; its not suitable for space-simulation cameras for example, keep this in mind). The content is not finished yet, but it might give you some valuable insight in some way and also provides clear code samples with full source code provided. You can find the camera section here: http://www.learnopengl.com/#!Getting-started/Camera and you might need some theoretical concepts from the previous coordinate systems tutorial here: http://www.learnopengl.com/#!Getting-started/Coordinate-Systems.

I'm not sure if this is what you're looking for but it's worth a shot smile.png

Edit: added some extra emphasis about this being an FPS camera only (thanks to Trienco).

In old opengl camera is imo just GluLookAt (yet with perspective settings), you need just some functions to move it in space

In modern opengl i dont know it this is still applicable (as i wasnt using yet modern ogl) Is it applicable in modern opengl? need some there in m-ogl write gluLookAt by self?

ps. camera can be a little confusing (by 'a little' i mean much ;/ )

presently i personaly am a bit lost in this, when i mend it i probably could share some more advice but not now (as i am angry with some details still) )

glu functions are not opengl, they are glu (gl-utility if i am right). They wrap opengl for convenience.

With the above tutorial I highly suggest to stop reading at (or skipping) the chapter Euler Angles, unless first person is all you need. Mostly because otherwise it might be 1-2 weeks before a "I have gimbal lock and/or my camera rotation is acting funny"-thread. Also, the assumption that 0,1,0 is always a working approximation for "up" will fail if your camera can be upside down (flight simulator, space game, fancy camera movement in cutscenes).

The easiest way is to store a regular transformation matrix for your camera, treat it like any other object and invert it to get your view matrix:

//Quick inverse (transpose rotation part, multiply translation with transposed rotation)
Matrix Camera::getViewMatrix() 
{
    return Matrix(
        transformation[0], transformation[4], transformation[8], 0,
        transformation[1], transformation[5], transformation[9], 0,
        transformation[2], transformation[6], transformation[10], 0,
        -(transformation[0]*transformation[12] + transformation[1]*transformation[13] + transformation[2]*transformation[14]),
        -(transformation[4]*transformation[12] + transformation[5]*transformation[13] + transformation[6]*transformation[14]), 
        -(transformation[8]*transformation[12] + transformation[9]*transformation[13] + transformation[10]*transformation[14]), 1);
}
 
//Going the matrix route for translation is a bit overkill, but better demonstrates the difference between moving along local or global axes
void Camera::move(float x, float y, float z, bool global, float distance) 
{
    Matrix translation = Matrix::createTranslation(x,y,z);
    if (global)
        transformation = transformation * translation;
    else 
        transformation = translation * transformation;
}
 
void Camera::rotate(float deg, float x, float y, float z, bool global)
{
    Matrix rotation = Matrix::rotationFromAxisAngle(deg, x,y,z);
    if (global) 
        transformation = transformation * rotation;
    else 
        transformation = rotation * transformation;
}

Edit: well, thank the forum gods for deleting everything after the code block when editing.

If whatever matrix library doesn't let you access by index (or has a different memory layout):
0-3: x-axis aka "camera right" (x,y,z,0)
4-7: y-axis aka "camera up" (x,y,z,0)
8-11: z-axis aka "camera forward" (x,y,z,0) (might be backward, depending on handed-ness)
12-15: origin aka "camera position" (x,y,z,1)

If you need zoom, this goes into the projection matrix (simply reduce the fov). Zooming is NOT scaling and NOT "moving closer" (or any other weird thing that causes clipping errors when zooming "too much").
f@dzhttp://festini.device-zero.de

With the above tutorial I highly suggest to stop reading at (or skipping) the chapter Euler Angles, unless first person is all you need. Mostly because otherwise it might be 1-2 weeks before a "I have gimbal lock and/or my camera rotation is acting funny"-thread. Also, the assumption that 0,1,0 is always a working approximation for "up" will fail if your camera can be upside down (flight simulator, space game, fancy camera movement in cutscenes).

Hey Trienco,

You're right and make a good point about the tutorials only explaining the concept of a first person camera. I probably should have made it more clear in my post that this explains a first person camera only. I'll be sure to add an extra section to the camera tutorial later explaining its limitations to make things more clear, thanks.

Hey Trienco,
You're right and make a good point about the tutorials only explaining the concept of a first person camera. I probably should have made it more clear in my post that this explains a first person camera only. I'll be sure to add an extra section to the camera tutorial later explaining its limitations to make things more clear, thanks.


Nah, your tutorial clearly states it's for an FPS style camera (one of the rare cases where even Euler Angles are just fine, since you'll always rotate in a fixed order). I just pointed out the limitations since the OP didn't specify any particular requirements and I wanted to prevent the usual cycle of using Euler angles -> gimbal lock -> "quaternions are the only way" -> thread asking for help with quaternions -> never realizing that quaternions aren't magic and can't do anything matrices can as well -> continuing the cycle of preaching about how quaternions are the only way to do proper rotations.

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

Hehe yeah oké :) thanks for clarifying. I'll make sure to add some extra statements nevertheless since it's a good insight that an FPS camera is not an all-round camera system with eular angles.

Hehe yeah oké smile.png thanks for clarifying. I'll make sure to add some extra statements nevertheless since it's a good insight that an FPS camera is not an all-round camera system with eular angles.

Well, I guess it would work for third person as well. Pretty much any situation where your side to side rotation is always around the global y-axis should be safe and the look-at approach is fine if the rotation around x never goes to or beyond +/-90°.

Btw. there is one downside to storing the orientation as a matrix (or quaternion): floats suck. After a bunch of rotations, the vectors that should be will be no longer normalized and in the case of a matrix, the rotation part will be no long orthogonal on top of it. So every once in a while, the matrix will need to be re-orthonormalized (basically 2 cross products and 3 normalizations. With a quat, that should be only a single normalization.

While this sounds like a great advantage:

-it only needs to be done every once in a while.

-even if done once per frame, it's nothing compared to all the other things going on

-unless your pipeline can natively use quats, you'll still have to convert them to a matrix every frame (so you don't really win anything)

-if you need info from the camera (like view direction for bill boarding), simply reading the 3rd column of the matrix is easier than having to calculate it by transforming 0,0,1 by your stored quat

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

This topic is closed to new replies.

Advertisement