Robadob / sdl_exp

Low-level graphics engine built over SDL2
MIT License
1 stars 2 forks source link

Environment Maps #52

Open Robadob opened 6 years ago

Robadob commented 6 years ago

This follows on from Materials/Lighting. Hopefully a quick branch for some nice resulting visuals.

https://learnopengl.com/Advanced-OpenGL/Cubemaps (high level pure reflection example + refraction) https://en.wikibooks.org/wiki/GLSL_Programming/GLUT/Glossy_Textures (Introduced shininessStrength, as a multiplier for level of glossiness, encoded in alpha channel of texture).

To access reflection sample we use

//Convert reflection vector back to world space
reflected = vec3(inverse(_viewMat) * vec4(reflected, 0.0));
//Sample environment map in that direction
frag_colour = texture(t_reflection, reflected);

Alternatively, replace reflect() with refract() to simulate light moving through materials of different light properties. Utilising the refractive index. This makes models appear transparent, rather than reflective/glossy. Refraction should actually occur twice (as the light exits the material too), however this is harder to achieve within complex models.

Material Refractive Index
Air 1.00
Water 1.33
Ice 1.309
Glass 1.52
Diamond 2.42

More@wikipedia

Ratio is calculated as source/destination.

Need to find more info on doing dynamic cubemaps, seen suggestion that rendering over a static skybox gives fastest results. Try this, although I can't see from their example how/if view matrix is transformed. Otherwise seems sensible. Another attempt here.

Robadob commented 6 years ago

Dynamic environment mapping now implemented in branch.

Robadob commented 6 years ago

Fixed the deer lighting issue on latest commit.

Fixed Bob model default shader reload bug, might not track custom textures but this isn't something I've really tested on Model's yet anyway (do they even support it??).

Robadob commented 6 years ago

This appears to have a clear example of using Fresnels law for localised reflection.

uniform float Kr, // intensity of reflection
uniform float KrMin, // typical: 0.05 * Kr
uniform float FresExp, // typical: 5.0

  float3 Nu = normalize(IN.LightingNormal);
   float3 Vu = normalize(IN.LightingEyeVec);
  float vdn = dot(Vu, Nb); // or "Nu" if unbumped - see text
   float fres = KrMin + (Kr - KrMin) * pow((1.0 - abs(vdn)), FresExp);
  float3 reflVect = normalize(reflect(Vu, Nb)); // yes, normalize
// now use the new intersection location as the 3D direction
    reflColor = fres * texCUBE(EnvMap, reflVect);

  float4 result = SurfColor * reflColor;

More: https://chetanjags.wordpress.com/2015/08/26/image-based-lighting/ https://learnopengl.com/PBR/IBL/Specular-IBL