forked from themrdemonized/xray-monolith
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHOM.cpp
More file actions
443 lines (390 loc) · 11.4 KB
/
HOM.cpp
File metadata and controls
443 lines (390 loc) · 11.4 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// HOM.cpp: implementation of the CHOM class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HOM.h"
#include "occRasterizer.h"
#include "../../xrEngine/GameFont.h"
#include "dxRenderDeviceRender.h"
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
float psOSSR = .001f;
void __stdcall CHOM::MT_RENDER()
{
MT.Enter();
bool b_main_menu_is_active = (g_pGamePersistent->m_pMainMenu && g_pGamePersistent->m_pMainMenu->IsActive());
if (MT_frame_rendered != Device.dwFrame && !b_main_menu_is_active)
{
CFrustum ViewBase;
ViewBase.CreateFromMatrix(Device.mFullTransform, FRUSTUM_P_LRTB + FRUSTUM_P_FAR);
Enable();
Render(ViewBase);
}
MT.Leave();
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHOM::CHOM()
{
bEnabled = FALSE;
m_pModel = 0;
m_pTris = 0;
#ifdef DEBUG
Device.seqRender.Add(this,REG_PRIORITY_LOW-1000);
#endif
}
CHOM::~CHOM()
{
#ifdef DEBUG
Device.seqRender.Remove(this);
#endif
}
#pragma pack(push,4)
struct HOM_poly
{
Fvector v1, v2, v3;
u32 flags;
};
#pragma pack(pop)
IC float Area(Fvector& v0, Fvector& v1, Fvector& v2)
{
float e1 = v0.distance_to(v1);
float e2 = v0.distance_to(v2);
float e3 = v1.distance_to(v2);
float p = (e1 + e2 + e3) / 2.f;
return _sqrt(p * (p - e1) * (p - e2) * (p - e3));
}
void CHOM::Load()
{
// Find and open file
string_path fName;
FS.update_path(fName, "$level$", "level.hom");
if (!FS.exist(fName))
{
Msg(" WARNING: Occlusion map '%s' not found.", fName);
return;
}
Msg("* Loading HOM: %s", fName);
IReader* fs = FS.r_open(fName);
IReader* S = fs->open_chunk(1);
// Load tris and merge them
CDB::Collector CL;
while (!S->eof())
{
HOM_poly P;
S->r(&P, sizeof(P));
CL.add_face_packed_D(P.v1, P.v2, P.v3, P.flags, 0.01f);
}
// Determine adjacency
xr_vector<u32> adjacency;
CL.calc_adjacency(adjacency);
// Create RASTER-triangles
m_pTris = xr_alloc<occTri>(u32(CL.getTS()));
for (u32 it = 0; it < CL.getTS(); it++)
{
CDB::TRI& clT = CL.getT()[it];
occTri& rT = m_pTris[it];
Fvector& v0 = CL.getV()[clT.verts[0]];
Fvector& v1 = CL.getV()[clT.verts[1]];
Fvector& v2 = CL.getV()[clT.verts[2]];
rT.adjacent[0] = (0xffffffff == adjacency[3 * it + 0]) ? ((occTri*)(-1)) : (m_pTris + adjacency[3 * it + 0]);
rT.adjacent[1] = (0xffffffff == adjacency[3 * it + 1]) ? ((occTri*)(-1)) : (m_pTris + adjacency[3 * it + 1]);
rT.adjacent[2] = (0xffffffff == adjacency[3 * it + 2]) ? ((occTri*)(-1)) : (m_pTris + adjacency[3 * it + 2]);
rT.flags = clT.dummy;
rT.area = Area(v0, v1, v2);
if (rT.area < EPS_L)
{
Msg("! Invalid HOM triangle (%f,%f,%f)-(%f,%f,%f)-(%f,%f,%f)", VPUSH(v0), VPUSH(v1), VPUSH(v2));
}
rT.plane.build(v0, v1, v2);
rT.skip = 0;
rT.center.add(v0, v1).add(v2).div(3.f);
}
// Create AABB-tree
m_pModel = xr_new<CDB::MODEL>();
m_pModel->build(CL.getV(), int(CL.getVS()), CL.getT(), int(CL.getTS()));
bEnabled = TRUE;
S->close();
FS.r_close(fs);
}
void CHOM::Unload()
{
xr_delete(m_pModel);
xr_free(m_pTris);
bEnabled = FALSE;
}
class pred_fb
{
public:
occTri* m_pTris;
Fvector camera;
public:
pred_fb(occTri* _t) : m_pTris(_t)
{
}
pred_fb(occTri* _t, Fvector& _c) : m_pTris(_t), camera(_c)
{
}
ICF bool operator()(const CDB::RESULT& _1, const CDB::RESULT& _2) const
{
occTri& t0 = m_pTris[_1.id];
occTri& t1 = m_pTris[_2.id];
return camera.distance_to_sqr(t0.center) < camera.distance_to_sqr(t1.center);
}
ICF bool operator()(const CDB::RESULT& _1) const
{
occTri& T = m_pTris[_1.id];
return T.skip > Device.dwFrame;
}
};
void CHOM::Render_DB(CFrustum& base)
{
//Update projection matrices on every frame to ensure valid HOM culling
float view_dim = occ_dim_0;
Fmatrix m_viewport = {
view_dim / 2.f, 0.0f, 0.0f, 0.0f,
0.0f, -view_dim / 2.f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
view_dim / 2.f + 0 + 0, view_dim / 2.f + 0 + 0, 0.0f, 1.0f
};
Fmatrix m_viewport_01 = {
1.f / 2.f, 0.0f, 0.0f, 0.0f,
0.0f, -1.f / 2.f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
1.f / 2.f + 0 + 0, 1.f / 2.f + 0 + 0, 0.0f, 1.0f
};
m_xform.mul(m_viewport, Device.mFullTransform);
m_xform_01.mul(m_viewport_01, Device.mFullTransform);
// Query DB
xrc.frustum_options(0);
xrc.frustum_query(m_pModel, base);
if (0 == xrc.r_count()) return;
// Prepare
CDB::RESULT* it = xrc.r_begin();
CDB::RESULT* end = xrc.r_end();
Fvector COP = Device.vCameraPosition;
end = std::remove_if(it, end, pred_fb(m_pTris));
std::sort(it, end, pred_fb(m_pTris, COP));
// Build frustum with near plane only
CFrustum clip;
clip.CreateFromMatrix(Device.mFullTransform,FRUSTUM_P_NEAR);
sPoly src, dst;
u32 _frame = Device.dwFrame;
#ifdef DEBUG
tris_in_frame = xrc.r_count();
tris_in_frame_visible = 0;
#endif
// Perfrom selection, sorting, culling
for (; it != end; it++)
{
// Control skipping
occTri& T = m_pTris[it->id];
u32 next = _frame + ::Random.randI(3, 10);
// Test for good occluder - should be improved :)
if (!(T.flags || (T.plane.classify(COP) > 0)))
{
T.skip = next;
continue;
}
// Access to triangle vertices
CDB::TRI& t = m_pModel->get_tris()[it->id];
Fvector* v = m_pModel->get_verts();
src.clear();
dst.clear();
src.push_back(v[t.verts[0]]);
src.push_back(v[t.verts[1]]);
src.push_back(v[t.verts[2]]);
sPoly* P = clip.ClipPoly(src, dst);
if (0 == P)
{
T.skip = next;
continue;
}
// XForm and Rasterize
#ifdef DEBUG
tris_in_frame_visible ++;
#endif
u32 pixels = 0;
int limit = int(P->size()) - 1;
for (int v = 1; v < limit; v++)
{
m_xform.transform(T.raster[0], (*P)[0]);
m_xform.transform(T.raster[1], (*P)[v + 0]);
m_xform.transform(T.raster[2], (*P)[v + 1]);
pixels += Raster.rasterize(&T);
}
if (0 == pixels)
{
T.skip = next;
continue;
}
}
}
void CHOM::Render(CFrustum& base)
{
if (!bEnabled) return;
Device.Statistic->RenderCALC_HOM.Begin();
Raster.clear();
Render_DB(base);
Raster.propagade();
MT_frame_rendered = Device.dwFrame;
Device.Statistic->RenderCALC_HOM.End();
}
ICF BOOL xform_b0(Fvector2& min, Fvector2& max, float& minz, Fmatrix& X, float _x, float _y, float _z)
{
float z = _x * X._13 + _y * X._23 + _z * X._33 + X._43;
if (z < EPS) return TRUE;
float iw = 1.f / (_x * X._14 + _y * X._24 + _z * X._34 + X._44);
min.x = max.x = (_x * X._11 + _y * X._21 + _z * X._31 + X._41) * iw;
min.y = max.y = (_x * X._12 + _y * X._22 + _z * X._32 + X._42) * iw;
minz = 0.f + z * iw;
return FALSE;
}
ICF BOOL xform_b1(Fvector2& min, Fvector2& max, float& minz, Fmatrix& X, float _x, float _y, float _z)
{
float t;
float z = _x * X._13 + _y * X._23 + _z * X._33 + X._43;
if (z < EPS) return TRUE;
float iw = 1.f / (_x * X._14 + _y * X._24 + _z * X._34 + X._44);
t = (_x * X._11 + _y * X._21 + _z * X._31 + X._41) * iw;
if (t < min.x) min.x = t;
else if (t > max.x) max.x = t;
t = (_x * X._12 + _y * X._22 + _z * X._32 + X._42) * iw;
if (t < min.y) min.y = t;
else if (t > max.y) max.y = t;
t = 0.f + z * iw;
if (t < minz) minz = t;
return FALSE;
}
IC BOOL _visible(Fbox& B, Fmatrix& m_xform_01)
{
// Find min/max points of xformed-box
Fvector2 min, max;
float z;
if (xform_b0(min, max, z, m_xform_01, B.min.x, B.min.y, B.min.z)) return TRUE;
if (xform_b1(min, max, z, m_xform_01, B.min.x, B.min.y, B.max.z)) return TRUE;
if (xform_b1(min, max, z, m_xform_01, B.max.x, B.min.y, B.max.z)) return TRUE;
if (xform_b1(min, max, z, m_xform_01, B.max.x, B.min.y, B.min.z)) return TRUE;
if (xform_b1(min, max, z, m_xform_01, B.min.x, B.max.y, B.min.z)) return TRUE;
if (xform_b1(min, max, z, m_xform_01, B.min.x, B.max.y, B.max.z)) return TRUE;
if (xform_b1(min, max, z, m_xform_01, B.max.x, B.max.y, B.max.z)) return TRUE;
if (xform_b1(min, max, z, m_xform_01, B.max.x, B.max.y, B.min.z)) return TRUE;
return Raster.test(min.x, min.y, max.x, max.y, z);
}
BOOL CHOM::visible(Fbox3& B)
{
if (!bEnabled) return TRUE;
if (B.contains(Device.vCameraPosition)) return TRUE;
return _visible(B, m_xform_01);
}
BOOL CHOM::visible(Fbox2& B, float depth)
{
if (!bEnabled) return TRUE;
return Raster.test(B.min.x, B.min.y, B.max.x, B.max.y, depth);
}
BOOL CHOM::visible(vis_data& vis)
{
if (Device.dwFrame < vis.hom_frame) return TRUE; // not at this time :)
if (!bEnabled) return TRUE; // return - everything visible
// Now, the test time comes
// 0. The object was hidden, and we must prove that each frame - test | frame-old, tested-new, hom_res = false;
// 1. The object was visible, but we must to re-check it - test | frame-new, tested-???, hom_res = true;
// 2. New object slides into view - delay test| frame-old, tested-old, hom_res = ???;
u32 frame_current = Device.dwFrame;
// u32 frame_prev = frame_current-1;
#ifdef DEBUG
Device.Statistic->RenderCALC_HOM.Begin ();
#endif
BOOL result = _visible(vis.box, m_xform_01);
u32 delay = 1;
if (result)
{
// visible - delay next test
delay = ::Random.randI(5 * 2, 5 * 5);
}
else
{
// hidden - shedule to next frame
}
vis.hom_frame = frame_current + delay;
vis.hom_tested = frame_current;
#ifdef DEBUG
Device.Statistic->RenderCALC_HOM.End ();
#endif
return result;
}
BOOL CHOM::visible(sPoly& P)
{
if (!bEnabled) return TRUE;
// Find min/max points of xformed-box
Fvector2 min, max;
float z;
if (xform_b0(min, max, z, m_xform_01, P.front().x, P.front().y, P.front().z)) return TRUE;
for (u32 it = 1; it < P.size(); it++)
if (xform_b1(min, max, z, m_xform_01, P[it].x, P[it].y, P[it].z)) return TRUE;
return Raster.test(min.x, min.y, max.x, max.y, z);
}
void CHOM::Disable()
{
bEnabled = FALSE;
}
void CHOM::Enable()
{
bEnabled = m_pModel ? TRUE : FALSE;
}
#ifdef DEBUG
void CHOM::OnRender ()
{
Raster.on_dbg_render();
if (psDeviceFlags.is(rsOcclusionDraw)){
if (m_pModel){
DEFINE_VECTOR (FVF::L,LVec,LVecIt);
static LVec poly; poly.resize(m_pModel->get_tris_count()*3);
static LVec line; line.resize(m_pModel->get_tris_count()*6);
for (int it=0; it<m_pModel->get_tris_count(); it++){
CDB::TRI* T = m_pModel->get_tris()+it;
Fvector* verts = m_pModel->get_verts();
poly[it*3+0].set(*(verts+T->verts[0]),0x80FFFFFF);
poly[it*3+1].set(*(verts+T->verts[1]),0x80FFFFFF);
poly[it*3+2].set(*(verts+T->verts[2]),0x80FFFFFF);
line[it*6+0].set(*(verts+T->verts[0]),0xFFFFFFFF);
line[it*6+1].set(*(verts+T->verts[1]),0xFFFFFFFF);
line[it*6+2].set(*(verts+T->verts[1]),0xFFFFFFFF);
line[it*6+3].set(*(verts+T->verts[2]),0xFFFFFFFF);
line[it*6+4].set(*(verts+T->verts[2]),0xFFFFFFFF);
line[it*6+5].set(*(verts+T->verts[0]),0xFFFFFFFF);
}
RCache.set_xform_world(Fidentity);
// draw solid
Device.SetNearer(TRUE);
RCache.set_Shader (dxRenderDeviceRender::Instance().m_SelectionShader);
RCache.dbg_Draw (D3DPT_TRIANGLELIST,&*poly.begin(),poly.size()/3);
Device.SetNearer(FALSE);
// draw wire
if (bDebug){
RImplementation.rmNear();
}else{
Device.SetNearer(TRUE);
}
RCache.set_Shader (dxRenderDeviceRender::Instance().m_SelectionShader);
RCache.dbg_Draw (D3DPT_LINELIST,&*line.begin(),line.size()/2);
if (bDebug){
RImplementation.rmNormal();
}else{
Device.SetNearer(FALSE);
}
}
}
}
void CHOM::stats()
{
if (m_pModel){
CGameFont& F = *Device.Statistic->Font();
F.OutNext (" **** HOM-occ ****");
F.OutNext (" visible: %2d", tris_in_frame_visible);
F.OutNext (" frustum: %2d", tris_in_frame);
F.OutNext (" total: %2d", m_pModel->get_tris_count());
}
}
#endif