2017-07-18 22 views
0

我寫以下片段着色器的一個遊戲引擎::在片段着色器「的錯誤與非常量表達式索引採樣器陣列被禁止在GLSL 1.30和後來」

#version 330 core 

layout (location = 0) out vec4 color; 

uniform vec4 colour; 
uniform vec2 light_pos; 

in DATA 
{ 
    vec4 position; 
    vec2 uv; 
    float tid; 
    vec4 color; 
} fs_in; 

uniform sampler2D textures[32]; 

void main() 
{ 
    float intensity = 1.0/length(fs_in.position.xy - light_pos); 

    vec4 texColor = fs_in.color; 

    if(fs_in.tid > 0.0){ 
     int tid = int(fs_in.tid + 0.5); 
     texColor = texture(textures[tid], fs_in.uv); 
    } 

    color = texColor * intensity; 
} 

texColor = texture(textures[tid], fs_in.uv);使線'在編譯着色器時,採用非常量表達式索引的採樣器數組在GLSL 1.30和更高版本的表達式中被禁止。

頂點着色器如果需要的話是:

#version 330 core 

layout (location = 0) in vec4 position; 
layout (location = 1) in vec2 uv; 
layout (location = 2) in float tid; 
layout (location = 3) in vec4 color; 

uniform mat4 pr_matrix; 
uniform mat4 vw_matrix = mat4(1.0); 
uniform mat4 ml_matrix = mat4(1.0); 

out DATA 
{ 
    vec4 position; 
    vec2 uv; 
    float tid; 
    vec4 color; 
} vs_out; 

void main() 
{ 
    gl_Position = pr_matrix * vw_matrix * ml_matrix * position; 
    vs_out.position = ml_matrix * position; 
    vs_out.uv = uv; 
    vs_out.tid = tid; 
    vs_out.color = color; 
} 
+3

什麼不清楚 – Sopel

+0

我該如何去解決它? – user7889861

回答

5

在GLSL 3.3分度爲採樣器陣列僅由一個integral constant expression允許(見GLSL 3.3 Spec, Section 4.1.7)。

在更現代的版本,從4.0 GLSL開始它是由dynamic uniform expressions允許指數採樣器陣列(見GLSL 4.0 Spec, Section 4.1.7

你實際上是在試圖什麼是指數由變這是根本不可能的陣列。如果這樣做絕對不可避免,那麼可以將2D紋理打包成2D紋理或3D紋理,並使用索引來處理紋理的圖層(或第三維)。

相關問題