Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

glActiveTexture(GL_TEXTURE1) not working in shader

1,736 views
Skip to first unread message

VelociChicken

unread,
Mar 23, 2012, 1:58:05 PM3/23/12
to
Hi OpenGl people, I've been driven crazy by my deferred rendering tests.

I can't get the shader to recognise that there are two textures.

One texture is call 'randomTex' the other called 'positionsTex'
It uses two shaders 'progDrawTerrain' and 'progDeferred' and they both
use randomTex.
The 'randomTex' works fine in 'progDrawTerrain' because it's the only
texture reference.
It doesn't work in 'progDeferred'as it needs two textures, and it's
simply just accessing the first texture all the time instead.
I don't need texture coordinates in the randomTex, as it produces random
numbers for fractal code.

Any ideas why?

I've used the 'GetUniform' on 'randomTex' twice, once for each shader in
the setup, but that's just a test, and I assume that it's the same
variable in all shaders linked in.

Thanks,
Dave



// SETUP...

GLint randomTex_loc = glGetUniformLocation(progDrawTerrain,
"randomTex");
glUniform1iARB(randomTex_loc, randomTex);

randomTex_loc = glGetUniformLocation(progDeferred, "randomTex");
glUniform1iARB(random_loc, randomTex);
GLint pos_loc = glGetUniformLocation(progDeferred, "positionTex");
glUniform1iARB(pos_loc, positionsTex);

// LOOP...
glUseProgram(progDrawTerrain);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, randomTex);

// Do rendering of landscape etc...
...
GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT |
GL_DEPTH_ATTACHMENT_EXT};
glDrawBuffers(2, buffers);
//.................................................

// Now do the full screen deferred render...

glUseProgram(progDeferred);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glDisable(GL_DEPTH_TEST);

glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, positionsTex);

glActiveTexture(GL_TEXTURE1);
glBindTexture( GL_TEXTURE_2D, randomTex); // Doesn't work at
all, and ends up using the positions texture.

glColor4f(1,1,1,1);
glBegin(GL_QUADS);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0);
glVertex3f( -1, -1, 0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 0);
glVertex3f( 1, -1, 0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 1);
glVertex3f( 1, 1, 0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 1);
glVertex3f( -1, 1, 0);
glEnd();



VelociChicken

unread,
Mar 23, 2012, 4:48:18 PM3/23/12
to
OK, it turns out I should not have used the texture IDs for 'glBindTexture.'
It's a little bit confusing as to how this function actually works with
shaders... :(
Cheers
Dave.

Nobody

unread,
Mar 24, 2012, 6:23:30 PM3/24/12
to
On Fri, 23 Mar 2012 20:48:18 +0000, VelociChicken wrote:

> OK, it turns out I should not have used the texture IDs for 'glBindTexture.'
> It's a little bit confusing as to how this function actually works with
> shaders... :(

The value passed to glUniform1i for a uniform variable of sampler type is
the texture unit number, i.e. a value of N refers to the texture unit
which is selected by glActiveTexture(GL_TEXTURE0 + N).

glBindTexture associates a texture name (handle) with a particular
target in the active texture unit. There is an implementation-defined
limit on the number of available texture units, but the number of textures
is limited only by available memory. A shader accesses texture *units*
rather than textures per se, so it can only access textures which are
bound to a texture unit.

More generally: many dynamically-created entity types can only be
manipulated while bound to some "target"; textures, buffers, framebuffers,
renderbuffers and VAOs all fall into this category. OTOH, shaders,
programs and samplers can be queried and/or manipulated by name (handle)
without having to be bound to a target.

0 new messages