Jump to content
  • Advertisement
Sign in to follow this  
Lode

OpenGL setting the opengl matrix

This topic is 4827 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

With gluLookAt, glOrtho, and other similar commands you can set the opengl matrix, but these commands don't allow you to give an arbitrary matrix. I have a 3x3 matrix and a position vector, and want to set the opengl matrix so that it simulatas a camera given by this 3x3 matrix and the position vector. How do I do this?

Share this post


Link to post
Share on other sites
Advertisement
I just looked into it, but I'm a bit confused about the different matrix modes. Which one should I set? (modelview or projection matrix)

Thanks!

Share this post


Link to post
Share on other sites
Alright, first things first: for OpenGL to apply a matrix transformation it needs to have a 4x4 matrix, so you'll have to do the conversion from your 3x3.

Since you want to create a camera simulation, you will be working directly with the modelview matrix, that is, GL_MODELVIEW. So, your code will look like this:

float matrix[16];

PopulateMatrix(&matrix);

glMatrixMode(GL_MODELVIEW);

glLoadMatrixf(matrix);

That'll load the matrix you have calculated as the new modelview matrix and update your view.

Share this post


Link to post
Share on other sites
If the screen is 1280x1024 pixels and I have given an orthonormal 3x3 camera matrix and a camera position, how to project objects properly on the screen with OpenGL?

Say the camera has an orthonormal 3x3 matrix and a position, turned into a 4x4 matrix. Then I want to transform the coordinates of a 3D object to camera space. Is this what the GL_MODELVIEW matrix does? I figured you have to take the inverse of the camera matrix because OpenGL assumes the camera to be always in the center, is this correct? And then I want to project the object from camera space to the 2D screen with it's 1280x1024 resolution. Which of the OpenGL matrices does that?

EDIT: tried to formulate it better

Share this post


Link to post
Share on other sites
Point2D = ProjectionMatrix * ModelViewMatrix * Point3D

With your 3x3 matrix and your position create the ModelViewMatrix then load via glLoadMatrix.
See also mesa 3D source code (glu source in project.c) for details.
I dont give you any algorithm because they could be wrong in your case: the idea is to copy the 3x3 matrix in the upper left part of the 4x4 and position in the form v = (x,y,z,1) in the last four elements.

Share this post


Link to post
Share on other sites
blizzard is right. I don't know if this will help you, but let me try to clarify an ackward decision in the OpenGL design. Traditionally the graphics pipeline uses three separate matrices, transforming a point from object coordinate system (vertices of an object are normally defined around a local coordinate system) to 2D screen coordinate system:

point_in_obj_coords * model_matrix -> point_in_world_coords
The model matrix is object specific. It transforms each vertex to world space. If your object is a car, the matrix will contain the position and orientation of the car in the world.

point_in_world_coords * view_matrix -> point_in_camera_coords
The view matrix depends on the position and orientation of your camera. It transforms points in world coordinate system to camera coordinate system. The matrix contains the translation and rotations of the camera but inversed.

point_in_camera_coords * projection_matrix -> point_in_screen_coords
The projection matrix depends on the properties of the camera (orthogonal or perspective projection) and defines the viewing volume. I'm not going to explain what's in it, glFrustum/glOrtho will do that for you. Or you can read it here.

OpenGL combines the model and view matrices in the modelview matrix. That's why the view matrix should always be the first thing to load on the modelview stack as the order in which the matrices are applied is crucial.

Tom

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!