Texturing using texelFetch ()

When I pass non-max values ​​to the texture buffer, when rendering it draws geometry with colors at maximum values. I found this problem when using glTexBuffer () API.

eg. Suppose my texture data is GLubyte, when I pass any value less than 255, then the color will be the same as the drawn 255, instead of a mixture of black and this color.
I tried on an AMD and nvidia card, but the results are the same. Can you tell me where the error may occur?

I copy my code here:

Vertical Shader:

in vec2 a_position; uniform float offset_x; void main() { gl_Position = vec4(a_position.x + offset_x, a_position.y, 1.0, 1.0); } 

Frag shader:

 out vec4 Color; uniform isamplerBuffer sampler; uniform int index; void main() { Color=texelFetch(sampler,index); } 

the code:

 GLubyte arr[]={128,5,250}; glGenBuffers(1,&bufferid); glBindBuffer(GL_TEXTURE_BUFFER,bufferid); glBufferData(GL_TEXTURE_BUFFER,sizeof(arr),arr,GL_STATIC_DRAW); glBindBuffer(GL_TEXTURE_BUFFER,0); glGenTextures(1, &buffer_texture); glBindTexture(GL_TEXTURE_BUFFER, buffer_texture); glTexBuffer(GL_TEXTURE_BUFFER, GL_R8, bufferid); glUniform1f(glGetUniformLocation(shader_data.psId,"offset_x"),0.0f); glUniform1i(glGetUniformLocation(shader_data.psId,"sampler"),0); glUniform1i(glGetUniformLocation(shader_data.psId,"index"),0); glGenBuffers(1,&bufferid1); glBindBuffer(GL_ARRAY_BUFFER,bufferid1); glBufferData(GL_ARRAY_BUFFER,sizeof(vertices4),vertices4,GL_STATIC_DRAW); attr_vertex = glGetAttribLocation(shader_data.psId, "a_position"); glVertexAttribPointer(attr_vertex, 2 , GL_FLOAT, GL_FALSE ,0, 0); glEnableVertexAttribArray(attr_vertex); glDrawArrays(GL_TRIANGLE_FAN,0,4); glUniform1i(glGetUniformLocation(shader_data.psId,"index"),1); glVertexAttribPointer(attr_vertex, 2 , GL_FLOAT, GL_FALSE ,0,(void *)(32) ); glDrawArrays(GL_TRIANGLE_FAN,0,4); glUniform1i(glGetUniformLocation(shader_data.psId,"index"),2); glVertexAttribPointer(attr_vertex, 2 , GL_FLOAT, GL_FALSE ,0,(void *)(64) ); glDrawArrays(GL_TRIANGLE_FAN,0,4); 

In this case, he draws all 3 squares with a dark red color.

+7
source share
1 answer
 uniform isamplerBuffer sampler; glTexBuffer(GL_TEXTURE_BUFFER, GL_R8, bufferid); 

There is your problem: they do not match.

You created a texture repository as unsigned 8-bit integers that are normalized to float when reading. But you told the shader that you are giving it signed 8-bit integers, which will be considered integers, not floats.

You confuse OpenGL by being inconsistent. Mismatch of sample types with texture formats leads to undefined behavior.

This should be samplerBuffer , not isamplerBuffer .

+2
source

All Articles