Ooops
October 17, 2021, 15:10:37

Author Topic: Faerghail (Remake)  (Read 9264 times)

Offline iWasAdam

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

Offline Krischan

  • Full Member
  • ***
  • Posts: 244
    • Krischan's Homepage
Re: Legend of Faerghail Remake
« Reply #16 on: January 26, 2018, 15:42:02 »
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);
}
« Last Edit: January 26, 2018, 15:44:45 by Krischan »
Kind regards
Krischan

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

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 3235
  • elgol
Re: Legend of Faerghail Remake
« Reply #17 on: January 26, 2018, 16:06:42 »
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, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry pi 3, pi 4, pi 400, BBC B, C64, ZX Spectrum

Offline GaborD

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


Offline iWasAdam

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

Offline Krischan

  • Full Member
  • ***
  • Posts: 244
    • Krischan's Homepage
Re: Legend of Faerghail Remake
« Reply #20 on: April 09, 2021, 19:27:06 »
Just a very small life sign of this project as I unfortunately spent more time on reading news than on coding and this project. In the last months, I've switched over to 64bit BlitzmaxNG and spent a lot of time working on my all-purpose PBR fragment shader, a test sandbox and PBR texture creation workflow. I've even managed to get BLIDE working with NG, it actually works fine in the portable mode :o

What you see here are actual live screenshots from my test engine - BlitzmaxNG + OpenB3D. In the next step I need to code a proper material load/unload system combined with a lightmapped BSP level loader which already exists but is not yet compatible for PBR materials. I've experimented with threaded loading to speed up the loading process as my PBR textures consists of three to four large files each (Diffuse, Normal, PBR with AO/Roughness/Metalness and optional Emission). The Flames/Candles are animated shader sprites, the golden MatMan and the Knights are metallic PBR textures while the walls are not-metallic. It is actually very easy to load 3rd party models - the models are taken from Sketchfab, imported in Blender, exported to Ultimate Unwrap as OBJ and then to B3D. The loader checks the first (diffuse) texture of a surfacem then loads the additional Normal/PBR textures and passes them to the shader.

In the sandbox, I've implemented a basic recyclement system of 8 OpenGL lights, the whole sandbox contains hundreds of lights which come to life only if they are close and in view. This needs a proper placement of lights to avoid flickering but works so far very well. Additional, there is a basic light flare occlusion system which sets the light flare sprites to Entityorder -1 if there is no obscurer in the camera view. It doesn't work with linepicks, I'm using a second, low resolution render pass on the depth map with colored areas here to find out if there is something blocking the view which is much faster (I had crashes in OpenB3D on the picking system so I had to write a different solution).
Kind regards
Krischan

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

Offline iWasAdam

  • Hero Member
  • *****
  • Posts: 2477
Re: Legend of Faerghail Remake
« Reply #21 on: April 12, 2021, 11:36:27 »
just one word: Lush!  ;D

Offline Zeotrope

  • Jr. Member
  • **
  • Posts: 27
Re: Legend of Faerghail Remake
« Reply #22 on: April 17, 2021, 13:43:36 »
looks really good :) great work

Offline markcwm

  • Hero Member
  • *****
  • Posts: 508
Re: Legend of Faerghail Remake
« Reply #23 on: April 17, 2021, 19:54:48 »
I'm not up to date in my 3d graphics experience but that looks ridiculously good! Can't really believe it's Openb3d. :o

I'll try to find that Linepick bug.

Offline Krischan

  • Full Member
  • ***
  • Posts: 244
    • Krischan's Homepage
Re: Legend of Faerghail Remake
« Reply #24 on: April 18, 2021, 14:50:39 »
I'm not up to date in my 3d graphics experience but that looks ridiculously good! Can't really believe it's Openb3d. :o

I'll try to find that Linepick bug.

I think I'm at least ten years behind current AAA games but yes it looks good so far for a homebrew indie project. For the linepick crash: I have currently no testbed scenario but I remember that it always happened when I tried to linepick a surface with many vertices like a level geometry, and "many" were about 10.000 to 30.000 I think. But like I said before, I've dumped linepick and use a second render pass which works better in my project.

If you don't believe that this is *your* OpenB3D mod, you can checkout this demo. If you have problems compiling the source, I've uploaded a ZIP with my portable lightweight BlitzmaxNG+BLIDE+OpenB3D development environment, this should work out of the box from any path, at least here 8) "Lightweight" means I've excluded Bruceys Modules in this ZIP as they're not needed yet for this demo to save space.

Download: ShaderSandboxDemo.zip
Download: BlitzMaxNGlight.zip
Kind regards
Krischan

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

Offline Krischan

  • Full Member
  • ***
  • Posts: 244
    • Krischan's Homepage
Re: Faerghail (Remake)
« Reply #25 on: June 17, 2021, 23:53:38 »
A small "update" just for fun. I've been playing around with Fuse and Mixamo to see how an animated character would look like in my shader sandbox dungeon. This is still BlitzmaxNG+OpenB3D ;D

And after I've found the Thriller animation sequence in Mixamo, I couldn't resist to create this video 8)

EDIT: new version with better camera angle, see four posts below for the video.
« Last Edit: June 18, 2021, 14:42:52 by Krischan »
Kind regards
Krischan

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

Offline Derron

  • Hero Member
  • *****
  • Posts: 3662
Re: Faerghail (Remake)
« Reply #26 on: June 18, 2021, 07:12:18 »
Seems your camera lens is not perfectly suited for this scene ... columns look distored.

The shadows are so harsh by indent (backside of the arms is "total black") or something your shaders create?


bye
Ron

Offline Krischan

  • Full Member
  • ***
  • Posts: 244
    • Krischan's Homepage
Re: Faerghail (Remake)
« Reply #27 on: June 18, 2021, 11:49:40 »
Yes, this is still the 120° FOV I'm using to run around, should have been 90° - I must create a more polished demo when I've fixed another issue.

Thanks for the notice with the "shadows" which are actual no shadows which are not implemented yet, if ever. It's just the NDotL value calculated in the shader to check which texels are facing toward the light source(s) and therefore are lit.

But there seem to be a bigger problem with the shading of models I didn't notice before. Placing the static model initially beside a light source - everything is lit correct (see 1.jpg) - the ground, the walls, the animated model. But animating the model - just turn it around in a dance move in the animation sequence and it seems that for some strange reason the normals don't turn around with the model - it's lit vice versa! (see 2.jpg) I think that's why the arms look so strange.

The model always stays at its position, only the vertices/faces move with the animation. Not animating the mesh and using TurnEntity mesh,0,1,0 instead results in a correct lighting while turning around beside a light source (see 3.jpg)

So I don't know how to fix this in the shader, as long as this is not a bug within OpenB3D. Any idea?
« Last Edit: June 18, 2021, 12:27:57 by Krischan »
Kind regards
Krischan

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

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 3235
  • elgol
Re: Faerghail (Remake)
« Reply #28 on: June 18, 2021, 13:15:51 »
That made me chuckle.   :))
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry pi 3, pi 4, pi 400, BBC B, C64, ZX Spectrum

Offline Krischan

  • Full Member
  • ***
  • Posts: 244
    • Krischan's Homepage
Re: Faerghail (Remake)
« Reply #29 on: June 18, 2021, 14:41:39 »
Uploaded a newer version with better camera angle, but still with the shading problem described above:



« Last Edit: June 18, 2021, 14:45:18 by Krischan »
Kind regards
Krischan

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

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal