GLSL Referencing a texture unit by variable

Started by
1 comment, last by empirical2 10 years, 7 months ago
Basically before I abandon this I just want to check if what I am doing is even possible. I have tried googling but guess im not using the right keywords.

This is my shader

[source]$VERTEX

varying vec2 vTexCoord;
varying vec4 vColor;
varying vec3 vertex_light_position;
varying vec3 vertex_normal;

void main(void)
{
vertex_normal = normalize(gl_NormalMatrix * gl_Normal);
vertex_light_position = normalize(gl_LightSource[0].position.xyz);
vTexCoord = gl_MultiTexCoord0;
vColor=gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}



$FRAGMENT

uniform sampler2D Texture[8];

varying vec2 vTexCoord;
varying vec4 vColor;
varying vec3 vertex_light_position;
varying vec3 vertex_normal;

void main (void)
{
int i1=int(vColor.r*10);
int i2=int(vColor.g*10);

vec4 c1= texture2D(Texture[i1], vTexCoord).rgba;
vec4 c2= texture2D(Texture[i2], vTexCoord).rgba;

float diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0)*1.3;
gl_FragColor=((c1*vColor.b)+(c2*(1.0-vColor.b)))*(diffuse_value+0.3);
gl_FragColor.a=1.0;
} [/source]

What am I trying to do?

Each triangle in the VBO is a blend of two textures. But the two textures vary with each triangle. I am trying to specify which two textures to use in the R & G color components. With conditional branching instead it works as I want, but I wanted to make it more efficient and scale-able for the future.

When I try to use the above code it compiles OK, but fails to link. I am unable to retrieve a link log.
However, if I use constants (e.g i1=0; i2=1) it compiles and links. I assume because all that is optimized away.

Im new to shaders so Im hoping someone will cast an eye over and say either "You cant do that" in which case I can switch to branching or look further into texture arrays, or "you can do that" in which case Ill plod on with it.

Cheers.
Advertisement

Looks like you want texture arrays; clicky.

Thanks very much that did the trick!

This topic is closed to new replies.

Advertisement