PageRenderTime 22ms CodeModel.GetById 18ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/src/SceneGraph_SceneBase/Culler.cpp

http://github.com/Akranar/daguerreo
C++ | 79 lines | 40 code | 10 blank | 29 comment | 7 complexity | 5b7b5bfa32d3fbcf91b96919464d1f7e MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, LGPL-3.0, GPL-2.0
 1#include "Culler.h"
 2#include "VisibleSet.h"
 3#include "ExLib_RendererBase.h"
 4#include "ExLib_BoundingVolume.h"
 5
 6Culler::Culler()
 7: 
 8camera(0),
 9visible_set(new VisibleSet())
10{
11}
12
13Culler::~Culler()
14{
15	delete visible_set;
16}
17
18void Culler::SetFrustum()
19{
20	/*
21	const float * near_plane = camera->GetNearPlane();
22	const float * far_plane = camera->GetFarPlane();
23	const float * left_plane = camera->GetLeftPlane();
24	const float * bottom_plane = camera->GetBottomPlane();
25	const float * right_plane = camera->GetRightPlane();
26	const float * top_plane = camera->GetTopPlane();
27
28	for (unsigned int i = 0; i < 4; ++i)
29	{
30		frustum[0][i] = near_plane[i];
31		frustum[1][i] = far_plane[i];
32		frustum[2][i] = left_plane[i];
33		frustum[3][i] = bottom_plane[i];
34		frustum[4][i] = right_plane[i];
35		frustum[5][i] = top_plane[i];
36	}
37	*/
38}
39
40bool AABBInHalfSpace(const AABB & box, const float * plane)
41{
42	/*
43		Main Idea: 
44            -> Separating-axis test with only normal of the plane.
45	*/
46
47	float radius = box.GetExtent(0) * abs(plane[0])
48				+ box.GetExtent(1) * abs(plane[1])
49				+ box.GetExtent(2) * abs(plane[2]);
50	float signed_distance = box.GetCenter()[0] * plane[0]
51							+ box.GetCenter()[1] * plane[1]
52							+ box.GetCenter()[2] * plane[2] 
53							+ plane[3];
54	return signed_distance + radius >= 0; //Only test if box is in positive side of plane.
55}
56
57/*
58bool SphereInHalfSpace(const BoundingSphere & sphere, const Plane & plane)
59{
60	float signed_distance = plane.GetNormal().dot(sphere.GetCenter()) + plane.GetD();
61	return signed_distance + sphere.GetRadius() >= 0; //Only test if sphere is in positive side of plane.
62}
63*/
64
65bool Culler::IsVisible(const BoundingVolume * bound) const
66{
67	if (!bound) return false;
68	const AABB * box = static_cast<const AABB *>(bound);
69
70	if (!AABBInHalfSpace(*box, frustum[0])) return false;
71	if (!AABBInHalfSpace(*box, frustum[1])) return false;
72	if (!AABBInHalfSpace(*box, frustum[2])) return false;
73	if (!AABBInHalfSpace(*box, frustum[3])) return false;
74	if (!AABBInHalfSpace(*box, frustum[4])) return false;
75	if (!AABBInHalfSpace(*box, frustum[5])) return false;
76
77	return true;
78
79}