Skip to content

Commit

Permalink
Correct sunlight direction.
Browse files Browse the repository at this point in the history
In all original stalker games it is slightly pointed up for some reason
so light direction wasn't synchronized with actual sun position.
  • Loading branch information
RainbowZerg committed Jun 27, 2018
1 parent 10ae347 commit a0c7726
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 155 deletions.
6 changes: 3 additions & 3 deletions src/Layers/xrRender/LightTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ void CROS_impl::update_smooth(IRenderable* O)
void CROS_impl::calc_sun_value(Fvector& position, IGameObject* _object)
{
#if RENDER == R_R1
light* sun = (light*)RImplementation.L_DB->sun_adapted._get();
light* sun = (light*)RImplementation.L_DB->sun._get();
#else
light* sun = (light*)RImplementation.Lights.sun_adapted._get();
light* sun = (light*)RImplementation.Lights.sun._get();
#endif
if (MODE & IRender_ObjectSpecific::TRACE_SUN)
{
Expand Down Expand Up @@ -530,7 +530,7 @@ void CROS_impl::prepare_lights(Fvector& position, IRenderable* O)
}

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

// Sun
float E = sun_smooth * sun->color.intensity();
Expand Down
149 changes: 43 additions & 106 deletions src/Layers/xrRender/Light_DB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,36 @@ CLight_DB::CLight_DB() {}
CLight_DB::~CLight_DB() {}
void CLight_DB::Load(IReader* fs)
{
IReader* F = 0;

// Lights itself
sun_original = NULL;
sun_adapted = NULL;
IReader* F = fs->open_chunk(fsL_LIGHT_DYNAMIC);
{
F = fs->open_chunk(fsL_LIGHT_DYNAMIC);
// Light itself
sun = NULL;

u32 size = F->length();
u32 size = F->length();
u32 element = sizeof(Flight) + 4;
u32 count = size / element;
VERIFY(count * element == size);
u32 count = (size / element);
VERIFY((count * element) == size);
v_static.reserve(count);
for (u32 i = 0; i < count; i++)
for (u32 i = 0; i < count; ++i)
{
Flight Ldata;
light* L = Create();
L->flags.bStatic = true;
L->set_type(IRender_Light::POINT);

#if RENDER == R_R1
L->set_shadow(false);
#else
L->set_shadow(true);
#endif
u32 controller = 0;
F->r(&controller, 4);
F->r(&Ldata, sizeof(Flight));

light* L = Create();
L->flags.bStatic = true;

if (Ldata.type == D3DLIGHT_DIRECTIONAL)
{
Fvector tmp_R;
tmp_R.set(1, 0, 0);

// directional (base)
sun_original = L;
L->set_type(IRender_Light::DIRECT);
L->set_shadow(true);
L->set_rotation(Ldata.direction, tmp_R);

// copy to env-sun
sun_adapted = L = Create();
L->flags.bStatic = true;
L->set_type(IRender_Light::DIRECT);
L->set_shadow(true);
L->set_rotation(Ldata.direction, tmp_R);
sun = L;
}
else
{
Expand All @@ -63,36 +48,25 @@ void CLight_DB::Load(IReader* fs)
tmp_R.set(1, 0, 0); // right

// point
v_static.push_back(L);
L->set_type(IRender_Light::POINT);
L->set_position(Ldata.position);
L->set_rotation(tmp_D, tmp_R);
L->set_range(Ldata.range);
L->set_color(Ldata.diffuse);
#if RENDER == R_R1
L->set_shadow(false);
#else
L->set_shadow(true);
#endif
L->set_active(true);
// R_ASSERT (L->spatial.sector );

v_static.push_back(L);
}
}

F->close();
}
R_ASSERT2(sun_original && sun_adapted, "Where is sun?");
F->close();

// fake spot
/*
if (0)
{
Fvector P; P.set(-5.58f, -0.00f + 2, -3.63f);
Fvector D; D.set(0,-1,0);
light* fake = Create();
fake->set_type (IRender_Light::SPOT);
fake->set_color (1,1,1);
fake->set_cone (deg2rad(60.f));
fake->set_direction (D);
fake->set_position (P);
fake->set_range (3.f);
fake->set_active (true);
}
*/
R_ASSERT2(sun, "Where is sun?");
}

#if RENDER != R_R1
Expand All @@ -102,9 +76,9 @@ void CLight_DB::LoadHemi()
if (FS.exist(fn_game, "$level$", "build.lights"))
{
IReader* F = FS.r_open(fn_game);

{
IReader* chunk = F->open_chunk(1); // Hemispheric light chunk
// Hemispheric light chunk
IReader* chunk = F->open_chunk(fsL_HEADER);

if (chunk)
{
Expand All @@ -116,38 +90,32 @@ void CLight_DB::LoadHemi()
for (u32 i = 0; i < count; i++)
{
R_Light Ldata;

chunk->r(&Ldata, sizeof(R_Light));

if (Ldata.type == D3DLIGHT_POINT)
// if (Ldata.type!=0)
{
light* L = Create();
L->flags.bStatic = true;
L->set_type(IRender_Light::POINT);

Fvector tmp_D, tmp_R;
tmp_D.set(0, 0, -1); // forward
tmp_R.set(1, 0, 0); // right

// point
v_hemi.push_back(L);
light* L = Create();
L->flags.bStatic = true;
L->set_type(IRender_Light::POINT);
L->set_position(Ldata.position);
L->set_rotation(tmp_D, tmp_R);
L->set_range(Ldata.range);
L->set_color(Ldata.diffuse.x, Ldata.diffuse.y, Ldata.diffuse.z);
L->set_active(true);
L->spatial.type = STYPE_LIGHTSOURCEHEMI;
L->set_attenuation_params(
Ldata.attenuation0, Ldata.attenuation1, Ldata.attenuation2, Ldata.falloff);
L->spatial.type = STYPE_LIGHTSOURCEHEMI;
// R_ASSERT (L->spatial.sector );

v_hemi.push_back(L);
}
}

chunk->close();
}
}

FS.r_close(F);
}
}
Expand All @@ -157,8 +125,7 @@ void CLight_DB::Unload()
{
v_static.clear();
v_hemi.clear();
sun_original.destroy();
sun_adapted.destroy();
sun.destroy();
}

light* CLight_DB::Create()
Expand All @@ -170,40 +137,31 @@ light* CLight_DB::Create()
return L;
}

#if RENDER == R_R1
void CLight_DB::add_light(light* L)
{
if (Device.dwFrame == L->frame_render)
return;
L->frame_render = Device.dwFrame;
#if RENDER == R_R1
if (L->flags.bStatic)
return; // skip static lighting, 'cause they are in lmaps
if (ps_r1_flags.test(R1FLAG_DLIGHTS))
RImplementation.L_Dynamic->add(L);
}
#endif

#if (RENDER == R_R2) || (RENDER == R_R3) || (RENDER == R_R4) || (RENDER == R_GL)
void CLight_DB::add_light(light* L)
{
if (Device.dwFrame == L->frame_render)
return;
L->frame_render = Device.dwFrame;
#else
if (RImplementation.o.noshadows)
L->flags.bShadow = FALSE;
if (L->flags.bStatic && !ps_r2_ls_flags.test(R2FLAG_R1LIGHTS))
return;
L->Export(package);
#endif
}
#endif // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) || (RENDER==R_GL)

void CLight_DB::Update()
{
// set sun params
if (sun_original && sun_adapted)
if (sun)
{
light* _sun_original = (light*)sun_original._get();
light* _sun_adapted = (light*)sun_adapted._get();
light* _sun = (light*)sun._get();
CEnvDescriptor& E = *g_pGamePersistent->Environment().CurrentEnv;
VERIFY(_valid(E.sun_dir));
#ifdef DEBUG
Expand All @@ -226,37 +184,16 @@ void CLight_DB::Update()
#endif

VERIFY2(E.sun_dir.y < 0, "Invalid sun direction settings in evironment-config");
Fvector OD, OP, AD, AP;
OD.set(E.sun_dir).normalize();
OP.mad(Device.vCameraPosition, OD, -500.f);
AD.set(0, -.75f, 0).add(E.sun_dir);
Fvector dir, pos;
dir.set(E.sun_dir).normalize();
pos.mad(Device.vCameraPosition, dir, -500.f);

// for some reason E.sun_dir can point-up
int counter = 0;
while (AD.magnitude() < 0.001 && counter < 10)
{
AD.add(E.sun_dir);
counter++;
}
AD.normalize();
AP.mad(Device.vCameraPosition, AD, -500.f);
sun_original->set_rotation(OD, _sun_original->right);
sun_original->set_position(OP);
sun_original->set_color(E.sun_color.x, E.sun_color.y, E.sun_color.z);
sun_original->set_range(600.f);
sun_adapted->set_rotation(AD, _sun_adapted->right);
sun_adapted->set_position(AP);
sun_adapted->set_color(
sun->set_rotation(dir, _sun->right);
sun->set_position(pos);
sun->set_color(
E.sun_color.x * ps_r2_sun_lumscale, E.sun_color.y * ps_r2_sun_lumscale, E.sun_color.z * ps_r2_sun_lumscale);
sun_adapted->set_range(600.f);

if (!GEnv.Render->is_sun_static())
{
sun_adapted->set_rotation(OD, _sun_original->right);
sun_adapted->set_position(OP);
}
sun->set_range(600.f);
}

// Clear selection
package.clear();
}
3 changes: 1 addition & 2 deletions src/Layers/xrRender/Light_DB.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class CLight_DB
xr_vector<ref_light> v_hemi;

public:
ref_light sun_original;
ref_light sun_adapted;
ref_light sun;
light_Package package;

public:
Expand Down
2 changes: 1 addition & 1 deletion src/Layers/xrRenderPC_GL/gl_R_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void CRender::Render()

// Configure
RImplementation.o.distortion = FALSE; // disable distorion
Fcolor sun_color = ((light*)Lights.sun_adapted._get())->color;
Fcolor sun_color = ((light*)Lights.sun._get())->color;
BOOL bSUN = ps_r2_ls_flags.test(R2FLAG_SUN) && u_diffuse2s(sun_color.r, sun_color.g, sun_color.b) > EPS;
if (o.sunstatic) bSUN = FALSE;
// Msg ("sstatic: %s, sun: %s",o.sunstatic?;"true":"false", bSUN?"true":"false");
Expand Down
10 changes: 5 additions & 5 deletions src/Layers/xrRenderPC_GL/gl_rendertarget_accum_direct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void CRenderTarget::accum_direct(u32 sub_phase)

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

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

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

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

// *** assume accumulator setted up ***
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
light* fuckingsun = (light*)RImplementation.Lights.sun._get();

// Common calc for quad-rendering
u32 Offset;
Expand Down Expand Up @@ -988,7 +988,7 @@ void CRenderTarget::accum_direct_lum()
phase_accumulator();

// *** assume accumulator setted up ***
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
light* fuckingsun = (light*)RImplementation.Lights.sun._get();

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

// Common constants (light-related)
Fvector L_clr;
Expand Down
2 changes: 1 addition & 1 deletion src/Layers/xrRenderPC_GL/gl_rendertarget_phase_combine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void CRenderTarget::phase_combine()

// sun-params
{
light* fuckingsun = (light*)RImplementation.Lights.sun_adapted._get();
light* fuckingsun = (light*)RImplementation.Lights.sun._get();
Fvector L_dir, L_clr;
float L_spec;
L_clr.set(fuckingsun->color.r, fuckingsun->color.g, fuckingsun->color.b);
Expand Down
6 changes: 3 additions & 3 deletions src/Layers/xrRenderPC_GL/r2_R_sun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ D3DXVECTOR2 BuildTSMProjectionMatrix_caster_depth_bounds(D3DXMATRIX& lightSpaceB
void CRender::render_sun()
{
PIX_EVENT(render_sun);
light* fuckingsun = (light*)Lights.sun_adapted._get();
light* fuckingsun = (light*)Lights.sun._get();
D3DXMATRIX m_LightViewProj;

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

void CRender::render_sun_near()
{
light* fuckingsun = (light*)Lights.sun_adapted._get();
light* fuckingsun = (light*)Lights.sun._get();
D3DXMATRIX m_LightViewProj;

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

void CRender::render_sun_cascade(u32 cascade_ind)
{
light* fuckingsun = (light*)Lights.sun_adapted._get();
light* fuckingsun = (light*)Lights.sun._get();

// calculate view-frustum bounds in world space
Fmatrix ex_project, ex_full, ex_full_inverse;
Expand Down
Loading

0 comments on commit a0c7726

Please sign in to comment.