2015-09-27 118 views
0

我在glsl中寫了一個射線追蹤器,它應該散射陰影(在這種情況下實際上只是由四個頂點構成的二維形狀),但是當代碼運行時,我看到球體的輪廓和包含它的正方形,而不僅僅是球體。我非常新的光線追蹤,所以我只是想弄清楚這是如何工作的。下面是什麼樣子(我添加了當使光標在範圍內移動了它的反應功能)Ray Tracer沒有正確渲染球體

enter image description here enter image description here

可能。這是什麼原因一些圖片?下面是一些來自片段着色器的相關代碼:

float raySphere(vec3 V, vec3 W, vec4 sph) { 
    float r = 1.0; 
    float b = 2.0* dot(V,W); 
    float c = dot(V, V) - (sph.w * sph.w); 
    float h = b*b - 4.0*c; 
    float t = (-b - sqrt(h))/2.0; 
    if(h <0.0 || t < 0.0) return 10000.; 
    return t; 
    } 

    // Diffusely shade a sphere. 
    // point is the x,y,z position of the surface point. 
    // sphere is the x,y,z,r definition of the sphere. 
    // material is the r,g,b color of the sphere. 
    vec3 shadeSphere(vec3 point, vec4 sphere, vec3 material) { 
     vec3 color = vec3(0.,0.,0.); 
     vec3 N = (point - sphere.xyz)/sphere.w; 
     float diffuse = max(dot(Ldir, N), 0.0); 
     vec3 ambient = material/5.0; 
     color = ambient + Lrgb * diffuse * max(0.0, dot(N , Ldir)); 
     return color; 
    } 

    void main(void) { 
     vec2 c = uCursor.xy; 
     Lrgb = vec3(2.,3.5,4.0); 
     Ldir = normalize(vec3(c.x, c.y, 1. - 2. * dot(c, c))); 
     float x = vPosition.x; 
     float y = vPosition.y; 
     float z = computeZ(vPosition.xy, 1.0); 
     // COMPUTE V AND W TO CREATE THE RAY FOR THIS PIXEL, 
     // USING vPosition.x AND vPosition.y. 
     vec2 uv = vPosition.xy/uCursor.xy; 

     //generate a ray 
     vec3 V, W; 
     //V = vec3(0.0,1.0,0.0); 
     //W = normalize(vec3(2.0,0.0,1.0)); 

     V = vec3(0.0, 1.0, 3.0); 
     W = normalize(vec3((-1.0 + 2.0 )*vec2(1.78,1.0), -1.0)); 

     //SET x,y,z AND r FOR sphere. 

     sphere = vec4(x,y,z,V + W); 

     //SET r,g,b FOR material. 

     vec3 color = vec3(5., 2., 3.); 
     float t = raySphere(V, W, sphere); 
     if (t < 10000.) 
     color = shadeSphere(V + t * W, sphere, material); 
     //color.r = 0.7; 

     color = pow(color, vec3(.45,.45,.45)); // Do Gamma correction. 

     gl_FragColor = vec4(color, 1.);  // Set opacity to 1. 
    } 

回答

1

的方式raySphere()隨你怎麼在main()t < 10000.)檢查命中沒有相處(-1)一起報告了一個小姐。

+0

是的,我一直在玩這個遊戲,因爲當我從''''''''''''''返回10000時,只顯示藍色方塊,但是當我返回-1時,我至少可以看到球體的輪廓當我將鼠標移到廣場上時。但我更新了代碼,以便兩者現在匹配。他們的任何其他明顯問題是否會導致只顯示廣場? – loremIpsum1771