I´ve implemented zooming of orthographic camera (just scaling it with its speed value):
viewMatrix = glm::scale(viewMatrix, glm::vec3(_cameraScale, _cameraScale, 1.0f));
And then applied these transformations (also translation by camera position) to the destination point (actually a cursor position vector) so I got its world positions:
void Camera::applyTransformationsToPoint(glm::vec3& pos) {
pos += _cameraPosition;
pos /= _cameraScale;
}
It worked fine, i was getting the right world position with diferent scales. However, this was done with normal scaling - to the origin of the matrix so basically when i scaled the matrix the whole camera moved to its origin and it looked a little bit weird. SO i have decided to imlement zoom to the middle of the screen feature. So basically what I do is (in pseudocode):
translate(screenWidth/2,screenHeight/2);
scaleTranslatedMatrix();
translate(-screenWidth/2,-screenHeight/2);
It works fine but the code above for transformation of screen coordinations to the world is now not working properly(im not getting the right world positions), any suggestions how to correct it? Thanks.
Edit1: This is how I apply the scaling (if anyone wondered):
void Camera::zoomOut(float dt) {
_cameraScale -= _scaleSpeed * dt;
if (_cameraScale < 0.4f) {
_cameraScale = 0.4f;
}
}
According to the first answer edit: As I have 4x4 matrix and using a vec3 for my point representation, i have to use temporary vec4 to perform the multiplication, however, it is not working correctly (even without scaling and just translating the view matrix) - for inverse matrix im using GLM library.
glm::vec3 Camera::applyTransformationsToPoint(glm::vec3 pos) {
glm::vec4 tmpVec(pos.x, pos.y, pos.z, 1.0f);
tmpVec = tmpVec * glm::inverse(viewMatrix);
return glm::vec3(tmpVec.x,tmpVec.y,tmpVec.z);
}