OpenGL(GLSL) Gradient with gsamplerBuffer:Reading buffer data in different formats

Started by
-1 comments, last by emt 10 years, 3 months ago

I am trying to use same buffer and reading it in two different formats.

The intention is to obtain a gradient at output, but not getting the desired output.

Using texelFetch(...),glTexBuffer(...) and gsamplerBuffer .

Kindly suggest the corrections/changes that may be required. Below are the relevant code snippets,


    // Relevant C code snippet
    // Fill the texture buffer data for gradient
    #define BUFFER_SIZE 256
    char* buffer = (char*)malloc(BUFFER_SIZE);
    for (i = 0; i < 32; i++)
    {
        buffer[2 * i]= (float)((float)i * 0.5 / 31.0);// left to right gradient 0....0.5
        buffer[(2 * i + 1) * 4 + 0] = (char)(128.0 / 31.0 * (float)(31 - i));//inverted gradient 0.5...0
        buffer[2 * (i + 1) * 4 + 1] = (char)(128.0 / 31.0 * (float)(31 - i));//inverted gradient 0.5...0
        buffer[2 * (i + 1) * 4 + 2] = (char)(128.0 / 31.0 * (float)(31 - i));//inverted gradient 0.5...0
        buffer[(2 * i + 1) * 4 + 3] = (char)255;        
    }
------------------------------------------------------------------------------------------------------------
    // Generate the two texture buffer
    glGenBuffers( 2,texBuffObj);
    glGenTextures(2, textureID);
    // Fill the data into buffer as GL_RGBA8
    glBindBuffer( GL_TEXTURE_BUFFER,texBuffObj[0]);
    glBufferData( GL_TEXTURE_BUFFER,BUFFER_SIZE,buffer,GL_DYNAMIC_DRAW);
    glBindTexture(GL_TEXTURE_BUFFER, textureID[0]);
    glTexBuffer(GL_TEXTURE_BUFFER,GL_RGBA8,texBuffObj[0]);
    // Fill the data into buffer as GL_RGBA32F
    glBindBuffer( GL_TEXTURE_BUFFER,texBuffObj[1]);
    glBufferData( GL_TEXTURE_BUFFER,BUFFER_SIZE,buffer,GL_DYNAMIC_DRAW);
    glBindTexture(GL_TEXTURE_BUFFER, textureID[1]);
    glTexBuffer(GL_TEXTURE_BUFFER,GL_RGBA32F,texBuffObj[1]);
------------------------------------------------------------------------------------------------------------
    #define BUFFER_OFFSET(offset) ((void *)(offset))
    // Use the GL_RGBA8 buffer in the draw call
    glBindTexture(GL_TEXTURE_BUFFER, textureID[0]);
    glDrawElements(GL_TRIANGLES, 3 , GL_UNSIGNED_SHORT, 0);
    // Use the GL_RGBA32F buffer in the draw call
    glBindTexture(GL_TEXTURE_BUFFER, textureID[1]);
    glDrawElements(GL_TRIANGLES, 3 , GL_UNSIGNED_SHORT, BUFFER_OFFSET(3,GLushort));    
------------------------------------------------------------------------------------------------------------
    //Fragment shader code
    #version 330 core
    uniform usamplerBuffer samplerRGBA8;
    uniform usamplerBuffer samplerRGBA32F;
    // Ouput data
    out vec4 color;
    in vec2 vs_posCoord;//not the texture coordinates
    void main()
    {
        vec4 colBuffRGBA8;
        vec4 colBuffRGBA32F;
        int offset = int(vs_posCoord.x + vs_posCoord.y) % 32;
        colBuffRGBA32F = texelFetch(samplerRGBA32F,int(offset)) ;
        colBuffRGBA8 = texelFetch(samplerRGBA8,int(offset) + 1);
        color = vec4(colBuffRGBA32F) + colBuffRGBA8;
    }

This topic is closed to new replies.

Advertisement