Skip to content

Commit 90864ad

Browse files
authored
Merge pull request #218 from RainbowZerg/xd_dev
Correct sunlight direction & mipmap bias control for DX10.
2 parents 9fd2d99 + 4a76ded commit 90864ad

30 files changed

+140
-198
lines changed

src/Layers/xrRender/LightTrack.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ void CROS_impl::update_smooth(IRenderable* O)
366366
void CROS_impl::calc_sun_value(Fvector& position, IGameObject* _object)
367367
{
368368
#if RENDER == R_R1
369-
light* sun = (light*)RImplementation.L_DB->sun_adapted._get();
369+
light* sun = (light*)RImplementation.L_DB->sun._get();
370370
#else
371-
light* sun = (light*)RImplementation.Lights.sun_adapted._get();
371+
light* sun = (light*)RImplementation.Lights.sun._get();
372372
#endif
373373
if (MODE & IRender_ObjectSpecific::TRACE_SUN)
374374
{
@@ -530,7 +530,7 @@ void CROS_impl::prepare_lights(Fvector& position, IRenderable* O)
530530
}
531531

532532
#if RENDER == R_R1
533-
light* sun = (light*)RImplementation.L_DB->sun_adapted._get();
533+
light* sun = (light*)RImplementation.L_DB->sun._get();
534534

535535
// Sun
536536
float E = sun_smooth * sun->color.intensity();

src/Layers/xrRender/Light_DB.cpp

Lines changed: 43 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,36 @@ CLight_DB::CLight_DB() {}
1010
CLight_DB::~CLight_DB() {}
1111
void CLight_DB::Load(IReader* fs)
1212
{
13-
IReader* F = 0;
14-
15-
// Lights itself
16-
sun_original = NULL;
17-
sun_adapted = NULL;
13+
IReader* F = fs->open_chunk(fsL_LIGHT_DYNAMIC);
1814
{
19-
F = fs->open_chunk(fsL_LIGHT_DYNAMIC);
15+
// Light itself
16+
sun = NULL;
2017

21-
u32 size = F->length();
18+
u32 size = F->length();
2219
u32 element = sizeof(Flight) + 4;
23-
u32 count = size / element;
24-
VERIFY(count * element == size);
20+
u32 count = (size / element);
21+
VERIFY((count * element) == size);
2522
v_static.reserve(count);
26-
for (u32 i = 0; i < count; i++)
23+
for (u32 i = 0; i < count; ++i)
2724
{
2825
Flight Ldata;
29-
light* L = Create();
30-
L->flags.bStatic = true;
31-
L->set_type(IRender_Light::POINT);
32-
33-
#if RENDER == R_R1
34-
L->set_shadow(false);
35-
#else
36-
L->set_shadow(true);
37-
#endif
3826
u32 controller = 0;
3927
F->r(&controller, 4);
4028
F->r(&Ldata, sizeof(Flight));
29+
30+
light* L = Create();
31+
L->flags.bStatic = true;
32+
4133
if (Ldata.type == D3DLIGHT_DIRECTIONAL)
4234
{
4335
Fvector tmp_R;
4436
tmp_R.set(1, 0, 0);
4537

46-
// directional (base)
47-
sun_original = L;
4838
L->set_type(IRender_Light::DIRECT);
4939
L->set_shadow(true);
5040
L->set_rotation(Ldata.direction, tmp_R);
5141

52-
// copy to env-sun
53-
sun_adapted = L = Create();
54-
L->flags.bStatic = true;
55-
L->set_type(IRender_Light::DIRECT);
56-
L->set_shadow(true);
57-
L->set_rotation(Ldata.direction, tmp_R);
42+
sun = L;
5843
}
5944
else
6045
{
@@ -63,36 +48,25 @@ void CLight_DB::Load(IReader* fs)
6348
tmp_R.set(1, 0, 0); // right
6449

6550
// point
66-
v_static.push_back(L);
51+
L->set_type(IRender_Light::POINT);
6752
L->set_position(Ldata.position);
6853
L->set_rotation(tmp_D, tmp_R);
6954
L->set_range(Ldata.range);
7055
L->set_color(Ldata.diffuse);
56+
#if RENDER == R_R1
57+
L->set_shadow(false);
58+
#else
59+
L->set_shadow(true);
60+
#endif
7161
L->set_active(true);
72-
// R_ASSERT (L->spatial.sector );
62+
63+
v_static.push_back(L);
7364
}
7465
}
75-
76-
F->close();
7766
}
78-
R_ASSERT2(sun_original && sun_adapted, "Where is sun?");
67+
F->close();
7968

80-
// fake spot
81-
/*
82-
if (0)
83-
{
84-
Fvector P; P.set(-5.58f, -0.00f + 2, -3.63f);
85-
Fvector D; D.set(0,-1,0);
86-
light* fake = Create();
87-
fake->set_type (IRender_Light::SPOT);
88-
fake->set_color (1,1,1);
89-
fake->set_cone (deg2rad(60.f));
90-
fake->set_direction (D);
91-
fake->set_position (P);
92-
fake->set_range (3.f);
93-
fake->set_active (true);
94-
}
95-
*/
69+
R_ASSERT2(sun, "Where is sun?");
9670
}
9771

9872
#if RENDER != R_R1
@@ -102,9 +76,9 @@ void CLight_DB::LoadHemi()
10276
if (FS.exist(fn_game, "$level$", "build.lights"))
10377
{
10478
IReader* F = FS.r_open(fn_game);
105-
10679
{
107-
IReader* chunk = F->open_chunk(1); // Hemispheric light chunk
80+
// Hemispheric light chunk
81+
IReader* chunk = F->open_chunk(fsL_HEADER);
10882

10983
if (chunk)
11084
{
@@ -116,38 +90,32 @@ void CLight_DB::LoadHemi()
11690
for (u32 i = 0; i < count; i++)
11791
{
11892
R_Light Ldata;
119-
12093
chunk->r(&Ldata, sizeof(R_Light));
12194

12295
if (Ldata.type == D3DLIGHT_POINT)
123-
// if (Ldata.type!=0)
12496
{
125-
light* L = Create();
126-
L->flags.bStatic = true;
127-
L->set_type(IRender_Light::POINT);
128-
12997
Fvector tmp_D, tmp_R;
13098
tmp_D.set(0, 0, -1); // forward
13199
tmp_R.set(1, 0, 0); // right
132100

133-
// point
134-
v_hemi.push_back(L);
101+
light* L = Create();
102+
L->flags.bStatic = true;
103+
L->set_type(IRender_Light::POINT);
135104
L->set_position(Ldata.position);
136105
L->set_rotation(tmp_D, tmp_R);
137106
L->set_range(Ldata.range);
138107
L->set_color(Ldata.diffuse.x, Ldata.diffuse.y, Ldata.diffuse.z);
139108
L->set_active(true);
109+
L->spatial.type = STYPE_LIGHTSOURCEHEMI;
140110
L->set_attenuation_params(
141111
Ldata.attenuation0, Ldata.attenuation1, Ldata.attenuation2, Ldata.falloff);
142-
L->spatial.type = STYPE_LIGHTSOURCEHEMI;
143-
// R_ASSERT (L->spatial.sector );
112+
113+
v_hemi.push_back(L);
144114
}
145115
}
146-
147116
chunk->close();
148117
}
149118
}
150-
151119
FS.r_close(F);
152120
}
153121
}
@@ -157,8 +125,7 @@ void CLight_DB::Unload()
157125
{
158126
v_static.clear();
159127
v_hemi.clear();
160-
sun_original.destroy();
161-
sun_adapted.destroy();
128+
sun.destroy();
162129
}
163130

