How to fix camera view to a object in OpenGL?

Started by
5 comments, last by AlexNaabal 6 years, 7 months ago

Plz forgive me about my poor english.

I'm new to OpenGL dev and this forums.  I followed the OpenGL tutorial from learnopengl, after finished the camera and model loading part, I wonder if I can do more about camera. 

I try to code a camera behavior that fix the view to a model, be more clear, like some games that you can fix the view to the enemy object, and while you walk around the view of camera still stick to the enemy object.  ( plz forgive my poor english )

I don't know the proper way to do that. My first thought is that If I need to bind camera view to an object, I need to know the location in the world coordinate of that object, then I can move the camera to see it.

I tried using raycasting picking to pick the object I loaded, but it is just that, I don't know what to do next. 

Could you guys tell me the way to do so? Some links of similar staff are great as well. Thanks.

Advertisement

You will need a view matrix created with a LookAt function to indicate the position and the angle of the camera. You can use a math API that provides that or roll your own. There are some good APIs that I know of that provide that:

  • GLM (C++)
  • MonoGame (C#): see files Vector2.cs, Vector3.cs, Vector4.cs, Quaternion.cs and Matrix.cs
  • LinMath (C)
  • If you make your own math API, use the MonoGame or LinMath as example. It's more of a copy-paste task.

The LookAt matrix is usually made using a function like this:


	Matrix CreateLookAtMatrix(Vector3 camera_pos, Vector3 target, Vector3 up)
	

Then you can send the matrix (which is a 4x4 matrix) to your shader in the form of a float array (4x4 matrix has 16 elements).

In your shader, you multiply the view matrix (that we made with the LookAt function) with your vertex.

Spoiler

 

Thanks ferreiradaselvaI had already implemented some basic camera function. I'm sorry I forgot to mention that I'm using C++, GLFW and OpenGL. I can move around in FPS style camera, or use mouse dragging the camera. My question is how to " have the camera keep tracking the object ", or maybe " Let my eye( the camera ) looking at the object while walking around " more clearly.

Still appreciate your reply.

I'm pretty sure you could do something like this


//In your update loop
float cameraDistance = 50.0f; //Some value that is within your nearZ / farZ
vec3 cameraPosition(playerPosition.x, playerPosition.y, playerPosition.z + cameraDistance);
camera.LookAt(cameraPosition, playerPosition, cameraUp);

Then as long as your camera gets passed to your shader you should be ok

You will still need a view matrix. It can be generated with LookAt() or one of the other rotation matrices:

If you want a 3rd person camera, use the LookAt() and set the 3D vector target with the same value of the player position. And the camera_pos with a value that is the player position minus an offset value to keep the camera distant.

If you want a 1st person camera, you set the 3D vector camera_pos with the same value of the player position, however, the target is a bit more complicated, you can create a matrix that is the multiplication of two rotation matrices (rotation at axis Z and at axis X):

  • axis Z is the direction of the player
  • axis X is the axis that will determine if the player is looking up or down.

Then you send that multiplied matrix to your shader.

1 hour ago, noodleBowl said:

I'm pretty sure you could do something like this



//In your update loop
float cameraDistance = 50.0f; //Some value that is within your nearZ / farZ
vec3 cameraPosition(playerPosition.x, playerPosition.y, playerPosition.z + cameraDistance);
camera.LookAt(cameraPosition, playerPosition, cameraUp);

Then as long as your camera gets passed to your shader you should be ok

1 hour ago, ferreiradaselva said:

You will still need a view matrix. It can be generated with LookAt() or one of the other rotation matrices:

If you want a 3rd person camera, use the LookAt() and set the 3D vector target with the same value of the player position. And the camera_pos with a value that is the player position minus an offset value to keep the camera distant.

If you want a 1st person camera, you set the 3D vector camera_pos with the same value of the player position, however, the target is a bit more complicated, you can create a matrix that is the multiplication of two rotation matrices (rotation at axis Z and at axis X):

  • axis Z is the direction of the player
  • axis X is the axis that will determine if the player is looking up or down.

Then you send that multiplied matrix to your shader.

Thanks noodleBowl and ferreiradaselva.  So the basic idea is update a lookAt( changing_cameraPos, fixed_objectPos, UpVector) for each frame? I wrote a demo to test it and it works, by setting the fixed_objectPos as (0,0,0), and use keyboard input to update cameraPos. 

And I find a word that might describe my question more clearly. It is the strafing. I try to code a circle strafing-ish movement.

So my final problem is about " get the 'fixed_objectPos' for lookAt function". I tried color picking and raycasting picking, but still confusing. Where I can read more about " the way to retrieve object position "? I've googled, and many answer leads me to picking. 

 

This topic is closed to new replies.

Advertisement