Ooops
April 07, 2020, 09:27:22 PM

Author Topic: Legend of Faerghail Remake  (Read 5997 times)

Offline iWasAdam

  • Hero Member
  • *****
  • Posts: 1557
Re: Legend of Faerghail Remake
« Reply #15 on: January 26, 2018, 01:52:48 PM »
how are you doing the lighting? Is it baked-in or realtime?

Offline Krischan

  • Full Member
  • ***
  • Posts: 211
    • Krischan's Homepage
Re: Legend of Faerghail Remake
« Reply #16 on: January 26, 2018, 03:42:02 PM »
Realtime. The level is based on a BSP with a lightmap and a shader applied to the textures. The "scripts/map.frag" is the shader for the level and the shader var "lightningintensity" controls the lightning. This value is mixed with the fog which is actually a depth value (far away=more dark). And in the main loop I use this code, the lightnings look pretty good I think:

Code: BlitzMax
  1. Global LI:Int = 0 ' Flag: Manual Lightning
  2. Global AM:Int = 1 ' Flag: Ambient Sounds
  3.  
  4. ' lightning variables
  5. Global lightning:Int = True
  6. Global lightningtimer:Int
  7. Global thunderlarge:Int = False
  8. Global lightningintensity:Int = 50
  9. Global lightninglight:Int = 512
  10. Global lightningshaderintensity:Float = 0.0
  11. Global thundertype:Int = Null
  12. Global oldthunder:Int = Null
  13. Global thundersound:TSound[6]
  14.  
  15. ' load thunder sounds
  16. For Local i:Int = 0 To 5
  17.  
  18.         thundersound[i] = LoadSound("sounds/thunder" + i + ".ogg")
  19.  
  20. Next
  21.  
  22. ' start a lightning
  23. If MilliSecs() > lightningtimer Or LI Or initflag Then
  24.  
  25.         ' create a new random lightning
  26.         lightning = True
  27.         lightningintensity = Rand(5, 30)
  28.         lightningtimer = MilliSecs() + Rand(0, 30000)
  29.         lightninglight = Rand(64, 256)
  30.        
  31.         ' store old thunder type
  32.         oldthunder = thundertype
  33.                        
  34.         ' randomly choose a new thunder type
  35.         Repeat
  36.        
  37.                 thundertype = Rand(1, 5)
  38.                
  39.         Until thundertype <> oldthunder
  40.                        
  41.         If initflag Then
  42.        
  43.                 lightningintensity = 25
  44.                 thundertype = 0
  45.                 lightninglight = 256
  46.                        
  47.         End If
  48.  
  49.         ' create a large thunder if there is a large amount of light
  50.         thunderlarge = False
  51.         If lightningintensity > 25 And lightninglight > 192 Then
  52.        
  53.                 thunderlarge = True
  54.                 thundertype = 0
  55.                
  56.         EndIf
  57.        
  58. EndIf
  59.  
  60. ' Execute Lightning
  61. If lightning Then
  62.  
  63.         ' use torchlight to increase lightning effect
  64.         torchintensity = 2.0
  65.  
  66.         ' decrease lightning light more than it increases
  67.         lightninglight:+Rand(-lightningintensity * 2, lightningintensity)
  68.                
  69.         ' limit lightning light
  70.         If lightninglight < 0 Then lightninglight = 0
  71.         If lightninglight > 255 Then lightninglight = 255
  72.        
  73.         ' Stop Lighting if there is no more light
  74.         If lightninglight = 0 Then lightning = 0
  75.                        
  76.         ' update shader
  77.         lightningshaderintensity = lightninglight / 16.0
  78.        
  79.         ' Play Thunder sound
  80.         If (Not ChannelPlaying(channel_thunder)) And AM = 1 Then
  81.        
  82.                 channel_thunder = CueSound(thundersound[thundertype])
  83.                 SetChannelVolume(channel_thunder, Rnd(0.125, 0.25))
  84.                 If thunderlarge Then SetChannelVolume(channel_thunder, Rnd(0.25, 0.5))
  85.                
  86.                 ResumeChannel channel_thunder
  87.                
  88.         EndIf
  89.  
  90. EndIf

The shader:
Code: [Select]
// en.wikibooks.org/wiki/GLSL_Programming/Blender/Lighting_of_Bumpy_Surfaces
// by Ferret

#define NUM_LIGHTS 2
varying mat3 localSurface2View;
varying vec4 texCoords;
varying vec4 lmCoords;
varying vec4 position;
uniform vec3 emission;
uniform float attspec;
uniform sampler2D colorMap;
uniform sampler2D normalMap;
uniform sampler2D lightMap;
uniform vec3 fogColor;

uniform float lightningshaderintensity;
uniform float torchintensity;
uniform int active;

