Skip to content

Commit

Permalink
Added experimental sun light hack.
Browse files Browse the repository at this point in the history
  • Loading branch information
nashmuhandes committed Mar 30, 2019
1 parent 9751a5f commit dbaead7
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/g_levellocals.h
Expand Up @@ -728,6 +728,11 @@ struct FLevelLocals
return savegamerestore
|| (info != nullptr && info->Snapshot.mBuffer != nullptr && info->isValid());
}

// [LAD] sunlight hack
DVector3 sunPos;
PalEntry sunColor;

};


Expand Down
4 changes: 4 additions & 0 deletions src/rendering/gl/renderer/gl_renderstate.cpp
Expand Up @@ -140,6 +140,10 @@ bool FGLRenderState::ApplyShader()
activeShader->muSpecularMaterial.Set(mGlossiness, mSpecularLevel);
activeShader->muAddColor.Set(mAddColor);

// [LAD] sunlight hack
activeShader->muSunPos.Set(mSunPos.vec);
activeShader->muSunColor.Set(mSunColor);

if (mGlowEnabled)
{
activeShader->muGlowTopColor.Set(mGlowTop.vec);
Expand Down
8 changes: 8 additions & 0 deletions src/rendering/gl/shaders/gl_shader.cpp
Expand Up @@ -266,6 +266,10 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char *
i_data += "#define uLightDist uLightAttr.r\n";
i_data += "uniform int uFogEnabled;\n";

// [LAD] sunlight hack
i_data += "uniform vec4 uSunPos;\n";
i_data += "uniform vec4 uSunColor;\n";

// dynamic lights
i_data += "uniform int uLightIndex;\n";

Expand Down Expand Up @@ -538,6 +542,10 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char *
muAddColor.Init(hShader, "uAddColor");
muTimer.Init(hShader, "timer");

// [LAD] sunlight hack
muSunPos.Init(hShader, "uSunPos");
muSunColor.Init(hShader, "uSunColor");

lights_index = glGetUniformLocation(hShader, "lights");
fakevb_index = glGetUniformLocation(hShader, "fakeVB");
modelmatrix_index = glGetUniformLocation(hShader, "ModelMatrix");
Expand Down
4 changes: 4 additions & 0 deletions src/rendering/gl/shaders/gl_shader.h
Expand Up @@ -258,6 +258,10 @@ class FShader
FBufferedUniform1f muAlphaThreshold;
FBufferedUniform2f muSpecularMaterial;
FBufferedUniform1f muTimer;

// [LAD] sunlight hack
FBufferedUniform4f muSunPos;
FBufferedUniformPE muSunColor;

int lights_index;
int modelmatrix_index;
Expand Down
4 changes: 4 additions & 0 deletions src/rendering/hwrenderer/scene/hw_renderstate.cpp
Expand Up @@ -39,6 +39,10 @@
//==========================================================================
void HWDrawInfo::SetColor(FRenderState &state, int sectorlightlevel, int rellight, bool fullbright, const FColormap &cm, float alpha, bool weapon)
{
// [LAD] sunlight hack
// I don't know where else to put this :S
state.SetSunParms();

if (fullbright)
{
state.SetColorAlpha(0xffffff, alpha, 0);
Expand Down
14 changes: 14 additions & 0 deletions src/rendering/hwrenderer/scene/hw_renderstate.h
Expand Up @@ -156,6 +156,10 @@ class FRenderState
FStateVec4 mDynColor;
FRenderStyle mRenderStyle;

// [LAD] sunlight hack
FStateVec4 mSunPos;
PalEntry mSunColor;

FMaterialState mMaterial;
FDepthBiasState mBias;

Expand Down Expand Up @@ -393,6 +397,16 @@ class FRenderState
mLightParms[0] = d;
}

void SetSunParms(void)
{
mSunPos.Set(primaryLevel->sunPos.X, primaryLevel->sunPos.Y, primaryLevel->sunPos.Z, 0.f);
PalEntry sc;
sc.r = primaryLevel->sunColor.r;
sc.g = primaryLevel->sunColor.g;
sc.b = primaryLevel->sunColor.b;
mSunColor = sc;
}

PalEntry GetFogColor() const
{
return mFogColor;
Expand Down
2 changes: 2 additions & 0 deletions src/scripting/vmthunks.cpp
Expand Up @@ -2981,6 +2981,8 @@ DEFINE_FIELD_BIT(FLevelLocals, flags3, removeitems, LEVEL3_REMOVEITEMS)
// [LAD]
DEFINE_FIELD_BIT(FLevelLocals, ladflags, noautomap, LADLEVEL_NOAUTOMAP)
DEFINE_FIELD_BIT(FLevelLocals, ladflags, nousersave, LADLEVEL_NOSAVEGAME)
DEFINE_FIELD(FLevelLocals, sunPos)
DEFINE_FIELD(FLevelLocals, sunColor)

DEFINE_FIELD_X(Sector, sector_t, floorplane)
DEFINE_FIELD_X(Sector, sector_t, ceilingplane)
Expand Down
6 changes: 6 additions & 0 deletions wadsrc/static/shaders/glsl/material_normal.fp
Expand Up @@ -59,6 +59,12 @@ vec3 ProcessMaterialLight(Material material, vec3 color)
}
}

// [LAD] sunlight hack
// Sun direction
vec3 sundir = normalize(uSunPos.xyz);
vec3 suncolor = uSunColor.rgb;
dynlight.rgb += suncolor * clamp(dot(normal, sundir), 0.0, 1.0);

vec3 frag = material.Base.rgb * clamp(color + desaturate(dynlight).rgb, 0.0, 1.4);

if (uLightIndex >= 0)
Expand Down
23 changes: 23 additions & 0 deletions wadsrc/static/shaders/glsl/material_specular.fp
Expand Up @@ -34,6 +34,23 @@ vec2 lightAttenuation(int i, vec3 normal, vec3 viewdir, float lightcolorA)
return vec2(attenuation, attenuation * specularLevel * pow(specAngle, phExp));
}

// [LAD] sunlight hack
vec2 sunlightAttenuation(vec3 normal, vec3 viewdir)
{
// Sun direction
vec3 sundir = normalize(uSunPos.xyz);

float attenuation = clamp(dot(normal, sundir), 0.0, 1.0);

float glossiness = uSpecularMaterial.x;
float specularLevel = uSpecularMaterial.y;

vec3 halfdir = normalize(viewdir + sundir);
float specAngle = clamp(dot(halfdir, normal), 0.0f, 1.0f);
float phExp = glossiness * 4.0f;
return vec2(attenuation, attenuation * specularLevel * pow(specAngle, phExp));
}

vec3 ProcessMaterialLight(Material material, vec3 color)
{
vec4 dynlight = uDynLightColor;
Expand Down Expand Up @@ -67,6 +84,12 @@ vec3 ProcessMaterialLight(Material material, vec3 color)
}
}

// [LAD] sunlight hack
vec3 suncolor = uSunColor.rgb;
vec2 attenuation = sunlightAttenuation(normal, viewdir);
dynlight.rgb += suncolor * attenuation.x;
specular.rgb += suncolor * attenuation.y;

dynlight.rgb = clamp(color + desaturate(dynlight).rgb, 0.0, 1.4);
specular.rgb = clamp(desaturate(specular).rgb, 0.0, 1.4);

Expand Down
2 changes: 2 additions & 0 deletions wadsrc/static/zscript/base.zs
Expand Up @@ -710,6 +710,8 @@ struct LevelLocals native
// [LAD]
native bool noautomap;
native bool nousersave;
native Vector3 sunPos;
native Color sunColor;

native String GetUDMFString(int type, int index, Name key);
native int GetUDMFInt(int type, int index, Name key);
Expand Down

0 comments on commit dbaead7

Please sign in to comment.