基本镜面底纹底纹、镜面、基本

2023-09-09 21:19:26 作者:空有一丝愁°ˉ

我用一个简单的code产生阴影球玩耍。我还没有完全理解数学,但我想出来的,因为我用code发挥。我不知道怎么一有可能实现的镜面底纹这个在此基础上code。有什么建议?

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;
 }

我在看这个线程和第二图像是我后。但我也并不完全了解数学有两种:麻烦与海防阴影

在此先感谢。

推荐答案

添加倒影首先你通过表面需要镜面光矢量正常

所有这些都是单位向量: - (黄色)光源 N - (AQUA)表面法线 研究 - (绿色)反射光的方向 电子 - (橙色​​),以眼/摄像机的方向 点: P - (红) - 渲染像素位置 - (洋红色) - 反射中点 轴系: - 布朗反射中点 黄色 - 光 灰色的 - 眼睛/相机 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 是knowns还是可以很容易地计算出 如果光线方向则是恒定的所有像素 如果光源是一个点,则 L = light_pos-P; L / = | L |; E = eye_pos-P; E / = | E |; 现在,你需要找点我用点积为这个 Q = P +(N *点(L,N)); 现在研究矢量容易 R =(P + 1)+ 2 *(Q-(P + 1)) - P = 2 *(QP)-1; 希望我没有做一个愚蠢的数学错误/错字的地方,但应该清楚我是如何得到方程 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

所以由 M = light_color乘以像素的颜色添加底纹*点(R,E)+ ambient_light; 要添加您需要添加到m的最大感言这是present仅在靠近研究 高光点 让 CA = COS(ANG​​)=点(R,E); 不需要 ANG 直接余弦是好的 现在,来限制反射圆锥大小做 CA =战俘(约5.0)可以使用任何指数 这会降低下面的一个值,所以这是很远小于原材料余弦 所以现在更大的指数较小的光斑尺寸锥形: M =(light_color * 0.5 *(CA +​​点(R,E)))+ ambient_light; 您还可以添加混合系数改变镜面和正常反射光的强度之间的比率 现在,渲染像素的 P 颜色= surface_color *米; 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;

希望我没忘了什么东西它是一个,而我codeD这样的事情...

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