2013-05-17 47 views
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功能投射的光線進入我們的眼睛,整個場面(通過所有的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; 
} 

回答

0

不知道這是否是您所遇到的問題,但你應該只設置isFound如果你相交一個球體。如果不相交,不要設置它。否則,您的圖片將僅受列表中最後一個球體的支配。

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

由於您的圖像完全是黑色,因此您的交叉編碼看起來好像是堵塞或視野太窄。如果你對上述改變沒有任何喜悅,也許你應該在你的x和y極限,眼球位置以及球體的位置和半徑上發佈細節。

我注意到的另一件事是r.dir.z = 0.0。你是否從中減去了眼睛的位置以獲得方向,還是你真正的光線方向?當然你需要給一個非零的z方向。通常您根據您的視圖平面設置xy,並提供常數z,如1-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; 
+0

是的,圖像仍然是黑色的。 :(我會發布上面的限制 – Karen

+0

檢查我最後編輯的z方向爲零 – paddy

+0

嗯......哦!它說,視圖矩形的邊界是min_x,max_x,min_y和max_y( z座標是0)等一下,所以他們所指的z座標實際上不是光線方向的z座標? – Karen