C++射线示踪剂ppm表示没有足够的数据来显示图像

C++ raytracer ppm says not enough data to display the image

本文关键字:数据 图像 显示图 显示 表示 ppm C++      更新时间:2023-10-16

制作光线跟踪器时,代码失败了,所以我决定重做整个过程并更改对象的逻辑,但现在由于某种原因,它一直给我一个错误,我已经尝试过多次编辑它。ImageMagick给我一个错误,说没有足够的数据来产生ppm除了关于对象的部分,ppm产生的主要逻辑没有太大变化,所以我真的不知道错误在哪里。main.cpp

vec color(const ray& r, float t, vec a, vec centre)
{
vector <Light> lighting;
lighting.push_back(Light(vec(0, 0, 50), vec(0, 0, -1)));
vec totalLight{0, 0, 0};
for(int i = 0; i <lighting.size(); i++){
if(t > 0.0){
vec hit = unit_vector(r.p_at_par(t) - centre);
vec L = unit_vector(lighting[i].position() - r.p_at_par(t));
vec R = L - 2.0*dot(L, hit)*hit;
vec S = vec(1, 1, 1)*pow(max(0.f, dot(R, vec(0, 0, -1))), 50);//Specular component
vec D = (a * max(0.f, dot(L, hit)) * 1.0);//Diffuse component
totalLight += S + D;
return totalLight;
}
}
}
float clamp(float a)
{
return (a > 255)? 255: (a < 0)? 0: a;
}
int main()
{
const int w = 200, h = 100;
FILE *fp;
fp = fopen("img.ppm", "wb");
fprintf(fp, "P6n%d %dn255n", w, h);
vec lower_corner(-2.0, -1.0, -1.0);
vec horizontal(4.0, 0.0, 0.0);
vec vertical(0.0, 2.0, 0.0);
vec origin(0.0, 0.0, 0.0);
vector <sphere> objects;
objects.push_back(sphere(vec(0,-100.5,-1), 100, vec(0, 1, 0)));
objects.push_back(sphere(vec(0, 0, -1), 0.5, vec(1, 0, 0)));
objects.push_back(sphere(vec(5, 5,-2), 3, vec(1, 0, 0)));
for(int j = h - 1; j >= 0; j--)
{
for(int i = 0; i < w; i++)
{
vec col(0, 0, 0);
static unsigned char pixel[3];
sphere* ClosestObject = NULL;
float u = float(i + random_double())/float(w);
float v = float(j + random_double())/float(h);
ray r(origin, lower_corner + u*horizontal + v*vertical);
float t = 0.0;
float t_near = 200000.0;
vec pixelColor(0.52 , 0.52 ,0.48);
for(int j = 0; j < objects.size(); j++)
{
if(t = objects[j].intersect(r))
{ 
if(t < t_near)
{
ClosestObject = &objects[j];
t_near = t;
}
}
if( t = 200000.0)
col = pixelColor;
else 
col = color(r, t, ClosestObject->color, ClosestObject->centre);
pixel[0] = int(clamp(col.r() * 255));
pixel[1] = int(clamp(col.g() * 255));
pixel[2] = int(clamp(col.b() * 255));
fwrite(pixel, 3, 1, fp);
}

}
fclose(fp);
return 0;
}
}

实际上,这是一条注释,但文本太多了…

你的压痕坏了。

因此,您没有注意到return 0;出现在外部for循环中,这一个:

for(int j = h - 1; j >= 0; j--)
{

以结尾

fclose(fp);
return 0;
}

此外,

fwrite(pixel, 3, 1, fp);

出现在最内环中

for (int j = 0; j < objects.size(); j++)

这也是IMHO的错误。

因此,生成的.ppm文件声称具有w×CCD_ 5像素,但它提供CCD_;CCD_ 7像素。

如果是objects.size() < h(我所期望的(,那么.ppm文件中的像素会太少,ImageMagick会注意到并抱怨。

你的源代码,在我的VS2017:中自动格式化

int main()
{
const int w = 200, h = 100;
FILE *fp;
fp = fopen("img.ppm", "wb");
fprintf(fp, "P6n%d %dn255n", w, h);
vec lower_corner(-2.0, -1.0, -1.0);
vec horizontal(4.0, 0.0, 0.0);
vec vertical(0.0, 2.0, 0.0);
vec origin(0.0, 0.0, 0.0);
vector <sphere> objects;
objects.push_back(sphere(vec(0, -100.5, -1), 100, vec(0, 1, 0)));
objects.push_back(sphere(vec(0, 0, -1), 0.5, vec(1, 0, 0)));
objects.push_back(sphere(vec(5, 5, -2), 3, vec(1, 0, 0)));
for (int j = h - 1; j >= 0; j--)
{
for (int i = 0; i < w; i++)
{
vec col(0, 0, 0);
static unsigned char pixel[3];
sphere* ClosestObject = NULL;
float u = float(i + random_double()) / float(w);
float v = float(j + random_double()) / float(h);
ray r(origin, lower_corner + u * horizontal + v * vertical);
float t = 0.0;
float t_near = 200000.0;
vec pixelColor(0.52, 0.52, 0.48);
for (int j = 0; j < objects.size(); j++)
{
if (t = objects[j].intersect(r))
{
if (t < t_near)
{
ClosestObject = &objects[j];
t_near = t;
}
}
if (t = 200000.0)
col = pixelColor;
else
col = color(r, t, ClosestObject->color, ClosestObject->centre);
pixel[0] = int(clamp(col.r() * 255));
pixel[1] = int(clamp(col.g() * 255));
pixel[2] = int(clamp(col.b() * 255));
fwrite(pixel, 3, 1, fp);
}

}
fclose(fp);
return 0;
}
}

请检查缩进并正确放置结束大括号。那么它应该像以前一样工作…