在C ++中对球体的光线跟踪中的纹理映射

问题描述 投票:0回答:1

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

有人可以让我知道我对遮阳功能的错误吗?非常感谢左上角是我得到的球体的图片。而底部是世界地图的纹理。

c++ textures texture-mapping raytracing
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.