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

Thread: how to display the data from FrameBuffer to displa

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    1

    how to display the data from FrameBuffer to displa

    Hi,

    I am a newbie to opengl. I need to output the data available in the frame buffer to the display. I have a version of code in which the data is read from the frame buffer and will be written to the file. Now I need to modify the code to display. I created a window, and I want the frame buffer output to be displayed to this output window. Please tell me how to do it.

    Current version of code that reads from framebuffer and writes to a file.


    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId);
    glReadBuffer(DRAW_BUFFERS[srcIndex]);
    glBindBuffer(GL_PIXEL_PACK_BUFFER,readbackBuffer[src]);
    glReadPixels(0,0,Width,height, GL_BGRA, GL_UNSIGNED_BYTE,0)); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);


    glBindBuffer(GL_PIXEL_PACK_BUFFER, readbackBuffer[src]);
    data = (BYTE *)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
    /* copy to a local buffer in application which is used for during file write */
    CopyData(dst,Width,data,Width,Height);

    glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
    glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);


    Now I want to directly write the output of 'DRAW_BUFFERS[srcIndex]' directly display to window.

    Thanks in advance.

    -Manchinasetti

  2. #2
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    14

    Re: how to display the data from FrameBuffer to displa

    Been messing around with these myself the last few days, so I can try to help. Bare in mind this is extremely new to me, but it works.
    I plan to tidy my implementation up alot, and it is about as simple as an FBO can get or there about.

    I think I'm starting down the right line, it works atleast

    Would also love any comments back for improvements... but I am already aware this is v.basic implementation.

    Code :
    // FBO Globals
     
    GLuint g_FBO;
    GLuint g_FBOImg;

    // Initiliased in main init()
    Code :
    GLvoid InitShaders()
    {
    	shader1 = new Shader( ..."filepath"... );
    	shader1->UseShader();
    	shader1->SetAttribute1i( "render", g_FBOImg );
    }
     
    GLvoid InitFBOTexture(GLuint &tex)
    {
    	glGenTextures( 1, &tex );
     
    	glActiveTexture( GL_TEXTURE0 + tex );
    	glBindTexture( GL_TEXTURE_2D, tex );
     
    	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, g_WindowWidth, g_WindowHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
    	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    }
     
     
    GLvoid InitFBO()
    {
    	GLuint m_Depthbuffer;
     
    	// Get handle to & bind FBO
    	glGenFramebuffersEXT( 1, &g_FBO );
    	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, g_FBO );
     
    	// Init depth render buffer and attach to bound FBO
    	glGenRenderbuffersEXT( 1, &m_Depthbuffer );
    	glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_Depthbuffer );
    	glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, g_WindowWidth, g_WindowHeight );
     
    	// Init and attach texture to FBO
    	InitFBOTexture( g_FBOImg );
    	glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_FBOImg, 0 );
     
    	// And attach it to the FBO so we can render to it
    	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_FBOImg, 0);
     
    	glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_Depthbuffer );
     
    	GLenum renderTarget[] = {GL_COLOR_ATTACHMENT0_EXT};
    	glDrawBuffers( 1, renderTarget );
    	// Check status of FBO
    	if ( glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT )
    	{
    		std::cout << "Failed to initialise FBO" << std::endl;
    		exit( 1 );
    	}
     
    	// Unbind FBO
    	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    }

    This is then in my RenderScene()
    Code :
    	// Bind FBO
    	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, g_FBO );
    	// Save viewport information and set to FBO's texture size
    	glPushAttrib( GL_VIEWPORT_BIT );
    	glViewport( 0, 0, g_WindowWidth, g_WindowHeight );
     
    	// Clear colour and depth buffer
    	glClearColor( ... );
    	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    	glLoadIdentity();
     
            // SET UP SCENESCENE.. RENDERED TO FBO
     
    	// Restore previous viewport information & disable FBO
    	glPopAttrib();
    	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
     
    	glClearColor( ... );
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
     
    	glDisable( GL_DEPTH_TEST );
    	ViewOrtho();
    	shader1->UseShader(); // Fragment shader to fullscreen quad
     
    	glBegin( GL_QUADS );
    			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    			glTexCoord2f(0,1);
    			glVertex2f( 0, 0 );
    			glTexCoord2f(0,0);
    			glVertex2f( 0, g_WindowHeight );
    			glTexCoord2f(1,0);
    			glVertex2f( g_WindowWidth, g_WindowHeight );
    			glTexCoord2f(1,1);
    			glVertex2f( g_WindowWidth, 0 );
    	glEnd();
    	ViewPerspective();
    	glEnable(GL_DEPTH_TEST);

    Basic fragment shader used to render to full screen quad
    Code :
    uniform sampler2D render;
     
    void main(void)
    {
    	gl_FragColor = texture2D( render,  gl_TexCoord[0].xy);
    }

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    14

    Re: how to display the data from FrameBuffer to displa

    o yeah, dont forget to clean up and delete stuff on close or whenever

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    14

    Re: how to display the data from FrameBuffer to displa

    just realised in my code had duplicate lines...

    Code :
    glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_FBOImg, 0 );
     
    // And attach it to the FBO so we can render to it
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_FBOImg, 0);

    obviously need only once

Posting Permissions

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