Matt-Lloyd
Matt-Lloyd

Reputation: 117

GLSL Texturing Multiple Fragments

I am attempting to convert my fixed pipeline code (OpenGL ES 1.1) to one using a shader pipeline (OpenGL ES 2.0). However only one texture seems to work and I get the error:

Validation Failed: Sampler error:
Samplers of different types use the same texture image unit.
- or -
A sampler's texture unit is out of range (greater than max allowed or negative).

In my fragment shader I have a uniform texture ID and the texture coordinates coming through from my vertex shader:

varying lowp vec4 colorVarying;
varying lowp vec2 texCoordsVarying;

uniform sampler2D texID;

void main()
{
    lowp vec4 colour = texture2D(texID, texCoordsVarying);
    gl_FragColor = colour * colorVarying;
}

As I understand it since I'm only using one texture I only need one active texture unit. So in my pipeline I'm setting up the textures by activating the appropriate texture unit, binding the current texture and setting the uniform value for the texture ID to the texture unit. I then proceed to draw the vertexes.

Here's the texturing part of my pipeline:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ((MNTexturedRect*)object).texID);
glUniform1i(uniforms[UNIFORM_TEXTURE], GL_TEXTURE0);

glVertexAttribPointer(ATTRIB_TEXTURE_COORDS, 2, GL_FLOAT, GL_FALSE, object.dataPerPoint*sizeof(GLfloat), &points[((MNTexturedRect*)object).texCoordOffset]);
glEnableVertexAttribArray(ATTRIB_TEXTURE_COORDS);

I've an idea that the fact the texture ID is uniform is causing the problem, but not sure how to tackle it. I have several objects, and it appears only the first texture loaded actually seems to work. I only have on sampler and not quite sure why it would complain about using the same texture unit, and my texture unit is in range as it is just GL_TEXTURE0.

Anyone any ideas as to why this is happening?

Upvotes: 1

Views: 1379

Answers (1)

Bahbar
Bahbar

Reputation: 18005

The parameter for glUniform1i for samplers is in the range [0:MAX[, not [GL_TEXTURE0:GL_TEXTURE0+MAX[.

so change your code to:

glUniform1i(..., 0);

Upvotes: 5

Related Questions