【问题标题】:Texture mapping in a ray tracing for sphere in C++C++ 中球体光线追踪中的纹理映射
【发布时间】:2019-04-12 04:30:39
【问题描述】:

我在 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 功能上做错了什么吗?非常感谢 左上角是我得到的球体的图片。而底部是世界地图的纹理。

【问题讨论】:

  • 可以上传截图吗?更好地了解您正在获得什么以及您希望实现什么会有所帮助。
  • @Liam M 我尝试上传,但因为我没有足够的声望来发布照片,所以无法上传。

标签: c++ textures texture-mapping raytracing


【解决方案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;

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

【讨论】:

  • 我很确定我的教授给出的计算是正确的。但是,我已经尝试过了,它给了我同样错误的图像。如果可能的话,你能告诉我,我的 shaderay 算法是正确的吗?谢谢
  • 图片哪里不对?这里有一个建议:与其从 PPM 中选择像素,不如尝试以程序方式生成颜色(作为 (u,v) 的函数)以确保您的 UV 映射正确,并将下面的代码隔离为问题的根源。
  • 感谢您的回复。但是你能解释更多关于如何在程序上生成颜色的信息吗?我不知道如何做到这一点。谢谢
  • 查看修改后的答案:)。
  • 我试过了,它产生了一个红色的球体。
猜你喜欢
  • 2012-06-03
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 1970-01-01
  • 2017-03-26
  • 2019-11-29
相关资源
最近更新 更多