Thread: Can't figure out cameras in OGL...

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071

    Can't figure out cameras in OGL...

    ok.....as much as iv'e tried, looking at tutorials, asking questions, and experementing, i can't get a first person camera working in OpenGL.

    if anyone can, give adivce, give examples, or even implement the camera for me (tho i don't really expect that to happen), i would be REALLY greatful, as iv'e been tryin to get this to work for well over a month.

    -psychopath

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well it would be easier to see what you are having problems with specifically. You say you experimented, so post up the code you have played around with and we will take a look at it. Us posting code would be the same as if you went and downloaded code from NeHe's Site.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Banned
    Join Date
    May 2004
    Posts
    129
    There are various ways to get this to work. The easiest way is to just calculate the pitch and yaw (rotation about the x axis and the y axis, respectively) angles. Then, calculate the position vector. Then, this is all you need to do:

    Code:
    void	R_GenerateViewMatrix(float	pitch, float yaw, float roll, Vector3 & translation)
    {
    	if(pitch)
    	{
    		NT_GL(glRotatef(-pitch,1,0,0));
    	}
    	if(yaw)
    	{
    		NT_GL(glRotatef(-yaw,0,1,0));
    	}
    	if(roll)
    	{
    		NT_GL(glRotatef(-roll,0,0,1));
    	}
    		NT_GL(glTranslatef(-translation.x,-translation.y,-translation.z));
    }
    The reason you rotate about the negative angles and translate in the negative direction is that your viewing plane stays stationary, and you rotate and translate the world around you in the opposite direction to give the illusion that you are moving.

    edit
    and, don't let the NT_GL prefix bother you...I'm working with my own implementation of OpenGL that I've (mostly) written, but trust me everything works the same way.

  4. #4
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    iv'e used code from many different tutorials, but still can't get it to work.
    ---
    my experimentations are as follows:

    variables:
    Code:
    float PositionX = 0.0;
    float PositionY = 0.8;
    float PositionZ = 5.0;
    float ViewX = 0.0;
    float ViewY = 0.8;
    float ViewZ = 0.0;
    float UpX = 0.0;
    float UpY = 1.0;
    float UpZ = 0.0;
    float CamSpeed = 0.1;
    float FarClipDist = 25.0;
    
    int SCREEN_WIDTH = 600;
    int SCREEN_HEIGHT = 518;
    POINT mousePos;
    int middleX = SCREEN_WIDTH  >> 1;				
    int middleY = SCREEN_HEIGHT >> 1;
    in the VK_UP event:
    Code:
    PositionZ = PositionZ + -CamSpeed;
    ViewZ = ViewZ + -CamSpeed;
    if (PositionZ < 0.2)
    {
    PositionZ = 0.2;
    }
    void RenderScene();
    in the VK_DOWN event:
    Code:
    PositionZ = PositionZ + CamSpeed;
    void RenderScene();
    in the VK_RIGHT event:
    Code:
    PositionX = PositionX + CamSpeed;
    ViewX = ViewX + CamSpeed;
    void RenderScene();
    in the VK_LEFT event:
    Code:
    PositionX = PositionX + -CamSpeed;
    ViewX = ViewX + -CamSpeed;
    void RenderScene();
    for mouse movement:
    Code:
    GetCursorPos(&mousePos);
    ViewX = mousePos.x / 50.0;
    ViewY = -mousePos.y / 50.0;
    the only problem with my method, is i can't figure out how to get the camera to move in the direction the camera is looking

    i will also try the code you posted vNvNation

    -psychopath

  5. #5
    Banned
    Join Date
    May 2004
    Posts
    129
    I personally don't feel like explaining this, just because it is relatively an elementary concept (but no worries, these same types of problems troubled everyone at once).

    Unless someone else here explains how to do it in detail, look for vector mathematics in a text book or online. Basically, you need to learn how to rotate vectors, which can be accomplished in 3 different ways(matrices, quaternions, or a rotation on an arbitrary plane). None of these things are trivial at first, so don't get too frustrated.

    Good luck.

  6. #6
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    yea....tho an elementary concept for most...i'm only 13 and am not supposed to even know what a vector is for another few years...lol...anyway....i'll probably figure it out somewhere along the way

    -psychopath

  7. #7
    Banned
    Join Date
    May 2004
    Posts
    129
    I completely agree, subsequently any progress you make is astounding

  8. #8
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    thanx!
    and i just thought of somthing.....with my method...would just increasing the viewZ work, as opposed to the PositionZ?
    Because the ViewX and ViewY tell it what direction to look, so then wouldn't the ViewZ be then right in front of where you are looking?

    just a thought

    -psychopath

  9. #9
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    anyone?

  10. #10
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    subsequently any progress you make is astounding


    would just increasing the viewZ work, as opposed to the PositionZ?
    try it out and see. Based on how you've explained your camera works, it seems that it should.

  11. #11
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    nope...diddn't work...not properly anyway.
    anyone have any other suggestions?

  12. #12
    Banned
    Join Date
    May 2004
    Posts
    129
    I will go through the process of doing this using gluLookAt, but I think that your knowledge of vector mathematics might not be good enough to get this to work. All this means is you may need to do a bit more academic work to really tackle this, which may in turn be a bit above your head. I absolutely am not trying to discourage you, I'm just stating a reality (you say you are 13).

    the first thing you need is the position. This is the easiest parameter to compute. In order to move your position X units in some direction, you must add a vector to your position that has a length of X units.

    In order to do this first part, you must first have a vector that has a length of X units that points in the direction you want to move in. When the direction you want to move in is just 'forward' or 'backwards' in stationary mode (when OpenGL is just looking down the negative Z axis into the screen) the vector you are adding to your position (known as the displacement vector) will look something like:

    Vector Displacement = Vector(0,0,-X);

    where -X is the number of units you want to move (remember, it is negative because when OpenGL starts out, by default you are looking down the negative Z axis).

    For right now, just tackle this moving back and forth into and out of the screen, because it gets a little bit more tricky when you start rotating the displacement vector (but, I will help you with that too when you get around to it).


    Okay, we are almost done with the first part. Once you have your correct position, you just pass it into the first 3 parameters of gluLookAt. Your code up to this point will look something like this (if you are using C++):

    Vector Displacement = Vector(0,0,-X);
    Vector Position += Displacement; //overloaded += operator

    gluLookAt(Position.x, Position.y, Position.z,x,x,x,x,x,x);

    Okay, now I can go on to talk about the next 3 parameters. The next 3 parameters define the point in 3D space that you are looking at. This point is equal to the position in 3D space of the camera plus the normalized unit vector that represents the view. In this case, for simplicity, you are looking in the same direction as Displacement. But, Displacement doesn't have a length of 1, its length is X (length is always the absolute value, even if all of the vector components are negative).

    If you don't know how to do this already, you compute the magnitude (length) of a vector the same way you would in 2D:

    float LengthIn3D = sqrt(Displacement.x ^2 + Displacement.y^2 + Displacement.z ^2);

    Then, to normalize the vector (meaning, to make sure it has a length of exactly 1 unit) you divide each component by the length;

    Displacement /= LengthIn3D; // /= operator in C++

    I suggest that you check to make sure LengthIn3D is not zero before doing this.

    I'm going to assume that you have a Normalize operator in your vector class which encapsulates that funcionality above.

    Okay, here is what your code should look like so far:

    Code:
    Vector Position = Vector(0,0,0); //start at the origin, note you won't see anything unless it is down the negative Z axis
    
    Vector Displacement = Vector(0,0,-X);
    Position += Position; //This moves the vector down the negative x axis (INTO the screen) by a length of X units
    
    Vector View = Displacement;
    View.Normalize(); //I showed this above
    
    Vector gluView = Position + View; //very important
    
    gluLookAt(Position.x, Position.y, Position.z, gluView.x, gluView.y, gluView.z,x,x,x);

    And, for the last three parameters, you are just going to use:

    0,1,0

    for right now, because you dont' absolutely need to fiddle with those just yet. All those do is define your up orientation. If you dont' do this when using gluLookAT, and if you look straight upwards, your entire scene will disappear, but for right now you are just going to be moving back and forth and up and down, but not looking straight up

    Your final version looks like this:
    gluLookAt(Position.x, Position.y, Position.z, gluView.x, gluView.y, gluView.z,0,1,0);
    Last edited by vNvNation; 07-19-2004 at 09:38 AM.

  13. #13
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    ok...thanks...it will take a while to get my head around it tho, but when/if i do i'll post

  14. #14
    Banned
    Join Date
    May 2004
    Posts
    129
    This is where there is no easy answer except to read what I have written, and try to think about the concepts. What I wrote should be basic enough for you to get a foot in the door of vector mathematics. Once you get used to this (which, I guarantee you will if you work at it), you can go on to more difficult things.

    Also, don't be too upset if you don't get any of this right away. There's nothing that says you must understand it in the first place, and you have your age working against you on this (but I still think you can get it).

    Good luck and ask any questions you have, even if you think they're stupid.

  15. #15
    Banned
    Join Date
    May 2004
    Posts
    129
    Just wondering, how are things going?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  3. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  4. Replies: 18
    Last Post: 12-05-2003, 12:06 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM