[英]Texture mapping in a ray tracing for sphere in C++
我在C ++中設置了一個簡單的光線跟蹤。 我想將紋理映射添加到球體。 它基本上只是將紋理從PPM文件映射到球體。 下面是我的代碼部分。
//Call shaderay from trace ray function
// index_of_winning_object = index of scene object array
// point = intersection point
Color shadeRay (int index_of_winning_object, Vect point, Ray ray){
double final_index2;
//if no intersection, return the background color (double confirm)
if (index_of_winning_object == -1) {
return bkgcolor;
}else{
Vect c = scene_objects[index_of_winning_object].getSphereCenter();
Vect p = point;
//Normal to the intersection point and sphere
Vect N = p.vectSub(c).normalize();
//Calculating the texture coordinate for a sphere
double temp = acos(N.getVectZ());
double temp2 = atan2(N.getVectY(), N.getVectX());
//u,v
double v = temp / 3.141592653589793;
if (temp2 < 0) {
temp2 = temp2 + (2 * 3.141592653589793);
}
double u = temp2 / (2 * 3.141592653589793);
// get_ppm_width = width of the sample texture ppm file like (picture.ppm)
// get_ppm_height = height of the sample texture ppm file like (picture.ppm)
int width = u * get_ppm_width;
int height = v * get_ppm_height;
//calculating the pixel of the ppm file, I store the pixel in get_array in RGB struct
// ___ ___ ___ ___ ___ ___ ___
// 0 1 2 3 4 5 6
// 7 8 9 10 11 12 13
// ___ ___ ___ ___ ___ ___ ___
// above is the example of the get_array. get_array is a one dimensional array of RGB struct
int px = height + (width * get_ppm_width);
// Get the color from the get_array
Color final (get_array[px].r, get_array[px].g, get_array[px].b );
return final;
}
}
有人可以讓我知道我對陰影功能做錯了什么嗎? 非常感謝左上方是我得到的球體的圖片。 而底部則是世界地圖的紋理。
計算N之后,嘗試使用以下算法計算(u,v):
u = 0.5 + arctan2(dz, dx) / (2*pi)
v = 0.5 - arcsin(dy) / pi
我懷疑那是你的問題。
嘗試通過僅使用UV坐標生成顏色而不是從紋理中拾取顏色來隔離問題。 您可以嘗試:
int red = u % 255;
int green = 0;
int blue = 0;
檢查結果。 它們是您期望看到的嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.