Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Fragment shader output range: Can fragment shader output be out [0.0, 1.0]?

  1. #1
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    10

    Fragment shader output range: Can fragment shader output be out [0.0, 1.0]?

    Hi all,

    Iím wondering the range of values that GLSL fragment shader can output.

    I had an experiment that I couldnít get data out of [0.0, 1.0] from fragment shader even if I used float-point framebuffer. The processing is following:
    a. create a framebuffer with float-point texture attachment.
    b. Bind this framebuffer;
    c. Use shader program, and set the gl_FragColor = vec4(-1.5, 1.5, 0.0, 0.0);
    d. Get the data of the texture buffer, and find the data is {0.0, 1.0, 0.0};

    Is there any way to get data out of [0.0, 1.0] from fragment shader? Thx!

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2009
    Posts
    268
    This should work, unless you have fragment color clamping enbled or read color clamping enabled.
    That or you are doing something else wrong - show the code if you can.

  3. #3
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    10
    virtual void OnInit()
    {
    glewInit();

    glClearColor(0.5f, 0.5f, 1.0f, 0.0f);

    // load (and compile, link) from file
    shader = SM.loadfromFile("vertexshader.txt","fragmentshader .txt");
    if (shaderRGB2YUV==0)
    std::cout << "Error Loading, compiling or linking shader\n";

    glGenTextures(1, &tempTex);
    glBindTexture(GL_TEXTURE_2D, tempTex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 640, 480, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glBindTexture(GL_TEXTURE_2D, 0);

    glGenFramebuffersEXT(1, &Fbo);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,Fbo);
    glFramebufferTextureEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,tempTex,0);
    if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)
    return ;
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);

    glEnable(GL_TEXTURE_2D);
    }
    virtual void OnResize(int w, int h)
    {
    if(h == 0) h = 1;

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    virtual void OnRender(void)
    {
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, Fbo);

    shader->begin();
    glBegin(GL_QUADS);
    glVertex2d(-1.0, -1.0);
    glVertex2d(1.0, -1.0);
    glVertex2d(1.0, 1.0);
    glVertex2d(-1.0, 1.0);
    glEnd();
    shader->end();

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
    glBindTexture(GL_TEXTURE_2D, tempTex);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,0.0); glVertex2d(-1.0, -1.0);
    glTexCoord2f(1.0,0.0); glVertex2d(1.0, -1.0);
    glTexCoord2f(1.0,1.0); glVertex2d(1.0, 1.0);
    glTexCoord2f(0.0,1.0); glVertex2d(-1.0, 1.0);
    glEnd();


    glutSwapBuffers();
    }

  4. #4
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    10
    Vertex shader:

    void main(void)
    {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    }

    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    Fragment shader:

    void main(void)
    {
    gl_FragColor = vec4(-1.5, 1.5, 0.0, 1.0);
    }

  5. #5
    Junior Member Newbie
    Join Date
    May 2009
    Posts
    12
    "glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 640, 480, 0, GL_RGBA, GL_FLOAT, NULL);"

    the GL_FLOAT parameter at the end of the method only describes the type of your client side data, such as the type of data you are giving to GL for each channel. In order to make your render target to be floating point you need to define InternalFormat to be floating point. e.g.

    "
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 640, 480, 0, GL_RGBA, GL_FLOAT, NULL);"
    Last edited by xahir; 06-13-2012 at 07:15 AM. Reason: gl prefix was missing

  6. #6
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    10
    Greate, It works! Thx a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •