2013-10-08 4 views
0

내가 지금처럼 2D 조명을 시뮬레이션하는 간단한 조각 쉐이더를 가지고 :GLSL - 여러 표시등을 추가하려고합니까?

화면에 하나의 빛을두고
struct Light 
{ 
    vec2 pos;  // Light position 
    float spread; // Light spread 
    float size; // Light bulb size 
}; 

void main(void) 
{ 
    Light light0; // Define the light 
    light0.pos = iMouse.xy; // Set the position to mouse coords 
    light0.spread = 500.0; 
    light0.size = 200.0; 

    float x_dis = light0.pos.x - gl_FragCoord.x; 
    float y_dis = light0.pos.y - gl_FragCoord.y; 

    float intensity = sqrt(pow(x_dis, 2.0) + pow(y_dis, 2.0))-light0.size; // Pythagorean Theorem - size 
    if(intensity < 0.0) 
     intensity = 0.0; 
    else 
     intensity /= light0.spread; // Divide the intensity by the spread 

    gl_FragColor = vec4(1.0-intensity, 1.0-intensity, 1.0-intensity, 1.0); 
} 

. (당신은 여기에서 그것을 볼 수 있습니다 https://www.shadertoy.com/view/XsX3Wl)

나는 생각, "이봐, 그거 꽤 쉬웠다. 나는 단지 for 루프를 만들고 빛의 강도를 결합 할 것이다!"

는 그래서 내가 무슨 짓을했는지 :이 작동하지 않습니다 https://www.shadertoy.com/view/4dX3Wl로 볼 수 있습니다

struct Light 
{ 
    vec2 pos;  // Light position 
    float spread; // Light spread 
    float size; // Light bulb size 
}; 

void main(void) 
{ 
    Light lights[2]; 

    Light light0; 
    light0.pos = iMouse.xy; 
    light0.spread = 500.0; 
    light0.size = 200.0; 

    Light light1; 
    light0.pos = iMouse.xy; 
    light0.spread = 500.0; 
    light0.size = 200.0; 

    float intensity = 0.0; 
    for(int i = 0; i < 2; i++) 
    { 
     float x_dis = lights[i].pos.x - gl_FragCoord.x; 
     float y_dis = lights[i].pos.y - gl_FragCoord.y; 

     float sub_ints = sqrt(pow(x_dis, 2.0) + pow(y_dis, 2.0))-lights[i].size; // Intensity relative to this light 
     if(sub_ints < 0.0) 
      sub_ints = 0.0; 

     sub_ints /= lights[i].spread; // Divide the intensity by the spread 

     intensity += sub_ints; 
    } 

    gl_FragColor = vec4(1.0-intensity, 1.0-intensity, 1.0-intensity, 1.0); 
} 

. 하지만 혼란 스럽네요? 특정 픽셀에 빛의 강도를 어떻게 축적합니까?

답변

2
Light light1; 
light0.pos = iMouse.xy; 
light0.spread = 500.0; 
light0.size = 200.0; 

여기에서 light1을 초기화하지 않습니다. 스프레드가 0 일 수 있으며이를 통해 나눌 때 NaN을 생성합니다.

내가 복사 &을 알고 있지만 붙여 넣기 오류가 발생하지만 쉽게 알아낼 수 있습니다. SO에 게시하기 전에 코드를 자세히 검토해야합니다.

+0

이외에도 OP는 배열을 사용하지 않고 두 개의 추가 표시등을 선언합니다. – derhass

+0

당신 말이 맞아요. 너무나 많은 오류를 쉽게 기대하지 않았습니다. – Marius

+1

여기에 더 나은 시작이 있습니다 (Marius와 derhass의 주석을 코드로 바꾸는 것) : 라이트 라이트 [2]; lights [0] .pos = iMouse.xy; lights [0] .spread = 400.0; lights [0] .size = 50.0; lights [1] .pos = iMouse.xy + vec2 (100,100); lights [1] .spread = 100.0; lights [1] .size = 100.0; –

관련 문제