-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathfow_lineoccluder.cpp
More file actions
238 lines (204 loc) · 6.63 KB
/
fow_lineoccluder.cpp
File metadata and controls
238 lines (204 loc) · 6.63 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "fow.h"
#include "fow_lineoccluder.h"
#include "fow_viewer.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
//-----------------------------------------------------------------------------
// Purpose: construct a line accluder from the given points. the normal is supplied, though if it doesn't match up with the points, then the points are swapped.
// Input : bx - starting x coord
// by - starting y coord
// ex - ending x coord
// ey - ending y coord
// vNormal - the normal coming from a pre-existing plane from which the line was formed
// nSlinceNum - the slice this occluder belongs to
//-----------------------------------------------------------------------------
CFoW_LineOccluder::CFoW_LineOccluder( float bx, float by, float ex, float ey, Vector2D &vNormal, int nSliceNum )
{
m_vStart.Init( bx, by );
m_vEnd.Init( ex, ey );
m_Plane.Init( bx, by, ex, ey );
if ( fabs( m_Plane.GetNormal().x - vNormal.x ) < 0.1f && fabs( m_Plane.GetNormal().y - vNormal.y ) < 0.1f )
{
m_vStart.Init( ex, ey );
m_vEnd.Init( bx, by );
m_Plane.Init( ex, ey, bx, by );
}
m_nSliceNum = nSliceNum;
}
CFoW_LineOccluder::CFoW_LineOccluder( Vector2D &vStart, Vector2D &vEnd, CFOW_2DPlane &Plane, int nSliceNum )
{
m_vStart = vStart;
m_vEnd = vEnd;
m_Plane = Plane;
m_nSliceNum = nSliceNum;
}
// #define SLOW_PATH 1
//-----------------------------------------------------------------------------
// Purpose: determine the occlusion of this line for the viewer
// Input : pFoW - the main FoW object
// pViewer - the viewer to obstruct
//-----------------------------------------------------------------------------
void CFoW_LineOccluder::ObstructViewer( CFoW *pFoW, CFoW_Viewer *pViewer )
{
int nUnits = pViewer->GetRadiusUnits();
int *pVisibility = pViewer->GetVisibilityRadius();
Vector vCenterLocation = pViewer->GetRealLocation(); // we don't want to use the grid centered location, as the z is not centered
float distance = m_Plane.DistanceFrom( vCenterLocation.x, vCenterLocation.y );
if ( distance < 0.0f )
{
return;
}
#ifdef SLOW_PATH
float flDegreeAmount = 360.0 / pViewer->GetRadiusUnits();
CFOW_2DPlane Edge1, Edge2;
Edge1.Init( vCenterLocation.x, vCenterLocation.y, m_vStart.x, m_vStart.y );
Edge2.Init( m_vEnd.x, m_vEnd.y, vCenterLocation.x, vCenterLocation.y );
float flCurrentDegree = 0.0f;
for ( int i = 0; i < nUnits; i++, flCurrentDegree += flDegreeAmount )
{
Vector Location = pViewer->GetLocation();
Location.x += cos( DEG2RAD( flCurrentDegree ) ) * pViewer->GetSize();
Location.y += sin( DEG2RAD( flCurrentDegree ) ) * pViewer->GetSize();
float flDistance = m_Plane.DistanceFromLineStart( vCenterLocation.x, vCenterLocation.y, Location.x, Location.y );
// flDistance *= pViewer->GetSize();
if ( flDistance >= 0.0f )
{
// distance += Viewer->GetSize();
flDistance *= flDistance;
if ( flDistance >= 0.0f && flDistance < pVisibility[ i ] )
{
if ( Edge1.PointInFront( Location.x, Location.y ) && Edge2.PointInFront( Location.x, Location.y ) )
{
pVisibility[ i ] = flDistance;
}
}
}
}
#else
const Vector2D vStraight( 0.0f, 1.0f );
Vector2D P1( m_vStart.x - vCenterLocation.x, m_vStart.y - vCenterLocation.y );
P1.NormalizeInPlace();
Vector2D P2( m_vEnd.x - vCenterLocation.x, m_vEnd.y - vCenterLocation.y );
P2.NormalizeInPlace();
#if 0
float flCurrentDegree = 0.0f;
for ( int i = 0; i < nUnits; i++, flCurrentDegree += flDegreeAmount )
{
Vector Location = pViewer->GetLocation();
Location.x += cos( DEG2RAD( flCurrentDegree ) ) * pViewer->GetSize();
Location.y += sin( DEG2RAD( flCurrentDegree ) ) * pViewer->GetSize();
float flDistance = m_Plane.DistanceFromLineStart( vCenterLocation.x, vCenterLocation.y, Location.x, Location.y );
// flDistance *= pViewer->GetSize();
if ( flDistance >= 0.0f )
{
// distance += Viewer->GetSize();
flDistance *= flDistance;
if ( flDistance >= 0.0f && flDistance < pVisibility[ i ] )
{
if ( Edge1.PointInFront( Location.x, Location.y ) && Edge2.PointInFront( Location.x, Location.y ) )
{
pVisibility[ i ] = flDistance;
}
}
}
}
#endif
if ( fabs( P1.Dot( P2 ) ) > 0.99995f )
{
return;
}
float flDot1 = vStraight.Dot( P1 );
float flPos = acos( flDot1 );
if ( P1.x < 0.0f )
{
flPos = 2.0f * M_PI_F - flPos;
}
float flDot2 = vStraight.Dot( P2 );
float flNeg = acos( flDot2 );
if ( P2.x < 0.0f )
{
flNeg = 2.0f * M_PI_F - flNeg;
}
if ( fabs( flPos - flNeg ) > M_PI_F )
{
if ( flPos < flNeg )
{
flPos += M_PI_F * 2.0f;
}
else
{
flNeg += M_PI_F * 2.0f;
}
}
// float flAng1 = RAD2DEG( flPos );
// float flAng2 = RAD2DEG( flNeg );
float flCurrentDegree, flFinishDegree;
int nStartIndex;
if ( flPos < flNeg )
{
flCurrentDegree = flPos;
flFinishDegree = flNeg;
}
else
{
flCurrentDegree = flNeg;
flFinishDegree = flPos;
}
/* if ( ( flFinishDegree - flCurrentDegree ) > M_PI_F )
{
float flTemp = flCurrentDegree;
flCurrentDegree = flFinishDegree;
flFinishDegree = flTemp + ( 2.0f * M_PI_F );
}
*/
nUnits = pViewer->GetRadiusUnits();
float flDegreeAmount = 2.0f * M_PI_F / nUnits;
nStartIndex = ( int )( flCurrentDegree / flDegreeAmount ) % nUnits;
if ( nStartIndex < 0 )
{
nStartIndex += nUnits;
}
// Vector vViewerLoc = pViewer->GetLocation();
// float flViewerRadius = pViewer->GetSize();
// float flMaxDistance = ( m_vStart - m_vEnd ).LengthSqr() + ( 60.0f * 60.0f );
#if 1
for ( int i = nStartIndex; flCurrentDegree < flFinishDegree; i++, flCurrentDegree += flDegreeAmount )
{
Vector2D vDelta;
if ( i >= nUnits )
{
i = 0;
}
vDelta.x = TableSin( flCurrentDegree );
vDelta.y = TableCos( flCurrentDegree );
#if 0
float flDistance = m_Plane.DistanceFromRay( vCenterLocation.x, vCenterLocation.y, vDelta.x, vDelta.y );
Vector2D vFinal = vCenterLocation.AsVector2D() + ( vDelta * flDistance );
float flDist1 = ( vFinal - m_vStart ).LengthSqr();
float flDist2 = ( vFinal - m_vEnd ).LengthSqr();
if ( flDistance >= 0.0f && ( flDist1 + flDist2 ) < flMaxDistance )
#else
// vDelta = ( vDelta * pViewer->GetSize() ) + vCenterLocation.AsVector2D();
// float flDistance = m_Plane.DistanceFromLineStart( vCenterLocation.x, vCenterLocation.y, vDelta.x, vDelta.y );
float flDistance = m_Plane.DistanceFromRay( vCenterLocation.x, vCenterLocation.y, vDelta.x, vDelta.y );
if ( flDistance >= 0.0f )
#endif
{
flDistance *= flDistance;
if ( flDistance < pVisibility[ i ] )
{
pVisibility[ i ] = flDistance;
}
}
}
#else
int nStart = ( 0.0f / 4.0f ) * nUnits;
int nEnd = ( 1.0f / 4.0f ) * nUnits;
for( ; nStart < nEnd; nStart++ )
{
pVisibility[ nStart ] /= 5.0f;
}
#endif
#endif
}
#include <tier0/memdbgoff.h>