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 の制限、目の位置、球の位置と半径の詳細を投稿する必要があります。

もう1つ気になったのは、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 に答える