I tried but again it made very little difference. I exaplan as below with code steps
Here is my pesudocode, I can not talk ,much of shader code as it is immaterial here.
MFC Part –
OnPaint()
{
// get screen context and set its properties
//This is similar to glutInit() where viewport settings are done
Get_and_Set_Context();
//Copy frame data to temp object
memcpy(temp, FrameData, sizeof(FrameData) );
// Bind current context to FBO id "fb"
glBindFrameBuffer(GL_FRAMEBUFFER , fb);
//Call function that internally calls shader code
MyFunction(); //I will expand this below
//Switch Back to Main Frame Buffer
glBindFrameBuffer(GL_FRAMEBUFFER , fb);
//restore view port settings
SetViewPort(Original_W, Original_H);
//set up a textuer object's propoerteis and fill it with orignal frame data
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, tex[3]); //where tex[4] are 4 texture ids created before hand
//set texture parameters
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
//copy back original frame data
memcpy( FrameData, temp, sizeof(FrameData) );
//delete temp
free(temp);
temp = null;
//attach FrameData to tex[3]
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0 , GL_RGB , FRameWidth , FrameHeight , 0 , GL_RGB, GL_UNSIGNED_BYTE, FrameData);
//draw to GL_FRONT buffer of the main frame buffer
glDrawBuffer(GL_FRONT); [I tried with GL_BLACK but again it waqs giving me twisted data with multiple copies on a yellow back ground. I tried with GL_FRONT and it gave me correct image.]
//Attach currently binded Frame data texture to FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, tex[3], 0);
//drawQuad
drawQuad(FrameWidth, FrameHeight); [Expanded later]
//swaps buffer .. similar to glutSwapBuffers()
SwapBuffers(m_hDC->GetSafeHdc());
//current REndering context is set to null ... as a good tip from MSDN
wglMakeCurrent(NULL, NULL );
//validation for error ..again tip taken from MSDN
ValidateRect(NULL);
}//end of OnPaint()
The drawQuad Function
drawQuad(W,H)
{
glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0, 0.0f , 0.0f);
glVertex2f( 0.0f, 0.0f);
glMultiTexCoord2f(GL_TEXTURE0, WD , 0.0f);
glVertex2f(WD , 0.0f);
glMultiTexCoord2f(GL_TEXTURE0, WD , HT);
glVertex2f( WD , HT );
glMultiTexCoord2f(GL_TEXTURE0, 0.0f , HT);
glVertex2f( 0.0f, HT );
glEnd();
}
and now the function that internally calls shader program
MyFunction()
{
//Take three buffers that would be used as textures.
float *A, *B, *C; //assume they are filled with some data, not necessarily with frame data...say for example non color data but corresponding to current frame under reference.
//Textures A,B and C have same format and dimension but unequal to that of current frame.
//Change viewport settings
SetViewPort(w,h); //specific to texsize
//set the properties of tex[0] ,tex[1] and tex[2] and associate them to A,B and C respectively.
SetTexture(tex[0],A);
SetTexture(tex[1],B);
SetTexture(tex[2],C);
//call shader
SetShader(); //does calculations and delivers results to first two out of four buffers.
//where buffers[4] has 4 color attachment points enabled
//read back data to A and B from shader
ReadBack(A, Buf[0]);
ReadBack(B, Buf[1]);
//copy A and B to some other temp variables accessible to other functions. and free A,B,C
}
The OnPaint() function is called for every frame hence you can assume that all three functions are called iteratively.
Observations:
For first frame everything works well. But for frame number 2 and onwards, the code crashes just after switching back to off-screen buffer from on-screen in OnPaint().
- SetShader uses glDrawBuffers(2, buffers) to write 2 streams of results on two of the 4 attached textures to FBO.
Help appreciated for above.Thanks.