0

I want to transfer a lot of images loaded by stb_image to fragment shader by texture buffer object in OpenGL.

I first merge all the image data (every image file is in RGB format), and use one unsigned char pointer named data to point to them, this means all image data are put together in the format of RGBRGB...

Then, I used the following code to send all image data to fragment shader:

unsigned tbo, tex;
glGenBuffers(1, &tbo);
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_BUFFER, tex);
glTexBuffer(GL_TEXTURE_BUFFER, GL_R8UI, tbo);
glBindTexture(GL_TEXTURE_BUFFER, 0);
[...]
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glBufferData(GL_TEXTURE_BUFFER, len * sizeof(unsigned char),
            NULL, GL_DYNAMIC_DRAW); // len is the length of data
glBufferSubData(GL_TEXTURE_BUFFER, 0, len * sizeof(unsigned char), data);

and in rendering loop:

shader.use();
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glBindTexture(GL_TEXTURE_BUFFER, tex);
[...]

in my fragment shader:

layout (binding = 0) uniform samplerBuffer image_texture_buffer;
out vec4 FragColor;
[...]
void main(){
    FragColor = texelFetch(image_texture_buffer, 0);
}
[...]

But FragColor is always black.

Edited on 2021/4/3: I used this function to merge all the image data:

void merge_data(unsigned char *res, int lena, unsigned char *data, int lenb) {
    //cout << lena << ' ' << lenb << endl;
    res = (unsigned char *)realloc(res, lena + lenb);
    //strcat_s((char *)res, sizeof data, (char *)data);
    for (int i = lena, j = 0; j < lenb; ++i, ++j) res[i] = data[j];
}

In addition, someone told me that the video memory data must be aligned with even numbers, which means that I must transmit the data to the shader in the format of RGBARGBA..., Is this the effect?

yys_c
  • 15
  • 6
  • Check for OpenGL errors. – user14063792468 Apr 02 '21 at 17:14
  • Have you tried using the other implementations of ```texelFetch``` https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/texelFetch.xhtml – vikAy Apr 02 '21 at 23:45
  • @vikAy I haven’t tried other texelFetch implementations, but for samplerBuffer, isn’t only `texelFetch(samplerBuffer, int)` available? – yys_c Apr 03 '21 at 01:54
  • @vikAy The answer with the highest number of approvals on [this page](https://stackoverflow.com/questions/7954927/passing-a-list-of-values-to-fragment-shader) said that `texture buffer` is one-dimensional, so I chose this function( `gvec4 texelFetch(gsampler1D sampler, int P,int lod);`) to got pixels on the picture. – yys_c Apr 03 '21 at 02:04
  • @yys_c: It specifically says "Though they are one-dimensional, ***they are not 1D textures.***" – Nicol Bolas Apr 03 '21 at 02:16
  • @NicolBolas I'm sorry for not reading it carefully. I did see that it is not a one-dimensional texture, but what function is needed to get the data in the texture buffer?(I saw [this example](https://gist.github.com/roxlu/5090067) before, which puzzled me) – yys_c Apr 03 '21 at 02:28

0 Answers0