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 8 of 8

Thread: Export values from Fragmentshader to Vertexshader/Mainprogram

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2006
    Location
    Germany, Thuringia
    Posts
    5

    Export values from Fragmentshader to Vertexshader/Mainprogram

    Hello guys,

    is it possible to export a calculated value from a fragmentshader back to the vertexshader?
    I try to implement a kind of realtime video composer.
    I have a bluebox video, wich is mapped as a texture on a plane. I use realtime chroma-keying to set the blue background transparent and then i have to realize some kind of "object-tracking" (tracking the position of the feet of an actor) to move the videoplane in a maya-scene.
    My first idea was to save this value in a texture-unit, and than read this value with a texture-lookup in the vertexshader. But there is no way to get write-access on a texture-unit.
    So i use the framebuffer to save this value in a pixel and use glReadPixels() to get this value. But it is very slow. I have to use two for-loops to read pixel per pixel of my videotexture.

    Is anyone having an idea how i can resolve this problem in a better way? It´s very importand for me, because it´s a part of my diploma thesis!

    P.S.: I hope somebody understands my "good" english.

  2. #2
    Intern Newbie
    Join Date
    May 2006
    Location
    Czech republic
    Posts
    43

    Re: Export values from Fragmentshader to Vertexshader/Mainprogram

    Hi, you can render to a texture, which is fast - glCopyTexImage2D ().

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2006
    Location
    Germany, Thuringia
    Posts
    5

    Re: Export values from Fragmentshader to Vertexshader/Mainprogram

    I do all these calculations in a fragmentshader and i have to export the result back to f.e. my vertexshader.
    Can i use this function in my fragmentprogram? O.o
    That will be helpful....

  4. #4
    Junior Member Newbie
    Join Date
    Jul 2006
    Location
    Germany, Thuringia
    Posts
    5

    Re: Export values from Fragmentshader to Vertexshader/Mainprogram

    hmmm...it won´t work...
    it´s just an another function to read from the framebuffer.
    That won´t resolve my problem. The problem is to export it from my Fragmentshader.
    In my fragmentshader i try to track the feet of an actor and the result (y-position in the videotexture) is my value i need to move the plane in the scene. So, to export the value out of my fragmentshader that is my problem.

    At the moment i draw a Point with "glBegin(GL_POINTS)" and "glVertex()" and by drawing this pixel i call my tracking function in my shader. Then i write the result at this position where my Pixel is in the framebuffer to read it in my mainprogram.

    This implementation is slow and incorrect.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jun 2004
    Location
    Slovakia Europe
    Posts
    109

    Re: Export values from Fragmentshader to Vertexshader/Mainprogram

    when you need values from fragment shader in vertex shader, just set-up a render target(texture which you will render in), render whatever you want, bind this render target as texture and sample it in vertex shader(you need Geforce 6 and up, beware of texture format, it must be float texture).

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2006
    Location
    Germany, Thuringia
    Posts
    5

    Re: Export values from Fragmentshader to Vertexshader/Mainprogram

    can give me an example code for that?

  7. #7
    Junior Member Newbie
    Join Date
    Jul 2006
    Location
    Binghamton, Ny
    Posts
    23

    Re: Export values from Fragmentshader to Vertexshader/Mainprogram

    Setup a 32bit Texture:
    Code :
    unsigned int tex_32;
    unsigned int tex_res = 64;
     
    glGenTextures( 1, &tex_32 );
    glBindTexture( GL_TEXTURE_2D, tex_32 );
     
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, tex_res, tex_res, GL_RGBA, GL_FLOAT, 0 );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    Setup Fbo to Render to:
    Code :
    unsigned int fbo;
     
    glGenFramebuffersEXT( 1, &fbo );
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo );
     
    glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_32, 0 );
     
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
    Render your Fragment Program results to this framebuffer:

    Code :
    // Enable/Setup your Shaders.
    glPushAttrib( GL_VIEWPORT_BIT );
    glViewport( 0, 0, tex_res, tex_res );
     
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo );
     
    // Draw calls.
     
    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 ); // reset rendering back to default framebuffer.
    // Disable/Shutdown your Shaders.
     
    glPopAttrib();
    You can sample textures using tex2D() with nVidia's vp40 profile, haven't tried others, I generally use Cg. Bind your 32bit texture to some texture unit, and just specify a uniform sampler with the cooresponding binding semantic in your vertex profile.

    OpenGL:
    Code :
    glActiveTexture( GL_TEXTURE2 );
    glBindTexture( GL_TEXTURE_2D, tex_32 );
    Cg Code:
    Code :
    v2f VertProg( a2v IN, uniform sampler2D frag_results : TEXUNIT2 )
    {
       v2f OUT;
     
       //...
     
       float result = tex2D( frag_results, float2( 0.f, 0.f ).r;
     
       //...
     
       return OUT;
    }

  8. #8
    Junior Member Newbie
    Join Date
    Jul 2006
    Location
    Germany, Thuringia
    Posts
    5

    Re: Export values from Fragmentshader to Vertexshader/Mainprogram

    ok thanks for help... i will try it.

Posting Permissions

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