4

In my program I use 2 textures: t0 and t1. t1 is additional, needed only in some cases:

.....
glActiveTexture ( GL_TEXTURE1 );
if ( mDisplayMode == EDM_DEFAULT ){
    glBindTexture( GL_TEXTURE_2D, 0 ); // In this case I don't need t1
}else{
    glBindTexture( GL_TEXTURE_2D, mglDegreeTexture ); // In this case t1 will overlap t0
}
shader->setUniformValue( "textureId1", 1 );

Shader for drawing:

.....
vec4 c1 = texture( textureId0, uv );
vec4 c2 = texture( textureId1, gridUV );
float a = c2.a;
gl_FragColor = ( 1.0 - a )*c1 + a*c2;

It's works fine: second texture overlap first when needed. Assuming that using glBindTexture( .. , 0 ) return zero texture (0,0,0,0), but after updating NVidia drivers to 314.07, my code procude black screen, like glBindTexture( .. , 0 ) return (0,0,0,1) texture.

I perform some test: With shader

....
vec4 c1 = texture( textureId0, uv );
vec4 c2 = texture( textureId1, gridUV );
float a = c2.a;
if (a == 1){
    gl_FragColor = vec4(1,0,0,0);
    return;
}
if ( a == 0){
    gl_FragColor = vec4(0,1,0,0);
    return;
}
gl_FragColor = ( 1.0 - a )*c1 + a*c2;

I get green screen on old driver (296, 306), and red on a new one. I tested it on 2 computers with Win 7 (GeForce 210) and win XP (GTX 260).

So my question is: It is correct to use glBindTexture with 0 as second parameter, and how I can achieve same result ( 0000 texture ) with a new driver?

Jeka
  • 1,364
  • 17
  • 33

1 Answers1

9

All the following refers to the core profile 3.2, but generally applies to all versions of GL.

The name 0 corresponds to the default texture objects (one per texture target: 1d, 2d, ... See 3.8.14, last paragraph).

So glBindtexture(2D, 0); gets you the default texture object. It does not mean disable texturing. Now texturing from the default object results in the usual operation, just on a texture object that you have not created yourself. The initial texture object is initially incomplete, so unless you modify it, it will behave following the "incomplete" rules.

Now the specification says (3.9.2, Texture Access paragraph) that sampling from an incomplete texture will return (0,0,0,1).

So your older drivers didn't behave according to the spec.

Bahbar
  • 17,760
  • 43
  • 62
  • Great! It was so logical to use (0,0,0,0) as "zero" texture ;( I fail to find out is there any way to define default texture? – Jeka Mar 07 '13 at 16:06
  • 1
    @Jeka: It's a texture like any other, so if you set it up as a e.g. 1x1 RGBA texture with its content as a single texel with 0,0,0,0, it will do what you're after. It's just not usual to use it that way, so you'll likely confuse anybody reading your code (oh, and potentially find more driver bugs) – Bahbar Mar 07 '13 at 16:12
  • Looks funny ;) 1x1 RGBA texture is my current solution, but I don't try to bind it with 0 – Jeka Mar 11 '13 at 07:57