I’m attempting to create a Camera class for a 3D OpenGL project. However I cannot figure out how to actually apply the camera to my scene. I have these Camera functions (amongst others):
void Camera::update(){
glm::vec3 direction(cos(_verticalAngle) * sin(_horizontalAngle), sin(_verticalAngle), cos(_verticalAngle) * cos(_horizontalAngle));
glm::vec3 right = glm::vec3(sin(_horizontalAngle - 3.14f/2.0f), 0, cos(_horizontalAngle - 3.14f/2.0f));
glm::vec3 up = glm::cross(right, direction);
_projectionMatrix = glm::perspective(_FoV, float(VIEWPORT_X) / float(VIEWPORT_Y), 0.1f, 250.0f);
_viewMatrix = glm::lookAt(_position, _position + direction, up);
}
glm::mat4 Camera::getProjectionMatrix(){
return _projectionMatrix;
}
glm::mat4 Camera::getViewMatrix(){
return _viewMatrix;
}
They were created from a tutorial, I’m not sure if they work though since I can’t test them. What I want to do is get OpenGL to use the view and projection matrices to simulate a camera. How exactly do I tell OpenGL to use those projection and view matrices, so that it properly simulates a camera separate from model’s transformations? I’m aware OpenGL will not accept glm matrices by default, but I have seen this type of thing in a few tutorials:
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix = getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
but glUniformMatrix4fv (which I think applies the camera transforms?) makes no sense to me. It always has something to do with shaders, which I have none of. I simply have a wireframe test mesh currently. Could someone provide me a code snippet for this problem?
I don’t know about using GLM, but I can help with the regular OpenGL part.
glUniformMatrix4fv updates a 4×4 uniform matrix at the location specified by MatrixID in a particular shader program.
I recommend working through Learning Modern 3D Graphics Programming, which is excellent as both a reference and guide.
For a discussion of how these uniforms are used within the GLSL shader program see:
Learning Modern 3D Graphics Programming – Chapter 3