Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 330 vs 331 for /trunk/Source Code/Rendering/Camera/Frustum.cpp

Diff revisions: vs.
  @@ -6,21 +6,25 @@
6 6
7 7 namespace Divide {
8 8
9 - Frustum::Frustum(Camera& parentCamera) : _pointsDirty(true), _parentCamera(parentCamera)
9 + Frustum::Frustum(Camera& parentCamera) : _pointsDirty(true),
10 + _parentCamera(parentCamera)
10 11 {
11 12 }
12 13
13 14 Frustum::FrustCollision Frustum::ContainsPoint(const vec3<F32>& point) const {
14 - for (const Plane<F32>& frustumPlane : _frustumPlanes)
15 - if (frustumPlane.classifyPoint(point) != Plane<F32>::POSITIVE_SIDE)
15 + for (const Plane<F32>& frustumPlane : _frustumPlanes) {
16 + if (frustumPlane.classifyPoint(point) != Plane<F32>::POSITIVE_SIDE) {
16 17 return FRUSTUM_OUT;
17 -
18 - return FRUSTUM_IN;
18 + }
19 + }
20 +
21 + return FRUSTUM_IN;
19 22 }
20 23
21 24 Frustum::FrustCollision Frustum::ContainsSphere(const vec3<F32>& center, F32 radius) const {
22 25 F32 distance = 0.0f;
23 - for (const Plane<F32>& frustumPlane : _frustumPlanes){
26 +
27 + for (const Plane<F32>& frustumPlane : _frustumPlanes) {
24 28 distance = frustumPlane.getDistance(center);
25 29
26 30 if (distance < -radius) {
  @@ -40,33 +44,37 @@
40 44
41 45 I32 iPtIn = 1, iTotalIn = 0, iInCount = 8;
42 46
43 - for (const Plane<F32>& frustumPlane : _frustumPlanes){
47 + for (const Plane<F32>& frustumPlane : _frustumPlanes) {
44 48 iInCount = 8;
45 49 iPtIn = 1;
46 50
47 - for(U8 c = 0; c < 8; ++c) {
51 + for (U8 c = 0; c < 8; ++c) {
48 52 if (frustumPlane.classifyPoint(boxCorners[c]) == Plane<F32>::NEGATIVE_SIDE) {
49 53 iPtIn = 0;
50 54 iInCount--;
51 55 }
52 56 }
53 57
54 - if(iInCount == 0) return FRUSTUM_OUT;
58 + if (iInCount == 0) {
59 + return FRUSTUM_OUT;
60 + }
61 +
55 62 iTotalIn += iPtIn;
56 63 }
57 64
58 65 return iTotalIn == 6 ? FRUSTUM_IN : FRUSTUM_INTERSECT;
59 66 }
60 67
61 - void Frustum::Extract(){
68 + void Frustum::Extract() {
69 +
62 70 _viewProjectionMatrixCache.set(GFX_DEVICE.getMatrix(VIEW_PROJECTION_MATRIX));
63 71
64 - if (_viewProjectionMatrixCacheOld != _viewProjectionMatrixCache) {
65 - _viewProjectionMatrixCacheOld.set(_viewProjectionMatrixCache);
66 - } else {
67 - return;
72 + if (_viewProjectionMatrixCacheOld == _viewProjectionMatrixCache) {
73 + return;
68 74 }
69 75
76 + _viewProjectionMatrixCacheOld.set(_viewProjectionMatrixCache);
77 +
70 78 Plane<F32>& rightPlane = _frustumPlanes[0];
71 79 Plane<F32>& leftPlane = _frustumPlanes[1];
72 80 Plane<F32>& bottomPlane = _frustumPlanes[2];
  @@ -75,25 +83,62 @@
75 83 Plane<F32>& nearPlane = _frustumPlanes[5];
76 84
77 85 const F32* mat = &_viewProjectionMatrixCache.mat[0];
78 - rightPlane.set( mat[3] - mat[0], mat[7] - mat[4], mat[11] - mat[ 8], mat[15] - mat[12]); rightPlane.normalize();
79 - leftPlane.set( mat[3] + mat[0], mat[7] + mat[4], mat[11] + mat[ 8], mat[15] + mat[12]); leftPlane.normalize();
80 - bottomPlane.set(mat[3] + mat[1], mat[7] + mat[5], mat[11] + mat[ 9], mat[15] + mat[13]); bottomPlane.normalize();
81 - topPlane.set( mat[3] - mat[1], mat[7] - mat[5], mat[11] - mat[ 9], mat[15] - mat[13]); topPlane.normalize();
82 - farPlane.set( mat[3] - mat[2], mat[7] - mat[6], mat[11] - mat[10], mat[15] - mat[14]); farPlane.normalize();
83 - nearPlane.set( mat[3] + mat[2], mat[7] + mat[6], mat[11] + mat[10], mat[15] + mat[14]); nearPlane.normalize();
86 +
87 + rightPlane.set(mat[ 3] - mat[ 0],
88 + mat[ 7] - mat[ 4],
89 + mat[11] - mat[ 8],
90 + mat[15] - mat[12]);
91 + rightPlane.normalize();
92 +
93 + leftPlane.set(mat[ 3] + mat[ 0],
94 + mat[ 7] + mat[ 4],
95 + mat[11] + mat[ 8],
96 + mat[15] + mat[12]);
97 + leftPlane.normalize();
98 +
99 + bottomPlane.set(mat[ 3] + mat[ 1],
100 + mat[ 7] + mat[ 5],
101 + mat[11] + mat[ 9],
102 + mat[15] + mat[13]);
103 + bottomPlane.normalize();
104 +
105 + topPlane.set(mat[ 3] - mat[ 1],
106 + mat[ 7] - mat[ 5],
107 + mat[11] - mat[ 9],
108 + mat[15] - mat[13]);
109 + topPlane.normalize();
110 +
111 + farPlane.set(mat[ 3] - mat[ 2],
112 + mat[ 7] - mat[ 6],
113 + mat[11] - mat[10],
114 + mat[15] - mat[14]);
115 + farPlane.normalize();
116 +
117 + nearPlane.set(mat[ 3] + mat[ 2],
118 + mat[ 7] + mat[ 6],
119 + mat[11] + mat[10],
120 + mat[15] + mat[14]);
121 + nearPlane.normalize();
122 +
84 123
85 124 _pointsDirty = true;
86 125 }
87 126
88 - void Frustum::intersectionPoint(const Plane<F32> & a, const Plane<F32> & b, const Plane<F32> & c, vec3<F32>& outResult){
127 + void Frustum::intersectionPoint(const Plane<F32> & a,
128 + const Plane<F32> & b,
129 + const Plane<F32> & c,
130 + vec3<F32>& outResult) {
131 +
89 132 outResult.set((a.getDistance() * (cross(b.getNormal(), c.getNormal()))) +
90 133 (b.getDistance() * (cross(c.getNormal(), a.getNormal()))) +
91 - (c.getDistance() * (cross(a.getNormal(), b.getNormal()))) / -dot(a.getNormal(), cross(b.getNormal(), c.getNormal())));
134 + (c.getDistance() * (cross(a.getNormal(), b.getNormal()))) /
135 + -dot(a.getNormal(), cross(b.getNormal(), c.getNormal())));
92 136 }
93 137
94 - void Frustum::updatePoints(){
95 - if (!_pointsDirty)
138 + void Frustum::updatePoints() {
139 + if (!_pointsDirty) {
96 140 return;
141 + }
97 142
98 143 const Plane<F32>& rightPlane = _frustumPlanes[0];
99 144 const Plane<F32>& leftPlane = _frustumPlanes[1];
  @@ -114,25 +159,28 @@
114 159 _pointsDirty = false;
115 160 }
116 161
117 - // get the frustum corners in WorldSpace. cornerWS must be a vector with at least 8 allocated slots
162 + // Get the frustum corners in WorldSpace. cornerWS must be a vector with at least 8 allocated slots
118 163 void Frustum::getCornersWorldSpace(vectorImpl<vec3<F32> >& cornersWS){
119 164 assert(cornersWS.size() >= 8);
120 165
121 166 updatePoints();
122 167
123 - for (U8 i = 0; i < 8; ++i)
168 + for (U8 i = 0; i < 8; ++i) {
124 169 cornersWS[i].set(_frustumPoints[i]);
170 + }
171 +
125 172 }
126 173
127 - // get the frustum corners in ViewSpace. cornerVS must be a vector with at least 8 allocated slots
174 + // Get the frustum corners in ViewSpace. cornerVS must be a vector with at least 8 allocated slots
128 175 void Frustum::getCornersViewSpace(vectorImpl<vec3<F32> >& cornersVS){
129 176 assert(cornersVS.size() >= 8);
130 177
131 178 updatePoints();
132 179
133 180 const mat4<F32>& viewMatrix = _parentCamera.getViewMatrix();
134 - for (U8 i = 0; i < 8; ++i)
181 + for (U8 i = 0; i < 8; ++i) {
135 182 cornersVS[i].set(viewMatrix.transform(_frustumPoints[i]));
183 + }
136 184 }
137 185
138 186 };