-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
Copy pathLightProjector.cpp
407 lines (361 loc) · 13.6 KB
/
LightProjector.cpp
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
// LightProjector.cpp: implementation of the CLightProjector class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LightProjector.h"
#include "Include/xrRender/RenderVisual.h"
#include "xrEngine/xr_object.h"
#include "Layers/xrRender/LightTrack.h"
// tir2.xrdemo -> 45.2
// tir2.xrdemo -> 61.8
const float P_distance = 50; // switch distance between LOW-q light and HIGH-q light
const float P_cam_dist = 200;
const float P_cam_range = 7.f;
const D3DFORMAT P_rtf = D3DFMT_A8R8G8B8;
const float P_blur_kernel = .5f;
const int time_min = 30 * 1000;
const int time_max = 90 * 1000;
const float P_ideal_size = 1.f;
float clipD(float R) { return P_distance * (R / P_ideal_size); }
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLightProjector::CLightProjector()
{
current = nullptr;
RT = nullptr;
//
RT.create("$user$projector", P_rt_size, P_rt_size, P_rtf);
// ref-str for faster const-search
c_xform = "m_plmap_xform";
c_clamp = "m_plmap_clamp";
c_factor = "m_plmap_factor";
cache.resize(P_o_count);
Device.seqAppActivate.Add(this);
}
CLightProjector::~CLightProjector()
{
Device.seqAppActivate.Remove(this);
RT.destroy();
}
void CLightProjector::set_object(IRenderable* O)
{
if (current == O)
return;
if ((nullptr == O) || (receivers.size() >= P_o_count))
current = nullptr;
else
{
if (!O->renderable_ShadowReceive() || O->renderable_Invisible() ||
((CROS_impl*)O->renderable_ROS())->shadow_recv_frame == Device.dwFrame)
{
current = nullptr;
return;
}
const vis_data& vis = O->GetRenderData().visual->getVisData();
Fvector C;
O->GetRenderData().xform.transform_tiny(C, vis.sphere.P);
float R = vis.sphere.R;
float D = C.distance_to(Device.vCameraPosition) + R;
if (D < clipD(R))
current = O;
else
current = nullptr;
if (current)
{
ISpatial* spatial = dynamic_cast<ISpatial*>(O);
if (nullptr == spatial)
current = nullptr;
else
{
auto& dsgraph = RImplementation.get_imm_context();
const auto& entity_pos = spatial->spatial_sector_point();
spatial->spatial_updatesector(dsgraph.detect_sector(entity_pos));
if (spatial->GetSpatialData().sector_id == IRender_Sector::INVALID_SECTOR_ID)
{
IGameObject* obj = dynamic_cast<IGameObject*>(O);
if (obj)
Msg("! Invalid object '%s' position. Outside of sector structure.", obj->cName().c_str());
current = nullptr;
}
}
}
if (current)
{
CROS_impl* LT = (CROS_impl*)current->renderable_ROS();
LT->shadow_recv_frame = Device.dwFrame;
receivers.push_back(current);
}
}
}
//
void CLightProjector::setup(int id)
{
if (id >= int(cache.size()) || id < 0)
{
//Log("! CLightProjector::setup - ID out of range");
return;
}
recv& R = cache[id];
float Rd = R.O->GetRenderData().visual->getVisData().sphere.R;
float dist = R.C.distance_to(Device.vCameraPosition) + Rd;
float factor = _sqr(dist / clipD(Rd)) * (1 - ps_r1_lmodel_lerp) + ps_r1_lmodel_lerp;
RCache.set_c(c_xform, R.UVgen);
Fvector& m = R.UVclamp_min;
RCache.set_ca(c_clamp, 0, m.x, m.y, m.z, factor);
Fvector& M = R.UVclamp_max;
RCache.set_ca(c_clamp, 1, M.x, M.y, M.z, 0.0f);
}
void CLightProjector::invalidate()
{
for (u32 c_it = 0; c_it < cache.size(); c_it++)
cache[c_it].dwTimeValid = 0;
}
void CLightProjector::OnAppActivate() { invalidate(); }
//
#include "Layers/xrRender/SkeletonCustom.h"
void CLightProjector::calculate()
{
#ifdef _GPA_ENABLED
TAL_SCOPED_TASK_NAMED("CLightProjector::calculate()");
#endif // _GPA_ENABLED
if (receivers.empty())
return;
// perform validate / markup
for (u32 r_it = 0; r_it < receivers.size(); r_it++)
{
// validate
BOOL bValid = TRUE;
IRenderable* O = receivers[r_it];
CROS_impl* LT = (CROS_impl*)O->renderable_ROS();
int slot = LT->shadow_recv_slot;
if (slot < 0 || slot >= P_o_count)
bValid = FALSE; // invalid slot
else if (cache[slot].O != O)
bValid = FALSE; // not the same object
else
{
// seems to be valid
Fbox bb;
bb.xform(O->GetRenderData().visual->getVisData().box, O->GetRenderData().xform);
if (cache[slot].BB.contains(bb))
{
// inside, but maybe timelimit exceeded?
if (Device.dwTimeGlobal > cache[slot].dwTimeValid)
bValid = FALSE; // timeout
}
else
bValid = FALSE; // out of bounds
}
//
if (bValid)
{
// Ok, use cached version
cache[slot].dwFrame = Device.dwFrame;
}
else
{
taskid.push_back(r_it);
}
}
if (taskid.empty())
return;
// Begin
RCache.set_RT(RT->pRT);
RCache.set_ZB(RImplementation.Target->rt_temp_zb->pRT);
RCache.ClearZB(RImplementation.Target->rt_temp_zb, 1.f, 0);
RCache.set_xform_world(Fidentity);
auto& dsgraph = RImplementation.get_imm_context();
// reallocate/reassociate structures + perform all the work
for (u32 c_it = 0; c_it < cache.size(); c_it++)
{
if (taskid.empty())
break;
if (Device.dwFrame == cache[c_it].dwFrame)
continue;
// found not used slot
int tid = taskid.back();
taskid.pop_back();
recv& R = cache[c_it];
IRenderable* O = receivers[tid];
const vis_data& vis = O->GetRenderData().visual->getVisData();
CROS_impl* LT = (CROS_impl*)O->renderable_ROS();
VERIFY2(_valid(O->GetRenderData().xform), "Invalid object transformation");
VERIFY2(_valid(vis.sphere.P), "Invalid object's visual sphere");
Fvector C;
O->GetRenderData().xform.transform_tiny(C, vis.sphere.P);
R.O = O;
R.C = C;
R.C.y += vis.sphere.R * 0.1f; //. YURA: 0.1 can be more
R.BB.xform(vis.box, O->GetRenderData().xform).scale(0.1f);
R.dwTimeValid = Device.dwTimeGlobal + ::Random.randI(time_min, time_max);
LT->shadow_recv_slot = c_it;
// Msg ("[%f,%f,%f]-%f",C.C.x,C.C.y,C.C.z,C.O->renderable.visual->vis.sphere.R);
// calculate projection-matrix
Fmatrix mProject;
float p_R = R.O->GetRenderData().visual->getVisData().sphere.R * 1.1f;
// VERIFY2 (p_R>EPS_L,"Object has no physical size");
VERIFY3(p_R > EPS_L, "Object has no physical size", R.O->GetRenderData().visual->getDebugName().c_str());
float p_hat = p_R / P_cam_dist;
float p_asp = 1.f;
float p_near = P_cam_dist - EPS_L;
float p_far = P_cam_dist + p_R + P_cam_range;
mProject.build_projection_HAT(p_hat, p_asp, p_near, p_far);
RCache.set_xform_project(mProject);
// calculate view-matrix
Fmatrix mView;
Fvector v_C, v_Cs, v_N;
v_C.set(R.C);
v_Cs = v_C;
v_C.y += P_cam_dist;
v_N.set(0, 0, 1);
VERIFY(_valid(v_C) && _valid(v_Cs) && _valid(v_N));
// validate
Fvector v;
v.sub(v_Cs, v_C);
;
#ifdef DEBUG
if ((v.x * v.x + v.y * v.y + v.z * v.z) <= flt_min)
{
IGameObject* OO = dynamic_cast<IGameObject*>(R.O);
Msg("Object[%s] Visual[%s] has invalid position. ", *OO->cName(), *OO->cNameVisual());
Fvector cc;
OO->Center(cc);
Log("center=", cc);
Log("visual_center=", OO->Visual()->getVisData().sphere.P);
Log("full_matrix=", OO->XFORM());
Log("v_N", v_N);
Log("v_C", v_C);
Log("v_Cs", v_Cs);
Log("all bones transform:--------");
CKinematics* K = dynamic_cast<CKinematics*>(OO->Visual());
for (u16 ii = 0; ii < K->LL_BoneCount(); ++ii)
{
Fmatrix tr;
tr = K->LL_GetTransform(ii);
Log("bone ", K->LL_BoneName_dbg(ii));
Log("bone_matrix", tr);
}
Log("end-------");
}
#endif
// handle invalid object-bug
if ((v.x * v.x + v.y * v.y + v.z * v.z) <= flt_min)
{
// invalidate record, so that object will be unshadowed, but doesn't crash
R.dwTimeValid = Device.dwTimeGlobal;
LT->shadow_recv_frame = Device.dwFrame - 1;
LT->shadow_recv_slot = -1;
continue;
}
mView.build_camera(v_C, v_Cs, v_N);
RCache.set_xform_view(mView);
// Select slot, set viewport
int s_x = c_it % P_o_line;
int s_y = c_it / P_o_line;
const D3D_VIEWPORT viewport = { s_x * P_o_size, s_y * P_o_size, P_o_size, P_o_size, 0.f, 1.f };
RCache.SetViewport(viewport);
// Clear color to ambience
Fvector& cap = LT->get_approximate();
RCache.ClearRT(RT, color_rgba_f(cap.x, cap.y, cap.z, (cap.x + cap.y + cap.z) / 4.f));
// calculate uv-gen matrix and clamper
Fmatrix mCombine;
mCombine.mul(mProject, mView);
Fmatrix mTemp;
float fSlotSize = float(P_o_size) / float(P_rt_size);
float fSlotX = float(s_x * P_o_size) / float(P_rt_size);
float fSlotY = float(s_y * P_o_size) / float(P_rt_size);
float fTexelOffs = (.5f / P_rt_size);
Fmatrix m_TexelAdjust = {0.5f /*x-scale*/, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f /*y-scale*/, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f /*z-range*/, 0.0f, 0.5f /*x-bias*/, 0.5f + fTexelOffs /*y-bias*/, 0.0f /*z-bias*/, 1.0f};
R.UVgen.mul(m_TexelAdjust, mCombine);
mTemp.scale(fSlotSize, fSlotSize, 1);
R.UVgen.mulA_44(mTemp);
mTemp.translate(fSlotX + fTexelOffs, fSlotY + fTexelOffs, 0);
R.UVgen.mulA_44(mTemp);
// Build bbox and render
Fvector min, max;
Fbox BB;
min.set(R.C.x - p_R, R.C.y - (p_R + P_cam_range), R.C.z - p_R);
max.set(R.C.x + p_R, R.C.y + 0, R.C.z + p_R);
BB.set(min, max);
R.UVclamp_min.set(min).add(.05f); // shrink a little
R.UVclamp_max.set(max).sub(.05f); // shrink a little
ISpatial* spatial = dynamic_cast<ISpatial*>(O);
if (spatial)
{
const auto& entity_pos = spatial->spatial_sector_point();
const auto sector_id = dsgraph.detect_sector(entity_pos);
spatial->spatial_updatesector(sector_id);
if (spatial->GetSpatialData().sector_id != IRender_Sector::INVALID_SECTOR_ID)
dsgraph.render_R1_box(spatial->GetSpatialData().sector_id, BB, SE_R1_LMODELS);
}
/*if (spatial)
{
dsgraph.o.sector_id = spatial->GetSpatialData().sector_id;
dsgraph.o.xform = mCombine;
dsgraph.o.view_pos = v_C;
dsgraph.build_subspace();
}*/
}
// Blur
/*
{
// Fill vertex buffer
u32 Offset;
if (RImplementation.o.ffp) {
FVF::TL2uv* pv = (FVF::TL2uv*) RImplementation.Vertex.Lock (8,geom_Blur.stride(),Offset);
RImplementation.ApplyBlur2 (pv, rt_size);
RImplementation.Vertex.Unlock (8,geom_Blur.stride());
} else {
FVF::TL4uv* pv = (FVF::TL4uv*) RImplementation.Vertex.Lock (4,geom_Blur.stride(),Offset);
RImplementation.ApplyBlur4 (pv,P_rt_size,P_rt_size,P_blur_kernel);
RImplementation.Vertex.Unlock (4,geom_Blur.stride());
}
// Actual rendering (pass0, temp2real)
RCache.set_RT (RT->pRT);
RCache.set_ZB (NULL);
RCache.set_Shader (sh_BlurTR );
RCache.set_Geometry (geom_Blur );
RCache.Render (D3DPT_TRIANGLELIST,Offset,0,4,0,2);
}
*/
// Finita la comedia
RCache.set_xform_project(Device.mProject);
RCache.set_xform_view(Device.mView);
}
#ifdef DEBUG
void CLightProjector::render()
{
/*
#define CLS(a) color_rgba(a,a,a,a)
RCache.set_xform_world (Fidentity);
Device.Resources->OnFrameEnd ();
for (u32 it=0; it<boxes.size(); it++)
{
Fvector C,D; boxes[it].get_CD (C,D);
RCache.dbg_DrawAABB (C,D.x,D.y,D.z,0xffffffff);
}
boxes.clear();
// Debug
{
// UV
Fvector2 p0,p1;
p0.set (.5f/P_rt_size, .5f/P_rt_size);
p1.set ((P_rt_size+.5f)/P_rt_size, (P_rt_size+.5f)/P_rt_size);
// Fill vertex buffer
u32 C = 0xffffffff, Offset;
u32 _w = P_rt_size/2, _h = P_rt_size/2;
FVF::TL* pv = (FVF::TL*) geom_Screen->Lock(4,Offset);
pv->set(0, float(_h), .0001f,.9999f, C, p0.x, p1.y); pv++;
pv->set(0, 0, .0001f,.9999f, C, p0.x, p0.y); pv++;
pv->set(float(_w), float(_h), .0001f,.9999f, C, p1.x, p1.y); pv++;
pv->set(float(_w), 0, .0001f,.9999f, C, p1.x, p0.y); pv++;
geom_Screen->Unlock (4);
// Actual rendering
RCache.set_Shader(sh_Screen);
RCache.Draw (geom_Screen,4,2,Offset,Device.Streams_QuadIB);
}
*/
}
#endif