//class to model the view frustum
//based on a tutorial from http://www.lighthouse3d.com/

#pragma once

#include "common.hpp"

#define ANG2RAD (3.14159265358979323846/180.0)


class frustrum {
public:
	frustrum() {}
	virtual ~frustrum() {}

	//calculate near and far plane distance and dimension
	void set_perspective(float angle, float ratio, float nearD, float farD);
	//calculate planes from camera info
	void set_camera(const glm::vec3 eye, const glm::vec3 center, const glm::vec3 up);

	//culling functions
	bool point_in_frustrum(const glm::vec3& point);
	bool sphere_in_frustrum(const glm::vec3& center, const float radius) const;
	//plane point distance
	GLfloat plane_point_distance(const glm::vec3& plane_norm, const glm::vec3& plane_pt, const glm::vec3& point) const;

	//debug information
	void debug() const;

private:
	enum frustrum_side{
		TOP = 0, BOTTOM, LEFT,
		RIGHT, NEARP, FARP
	};

	GLfloat ratio, angle, nearD, farD;
	GLfloat near_dist, far_dist;
	glm::vec2 near_dim, far_dim; //near and far plane dimensions
	glm::vec3 plane_normal[6]; // normals of the 6 planes of the frustrum
	glm::vec3 plane_point[6];  //points of the 6 planes of the frustrum
	glm::vec3 current_point; //the current point to check against the frustrum
};