我不得不生成一個黑圈的圖像,黑色的是(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功能投射的光線進入我們的眼睛,整個場面(通過所有的X會在改變y之前的座標)。如果光線與球體相交,則像素爲黑色,因此最終形成黑色圓圈。
這裏是在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 test1.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;
}
是的,圖像仍然是黑色的。 :(我會發布上面的限制 – Karen
檢查我最後編輯的z方向爲零 – paddy
嗯......哦!它說,視圖矩形的邊界是min_x,max_x,min_y和max_y( z座標是0)等一下,所以他們所指的z座標實際上不是光線方向的z座標? – Karen