我正在尝试用 C 制作一个基本的光线追踪器,并决定使用一个.tga
文件作为输出渲染文件。但无论出于何种原因,创建的文件在我能找到的每个 tga 文件查看器中都显示为纯白色。我写入的文件的 hexdump 的开头是 here,很明显图像的大片区域应该是黑色的。
写入文件的循环是:
void raycast(uint16_t width, uint16_t height, FILE* file, Sphere* sphere) {
Vec3* camera = makeVec3(0, 0, 0);
Vec3* light = makeVec3(1, -1, -1);
int count = 0;
double zMax = 1 / cos(toRads(vFOV) / 2.0);
double yMax = 1 / cos(toRads(hFOV) / 2.0);
for (uint16_t i = 0; i < height; i++) {
for (uint16_t j = 0; j < width; j++) {
int16_t zOffset = (height / 2 - i);
double zprop = zOffset / (height / 2.0);
double zCoord = zMax * zprop;
int16_t yOffset = (width / 2 - j);
double yprop = yOffset / (width / 2.0);
double yCoord = yMax * yprop;
Vec3* coord = makeVec3(1, yCoord, zCoord);
normalize(coord);
Ray* ray = makeRay(camera, coord);
Vec3* intersect = sphereIntersect(sphere, ray);
if (intersect == NULL) {
fputc(255, file);
fputc(255, file);
fputc(255, file);
} else {
printf("disp black\n");
fputc(0x00, file);
fputc(0x00, file);
fputc(0x00, file);
count++;
}
}
}
printf("%d\n", count);
}
我看到disp black
打印到控制台,并count
增加到超过 300,000,因此将零写入文件显然没有任何问题。我尽力遵循我在这里找到的标题格式,这有问题吗?
我还注意到图像是我放入块中的任何内容的纯色if
,就好像if
总是采取声明一样。
编辑:编写标题的代码
static void writeHeader(FILE* file) {
static uint16_t width = 800;
static uint16_t height = 600;
// 800x600 image, 24 bits per pixel
unsigned char header[18] = {
0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00, 0x00,
(unsigned char)(width & 0x00FF), (unsigned char)((width & 0xFF00) / 256),
(unsigned char)(height & 0x00FF), (unsigned char)((height & 0xFF00) / 256),
0x08, 0x00
};
fwrite(header, sizeof(char), 18, file);
}