Thread: 2D camera viewing volume

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    2D camera viewing volume

    Hi Guys,

    I'm working on implementing a 2D camera in OpenGL 3.0. I'm currently stuck with determining the "viewing volume" of the camera, so I can perform culling on world coordinates that are outside the camera.

    The final matrix used to process verticies is calculated manually by:
    Code:
    Matrix2D ModelViewProjection = ModelMatrix * ViewMatrix * ProjectionMatrix;
    The ModelViewProjection matrix is then passed to the GLSL vertex shaders.

    The ViewMatrix and ProjectionMatrix both come from the camera.

    When the camera is created, ViewMatrix is loaded to the identity matrix and the ProjectionMatrix is loaded with an orthographic projection matrix from 0 to viewportWidth for x, and 0 to viewportHeight for y. The ViewMatrix is then updated to "move the world" based on camera movements, negative translation, negative rotation and then scaling (in that order). The translations of the camera are kept in the camera class (so I have the rotation, translation, etc) -- these don't need to be extracted from the matrix.

    My question is, how do I determine if a given world coordinate is visible by the camera? (i.e. it should be drawn)? How do I convert the "camera viewing volume" to world coordinates, it's not the inverse of the ViewMatrix? If I can do that, I can simply check if the given world coordinate is in the camera's "world coordinate viewing volume".

    For reference, calculating the ViewMatrix:
    Code:
    void Camera::UpdateViewMatrix()
    {
       myViewMatrix.LoadIdentity();
    
       // we're really moving the 'world', hence negative
       myViewMatrix.Translate(-myPosition.GetX(), -myPosition.GetY());
    
       myViewMatrix.Rotate(-myRotation);
    
       myViewMatrix.Scale(myZoomFactor, myZoomFactor);
    }
    And calculating the ModelViewProjection matrix that is passed to the GL shaders:
    Code:
    void Camera::CalculateModelViewProjectionMatrix(Matrix2D & aModelMatrix) const
    {
       // model -> eye
       aWorldMatrix.Multiply(GetViewMatrix());
    
       // eye -> clip
       aWorldMatrix.Multiply(GetProjectionMatrix());
    }
    Thanks for any input,
    Zac
    Last edited by zacs7; 01-08-2011 at 08:52 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    2D Camera??? So... your projection will be in 1D?
    Devoted my life to programming...

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There is no frustum in 2D. The 'frustum' is whatever screen coordinates lie within the screen. Usually 2D systems are tile based and you have an algorithm that computes where the player is in the tile map and it draws the surrounding tiles according to that.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Oh okay :-).

    Thanks guys.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    Are you sure that you are using the correct order of operation on your matrix'? Open gl uses a left handed coordinate system, so you have to do your matrix multiplication as Projection * View * Translation * Scaling * Rotation.


    But, if it works, then keep going.

    Onto your question.

    There are many ways.. The first one that comes to mind is you can transform the vertex by the ModelViewProjection, then make sure to divide the vertex by its w component and then make sure all the components of the vertex are greater than or equal to -1 AND less than or equal to 1. If its true, then the vertex is within the cameras viewing bounds;

    Code:
    bool InCamerasBounds(Vertex testpoint){
    
    testpoint*=ModelViewProjection;// transform the point
    testpoint.x/=testpoint.w;
    testpoint.y/=testpoint.w;
    testpoint.z/=testpoint.w;
    if((testpoint.x <-1.0) | (testpoint.x >1)) return false;// outside the frustum
    if((testpoint.y <-1.0) | (testpoint.y >1)) return false;// outside the frustum
    // the z tests for 0 and positive 1 
    if((testpoint.z <0.0) | (testpoint.z >1)) return false;// outside the frustum
    return true;// inside the frustum
    }

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Projection * View * Translation * Scaling * Rotation.
    ModelViewProjection
    These statements are contradictory.

    http://www.cs.cmu.edu/afs/cs/academi.../03/lec03a.pdf
    Last edited by VirtualAce; 01-11-2011 at 09:51 PM.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    I made a typo when I said left handed, I meant to say Opengl uses a right handed coordinate system.

    I realize the name of the variable does not fit the right handed system, which is why I asked the question.

    The link you posted supported my post, thanks for the backup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Camera problem
    By VirtualAce in forum Game Programming
    Replies: 1
    Last Post: 08-14-2010, 01:23 PM
  2. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. sizing problem
    By beene in forum Game Programming
    Replies: 5
    Last Post: 11-14-2006, 03:38 AM
  5. Camera rotation/movement in 3D world
    By tegwin in forum Game Programming
    Replies: 11
    Last Post: 01-24-2003, 01:43 PM