File: bofrustum.cpp

package info (click to toggle)
boson 0.13-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 12,992 kB
  • ctags: 29,287
  • sloc: cpp: 203,902; ansic: 9,959; makefile: 75; sh: 64; sed: 47
file content (185 lines) | stat: -rw-r--r-- 5,145 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
    This file is part of the Boson game
    Copyright (C) 2005 Andreas Beckermann (b_mann@gmx.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "math/bofrustum.h"

#include <math.h>
#include <stdio.h>

void BoFrustum::loadViewFrustum(const BoMatrix& m)
{
 // Extract the numbers for the RIGHT plane
 mPlanes[0].loadPlane(BoVector3Float(m[3] - m[0], m[7] - m[4], m[11] - m[8]), m[15] - m[12]);

 // Extract the numbers for the LEFT plane
 mPlanes[1].loadPlane(BoVector3Float(m[3] + m[0], m[7] + m[4], m[11] + m[8]), m[15] + m[12]);

 // Extract the BOTTOM plane
 mPlanes[2].loadPlane(BoVector3Float(m[3] + m[1], m[7] + m[5], m[11] + m[9]), m[15] + m[13]);

 // Extract the TOP plane
 mPlanes[3].loadPlane(BoVector3Float(m[3] - m[1], m[7] - m[5], m[11] - m[9]), m[15] - m[13]);

 // Extract the FAR plane
 mPlanes[4].loadPlane(BoVector3Float(m[3] - m[2], m[7] - m[6], m[11] - m[10]), m[15] - m[14]);

 // Extract the NEAR plane
 mPlanes[5].loadPlane(BoVector3Float(m[3] + m[2], m[7] + m[6], m[11] + m[10]), m[15] + m[14]);
}

void BoFrustum::loadPickViewFrustum(const BoRect2Float& pickRect, const int* viewport, const BoMatrix& modelview, const BoMatrix& _projection)
{
 const BoVector2Float pickCenter = pickRect.center();
 const float viewX = (float)viewport[0];
 const float viewY = (float)viewport[1];
 const float viewW = (float)viewport[2];
 const float viewH = (float)viewport[3];
 const float pickW = QMAX(pickRect.width(), 1);
 const float pickH = QMAX(pickRect.height(), 1);
 const float pickCenterX = pickCenter.x();
 const float pickCenterY = viewH - pickCenter.y();

 BoMatrix pick;
 const float scaleX = viewW / pickW;
 const float scaleY = viewH / pickH;
 pick.scale(scaleX, scaleY, 1.0f);

 float translateX;
 float translateY;
 translateX = 1.0f - 2.0f * (pickCenterX - viewX) / viewW;
 translateY = 1.0f - 2.0f * (pickCenterY - viewY) / viewH;

 pick.translate(translateX, translateY, 0.0f);

 BoMatrix projection(pick);
 projection.multiply(&_projection);
 loadViewFrustum(modelview, projection);
}

float BoFrustum::sphereInFrustum(const BoVector3Float& pos, float radius) const
{
 // FIXME: performance: we might unroll the loop and then make this function
 // inline. We call it pretty often!
 float distance;
 for (int p = 0; p < 6; p++) {
	distance = mPlanes[p].distance(pos);
	if (distance <= -radius) {
		return 0;
	}
 }

 // we return distance from near plane + radius, which is how far the object is
 // away from the camera!
 return distance + radius;
}

bofixed BoFrustum::sphereInFrustum(const BoVector3Fixed& pos, bofixed radius) const
{
 // FIXME: performance: we might unroll the loop and then make this function
 // inline. We call it pretty often!
 bofixed distance;
 for (int p = 0; p < 6; p++) {
	distance = mPlanes[p].distance(pos);
	if (distance <= -radius) {
		return 0;
	}
 }

 // we return distance from near plane + radius, which is how far the object is
 // away from the camera!
 return distance + radius;
}

int BoFrustum::sphereCompleteInFrustum(const BoVector3Float& pos, float radius, float* dist) const
{
 float distance;
 int c = 0;
 for (int p = 0; p < 6; p++) {
	distance = mPlanes[p].distance(pos);
	if (distance <= -radius) {
		return 0;
	}
	if (distance > radius) {
		c++;
	}
 }
 if (dist) {
	*dist = distance + radius;
 }
 if (c == 6) {
	return 2;
 }
 return 1;
}

int BoFrustum::sphereCompleteInFrustum(const BoVector3Fixed& pos, bofixed radius, bofixed* dist) const
{
 bofixed distance;
 int c = 0;
 for (int p = 0; p < 6; p++) {
	distance = mPlanes[p].distance(pos);
	if (distance <= -radius) {
		return 0;
	}
	if (distance > radius) {
		c++;
	}
 }
 if (dist) {
	*dist = distance + radius;
 }
 if (c == 6) {
	return 2;
 }
 return 1;
}

bool BoFrustum::boxInFrustum(const BoVector3Float& min, const BoVector3Float& max) const
{
 for (int p = 0; p < 6; p++) {
	if (mPlanes[p].distance(min.x(), min.y(), min.z()) > 0.0f) {
		continue;
	}
	if (mPlanes[p].distance(max.x(), min.y(), min.z()) > 0.0f) {
		continue;
	}
	if (mPlanes[p].distance(min.x(), max.y(), min.z()) > 0.0f) {
		continue;
	}
	if (mPlanes[p].distance(max.x(), max.y(), min.z()) > 0.0f) {
		continue;
	}
	if (mPlanes[p].distance(min.x(), min.y(), max.z()) > 0.0f) {
		continue;
	}
	if (mPlanes[p].distance(max.x(), min.y(), max.z()) > 0.0f) {
		continue;
	}
	if (mPlanes[p].distance(min.x(), max.y(), max.z()) > 0.0f) {
		continue;
	}
	if (mPlanes[p].distance(max.x(), max.y(), max.z()) > 0.0f) {
		continue;
	}
	return false;
 }
 return true;
}