Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C++
Technical Blog

3D Engine, Camera System

Rate me:
Please Sign up or sign in to vote.
1.55/5 (6 votes)
14 Apr 2019MIT 2.6K   1  
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;
	};
}
This article was originally posted at https://vorbrodt.blog/2019/04/13/3d-engine-camera-system

License

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


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --