1

我必须生成一个黑色圆圈的图像,黑色是 (0, 0 , 0),白色是 (1, 1, 1),但我一直得到一个完全黑色的图像。这是我的所有代码:

#include "cast.h"
#include "collisions.h"
#include <stdio.h>
#include "math.h"

int cast_ray(struct ray r, struct sphere spheres[], int num_spheres)
{
   int isFound;
   struct maybe_point mp;
   isFound = 0;

   for (int i = 0; i < num_spheres; i++)
   {
      mp = sphere_intersection_point(r, spheres[i]);

      if (mp.isPoint == 1)
      {
         isFound = 1;
      }
      else
      {
         isFound = 0;
      }
   }
   return isFound;
}

void print_pixel(double a, double b, double c)
{
   int i, j, k;

   i = a * 255;
   j = b * 255;
   k = c * 255;

   printf("%d %d %d ", i, j, k);
}

void cast_all_rays(double min_x, double max_x, double min_y, double max_y,
                  int width, int height, struct point eye,
                  struct sphere spheres[], int num_spheres)
{
   double width_interval, height_interval, y, x;
   int intersect;

   width_interval = (max_x - min_x)/width;
   height_interval = (max_y - min_y)/height;

   for (y = max_y; y > min_y; y = y - height_interval)
   {
      for (x = min_x; x < max_x; x = x + width_interval)
      {
         struct ray r;
         r.p = eye;
         r.dir.x = x;
         r.dir.y = y;
         r.dir.z = 0.0;

         intersect = cast_ray(r, spheres, num_spheres);

            if (intersect != 0)
            {
               print_pixel (0, 0, 0);
            }
            else
            {
               print_pixel (1, 1, 1);
            }
      }

我已经有了我知道是正确的函数,可以确定光线是否与球体相交。我用来查找交点的函数在函数 cast_ray 中。

    sphere_intersection_point(r, spheres[i]);

print_pixel 函数通过将整数值乘以最大颜色值(即 255)来转换整数值。

cast_all_rays 函数将光线从我们的眼睛投射到整个场景中(在更改 y 之前经过所有 x 坐标)。如果光线与球体相交,则像素为黑色,因此最终形成黑色圆圈。

以下是 x、y 和半径的限制(注意:我使用的是 PPM 格式):

Eye at <0.0, 0.0, -14.0>.
A sphere at <1.0, 1.0, 0.0> with radius 2.0.
A sphere at <0.5, 1.5, -3.0> with radius 0.5.
min_x at -10, max_x at 10, min_y of -7.5, max_y at 7.5, width=1024, and height=768.

我需要生成一个黑色圆圈的图像,但我不断得到一个完全黑色的图像。我有一种感觉,问题出在 cast_all_rays 函数中,但我似乎无法找到它是什么。帮助表示赞赏!谢谢。

以防万一我的测试出现问题,这是我的 cast_all_rays 的 test.c 文件:

#include "collisions.h"
#include "data.h"
#include "cast.h"
#include <stdio.h>

void cast_all_rays_tests(void)
{
   printf("P3\n");
   printf("%d %d\n", 1024, 768);
   printf("255\n");

   double min_x, max_x, min_y, max_y;
   int width, height;
   struct point eye;
   struct sphere spheres[2];

   eye.x = 0.0;
   eye.y = 0.0;
   eye.z = -14.0;
   spheres[0].center.x = 1.0;
   spheres[0].center.y = 1.0;
   spheres[0].center.z = 0.0;
   spheres[0].radius = 2.0;
   spheres[1].center.x = 0.5;
   spheres[1].center.y = 1.5;
   spheres[1].center.z = -3.0;
   spheres[1].radius = 0.5;
   min_x = -10;
   max_x = 10;
   min_y = -7.5;
   max_y = 7.5;

   cast_all_rays(min_x, max_x, min_y, max_y, width, height, eye, spheres, num_spheres);
}

int main()

{
   cast_all_rays_tests();

   return 0;
}
4

1 回答 1

0

不确定这是否是您遇到的问题,但您应该只isFound在与球体相交时设置。不相交就不要设置。否则,您的图像将仅由列表中的最后一个球体控制。

  if (mp.isPoint == 1)
  {
     isFound = 1;
  }
  //else
  //{
  //   isFound = 0;
  //}

由于您的图像完全是黑色的,因此您的交叉路口代码似乎是错误的,或者您的视野太窄了。如果您对上述更改不满意,也许您应该发布有关 x 和 y 限制、眼睛位置以及球体的位置和半径的详细信息。

我注意到的另一件事是r.dir.z = 0.0。你是从这个减去眼睛的位置来得到一个方向,还是你的真实光线方向?当然,您需要提供非零 z 方向。通常,您根据您的视图平面设置 和 并提供一个常量x,例如或。yz1-1

[编辑]

为了让下面的评论更清楚,我相信你没有正确设置你的光线方向。相反,您只需将方向设置为视图平面的像素位置,而忽略眼睛位置。以下会更常见:

     struct ray r;
     r.p = eye;
     r.dir.x = x - eye.x;
     r.dir.y = y - eye.y;
     r.dir.z = 0.0 - eye.z;
于 2013-05-17T04:10:20.533 回答