I've a very old project in openGL that used glRotate, glTranslate, and other glMatrix functions, and I'm updating it to the more modern approach of using matrices for calculations.
But I'm having some problems because I have very little to no experience with matrices (I always avoided it in my older project, I preferred using basic trigonometry because I found it to be easier), and I'd like to ask these questions:
I'm calculating, on each frame, this matrix:
mat4 mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
mat4 mView = glm::lookAt(
glm::vec3(_camera.posX, _camera.posY, _camera.posZ), //Camera Position
glm::vec3(_camera.posX + _camera.directionX, _camera.posY - _camera.directionY, _camera.posZ - _camera.directionZ), //Eye Position
glm::vec3(0, 1, 0) //Head Up
);
mat4 mModel = glm::mat4(1.0f);
mat4 mMVP = mProjection * mView * mModel;
I'd like to change my object's position, so I tried this:
mat4 mModel = glm::mat4(1, 0, 0, object->posX,
0, 1, 0, object->posY,
0, 0, 1, object->posZ,
0, 0, 0, 1);
Because I read that the last column's represents the x, y, z translating positions of the plane.
But I guess I'm mistaken, because with this my object seems "stuck" and doesn't move at all (and my navigation stops).
I thought of multiplying it by a vec3(posX, posY, posZ), but then wouldn't the result be a another vec3? I read that when multiplying matrices such as 4x4 and a 4x2, the result is a 4x2, so I don't know how I can calculate this. I guess rotations will be even more complicated...
Another question I have is, I had a targetX, targetY, targetZ for my camera object, and I did this:
glTranslatef(-_camera.targetX, -_camera.targetY, -_camera.targetZ); //go to the target
glRotatef(_camera.RotationX(), 1.0f, 0.0f, 0.0f); //rotateX
glRotatef(_camera.RotationY(), 0.0f, 1.0f, 0.0f); //rotateY
glTranslatef(-_camera.posX, -_camera.posY, -_camera.posZ); //to go camera position
//Loop to draw all objects, assuming "camera position" as the new identity
And this way I could control where I was looking to, and change the zoom or switch to first person by making the target = position.
With the matrices' calculations I posted above, I don't know how I can achieve this, since the camera's position I send to the mView should change according to where I'm looking, and the center target as well right?
Any help on how I can achieve this?
Any info on how can I get to understand matrices and it's relations to the world space is really appreciated, I'm reading some tutorials about it but it still feels very overwhelming, too much information that I can't relate to the world's coordinates.
Thanks!