C++射线示踪剂ppm表示没有足够的数据来显示图像
C++ raytracer ppm says not enough data to display the image
制作光线跟踪器时,代码失败了,所以我决定重做整个过程并更改对象的逻辑,但现在由于某种原因,它一直给我一个错误,我已经尝试过多次编辑它。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;
}
}
请检查缩进并正确放置结束大括号。那么它应该像以前一样工作…
相关文章:
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- OpenCV(C++)处理来自JavaScript / Web Assembly的图像数据
- VTK 图像数据,访问数组中的 1 个图像
- 处理来自IMFSourceReader和IMFSample的图像数据
- 如何返回定义良好的内存部分?例如来自图像数据的像素的颜色值
- 如何从amd gpus的显示输出中获取图像数据
- 如何将OpenCV图像数据从Python转换为C++?
- WIC - Exif 元数据查询 - 如何获取图像说明
- 这是我的程序,C++使用 OpenCv 库从数据集中读取图像序列,但它没有运行
- 以高数据速率发送图像数据超过提升::asio::udp?
- 如何将图像数据从搜索窗口中移动到本地存储器OpenCL
- 如何从 GIFLIB 保存图像结构中获取 RGB 颜色数据
- 如何将图像转换为C 中的RGB数据值
- 在C 中显示图像及其元数据
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- 表示图像矩阵的理想数据结构是什么?
- 如何从内存中读取原始图像 (YUV 4:2:2) 格式的图像数据
- 如何使用JNI + OpenCV + NDK获取原始的android相机传感器图像数据
- 使用Sapera拍摄图像并将数据复制到矢量
- 将图像数据尽快写入磁盘