2017-07-18 1 views
0

ich das folgende Fragment-Shader für eine Spiel-Engine schreibe:'Fehler: Sampler-Arrays indiziert mit nicht konstanten Ausdrücke werden in GLSL 1.30 verboten und später' in Fragment-Shader

#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; 
} 

Die Linie texColor = texture(textures[tid], fs_in.uv); die Ursachen " Sampler-Arrays, die mit nicht konstanten Ausdrücken indiziert sind, sind in GLSL 1.30 und späteren Ausdrücken verboten, wenn der Shader kompiliert wird.

Vertex-Shader, wenn erforderlich ist:

#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

Was ist unklar über das – Sopel

+0

Wie würde ich über die Lösung gehen? – user7889861

Antwort

5

In GLSL 3.3 Indizierung für sampler Arrays wird nur von einem integral constant expression erlaubt (siehe GLSL 3.3 Spec, Section 4.1.7).

In moderneren Version von GLSL starten 4.0 ihm zu indizieren Sampler-Arrays von dynamic uniform expressions erlaubt ist (siehe GLSL 4.0 Spec, Section 4.1.7)

Was Sie tatsächlich versuchen, zu indizieren ist das Array durch eine veränderliche, das ist einfach unmöglich. Wenn dies absolut unvermeidbar ist, könnten Sie die 2D-Texturen in eine 2D-Array-Textur oder in eine 3D-Textur packen und den Index verwenden, um die Ebene (oder die 3. Dimension) der Textur zu adressieren.

Verwandte Themen