Skip to content

Commit a0c7726

Browse files
committed
Correct sunlight direction.
In all original stalker games it is slightly pointed up for some reason so light direction wasn't synchronized with actual sun position.
1 parent 10ae347 commit a0c7726

19 files changed

+91
-155
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/xrRenderPC_GL/gl_R_render.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void CRender::Render()
242242

243243
// Configure
244244
RImplementation.o.distortion = FALSE; // disable distorion
245-
Fcolor sun_color = ((light*)Lights.sun_adapted._get())->color;
245+
Fcolor sun_color = ((light*)Lights.sun._get())->color;
246246
BOOL bSUN = ps_r2_ls_flags.test(R2FLAG_SUN) && u_diffuse2s(sun_color.r, sun_color.g, sun_color.b) > EPS;
247247
if (o.sunstatic) bSUN = FALSE;
248248
// Msg ("sstatic: %s, sun: %s",o.sunstatic?;"true":"false", bSUN?"true":"false");

src/Layers/xrRenderPC_GL/gl_rendertarget_accum_direct.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void CRenderTarget::accum_direct(u32 sub_phase)
4545

4646
// TODO: DX10: Remove half pixe offset
4747
// *** assume accumulator setted up ***
48-
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
48+
light* fuckingsun = (light*)RImplementation.Lights.sun._get();
4949

5050
// Common calc for quad-rendering
5151
u32 Offset;
@@ -342,7 +342,7 @@ void CRenderTarget::accum_direct_cascade(u32 sub_phase, Fmatrix& xform, Fmatrix&
342342

343343
// TODO: DX10: Remove half pixe offset
344344
// *** assume accumulator setted up ***
345-
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
345+
light* fuckingsun = (light*)RImplementation.Lights.sun._get();
346346

347347
// Common calc for quad-rendering
348348
u32 Offset;
@@ -780,7 +780,7 @@ void CRenderTarget::accum_direct_f(u32 sub_phase)
780780
u_setrt(rt_Generic_0_r,NULL,NULL, RImplementation.Target->rt_MSAADepth->pZRT);
781781

782782
// *** assume accumulator setted up ***
783-
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
783+
light* fuckingsun = (light*)RImplementation.Lights.sun._get();
784784

785785
// Common calc for quad-rendering
786786
u32 Offset;
@@ -988,7 +988,7 @@ void CRenderTarget::accum_direct_lum()
988988
phase_accumulator();
989989

990990
// *** assume accumulator setted up ***
991-
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
991+
light* fuckingsun = (light*)RImplementation.Lights.sun._get();
992992

993993
// Common calc for quad-rendering
994994
u32 Offset;
@@ -1179,7 +1179,7 @@ void CRenderTarget::accum_direct_volumetric(u32 sub_phase, const u32 Offset, con
11791179
// Perform lighting
11801180
{
11811181
// *** assume accumulator setted up ***
1182-
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
1182+
light* fuckingsun = (light*)RImplementation.Lights.sun._get();
11831183

11841184
// Common constants (light-related)
11851185
Fvector L_clr;

src/Layers/xrRenderPC_GL/gl_rendertarget_phase_combine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void CRenderTarget::phase_combine()
164164

165165
// sun-params
166166
{
167-
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
167+
light* fuckingsun = (light*)RImplementation.Lights.sun._get();
168168
Fvector L_dir, L_clr;
169169
float L_spec;
170170
L_clr.set(fuckingsun->color.r, fuckingsun->color.g, fuckingsun->color.b);

src/Layers/xrRenderPC_GL/r2_R_sun.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ D3DXVECTOR2 BuildTSMProjectionMatrix_caster_depth_bounds(D3DXMATRIX& lightSpaceB
329329
void CRender::render_sun()
330330
{
331331
PIX_EVENT(render_sun);
332-
light* fuckingsun = (light*)Lights.sun_adapted._get();
332+
light* fuckingsun = (light*)Lights.sun._get();
333333
D3DXMATRIX m_LightViewProj;
334334

335335
// calculate view-frustum bounds in world space
@@ -820,7 +820,7 @@ void CRender::render_sun()
820820

821821
void CRender::render_sun_near()
822822
{
823-
light* fuckingsun = (light*)Lights.sun_adapted._get();
823+
light* fuckingsun = (light*)Lights.sun._get();
824824
D3DXMATRIX m_LightViewProj;
825825

826826
// calculate view-frustum bounds in world space
@@ -1111,7 +1111,7 @@ void CRender::render_sun_cascades()
11111111

11121112
void CRender::render_sun_cascade(u32 cascade_ind)
11131113
{
1114-
light* fuckingsun = (light*)Lights.sun_adapted._get();
1114+
light* fuckingsun = (light*)Lights.sun._get();
11151115

11161116
// calculate view-frustum bounds in world space
11171117
Fmatrix ex_project, ex_full, ex_full_inverse;

0 commit comments

Comments
 (0)