void main()
{

vec4 encodedNormal = texture2D(normalMap, vec2(texCoords));
vec4 base = texture2D(colorMap, vec2(texCoords));
vec4 lm = texture2D(lightMap, vec2(lmCoords));

//lm *= 2.0;

vec3 localCoords = normalize(vec3(2.0, 2.0, 1.0) * vec3(encodedNormal) - vec3(1.0, 1.0, 0.0));
vec3 normalDirection = normalize(localSurface2View * localCoords);

vec3 viewDirection = -normalize(vec3(position));
vec3 lightDirection;
float attenuation;

vec3 totalLighting = (vec3(gl_LightModel.ambient) * emission);

for (int i = 0;i < NUM_LIGHTS;i++)
{
if (0.0 == gl_LightSource[i].position.w)
{
attenuation = 1.0;
lightDirection = normalize(vec3(gl_LightSource[i].position));
}
else // point light or spotlight (or other kind of light)
{
vec3 positionToLightSource = vec3(gl_LightSource[i].position - position);
float distance = length(positionToLightSource);
float lightdist = gl_LightSource[i].constantAttenuation
+ (gl_LightSource[i].linearAttenuation * distance)
+ (gl_LightSource[i].quadraticAttenuation * sqrt(distance));

attenuation = 1.0 / lightdist;
lightDirection = normalize(positionToLightSource);

if (gl_LightSource[i].spotCutoff <= 90.0)
{
float clampedCosine = max(0.0, dot(-lightDirection, gl_LightSource[i].spotDirection));

if (clampedCosine < gl_LightSource[i].spotCosCutoff)
{
attenuation = 0.0;
}
else
{
attenuation = attenuation * pow(clampedCosine, gl_LightSource[i].spotExponent);   
}
}
}

vec3 ambientLighting = ((vec3(gl_LightModel.ambient) * emission) + lm);

vec3 diffuseReflection = attenuation
* vec3(gl_LightSource[i].diffuse)
* emission
* max(0.0, dot(normalDirection, lightDirection));
 
vec3 specularReflection;
if (dot(normalDirection, lightDirection) < 0.0)
{
specularReflection = vec3(0.0, 0.0, 0.0);
}
else
{
specularReflection = vec3(attenuation * attspec)
* vec3(gl_LightSource[i].specular)
* vec3(gl_FrontMaterial.specular)
* pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)),
gl_FrontMaterial.shininess);
}

totalLighting += (ambientLighting * vec3(base) + diffuseReflection * vec3(base) + specularReflection) * (1.0 / NUM_LIGHTS);
}

// fog
float dist = distance(position, vec4(0.0, 0.0, 0.0, 1.0));
float density = (dist / 500.0) * 0.5;
const float e = 2.71828;
float fogFactor = (density * gl_FragCoord.z);
fogFactor *= fogFactor;
fogFactor = clamp(pow(e, -fogFactor), 0.0, 1.0);
totalLighting = mix(fogColor, totalLighting, fogFactor);

//totalLighting = vec3(lm.r, lm.g, lm.b);

// lightning strike
if (lightningshaderintensity > 0.0)
{
float luminance = 0.2126 * totalLighting.r + 0.7152 * totalLighting.g + 0.0722 * totalLighting.b;
totalLighting.r = max(totalLighting.r, luminance * lightningshaderintensity*(2.0*fogFactor));
totalLighting.g = max(totalLighting.g, luminance * lightningshaderintensity*(2.0*fogFactor));
totalLighting.b = max(totalLighting.b, luminance * lightningshaderintensity*(2.0*fogFactor));

//if (totalLighting.r < 0.05) totalLighting.r = lightningshaderintensity;
//if (totalLighting.g < 0.05) totalLighting.g = lightningshaderintensity;
//if (totalLighting.b < 0.05) totalLighting.b = lightningshaderintensity;
}

// switch: No bumpmapping
if (active == 0)
{
totalLighting = base * lm;
}

gl_FragColor = vec4(totalLighting*torchintensity, base.a);
}
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
My Blitzbasic Archive | Extrasolar Project | My Github projects

Online Steve Elliott

  • Hero Member
  • *****
  • Posts: 2305
  • elgol developer
Re: Legend of Faerghail Remake
« Reply #17 on: January 26, 2018, 04:06:42 PM »
The demo worked without any glitches and looked good, but as others have mentioned movement is too fast (even when running at 60FPS).

A nice atmosphere so progressing well.  I'm sure collisions will be added at some point ;)
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Catalina, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.3, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).

Offline GaborD

  • Sr. Member
  • ****
  • Posts: 330
Re: Legend of Faerghail Remake
« Reply #18 on: January 27, 2018, 11:21:03 AM »
Looks really good.
I like the overall color scheme, sets the mood well.


Offline iWasAdam

  • Hero Member
  • *****
  • Posts: 1557
Re: Legend of Faerghail Remake
« Reply #19 on: January 28, 2018, 02:52:28 PM »
Thanks for the update and code to have a quick look at  ;D

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal