2012-07-06 44 views
1

我正在編寫GLSL程序,作爲運行在閉源3D應用程序Maya內的插件的一部分。 Maya使用固定功能管道來定義它的燈光,所以我的程序必須使用兼容性配置文件從gl_LightSource陣列中獲取它的光信息。我的燈光評估工作正常(thanks Nicol Bolas)除了一件事,我不知道如何確定陣列中的特定燈是啓用還是禁用。以下是我迄今爲止:檢測glsl兼容性配置文件中是否啓用了gl_LightSource

#version 410 compatibility 

vec3 incidentLight (in gl_LightSourceParameters light, in vec3 position) 
{ 
    if (light.position.w == 0) { 
     return normalize (-light.position.xyz); 
    } else { 
     vec3 offset = position - light.position.xyz; 
     float distance = length (offset); 
     vec3 direction = normalize (offset); 
     float intensity; 
     if (light.spotCutoff <= 90.) { 
      float spotCos = dot (direction, normalize (light.spotDirection)); 
      intensity = pow (spotCos, light.spotExponent) * 
        step (light.spotCosCutoff, spotCos); 
     } else { 
      intensity = 1.; 
     } 
     intensity /= light.constantAttenuation + 
       light.linearAttenuation * distance + 
       light.quadraticAttenuation * distance * distance; 
     return intensity * direction; 
    } 
} 

void main() 
{ 
    for (int i = 0; i < gl_MaxLights; ++i) { 
     if (/* ??? gl_LightSource[i] is enabled ??? */ 1) { 
      vec3 incident = incidentLight (gl_LightSource[i], position); 
      <snip> 
     } 
    } 
    <snip> 
} 

當瑪雅使新的燈我的程序工作正常,但是當瑪雅禁用先前啓用的光,想必使用glDisable (GL_LIGHTi),它的參數是不是gl_LightSource陣列和gl_MaxLights在重置明顯不會改變,所以我的程序繼續在它的着色計算中使用那些陳舊的燈光信息。儘管我沒有在上面顯示它,但光線顏色(例如gl_LightSource[i].diffuse)在禁用後也會繼續顯示非零值。

Maya使用固定功能管線(無GLSL)繪製所有其他幾何圖形,並且這些對象正確地忽略禁用的燈光,我怎樣才能模仿GLSL中的這種行爲?

回答

1

不幸的是我看了GLSL規範,我沒有看到任何提供這些信息的東西。我也看到了another thread這似乎得出了相同的結論。

有什麼方法可以修改插件中的燈光值,或添加一個額外的可用作啓用/禁用標誌的統一標誌嗎?

+0

是的,在C++中,我可以測試所有燈的啓用狀態,並將其作爲單獨的統一信息傳遞或以某種方式重置它們的值。在規範中多麼奇怪的監督... – atb 2012-07-06 18:26:51

2
const vec4 AMBIENT_BLACK = vec4(0.0, 0.0, 0.0, 1.0); 
const vec4 DEFAULT_BLACK = vec4(0.0, 0.0, 0.0, 0.0); 

bool isLightEnabled(in int i) 
{  
    // A separate variable is used to get  
    // rid of a linker error.  
    bool enabled = true;  
    // If all the colors of the Light are set  
    // to BLACK then we know we don't need to bother  
    // doing a lighting calculation on it.  

    if ((gl_LightSource[i].ambient == AMBIENT_BLACK) &&   
     (gl_LightSource[i].diffuse == DEFAULT_BLACK) &&   
     (gl_LightSource[i].specular == DEFAULT_BLACK))   
    enabled = false;  
    return(enabled); 
}