基本镜面反射着色 [英] Basic Specular Shading

查看:25
本文介绍了基本镜面反射着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个生成阴影球体的简单代码.我还没有完全理解数学,但我在玩代码时正在弄清楚.我想知道如何根据此代码实现镜面反射着色.有什么建议吗?

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 wondering how one might implement specular shading into this based on this code. Any suggestions?

for (y=0;y<screenHeight;y++)
for (x=0;x<screenWidth;x++)
if (sqr((x-xcenter)*(x-xcenter)+(y-ycenter)*(y-ycenter))<radius)
 {
 vx=(x-xcenter);
 vy=(y-xcenter);
 vz=sqr(radius*radius-vx*vx-vy*vy);
 vl=sqr(vx*vx+vy*vy+vz*vz);
 co_angle=(lx*vx+ly*vy+lz*vz)/(ll*vl);
 pixel=co_angle*255;
 }

我正在看这个帖子,第二张图片就是我想要的.但我也不完全理解那里的数学:Phong 着色问题

I was looking at this thread and the second image is what I am after. But I also don't fully understand the math there either: Trouble with Phong Shading

提前致谢.

推荐答案

要添加反射首先你需要通过表面法线的镜面光向量

  • 所有这些都是单位向量:
  • l -(黄色)到光源
  • n - (aqua) 表面法线
  • r -(绿色)反射光方向
  • e -(橙色)到眼睛/相机的方向
  • 要点:
  • p - (red) - 渲染像素位置
  • q - (洋红色) - 反射中点
  • 轴:
  • 棕色 - 反射中点
  • 黄色 - 点亮
  • 灰色 - 眼睛/相机
  • 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

那怎么做呢?

  • n,p,e,l 是已知的或容易计算的
  • 如果光是定向的,则 l 对于所有像素都是恒定的
  • 如果光源是一个点,则 l=light_pos-p;l/=|l|;
  • e=eye_pos-p;e/=|e|;
  • 现在你需要找到点 q 我用点积来做这个 q=p+(n*dot(l,n));
  • 现在r向量很简单r=(p+l)+2*(q-(p+l))-p=2*(qp)-l;
  • 希望我没有在某处犯下愚蠢的数学错误/错别字,但应该清楚我是如何获得方程的
  • 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

现在你有了反射向量 r

  • 因此通过将像素颜色乘以 m=light_color*dot(r,e)+ambient_light;
  • 添加阴影
  • 要添加镜面反射点,您需要将仅出现在 r
  • 附近的最大反射添加到 m
  • so ca=cos(ang)=dot(r,e); 你不需要 ang 直接余弦就可以了
  • 现在要限制镜面锥的大小,ca=pow(ca,5.0) 可以使用任何指数
  • 这会将值降低到 1 以下,因此它远小于原始余弦
  • 指数越大,光斑尺寸越小,所以现在:
  • m=(light_color*0.5*(ca+dot(r,e)))+ambient_light;
  • 您还可以添加混合系数来改变镜面反射和法线反射光强度之间的比率
  • 现在用 color=surface_color*m;
  • 渲染像素 p
  • 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...

这篇关于基本镜面反射着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