Jump to content
  • Advertisement
Sign in to follow this  
openglpro

OpenGL OpenGL camera

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

How can I create an opengl camera?


Hi there,
nice name for asking that question :3

In general a camera is realized over a view-matrix.
This is a 4x4-matrix, which moves and rotates your scene against the camera.
So if your virtual camera rotates right, the view-matrix needs to rotate the vertices of your scene to the left.
If your virtual camera moves forward, the view-matrix needs to move the vertices of your scene backwards.

I would highly recommend to read about the basics of 3D-maths and computer-graphics matrices.
Have a look at the red book's chapter about viewing.

If you're using the deprecated fixed-function-pipeline (glBegin(), glVertex(), ... instead of shaders) you can change the view-matrix with the glTranslate- and glRotate-functions. Those functions change the matrix like you would expect it from a camera.

// Choose the Model-View-Matrix
glMatrixMode(GL_MODELVIEW);

// Rotate the camera 90 degrees around it's x-axis
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);

// Move the camera-position along it's new x-axis (after rotation)
glTranslatef(10.0f, 0.0f, 0.0f);

If you want to use shader, you need an extern matrix-class or a 16-float array representing that matrix, which you then load onto the gpu-memory.

Share this post


Link to post
Share on other sites

[quote name='openglpro' timestamp='1323561214' post='4892665']
How can I create an opengl camera?


Hi there,
nice name for asking that question :3

In general a camera is realized over a view-matrix.
This is a 4x4-matrix, which moves and rotates your scene against the camera.
So if your virtual camera rotates right, the view-matrix needs to rotate the vertices of your scene to the left.
If your virtual camera moves forward, the view-matrix needs to move the vertices of your scene backwards.

I would highly recommend to read about the basics of 3D-maths and computer-graphics matrices.
Have a look at the red book's chapter about viewing.

If you're using the deprecated fixed-function-pipeline (glBegin(), glVertex(), ... instead of shaders) you can change the view-matrix with the glTranslate- and glRotate-functions. Those functions change the matrix like you would expect it from a camera.

// Choose the Model-View-Matrix
glMatrixMode(GL_MODELVIEW);

// Rotate the camera 90 degrees around it's x-axis
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);

// Move the camera-position along it's new x-axis (after rotation)
glTranslatef(10.0f, 0.0f, 0.0f);

If you want to use shader, you need an extern matrix-class or a 16-float array representing that matrix, which you then load onto the gpu-memory.
[/quote]

If he is using old OpenGL he could far more easily use gluLookAt. (Which makes implementing a camera trivial)

http://www.dei.isep....uLookAt.3G.html has a description of how the function works aswell if you wish to replicate it using a modern OpenGL version.

Share this post


Link to post
Share on other sites

[quote name='Fnord42' timestamp='1323564855' post='4892677']
[quote name='openglpro' timestamp='1323561214' post='4892665']
How can I create an opengl camera?


Hi there,
nice name for asking that question :3

In general a camera is realized over a view-matrix.
This is a 4x4-matrix, which moves and rotates your scene against the camera.
So if your virtual camera rotates right, the view-matrix needs to rotate the vertices of your scene to the left.
If your virtual camera moves forward, the view-matrix needs to move the vertices of your scene backwards.

I would highly recommend to read about the basics of 3D-maths and computer-graphics matrices.
Have a look at the red book's chapter about viewing.

If you're using the deprecated fixed-function-pipeline (glBegin(), glVertex(), ... instead of shaders) you can change the view-matrix with the glTranslate- and glRotate-functions. Those functions change the matrix like you would expect it from a camera.

// Choose the Model-View-Matrix
glMatrixMode(GL_MODELVIEW);

// Rotate the camera 90 degrees around it's x-axis
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);

// Move the camera-position along it's new x-axis (after rotation)
glTranslatef(10.0f, 0.0f, 0.0f);

If you want to use shader, you need an extern matrix-class or a 16-float array representing that matrix, which you then load onto the gpu-memory.
[/quote]

If he is using old OpenGL he could far more easily use gluLookAt. (Which makes implementing a camera trivial)

http://www.dei.isep....uLookAt.3G.html has a description of how the function works aswell if you wish to replicate it using a modern OpenGL version.
[/quote]
Nice addition, completely forgot to mention gluLookAt.
But it doesn't always make camera-implementation more trivial.
If he goes for first-person mouse-look for example, it would be easier to just add the the difference of the mouseposition to the rotation around the y-axis.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!