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

C++ 光线跟踪中的纹理映射球体,c++,graphics,texture-mapping,raytracing,C++,Graphics,Texture Mapping,Raytracing,我正在尝试使用带有cimg的.bmp图像对球体进行纹理处理,但纹理处理不正确。 这是纹理图像 这就是结果 这是我计算交点处颜色的代码 virtual RGBColor getTexture(Point3D pos){ //normal of intersection point and sphere Vector3D N = this->getNormal(pos); N.normalize(); double u = 0.5 + atan2(N.ge

我正在尝试使用带有cimg的.bmp图像对球体进行纹理处理,但纹理处理不正确。 这是纹理图像

这就是结果

这是我计算交点处颜色的代码

virtual RGBColor getTexture(Point3D pos){
    //normal of intersection point and sphere
    Vector3D N = this->getNormal(pos);
    N.normalize();


    double u = 0.5 + atan2(N.getY(), N.getX()) / (2 * 3.141592653589793);
    double v = 0.5 - asin(N.getY()) / 3.141592653589793;

    CImg<int> image("pattern.bmp");
    float width = u * 150;//105 is image width
    float height = v * 105;
    float r;
    float g;
    float b;

     r = image(width, height, 0, 0);
     g = image(width, height, 0, 1);
     b = image(width, height, 0, 2);

    return(RGBColor(r,g,b));
}
虚拟RGBColor getTexture(Point3D pos){
//交点与球面的法线
Vector3D N=此->获取法线(位置);
N.正常化();
双u=0.5+atan2(N.getY(),N.getX())/(2*3.141592653589793);
双v=0.5-asin(N.getY())/3.141592653589793;
CImg图像(“pattern.bmp”);
float width=u*150;//105是图像宽度
浮子高度=v*105;
浮子r;
浮球g;
浮球b;
r=图像(宽度、高度、0、0);
g=图像(宽度、高度,0,1);
b=图像(宽度、高度、0、2);
返回(RGB颜色(r,g,b));
}
Pos是交叉点。
如果“105是图像宽度”,那么为什么要乘以150呢?为什么不使用
image.width()
image.height()
来确保这些值是正确的?为什么不添加一些断言以确保
u
v
正确规范化?感谢您的回复。105是一个拼写错误,我对Cimg不是很熟悉,所以我不知道.width()。我添加了u、v、宽度和高度的断言语句,但仍然得到相同的结果为什么球体是长方形(比高宽)?这表明你还有其他的几何问题。我想这是因为我的视野和屏幕宽度设置不正确造成的,但我会调查一下。谢谢,我觉得自己像个傻瓜。我的向量getXYZ方法都返回z。非常感谢你的帮助