Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

3D Engine, Camera System

1.55/5 (6 votes)
14 Apr 2019MIT 2.7K  
3D Engine, Camera System

I just implemented a basic POV (Point of View) class to serve as a view camera into the world. Internally, it uses a point of origin, view direction, and up vector for its orientation. Still not sure how to implement it using quaternions… working on it though. Also, there’s an issue when looking straight up or down since the angle between direction and up vector approaches zero. Not sure how I’m going to deal with this yet.

Here it is in action:

pov.hpp:

#pragma once

#include "types.hpp"
#include "vector.hpp"
#include "point.hpp"
#include "transforms.hpp"

namespace engine
{
	class pov
	{
	public:
		pov(const point& origin = ORIGIN, const vector& direction = -UNIT_Z, 
                                  const vector& up = UNIT_Y)
		: m_origin{ origin }, m_direction{ direction }, m_up{ up }
		{
			m_direction.normalize();
			m_up.normalize();
		}

		void move(real forward, real size)
		{
			auto v = (m_up ^ m_direction).normal();
			m_origin += forward * m_direction + size * v;
		}

		void turn(real angle)
		{
			auto m = engine::rotate(angle, m_up);
			m_direction *= m;
		}

		void look(real angle)
		{
			auto size = (m_up ^ m_direction).normal();
			auto m = engine::rotate(angle, size);
			m_direction *= m;
		}

		void roll(real angle)
		{
			auto m = engine::rotate(angle, m_direction);
			m_up *= m;
		}

		matrix view_matrix() const
		{
			auto at = m_origin + m_direction;
			return look_at(m_origin, at, m_up);
		}

	private:
		point m_origin;
		vector m_direction;
		vector m_up;
	};
}

License

This article, along with any associated source code and files, is licensed under The MIT License