Camera in OpenGL

Camera class for OpenGL scene navigation.

When the topic of camera implementation in OpenGL is introduced, most of the resources I have come across opt for a first-person shooter (FPS) style system. This make sense for those who are interested in learning OpenGL for game applications, but from a UX standpoint it is a sub optimal scheme for scene navigation. The type of camera found in most 3D modeling software (Maya, Houdini, Cinema 4D, Blender, Rhino to name just a few) offers a more intuitive way to navigate around in 3D space. Since I'm more interested in applications for content creation, I have implemented this type of camera for use in my OpenGL projects.

The Code

The object expects as inputs two floats (I recommend using delta x and y cursor coordinates) and enables tumbling, tracking and dollying. Object outputs view and projection matrices for use in your shader.

You can find the .h and .cpp files here. Below is the .h file to give an idea of available methods:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once

#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>


class cameraEuler
{

public:
	cameraEuler(float width = 1280.0f, float height = 720.0f);


	void home();

	void tumble(float delta_xpos, float delta_ypos);
	void track(float delta_xpos, float delta_ypos);
	void dolly(float delta_xpos, float delta_ypos);
	void zoom(float delta_xpos, float delta_ypos);


	void setAspectRatio(float width, float height);

	glm::mat4 getPivotMatrix() const;
	glm::mat4 getViewMatrix() const;
	glm::mat4 getProjectionMatrix() const;

	~cameraEuler();


private:

	const float TUMBLE_SPEED = 0.005f;
	const float TRACK_SPEED = 0.005f;
	const float DOLLY_SPEED = 0.005f;
	const float ZOOM_SPEED = 0.05f;

	const float PITCH_CONSTRAINT_UPPER = 179.9f;
	const float PITCH_CONSTRAINT_LOWER = 0.1f;
	const glm::vec3 WORLDUP = glm::vec3(0.0f, 1.0f, 0.0f);

	const float MIN_FOV = 18.0f;
	const float MAX_FOV = 120.0f;

	float frontSwitch = 1.0f;


	// Camera params
	float pitch;
	float aspectRatio;
	float fov = 45.0f;

	glm::vec3 pivot;
	glm::vec3 position;

	glm::vec3 front;
	glm::vec3 up;
	glm::vec3 right;

	glm::mat4 view;
	glm::mat4 projection;

	void updateCamera();
};