

The next vector that we need is a right vector that represents the positive x-axis of the camera space. The name direction vector is not the best chosen name, since it is actually pointing in the reverse direction of what it is targeting. Glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget) If we switch the subtraction order around we now get a vector pointing towards the camera's positive z-axis: For the view matrix's coordinate system we want its z-axis to be positive and because by convention (in OpenGL) the camera points towards the negative z-axis we want to negate the direction vector. Remember that if we subtract two vectors from each other we get a vector that's the difference of these two vectors? Subtracting the camera position vector from the scene's origin vector thus results in the direction vector we want. For now we let the camera point to the origin of our scene: (0,0,0). The next vector required is the camera's direction e.g. Glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f) ĭon't forget that the positive z-axis is going through your screen towards you so if we want the camera to move backwards, we move along the positive z-axis. We set the camera at the same position we've set the camera in the previous chapter:

The camera position is a vector in world space that points to the camera's position.

A careful reader may notice that we're actually going to create a coordinate system with 3 perpendicular unit axes with the camera's position as the origin. To define a camera we need its position in world space, the direction it's looking at, a vector pointing to the right and a vector pointing upwards from the camera. When we're talking about camera/view space we're talking about all the vertex coordinates as seen from the camera's perspective as the origin of the scene: the view matrix transforms all the world coordinates into view coordinates that are relative to the camera's position and direction. We'll also discuss keyboard and mouse input and finish with a custom camera class. We will discuss a fly style camera that allows you to freely move around in a 3D scene. In this chapter we'll discuss how we can set up a camera in OpenGL. OpenGL by itself is not familiar with the concept of a camera, but we can try to simulate one by moving all objects in the scene in the reverse direction, giving the illusion that we are moving. In the previous chapter we discussed the view matrix and how we can use the view matrix to move around the scene (we moved backwards a little).
