0

How to implement a shader to create highlights/shadows ? I found this code for the highlights part (taken from https://gitlab.bestminr.com/bestminr/FrontShaders/blob/master/shaders/) but i can't find the equivalent for the shadows

varying vec4 coord;

uniform sampler2D texture;
uniform float highlights;

const float a = 1.357697966704323E-01;
const float b = 1.006045552016985E+00;
const float c = 4.674339906510876E-01;
const float d = 8.029414702292208E-01;
const float e = 1.127806558508491E-01;

void main() {
    vec4 color = texture2D(texture, coord.xy);
    float maxx = max(color.r, max(color.g, color.b));
    float minx = min(color.r, min(color.g, color.b));
    float lum = (maxx+minx)/2.0;
    float x1 = abs(highlights);
    float x2 = lum;
    float lum_new =  lum < 0.5 ? lum : lum+ a * sign(highlights) * exp(-0.5 * (((x1-b)/c)*((x1-b)/c) + ((x2-d)/e)*((x2-d)/e)));
    // gl_FragColor = color * lum_new / lum;
    gl_FragColor = vec4(color * lum_new / lum);
}
zeus
  • 11,289
  • 6
  • 52
  • 142
  • do you mean shadows in the sense of the dark side of an object or the shadows projected by an object? The first one is quite easy, you just need to use the normal of the fragment, if the cos(norma, lightDIr) < 0 then it's dark. To get projection shadows is more complicated. – florent teppe Jun 26 '18 at 12:15
  • @florentteppe I mean shadows in the sense of the dark side of an object ... – zeus Jun 26 '18 at 12:16
  • Possible duplicate of [OpenGL - vertex normals in OBJ](https://stackoverflow.com/questions/31850484/opengl-vertex-normals-in-obj) – Spektre Jun 27 '18 at 07:00

1 Answers1

1

To get the dark side of an object to be actually dark you need 3 things available: -the normal of your fragment -the position of your fragment -the position of your light -the intensity of the light after attenuation on your fragment

The general idea of the cos() calculation is that if cos>0 means that the light hits the front of the fragment, but if it's negative it means that the -lightDir gos the opposit direction to the normal, which means lightDir actually hits the fragment from the back.

in vec3 fragPos;
in vec3 fragNormal;

uniform vec3 lightPos

vec3 lightIntensity;


   vec3 lightShadowCalc(vec3 fragPos, vec3 fragNormal, vec3 lightPos, vec3 lightIntensity)
{
   vec3 lightDir = normalize(fragPos - lightPos);
   vec3 newIntensity = lightIntensity * cos(fragNormal, -lightDir)
   newIntensity = max(vec3(0), newIntensity);

   return newIntensity;

}

this should be something like that hope it helps.

florent teppe
  • 509
  • 5
  • 12