164131
light* CLight_DB::Create()
@@ -170,40 +137,31 @@ light* CLight_DB::Create()
170137
return L;
171138
}
172139

173-
#if RENDER == R_R1
174140
void CLight_DB::add_light(light* L)
175141
{
176142
if (Device.dwFrame == L->frame_render)
177143
return;
178144
L->frame_render = Device.dwFrame;
145+
#if RENDER == R_R1
179146
if (L->flags.bStatic)
180147
return; // skip static lighting, 'cause they are in lmaps
181148
if (ps_r1_flags.test(R1FLAG_DLIGHTS))
182149
RImplementation.L_Dynamic->add(L);
183-
}
184-
#endif
185-
186-
#if (RENDER == R_R2) || (RENDER == R_R3) || (RENDER == R_R4) || (RENDER == R_GL)
187-
void CLight_DB::add_light(light* L)
188-
{
189-
if (Device.dwFrame == L->frame_render)
190-
return;
191-
L->frame_render = Device.dwFrame;
150+
#else
192151
if (RImplementation.o.noshadows)
193152
L->flags.bShadow = FALSE;
194153
if (L->flags.bStatic && !ps_r2_ls_flags.test(R2FLAG_R1LIGHTS))
195154
return;
196155
L->Export(package);
156+
#endif
197157
}
198-
#endif // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) || (RENDER==R_GL)
199158

200159
void CLight_DB::Update()
201160
{
202161
// set sun params
203-
if (sun_original && sun_adapted)
162+
if (sun)
204163
{
205-
light* _sun_original = (light*)sun_original._get();
206-
light* _sun_adapted = (light*)sun_adapted._get();
164+
light* _sun = (light*)sun._get();
207165
CEnvDescriptor& E = *g_pGamePersistent->Environment().CurrentEnv;
208166
VERIFY(_valid(E.sun_dir));
209167
#ifdef DEBUG
@@ -226,37 +184,16 @@ void CLight_DB::Update()
226184
#endif
227185

228186
VERIFY2(E.sun_dir.y < 0, "Invalid sun direction settings in evironment-config");
229-
Fvector OD, OP, AD, AP;
230-
OD.set(E.sun_dir).normalize();
231-
OP.mad(Device.vCameraPosition, OD, -500.f);
232-
AD.set(0, -.75f, 0).add(E.sun_dir);
187+
Fvector dir, pos;
188+
dir.set(E.sun_dir).normalize();
189+
pos.mad(Device.vCameraPosition, dir, -500.f);
233190

234-
// for some reason E.sun_dir can point-up
235-
int counter = 0;
236-
while (AD.magnitude() < 0.001 && counter < 10)
237-
{
238-
AD.add(E.sun_dir);
239-
counter++;
240-
}
241-
AD.normalize();
242-
AP.mad(Device.vCameraPosition, AD, -500.f);
243-
sun_original->set_rotation(OD, _sun_original->right);
244-
sun_original->set_position(OP);
245-
sun_original->set_color(E.sun_color.x, E.sun_color.y, E.sun_color.z);
246-
sun_original->set_range(600.f);
247-
sun_adapted->set_rotation(AD, _sun_adapted->right);
248-
sun_adapted->set_position(AP);
249-
sun_adapted->set_color(
191+
sun->set_rotation(dir, _sun->right);
192+
sun->set_position(pos);
193+
sun->set_color(
250194
E.sun_color.x * ps_r2_sun_lumscale, E.sun_color.y * ps_r2_sun_lumscale, E.sun_color.z * ps_r2_sun_lumscale);
251-
sun_adapted->set_range(600.f);
252-
253-
if (!GEnv.Render->is_sun_static())
254-
{
255-
sun_adapted->set_rotation(OD, _sun_original->right);
256-
sun_adapted->set_position(OP);
257-
}
195+
sun->set_range(600.f);
258196
}
259-
260197
// Clear selection
261198
package.clear();
262199
}

src/Layers/xrRender/Light_DB.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class CLight_DB
1010
xr_vector<ref_light> v_hemi;
1111

1212
public:
13-
ref_light sun_original;
14-
ref_light sun_adapted;
13+
ref_light sun;
1514
light_Package package;
1615

1716
public:

src/Layers/xrRender/r__dsgraph_build.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,15 +837,16 @@ void D3DXRenderBase::Reset(HWND hWnd, u32& dwWidth, u32& dwHeight, float& fWidth
837837
void D3DXRenderBase::SetupStates()
838838
{
839839
HW.Caps.Update();
840-
#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL)
841-
// TODO: DX10: Implement Resetting of render states into default mode
842-
// VERIFY(!"D3DXRenderBase::SetupStates not implemented.");
840+
#if defined(USE_OGL)
841+
// TODO: OGL: Implement SetupStates().
842+
#elif defined(USE_DX10) || defined(USE_DX11)
843+
SSManager.SetMaxAnisotropy(ps_r__tf_Anisotropic);
844+
SSManager.SetMipLODBias(ps_r__tf_Mipbias);
843845
#else // USE_DX10
844846
for (u32 i = 0; i < HW.Caps.raster.dwStages; i++)
845847
{
846-
float fBias = -.5f;
847-
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, 4));
848-
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MIPMAPLODBIAS, *(LPDWORD)&fBias));
848+
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, ps_r__tf_Anisotropic));
849+
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MIPMAPLODBIAS, *(LPDWORD)&ps_r__tf_Mipbias));
849850
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
850851
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
851852
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR));

