There was an unpleasant problem in my program when I tried to use the same texture (number 0) for different types of textures (i.e. normal 2D texture and cube map) in my shader. It appeared so that GL returns 502H (Invalid Operation) after the first call to glDrawArrays. In my application code, I load textures into different texture targets:
void setup_textures() { unsigned int width, height; int components; unsigned int format; float param[8]; vector<unsigned char> pngData; GLenum texture_target; glGenTextures(2, textures); glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, param); for(int i = 0; i < 2; i++) { texture_target = (i == 0) ? (GL_TEXTURE_CUBE_MAP) : (GL_TEXTURE_2D);
In my rendering function, I bind the texture to its corresponding target and tell the shader which type of texture to use (using a unified bool):
void render(HDC hdc) { GLenum texture_target; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); for(int i = 0; i < 2; i++) { texture_target = (i == 0) ? (GL_TEXTURE_CUBE_MAP) : (GL_TEXTURE_2D); glBindVertexArray(objVertexArray[i]); glActiveTexture(GL_TEXTURE0); glBindTexture(texture_target, textures[i]); glUniform1i(uniIsCubeMap, texture_target == GL_TEXTURE_CUBE_MAP);
In the fragment shader, the texture type is determined based on the is_cube_map uniformity:
#version 330 core uniform sampler2D texture_map; uniform samplerCube texture_map_cube; uniform bool is_cube_map; smooth in vec3 texcoords; out vec4 fragcolor; void main(void) { vec4 texel; if(is_cube_map) { texel = textureCube(texture_map_cube, texcoords.stp); } else { texel = texture(texture_map, texcoords.st); } fragcolor = texel; }
I also set both forms of the texture sampler to 0 (texture unit number 0) in my application code:
glUniform1iARB(uniTextureMap, 0); glUniform1iARB(uniTextureMapCube, 0);
What is the problem? Is this really implausible?
source share