Sign in to follow this  

OpenGL More FBO and buffer questions...

Recommended Posts

Hello again, I hope people will indulge me in my idiocy a bit longer on this forum as I'm having a bit of trouble getting the "OpenGL Frame Buffer Object 101" to work properly... Basically I'm trying to render the images to a depth buffer in the hopes of achieving a higher color resolution. I read somewhere that pixels rendered to the color buffer are all clamped and to 0-255 values where as the same is not true for example the depth buffer...? Perhaps I am wrong in this assumption? Anyways - I am hoping someone has the time to glance at some of my code and see if they can spot anything out of order... Just to get something out of of the way - If I do NOT render to the FBO object - i.e call "" before rendering, I get a pretty blue/white rotating wierd triangle. Some initialization code
gpuAiVertexProgram = cgCreateProgramFromFile(gpuAiContext,

if(gpuAiVertexProgram != NULL)
	/* Vertex shader only needs to be loaded once */

	/* Bind parameters to give access to variables in the shader */
	//KdParam = cgGetNamedParameter(VertexProgram, "Kd");
	ModelViewProjParam = cgGetNamedParameter(gpuAiVertexProgram, "ModelViewProj");
	ModelViewProjITParam = cgGetNamedParameter(gpuAiVertexProgram, "ModelViewProjIT");
	//VertexColorParam = cgGetNamedParameter(VertexProgram, "IN.VertexColor");

// Generate Frame buffer so we can use it!
glGenFramebuffersEXT(1, &fbo);							// Get a handle
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);			// GL_FRAMEBUFFER_EXT is apparently the only bindable destination right now.
// Generate a Renderbuffer for the FBO
glGenRenderbuffersEXT(1, &depthbuffer);				// Get a handle
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, 512, 512);	// Define storage space for the renderbuffer

// Generate a texture
glGenTextures(1, &img);
glBindTexture(GL_TEXTURE_2D, img);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

// Attach function FBO<->Renderbuffer

// Attach function FBO<->Texture

if (checkFramebufferStatus()) {
	printf("Successfully attached FBO to Depthbuffer! \n");
} else {

The display code...


if (enableCG) {

	if(ModelViewProjParam != NULL)
		cgGLSetStateMatrixParameter(ModelViewProjParam,						CG_GL_MODELVIEW_PROJECTION_MATRIX,

	if(ModelViewProjITParam != NULL)


glViewport(0,0,512, 512);

	//glVertex3f(-0.8f, 0.8f, -114.0f);
	//glVertex3f(-0.7f, 0.8f, -114.0f);

	//glVertex3f(-0.75f, 0.9f, -114.0f);

	glVertex3f(-0.65f, 0.9f, 2.0f);
	//glVertex3f(-0.6f, 0.8f, -114.0f);


if (enableCG) {

// swapping the buffers causes the rendering above to be 
// shown
// finally increase the angle for the next frame


I'm using imdebug (which is a small tool to read an image into an extra tool and then look closer at it) to peer into the various buffers and have a look at whats inside. Here are the calls I use... The odd thing is... If I use imdebug to read from the screen it works fine when I render to it, but also when rendering to the depth buffer is active (or should be) - only then I see the blue triangle basically overwriting itself creating lots of layers of itself... Hmmm perhaps that needs clarification...
switch(key) {
	case GLUT_KEY_F1 :
		if (enableCG) {
			enableCG = false;
			printf("Cg Disabled...\n");
		} else {
			enableCG = true;
			printf("Cg Enabled...\n");
	case GLUT_KEY_F2 :
		if (rendertoBF) {
			rendertoBF = false;
			glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
		} else {
			rendertoBF = true;
			glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
	case GLUT_KEY_F3 :
		// Try to use imdebug to display whats in the texture attached to the depthbuffer
		imdebug("rgba  w=%d h=%d %p", 512, 512, &img);
	case GLUT_KEY_F4 :
		// Try to use imdebug to render directly from the depth buffer
	case GLUT_KEY_F5 :
		printf("Read pixel from screen (color buffer?) into imdebug");

Any and all answers are much appreciated! Regards, Gazoo

Share this post

Link to post
Share on other sites
If you don't want your colors to be clamped to 0-1 you need to use floating point buffer format. But if you use a floating point texture render target and render that texture you should be able to get around it that way since the data should be saved outside the 0-1 limit.

Share this post

Link to post
Share on other sites
Ok - so technically it should be possible to get some values that are not clamped... That's at least encouraging... Now if only I could figure out why my program crashes when I try to read the texture using the above code...



Share this post

Link to post
Share on other sites
Well - the reason for the program crashing was of course my own doing. I provided imdebug with an OpenGL pointer to the texture and not a pointer to local memory where an array filled with "pixels" awaited. As a result it read the openGL pointer and kept going into memory that it wasn't allowed to read, which lead to a crash...

I still haven't figured out why reading directly from the depth buffer gives me a completely white image...



Share this post

Link to post
Share on other sites
Depth buffer values are indeed clamped, but into a [0,1] range as a floating point value (16 or 24bit depending on hardware and requested depth). If you want unclampped data outside of the [0,1] range then you need to use floating point textures or render targets as render targets for the FBO.

Share this post

Link to post
Share on other sites

Thanks for the replies... One further question thou... In the "OpenGL Frame Buffer Object 101" tutorial, you start off with attaching a depthbuffer to the FBO... I have messed around with the code a bit and I am lead to believe that that the depthbuffer doesn't really influence or affect the rendering to a texture by use of the same FBO.

Is this correct? I assume the depth buffer is only in the tutorial in order to show that it is possible to use the FBO to render into an existing buffer instead of a texture?



Share this post

Link to post
Share on other sites
Nope, the depth buffer is attached so that you can do depth culling on objects rendered to the FBO just as you would when rendering to the normal colour buffer when drawing.

So, it does affect what appears in the texture during the render, much how it affects what appears in the main framebuffer when you render 'normally'.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By test opty
      Hi all,
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
  • Popular Now