View Full Version : Export values from Fragmentshader to Vertexshader/Mainprogram

07-24-2006, 06:05 AM
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. ;)

07-24-2006, 08:14 AM
Hi, you can render to a texture, which is fast - glCopyTexImage2D ().

07-24-2006, 08:42 AM
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....

07-24-2006, 08:58 AM
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. :(

Matt Zamborsky
07-24-2006, 09:03 AM
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).

07-24-2006, 09:20 AM
can give me an example code for that?

07-24-2006, 06:19 PM
Setup a 32bit Texture:

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:

unsigned int fbo;

glGenFramebuffersEXT( 1, &fbo );
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo );


glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );Render your Fragment Program results to this framebuffer:

// 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.


glActiveTexture( GL_TEXTURE2 );
glBindTexture( GL_TEXTURE_2D, tex_32 );Cg 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;

07-26-2006, 07:10 AM
ok thanks for help... i will try it.