Results 1 to 7 of 7

Thread: Full precision RenderBuffer

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2011
    Location
    Girona, Catalonia
    Posts
    17

    Full precision RenderBuffer

    Hi guys,

    I would like to render to a RenderBuffer with "float" precision but I get "unsigned char" precision. If I put a value of 0.1 to a gl_FragColor I get 0.0980392.
    Why 0.0980392 instead of 0.1?
    If I want to represent 0.1 from [0, 1] to [0, 255] I get the value 25 because I can not represent 25.5. If I want to convert 25 to [0, 1] again, then it appears the magic value 0.0980392.

    I don't want a conersion to [0, 255], I want the full precision but I don't know what should I do. Some help please?

    This is the code:

    Initialization of renderbuffer and framebuffer:
    Code :
    GLuint renderBuffer;
    GLuint frameBuffer;
     
    glGenRenderbuffers( 1, &renderBuffer );
    glBindRenderbuffer( GL_RENDERBUFFER, renderBuffer );
    glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA, mWinWidth, mWinHeight );
    CHECK_GL_ERROR();
     
    glGenFramebuffers( 1, &frameBuffer );
    glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
    glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);
    if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        CECK_GL_ERROR();
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );

    Preparing for drawing:
    Code :
    glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
    glDrawBuffer(GL_COLOR_ATTACHMENT0);
    glViewport( 0, 0, mWinWidth, mWinHeight );
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    Getting the pixels:
    Code :
    float *pixels = new float [4 * mWinWidth * mWinHeight];
     
    glBindRenderbuffer( GL_RENDERBUFFER, renderBuffer );
    glReadBuffer(renderBuffer);
    glReadPixels(0, 0, mWinWidth, mWinHeight, GL_RGBA, GL_FLOAT, pixels);

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674

    Re: Full precision RenderBuffer

    Use a floating point image format instead of an unsigned normalized one.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2011
    Location
    Girona, Catalonia
    Posts
    17

    Re: Full precision RenderBuffer

    If I change this line:
    Code :
    glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA, mWinWidth, mWinHeight );
    For this one:
    Code :
    glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA32F, mWinWidth, mWinHeight );

    And this line:
    Code :
    glReadPixels(0, 0, mWinWidth, mWinHeight, GL_RGBA, GL_FLOAT, pixels);
    For this one:
    Code :
    glReadPixels(0, 0, mWinWidth, mWinHeight, GL_RGBA32F, GL_FLOAT, pixels);

    I get and GL_INVALID_ENUM after this line:
    Code :
    glReadBuffer(renderBuffer);
    And after this line:
    Code :
    glReadPixels(0, 0, mWinWidth, mWinHeight, GL_RGBA32F, GL_FLOAT, pixels);
    Should I change something else? What is wrong?

    Thanks

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Full precision RenderBuffer

    Quote Originally Posted by Limdor
    I get and GL_INVALID_ENUM after this line:
    Code :
    glReadBuffer(renderBuffer);
    And after this line:
    Code :
    glReadPixels(0, 0, mWinWidth, mWinHeight, GL_RGBA32F, GL_FLOAT, pixels);
    Because the fifth argument is the format which should be GL_RGBA not GL_RGBA32F.
    Regards,
    Mobeen

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2011
    Location
    Girona, Catalonia
    Posts
    17

    Re: Full precision RenderBuffer

    Quote Originally Posted by mobeen
    Because the fifth argument is the format which should be GL_RGBA not GL_RGBA32F.
    This solve the problem in line:
    Code :
    glReadPixels(0, 0, mWinWidth, mWinHeight, GL_RGBA32F, GL_FLOAT, pixels);
    But not in the line:
    Code :
    glReadBuffer(renderBuffer);
    Thx

  6. #6
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674

    Re: Full precision RenderBuffer

    "renderBuffer" is not an enumerator; it is an object. glReadBuffer tells OpenGL which attachment you want to read from. Not which object. That is, GL_COLOR_ATTACHMENT0, 1, 2, etc.

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2011
    Location
    Girona, Catalonia
    Posts
    17

    Re: Full precision RenderBuffer

    Thanks, problem solved.

    This is the final code:

    Initialization of renderbuffer and framebuffer:
    Code :
    GLuint renderBuffer;
    GLuint frameBuffer;
     
    glGenRenderbuffers( 1, &renderBuffer );
    glBindRenderbuffer( GL_RENDERBUFFER, renderBuffer );
    glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA32F, mWinWidth, mWinHeight );
    CHECK_GL_ERROR();
     
    glGenFramebuffers( 1, &frameBuffer );
    glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
    glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);
    if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        CECK_GL_ERROR();
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );

    Preparing for drawing:
    Code :
    glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
    glDrawBuffer(GL_COLOR_ATTACHMENT0);
    glViewport( 0, 0, mWinWidth, mWinHeight );
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    Getting the pixels:
    Code :
    float *pixels = new float [4 * mWinWidth * mWinHeight];
     
    glBindRenderbuffer( GL_RENDERBUFFER, renderBuffer );
    glReadBuffer(GL_COLOR_ATTACHMENT0);
    glReadPixels(0, 0, mWinWidth, mWinHeight, GL_RGBA, GL_FLOAT, pixels);

Similar Threads

  1. srgb Renderbuffer?
    By Hollis in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 10-11-2011, 08:55 AM
  2. FBO renderbuffer how it works
    By OGNewBee in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-24-2011, 01:01 AM
  3. how to present the renderbuffer?
    By killpig in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-20-2010, 12:22 AM
  4. FBO - texture or renderbuffer
    By zed in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 08-04-2007, 04:06 PM
  5. single precision floats vs. double precision
    By BigD in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 11-02-2001, 12:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean