Add dynamic light to sprites

This commit is contained in:
Magnus Norddahl 2017-02-23 06:01:01 +01:00
parent c6235fb674
commit ef41e8e54e
3 changed files with 445 additions and 15 deletions

File diff suppressed because it is too large Load diff

View file

@ -131,6 +131,10 @@ namespace swrenderer
int desaturate = shade_constants.desaturate; int desaturate = shade_constants.desaturate;
<? } ?> <? } ?>
__m128i dynlight = _mm_cvtsi32_si128(args.DynamicLight());
dynlight = _mm_unpacklo_epi8(dynlight, _mm_setzero_si128());
dynlight = _mm_shuffle_epi32(dynlight, _MM_SHUFFLE(1,0,1,0));
int count = args.Count(); int count = args.Count();
int pitch = RenderViewport::Instance()->RenderTarget->GetPitch(); int pitch = RenderViewport::Instance()->RenderTarget->GetPitch();
uint32_t fracstep = args.TextureVStep(); uint32_t fracstep = args.TextureVStep();
@ -274,7 +278,9 @@ namespace swrenderer
function Shade($blendVariant, $isSimpleShade) function Shade($blendVariant, $isSimpleShade)
{ {
if ($blendVariant == "copy" || $blendVariant == "shaded") return; if ($blendVariant == "copy" || $blendVariant == "shaded") return;
?>
__m128i material = fgcolor;
<?
if ($isSimpleShade == true) if ($isSimpleShade == true)
{ ?> { ?>
fgcolor = _mm_srli_epi16(_mm_mullo_epi16(fgcolor, mlight), 8); fgcolor = _mm_srli_epi16(_mm_mullo_epi16(fgcolor, mlight), 8);
@ -297,8 +303,11 @@ namespace swrenderer
fgcolor = _mm_mullo_epi16(fgcolor, mlight); fgcolor = _mm_mullo_epi16(fgcolor, mlight);
fgcolor = _mm_srli_epi16(_mm_add_epi16(shade_fade, fgcolor), 8); fgcolor = _mm_srli_epi16(_mm_add_epi16(shade_fade, fgcolor), 8);
fgcolor = _mm_srli_epi16(_mm_mullo_epi16(fgcolor, shade_light), 8); fgcolor = _mm_srli_epi16(_mm_mullo_epi16(fgcolor, shade_light), 8);
<? } <? } ?>
}
fgcolor = _mm_add_epi16(fgcolor, _mm_srli_epi16(_mm_mullo_epi16(material, dynlight), 8));
fgcolor = _mm_min_epi16(fgcolor, _mm_set1_epi16(255));
<? }
function Blend($blendVariant) function Blend($blendVariant)
{ {

View file

@ -246,28 +246,41 @@ namespace swrenderer
float ly = (float)(lightX * ViewTanCos + lightY * ViewTanSin - pos.Y); float ly = (float)(lightX * ViewTanCos + lightY * ViewTanSin - pos.Y);
float lz = (float)(lightZ - pos.Z); float lz = (float)(lightZ - pos.Z);
bool is_point_light = (node->lightsource->flags4 & MF4_ATTENUATE) != 0; // Attenuated lights disabled for sprites for now to keep consistency with the GL renderer
//bool is_point_light = (node->lightsource->flags4 & MF4_ATTENUATE) != 0;
float LdotL = lx * lx + ly * ly + lz * lz; float LdotL = lx * lx + ly * ly + lz * lz;
float NdotL = is_point_light ? -ly : 0.0f; float NdotL = 1.0f;//is_point_light ? -ly : 1.0f;
float radius = node->lightsource->GetRadius(); float radius = node->lightsource->GetRadius();
if (radius * radius >= LdotL && NdotL > 0.0f) if (radius * radius >= LdotL && NdotL > 0.0f)
{ {
uint32_t red = light->GetRed();
uint32_t green = light->GetGreen();
uint32_t blue = light->GetBlue();
float distance = sqrt(LdotL); float distance = sqrt(LdotL);
float attenuation = distance / radius * NdotL; float attenuation = (1.0f - distance / radius) * NdotL;
lit_red += red * attenuation; if (attenuation > 0.0f)
lit_red += green * attenuation; {
lit_red += blue * attenuation; float red = light->GetRed() * (1.0f / 255.0f);
float green = light->GetGreen() * (1.0f / 255.0f);
float blue = light->GetBlue() * (1.0f / 255.0f);
/*if (light->IsSubtractive())
{
float bright = FVector3(lr, lg, lb).Length();
FVector3 lightColor(lr, lg, lb);
red = (bright - lr) * -1;
green = (bright - lg) * -1;
blue = (bright - lb) * -1;
}*/
lit_red += red * attenuation;
lit_green += green * attenuation;
lit_blue += blue * attenuation;
}
} }
} }
node = node->nextLight; node = node->nextLight;
} }
lit_red = MIN(lit_red, 255.0f); lit_red = clamp(lit_red * 255.0f, 0.0f, 255.0f);
lit_green = MIN(lit_green, 255.0f); lit_green = clamp(lit_green * 255.0f, 0.0f, 255.0f);
lit_blue = MIN(lit_blue, 255.0f); lit_blue = clamp(lit_blue * 255.0f, 0.0f, 255.0f);
vis->dynlightcolor = (((uint32_t)lit_red) << 16) | (((uint32_t)lit_green) << 8) | ((uint32_t)lit_blue); vis->dynlightcolor = (((uint32_t)lit_red) << 16) | (((uint32_t)lit_green) << 8) | ((uint32_t)lit_blue);
} }
else else