Jump to content
  • Advertisement
Sign in to follow this  
empirical2

GLSL Referencing a texture unit by variable

This topic is 2132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!