Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 emt   Members   -  Reputation: 134

Like
0Likes
Like

Posted 11 January 2014 - 05:25 AM

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;
    }


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS