# 2D camera viewing volume

• 01-08-2011
zacs7
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
• 01-09-2011
GReaper
2D Camera??? So... your projection will be in 1D? :p
• 01-09-2011
VirtualAce
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.
• 01-11-2011
zacs7
Oh okay :-).

Thanks guys.
• 01-11-2011
smasherprog
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.

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 }```
• 01-11-2011
VirtualAce
Quote:

Projection * View * Translation * Scaling * Rotation.
Quote:

ModelViewProjection