Cannot render to front buffer

Hi guys,

It’s just one problem after another…

I’m trying to draw something directly to the front buffer, but unfortunately it’s just not working. Here’s my code, using the NeHe tutorials for testing again:



// draw a triangle to the front buffer

	glVertex3f( 0.0f, 1.0f, 0.0f);					
	glVertex3f(-1.0f,-1.0f, 0.0f);					
	glVertex3f( 1.0f,-1.0f, 0.0f);					

// draw a square to the back buffer


	glVertex3f(-1.0f, 1.0f, 0.0f);					
	glVertex3f( 1.0f, 1.0f, 0.0f);					
	glVertex3f( 1.0f,-1.0f, 0.0f);					
	glVertex3f(-1.0f,-1.0f, 0.0f);					

// swap back to front

When I run this, it should only display the square - which was drawn to the back buffer, right? Well what I’m seeing is both the triangle and square on the screen at the same time, like it’s simply drawing both objects to the back buffer.


Any help?!


I guess it means the swap happens and then the triangle is rendered to the front buffer.

It’s not important. Just render to back buffer everything.

If you want to render to front buffer, render everything to it.
Then call glFlush() instead of the SwapBuffers

You are clearing only the front buffer which will become the back buffer after the swap so it is possible that when you render the square for the next frame, the triangle is already in the back buffer from the front buffer rendering done in previous frame.

Try to clear the back buffer before you render that square.

The thing is, it works fine on my laptop at home, just not on my machine at work.

There’s either some driver issues or something else going on that’s preventing it from working. I was just wondering if this was a well known issue or not. I guess not…

I’ll have to find some other way around it. Thanks for your help.


Well, this is not really an “issue” because what Komat said makes perfect sence. AFAIK, after the swap contents of the back buffer are undefined, so you should clear it every time. What are you trying to do, anyway?

The thing is, it works fine on my laptop at home, just not on my machine at work.
To expand on what Zengar said, what you have are two definitions of “undefined”.

In the case of your laptop, it just so happens that the swapping of buffers is a memory copy. That is, it copies the back buffer to the front buffer.

The OpenGL spec does not require this behavior. It only requires that what was in the back is now in the front; the back buffer could be what it was before, what the front buffer was, or total garbage. It is undefined.

So your main PC’s implementation probably does a real swap. That is, it takes the front and back buffer pointers and exchanges them, so that the back buffer memory is now called the front buffer.

I’ve got it working fine now, thanks guys. I’ve found that by putting a glFlush command before I switch back to the back buffer, it forces it to draw the way I was hoping. Hopefully it’ll work on my laptop when I try it tonight…

What my program does is this: it’s a high-poly modelling program that allows you to edit vertices, etc.

As the scene is mostly static, it’s not constantly updating every frame - it only redraws when the user click-drags the mouse on the window to rotate the scene about. So I’m using the front buffer to store my drawn scene, and the back buffer to store my color selection objects.

When the user moves the cursor across the scene normally, it highlights any vertexes you move over. It does this by reading the back ‘selection’ buffer to see what vertex the mouse is over. But I don’t want it to have to redraw the whole back or front buffers just to highlight a single vertex in a different color, I just want it to redraw that vertex.

So I needed a way to draw stuff only to the front buffer, without disturbing the back in any way.

It seems to be working now, so fingers crossed it works on the other machines I try it on.

Many thanks for your help. :smiley:


Well, this is not really an “issue” because what Komat said makes perfect sence. AFAIK, after the swap contents of the back buffer are undefined…?
I can understand you are happy that it is working, but that is not good enough because like the quote says “undefined”. It’s possible that in the future it will stop working with newer drivers or it might not work on other people’s machine.

Render your scene to a FBO instead.
The alternative is WGL_ARB_buffer_region but I prefer FBO.

I also suggest that you never render to the front buffer. The back buffer exists for a good reason.

Thanks. The way it works now, I don’t actually need to physically swap the buffers around to draw to the front, so there’s no ‘undefined’ as such. The back buffer remains untouched. It only goes like:





And I’m only writing this software for myself, not for anyone else, so if it does go wrong I can easily fix it without any probs. I’ve commented my code with the suggestion that I may need to use FBO’s if it doesn’t work in future, so thanks for the advice.

And it does work fine on my laptop BTW. :slight_smile: