Shocker's picture

FBO off-screen-rendering problem

I'd like to draw 3D directly to Screen. And I tried using WIN32 API - Layered Window with FBO.
It succeeded in creating transparency enabled window . But a problem has occurred in essential Drawing 3D .
Drawing surfaces of Cube was incorrectly . It seems that Depth buffer couldn't work well .
Could you tell me where a mistake is ?

AttachmentSize
Form1.cs31.45 KB

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

To use depth test with FBOs, you have to attach a depth texture (or renderbuffer) first. In your PreparingGL_FBO method, create a texture using the DepthComponent internal format and attach it with:

GL.GenTextures(1, out depth_texture);
GL.BindTextures(TextureTarget.Texture2D, depth_texture);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent, Width, Height, 0, OpenTK.Graphics.PixelFormat.DepthComponent, PixelType.UnsignedByte, IntPtr.Zero);
 
...
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureTarget.Texture2D,texture, 0);

Afterwards, depth test should work as expected.

Edit: Some drivers need you to specify the exact bitdepth of the texture, e.g. PixelInternalFormat.DepthComponent24.

Shocker's picture

Thank you !
I have thought that the FBO needs a Depth texture when it is needed to get Depth buffer as an image .

martinsm's picture

Yes, texture attachements is needed when you want to get rendered image (depth or color) as texture. If you don't, then use renderbuffer attachment.

Shocker's picture

I haven't understood use of Render buffer clearly .
Thanks to you, I get a some knowledge .

ennael's picture

Hi i am currently drawing offscreen to a renderbuffer attached to and FBO. I need to display the contents of the FBO back to "on-screen" i.e. the window. How can you do that without having to go to main memory by using the glreadpixels() and gldrawpixels()? If i should use glcopypixels() how can i do that because i am trying to get that working and only getting a blank screen back!

glBindFramebufferEXT(GL_READ_FRAMEBUFFER,bufferID);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER,0);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glDrawBuffer(GL_FRONT);
glRasterPos2d(0.0, 0.0);
glCopyPixels(0, 0, 500, 500, GL_COLOR);

Do i need to use renderbuffer for better performance or can i just use the texture, as with the latter averything is working fine?

the Fiddler's picture

You need to use GL.BlitFramebuffer to copy ("resolve") the renderbuffer to a texture.

The main difference between renderbuffers and textures are that the former can be multisampled while the latter generally cannot (unless the drivers expose multisampled textures, and many do not).

ennael's picture

so if the extension for blitting is not supported on the graphics card renderer then there is no other way i can do this pixel transfer?

the Fiddler's picture

Unless you use a multisampled render target, glCopyPixels should work.

I don't know if you can copy directly to GL_FRONT, however. It may be possible, but I have never tried this - I always use an intermediate texture, first. Try using GL_BACK.

ennael's picture

i tried to use GL_BACK but still all i am getting are just artifacts (parts of the desktop current windows) rather than my drawing