src/Layers/xrRender/xrRender_console.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ float ps_r__ssaDONTSORT = 32.f; // RO
100100
float ps_r__ssaHZBvsTEX = 96.f; // RO
101101

102102
int ps_r__tf_Anisotropic = 8;
103+
float ps_r__tf_Mipbias = 0.0f;
103104

104105
// R1
105106
float ps_r1_ssaLOD_A = 64.f;
106107
float ps_r1_ssaLOD_B = 48.f;
107-
float ps_r1_tf_Mipbias = 0.0f;
108108
Flags32 ps_r1_flags = {R1FLAG_DLIGHTS}; // r1-only
109109
float ps_r1_lmodel_lerp = 0.1f;
110110
float ps_r1_dlights_clip = 40.f;
@@ -119,7 +119,6 @@ int ps_r1_SoftwareSkinning = 0; // r1-only
119119
// R2
120120
float ps_r2_ssaLOD_A = 64.f;
121121
float ps_r2_ssaLOD_B = 48.f;
122-
float ps_r2_tf_Mipbias = 0.0f;
123122

124123
// R2-specific
125124
Flags32 ps_r2_ls_flags = {R2FLAG_SUN
@@ -299,9 +298,10 @@ class CCC_tf_MipBias : public CCC_Float
299298
if (nullptr == HW.pDevice)
300299
return;
301300

302-
#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL)
303-
// TODO: DX10: Implement mip bias control
304-
//VERIFY(!"apply not implmemented.");
301+
#if defined(USE_OGL)
302+
// TODO: OGL: Implement mipmap bias control.
303+
#elif defined(USE_DX10) || defined(USE_DX11)
304+
SSManager.SetMipLODBias(*value);
305305
#else // USE_DX10
306306
for (u32 i = 0; i < HW.Caps.raster.dwStages; i++)
307307
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MIPMAPLODBIAS, *((LPDWORD)value)));
@@ -725,12 +725,12 @@ void xrRender_initconsole()
725725
#endif // DEBUG
726726

727727
CMD2(CCC_tf_Aniso, "r__tf_aniso", &ps_r__tf_Anisotropic); // {1..16}
728+
CMD2(CCC_tf_MipBias, "r__tf_mipbias", &ps_r__tf_Mipbias); // {-3 +3}
728729

729730
// R1
730731
CMD4(CCC_Float, "r1_ssa_lod_a", &ps_r1_ssaLOD_A, 16, 96);
731732
CMD4(CCC_Float, "r1_ssa_lod_b", &ps_r1_ssaLOD_B, 16, 64);
732733
CMD4(CCC_Float, "r1_lmodel_lerp", &ps_r1_lmodel_lerp, 0, 0.333f);
733-
CMD2(CCC_tf_MipBias, "r1_tf_mipbias", &ps_r1_tf_Mipbias); // {-3 +3}
734734
CMD3(CCC_Mask, "r1_dlights", &ps_r1_flags, R1FLAG_DLIGHTS);
735735
CMD4(CCC_Float, "r1_dlights_clip", &ps_r1_dlights_clip, 10.f, 150.f);
736736
CMD4(CCC_Float, "r1_pps_u", &ps_r1_pps_u, -1.f, +1.f);
@@ -751,7 +751,6 @@ void xrRender_initconsole()
751751
// R2
752752
CMD4(CCC_Float, "r2_ssa_lod_a", &ps_r2_ssaLOD_A, 16, 96);
753753
CMD4(CCC_Float, "r2_ssa_lod_b", &ps_r2_ssaLOD_B, 32, 64);
754-
CMD2(CCC_tf_MipBias, "r2_tf_mipbias", &ps_r2_tf_Mipbias);
755754

756755
// R2-specific
757756
CMD2(CCC_R2GM, "r2em", &ps_r2_gmaterial);
@@ -920,14 +919,4 @@ void xrRender_initconsole()
920919
//CMD3(CCC_Mask, "r2_sun_ignore_portals", &ps_r2_ls_flags, R2FLAG_SUN_IGNORE_PORTALS);
921920
}
922921

923-
void xrRender_apply_tf()
924-
{
925-
Console->Execute("r__tf_aniso");
926-
#if RENDER == R_R1
927-
Console->Execute("r1_tf_mipbias");
928-
#else
929-
Console->Execute("r2_tf_mipbias");
930-
#endif
931-
}
932-
933922
#endif

0 commit comments

Comments
 (0)