Frag shader:
out vec4 Color;
uniform samplerBuffer sampler;
uniform int index;
void main()
{
Color=normalize(texelFetch(sampler,index));
}
I am using glTexBuffer() for texturing for all the internal formats given at http://www.opengl.org/sdk/docs/man3/xhtml/glTexBuffer.xml. Above shader works only for ubyte, ushort normalised types and float, halffloat unnormalised types. For the rest of the internal formats, it does not apply texture on geometry.
What do I need to change to give me the desired effect?
code
GLbyte arr[]={124,5,126};
glGenBuffers(1,&bufferid);
glBindBuffer(GL_TEXTURE_BUFFER,bufferid);
glBufferData(GL_TEXTURE_BUFFER,sizeof(arr),arr,GL_STATIC_DRAW);
glGenTextures(1, &buffer_texture);
glBindTexture(GL_TEXTURE_BUFFER, buffer_texture);
glTexBuffer(GL_TEXTURE_BUFFER, GL_R8I, bufferid);
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);
I have to apply texture using all the internal formats.
If the “desired effect” is to have a single shader which can work with any format of image data, then there’s no way to do it. Or at least, no simple way.
You could have three different samplers, bound to three different texture image units, corresponding to the three different possible formats (float, signed-int, unsigned-int). You create 3 different buffer textures (using the same buffer), and bind the appropriate texture to appropriate sampler for the kind of data you want the shader to use. Then you pass a uniform that defines which sampler should be used.
But other than conditional logic like that, no. The sampler’s type is used to determine how to interpret the data given to the shader, and it must match with the texture’s format.
In general, a shader is designed to expect certain specific data, not any old arbitrary thing that the client code wants to hurl at it. A shader that pulls floats out of a texture doesn’t care if it’s normalized-integers or 16-bit floats, or R11F_G11F_B10F or whatever; the shader just wants floats. It’s not appropriate to shove images at a shader that expects floats.