gpt4 book ai didi

c++ - C++ 球体光线追踪中的纹理映射

转载 作者:行者123 更新时间:2023-11-28 07:01:26 30 4
gpt4 key购买 nike

我已经在 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;
}
}

谁能告诉我我在 shaderay 函数上做错了什么?多谢左上角是我得到的球体的图片。而底部是世界地图的纹理。 enter image description here

最佳答案

计算 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;

检查结果。它们是您期望看到的吗?

关于c++ - C++ 球体光线追踪中的纹理映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22420778/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com