Basic Specular Shading

前端 未结 1 1212
死守一世寂寞
死守一世寂寞 2021-01-16 22:07

I am playing around with a simple code that generates a shaded sphere. I don\'t yet fully understand the math but I am figuring it out as I play with the code. I was wonderi

相关标签:
1条回答
  • 2021-01-16 22:22

    To add reflection first you need mirror light vector by surface normal

    • reflections
    • all these are unit vectors:
    • l - (yellow) to light source
    • n - (aqua) surface normal
    • r - (green) reflected light direction
    • e - (orange) to eye/camera direction
    • points:
    • p - (red) - rendered pixel position
    • q - (magenta) - reflection midpoint
    • axises:
    • brown - reflection midpoint
    • yellow - to light
    • gray - to eye/camera

    So how to do it?

    • n,p,e,l are knowns or can be easily computed
    • if light is directional then l is constant for all pixels
    • if light source is a point then l=light_pos-p; l/=|l|;
    • e=eye_pos-p; e/=|e|;
    • now you need to find point q I use dot product for this q=p+(n*dot(l,n));
    • now the r vector is easy r=(p+l)+2*(q-(p+l))-p=2*(q-p)-l;
    • hope I did not make a silly math mistake/typo somewhere but it should be clear how I obtain the equations

    now you have reflection vector r

    • so add shading by multiplying pixel color by m=light_color*dot(r,e)+ambient_light;
    • to add specular spots you need add to m the maximal reflections which are present only near r
    • so ca=cos(ang)=dot(r,e); you do not need ang directly cosine is fine
    • now to limit the specular cone size do ca=pow(ca,5.0) can use any exponent
    • this will lower the values below one so it is much much less then raw cosine
    • the bigger the exponent the smaller spot cone size so now:
    • m=(light_color*0.5*(ca+dot(r,e)))+ambient_light;
    • you can also add mixing coefficients to change the ratio between specular and normal reflection light strength
    • now render pixel p with color=surface_color*m;

    Hope I didn't forget something it is a while I coded something like this...

    0 讨论(0)
提交回复
热议问题