我一直在閱讀大量關於光線追蹤和陰影的文章,但我的光線追蹤圖像看起來不太好。我正在談論鏡面高光附近非常明亮的綠色區域。結果綠色在這裏最大,看起來像。 如何調整顏色和/或陰影計算以使其看起來正確?光線追蹤 - 如何將漫反射和鏡面反射色相結合?
(不要介意愚蠢的代碼,我只是試圖讓原則正確第一)。
下面是它的外觀:
這裏只有擴散成分:
這裏只是鏡面分量:
編輯:改變漫反射到顏色diffuseColor = ColorMake(0.0f,0.6f,0.0f); 然後圖像看起來是這樣的:
Point lightPosition = PointMake(-100.0f, 100.0f, -100.0f);
Color diffuseColor = ColorMake(0.0f, 1.0f, 0.0f);
Color specularColor = ColorMake(1.0f, 1.0f, 1.0f);
Color pixelColor = ColorMake(0.0f, 0.0f, 0.0f);
// Trace...
// Diffuse
Point intersectionPosition = PointMake(x, y, z);
Vector intersectionNormal = VectorMake((x - xs)/rs, (y - ys)/rs, (z - zs)/rs);
Vector intersectionNormalN = VectorNormalize(intersectionNormal);
Vector lightVector = VectorSubtract(lightPosition, intersectionPosition);
VectorlightVectorN = VectorNormalize(lightVector);
float cosTheta = VectorDotProduct(intersectionNormalN, lightVectorN);
if (cosTheta < 0.0f)
{
cosTheta = 0.0f;
}
pixelColor = ColorMultScalar(diffuseColor, cosTheta);
// Specular
Vector incomVector = VectorSubtract(intersectionPosition, lightPosition);
Vector incomVectorN = VectorNormalize(incomVector);
float myDot = - VectorDotProduct(incomVectorN, intersectionNormalN);
float myLen = 2.0f * myDot;
Vector tempNormal = VectorMultScalar(intersectionNormalN, myLen);
Vector reflectVector = VectorAdd(tempNormal, incomVectorN);
Vector reflectVectorN = VectorNormalize(reflectVector);
float mySpec = MAX(-VectorDotProduct(reflectVectorN, incomVectorN), 0);
mySpec = powf(mySpec, 5);
specularColor = ColorMultScalar(specularColor, mySpec);
pixelColor = ColorAdd(pixelColor, specularColor);
pixelColor = ColorClamp(pixelColor);
[self putPixelatX:i andY:j andR:pixelColor.r andG:pixelColor.g andB:pixelColor.b];
這是不正確的,這是重點。不能有「正確性的保證」;) - 這實際上只是一種用便利的數學函數着色像素的方法(例如,用於phong的函數,以及用於環境的任意值)。至少這是我看到它的方式。最重要的是; PBRT FTW !! 1 – 2013-03-26 15:48:46
雖然你的觀點是100%有效的,但很多進入光線追蹤的人都沒有意識到這一點 - 他們通常被漂亮的圖片所左右,不知道需要一大堆的調整:) – 2013-03-26 18:44:51