Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Plane.h
- #pragma once
- #include "glm/glm.hpp"
- class Plane
- {
- public:
- float d;
- glm::vec3 normal;
- Plane() {
- this->normal = glm::vec3(0);
- this->d = 0;
- };
- void set3Points(glm::vec3 p0, glm::vec3 p1, glm::vec3 p2) {
- this->normal = glm::normalize(glm::cross(p1 - p0, p2 - p0));
- this->d = glm::dot(-this->normal, p0);
- };
- void setNormalAndPoint(glm::vec3 normal, glm::vec3 point) {
- this->normal = normal;
- this->d = glm::dot(-this->normal, point);
- };
- };
- //Frustum.h
- #include "glm/glm.hpp"
- #include "Plane.h"
- class Frustum
- {
- private:
- enum {
- TOP = 0, BOTTOM, LEFT,
- RIGHT, NEARPLANE, FARPLANE
- };
- static enum { OUTSIDE, INTERSECT, INSIDE };
- Plane plane[6];
- glm::vec3 nearTopLeft, nearTopRight, nearBottomLeft, nearBottomRight, farTopLeft, farTopRight, farBottomLeft, farBottomRight;
- float nearDistance, farDistance, ratio, viewAngle;
- float nearWidth, nearHeight, farWidth, farHeight;
- public:
- Frustum(float viewAngle, float ratio, float nearDistance, float farDistance);
- void setCamDef(glm::vec3 pos, glm::vec3 dir, glm::vec3 up, glm::vec3 right);
- bool pointInFrustum(glm::vec3 point);
- bool sphereInFrustum(glm::vec3 center, float radius);
- //int boxInFrustum(AABox &b);
- };
- //Frustum.cpp
- #include "Frustum.h"
- Frustum::Frustum(float viewAngle, float ratio, float nearDistance, float farDistance)
- {
- // store the information
- this->ratio = ratio;
- this->viewAngle = viewAngle;
- this->nearDistance = nearDistance;
- this->farDistance = farDistance;
- // compute width and height of the near and far plane sections
- float tangHolder = glm::tan(glm::radians(this->viewAngle) / 2);
- this->nearHeight = tangHolder * this->nearDistance;
- this->nearWidth = this->nearHeight * this->ratio;
- this->farHeight = tangHolder * this->farDistance;
- this->farWidth = this->farHeight * this->ratio;
- }
- void Frustum::setCamDef(glm::vec3 pos, glm::vec3 dir, glm::vec3 up, glm::vec3 right)
- {
- glm::vec3 vectorHolder, normalHolder;
- glm::vec3 nearCenter = pos + dir * this->nearDistance;
- glm::vec3 farCenter = pos + dir * this->farDistance;
- this->plane[NEARPLANE].setNormalAndPoint(-dir, nearCenter);
- this->plane[FARPLANE].setNormalAndPoint(dir, farCenter);
- vectorHolder = glm::normalize((nearCenter + up * this->nearHeight) - pos);
- normalHolder = glm::cross(right, vectorHolder);
- this->plane[TOP].setNormalAndPoint(normalHolder, pos);
- vectorHolder = glm::normalize((nearCenter - up * this->nearHeight) - pos);
- normalHolder = glm::cross(vectorHolder, right);
- this->plane[BOTTOM].setNormalAndPoint(normalHolder, pos);
- vectorHolder = glm::normalize((nearCenter - right * this->nearWidth) - pos);
- normalHolder = glm::cross(up, vectorHolder);
- this->plane[LEFT].setNormalAndPoint(normalHolder, pos);
- vectorHolder = glm::normalize((nearCenter + right * this->nearWidth) - pos);
- normalHolder = glm::cross(vectorHolder, up);
- this->plane[RIGHT].setNormalAndPoint(normalHolder, pos);
- }
- bool Frustum::pointInFrustum(glm::vec3 point)
- {
- if (0 >= glm::dot(this->plane[FARPLANE].normal, point) + this->plane[FARPLANE].d
- && 0 >= glm::dot(this->plane[NEARPLANE].normal, point) + this->plane[NEARPLANE].d
- && 0 >= glm::dot(this->plane[TOP].normal, point) + this->plane[TOP].d
- && 0 >= glm::dot(this->plane[BOTTOM].normal, point) + this->plane[BOTTOM].d
- && 0 >= glm::dot(this->plane[RIGHT].normal, point) + this->plane[RIGHT].d
- && 0 >= glm::dot(this->plane[LEFT].normal, point) + this->plane[LEFT].d)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool Frustum::sphereInFrustum(glm::vec3 center, float radius)
- {
- if (0 >= glm::dot(this->plane[FARPLANE].normal, center - this->plane[FARPLANE].normal * 0.5f) + this->plane[FARPLANE].d
- && 0 >= glm::dot(this->plane[NEARPLANE].normal, center) + this->plane[NEARPLANE].d
- && 0 >= glm::dot(this->plane[TOP].normal, center - this->plane[TOP].normal * radius * 1.5f) + this->plane[TOP].d
- && 0 >= glm::dot(this->plane[BOTTOM].normal, center) + this->plane[BOTTOM].d
- && 0 >= glm::dot(this->plane[RIGHT].normal, center) + this->plane[RIGHT].d
- && 0 >= glm::dot(this->plane[LEFT].normal, center) + this->plane[LEFT].d)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement