Binding a zero texture in OpenGL

人走茶凉 提交于 2019-12-06 08:44:34

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15273674/binding-a-zero-texture-in-opengl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!