PDA

View Full Version : Cannot render to front buffer



Darren Taylor
05-08-2007, 05:14 AM
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:

glDrawBuffer(GL_FRONT);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// draw a triangle to the front buffer
glTranslatef(-1.5f,0.0f,-6.0f);
glColor3f(1.0f,0.0f,0.0f);

glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();

// draw a square to the back buffer
glDrawBuffer(GL_BACK);

glTranslatef(3.0f,0.0f,0.0f);
glColor3f(0.5f,0.5f,1.0f);

glBegin(GL_QUADS);
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);
glEnd();

// swap back to front
SwapBuffers(hDC);


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.

:confused:

Any help?!

Daz.

V-man
05-08-2007, 06:25 AM
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

Komat
05-08-2007, 06:27 AM
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.

Darren Taylor
05-08-2007, 01:06 PM
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.

Daz

Zengar
05-08-2007, 02:26 PM
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?

Korval
05-08-2007, 03:24 PM
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.

Darren Taylor
05-09-2007, 03:43 AM
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. :D

Daz.

V-man
05-09-2007, 10:45 AM
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.

Darren Taylor
05-09-2007, 02:56 PM
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:

glDrawBuffer(GL_FRONT);

glBegin(GL_POINTS);
glVertex3f(vertX,vertY,vertZ);
glEnd();

glFlush();

glDrawBuffer(GL_BACK);

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. :)

Daz.