gpt4 book ai didi

c++ - 使用光线追踪翻转球体上的 2D 纹理

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:16 24 4
gpt4 key购买 nike

我正在研究我的光线追踪器,我认为我已经取得了一些重大成就。我目前正在尝试将纹理图像放置到对象上。但是,它们的位置不太好。它们似乎在球体上翻转。这是我当前代码的最终图像:Look at how North and South America appears

相关代码如下:

-打开图像的图像类

class Image
{
public:
Image() {}

void read_bmp_file(char* filename)
{
int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header

// extract image height and width from header
width = *(int*)&info[18];
height = *(int*)&info[22];

int size = 3 * width * height;
data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);

for(i = 0; i < size; i += 3)
{
unsigned char tmp = data[i];
data[i] = data[i+2];
data[i+2] = tmp;
}
/*Now data should contain the (R, G, B) values of the pixels. The color of pixel (i, j) is stored at
data[j * 3* width + 3 * i], data[j * 3 * width + 3 * i + 1] and data[j * 3 * width + 3*i + 2].

In the last part, the swap between every first and third pixel is done because windows stores the
color values as (B, G, R) triples, not (R, G, B).*/
}

public:
int width;
int height;
unsigned char* data;
};

-纹理类

class Texture: public Material
{
public:
Texture(char* filename): Material() {
image_ptr = new Image;
image_ptr->read_bmp_file(filename);
}
virtual ~Texture() {}

virtual void set_mapping(Mapping* mapping)
{ mapping_ptr = mapping;}

virtual Vec get_color(const ShadeRec& sr) {
int row, col;

if(mapping_ptr)
mapping_ptr->get_texel_coordinates(sr.local_hit_point, image_ptr->width, image_ptr->height, row, col);

return Vec (image_ptr->data[row * 3 * image_ptr->width + 3*col ]/255.0,
image_ptr->data[row * 3 * image_ptr->width + 3*col+1]/255.0,
image_ptr->data[row * 3 * image_ptr->width + 3*col+2]/255.0);
}
public:
Image* image_ptr;
Mapping* mapping_ptr;
};

-映射类

class SphericalMap: public Mapping
{
public:
SphericalMap(): Mapping() {}
virtual ~SphericalMap() {}

virtual void get_texel_coordinates (const Vec& local_hit_point,
const int hres,
const int vres,
int& row,
int& column) const
{
float theta = acos(local_hit_point.y);
float phi = atan2(local_hit_point.z, local_hit_point.x);

if(phi < 0.0)
phi += 2*PI;

float u = phi/(2*PI);
float v = (PI - theta)/PI;

column = (int)((hres - 1) * u);
row = (int)((vres - 1) * v);
}
};

-本地生命值:

virtual void Sphere::set_local_hit_point(ShadeRec& sr)
{
sr.local_hit_point.x = sr.hit_point.x - c.x;
sr.local_hit_point.y = (sr.hit_point.y - c.y)/R;
sr.local_hit_point.z = sr.hit_point.z -c.z;
}

-这就是我在 main 中构建球体的方式:

Texture* t1 = new Texture("Texture\\earthmap2.bmp");
SphericalMap* sm = new SphericalMap();
t1->set_mapping(sm);
t1->set_ka(0.55);
t1->set_ks(0.0);
Sphere *s1 = new Sphere(Vec(-60,0,50), 149);
s1->set_material(t1);
w.add_object(s1);

抱歉,代码很长,但如果我知道问题可能发生在哪里,我会发布那部分。最后,这就是我从 main 调用 get_color() 函数的方式:

xShaded +=  sr.material_ptr->get_color(sr).x * in.x * max(0.0, sr.normal.dot(l)) + 
sr.material_ptr->ks * in.x * pow((max(0.0,sr.normal.dot(h))),1);
yShaded += sr.material_ptr->get_color(sr).y * in.y * max(0.0, sr.normal.dot(l)) +
sr.material_ptr->ks * in.y * pow((max(0.0,sr.normal.dot(h))),1);
zShaded += sr.material_ptr->get_color(sr).z * in.z * max(0.0, sr.normal.dot(l)) +
sr.material_ptr->ks * in.z * pow((max(0.0,sr.normal.dot(h))),1);

最佳答案

在黑暗中拍摄:如果没记错的话,BMP 是自下而上存储的,而许多其他图像格式是自上而下的。这可能是问题所在吗?也许您的文件阅读器只需要反转行?

关于c++ - 使用光线追踪翻转球体上的 2D 纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11780147/

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