# Double buffering

**URL:** <https://community.khronos.org/t/double-buffering/27791>\
**Category:** OpenGL: Basic Coding\
**Created:** [April 16, 2001, 6:34am UTC](https://community.khronos.org/t/double-buffering/27791 "2001-04-16T06:34:00Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![link19](https://community.khronos.org/letter_avatar_proxy/v4/letter/l/df705f/32.png) [@link19](https://community.khronos.org/u/link19)\
**Post date:** [April 16, 2001, 6:34am UTC](https://community.khronos.org/t/double-buffering/27791/1 "2001-04-16T06:34:00Z")

</div>

When i rotate an object, i get heavy flickering. I understand that double buffering will prevent this, however i do not know how to implement it.  
I did a search in this forum and found that its quite simple using GLUT, but i am not. There was no help for implementing it for Win32/MFC. Im using VC++6.0 and would appreciate suggestions.  
TIA

---

<div class="post-metadata">

**Author:** ![link19](https://community.khronos.org/letter_avatar_proxy/v4/letter/l/df705f/32.png) [@link19](https://community.khronos.org/u/link19)\
**Post date:** [April 16, 2001, 6:45am UTC](https://community.khronos.org/t/double-buffering/27791/2 "2001-04-16T06:45:00Z")

</div>

I realized i have to set the PFD\_DOUBLEBUFFER flag in my SetWindowPixelFormat() method. Then i call SwapBuffers(dc.GetSafeHdc()); in my OnPaint()…but this has no effect on my view. Still have flickering when animating my object.  
Ideas please??!

---

<div class="post-metadata">

**Author:** ![Warrior](https://community.khronos.org/letter_avatar_proxy/v4/letter/w/2bfe46/32.png) [@Warrior](https://community.khronos.org/u/Warrior)\
**Post date:** [April 16, 2001, 7:27am UTC](https://community.khronos.org/t/double-buffering/27791/3 "2001-04-16T07:27:00Z")

</div>

you on the right track.  
you do need to specify PFD\_DOUBLEBUFFER when filling in your PIXELFORMATDESCRIPTOR structure. then, to flip the buffers you call SwapBuffers(HDC).

if you need more info, your best bet is to read NeHe’s tutorial on setting up a window in Windows. go to [http://nehe.gamedev.net](http://nehe.gamedev.net) for this tutorial

- Dave

[This message has been edited by Warrior (edited 04-16-2001).]

---

<div class="post-metadata">

**Author:** ![danny1238](https://community.khronos.org/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@danny1238](https://community.khronos.org/u/danny1238)\
**Post date:** [April 17, 2001, 3:03am UTC](https://community.khronos.org/t/double-buffering/27791/4 "2001-04-17T03:03:00Z")

</div>

I’ve just started learning how to program in OpenGL through a book called “OpenGL SuperBible”. I came across the topics of double buffering and single buffering. To enable double buffering you should initialize it within the display mode before creating the window.

glutInitDisplayMode(GLUT\_DOUBLE | GLUT\_RGB);

Also, you should use

glutSwapBuffers();

at the end of the Rendering function instead of

glFlush();

---

<div class="post-metadata">

**Author:** ![link19](https://community.khronos.org/letter_avatar_proxy/v4/letter/l/df705f/32.png) [@link19](https://community.khronos.org/u/link19)\
**Post date:** [April 17, 2001, 4:59am UTC](https://community.khronos.org/t/double-buffering/27791/5 "2001-04-17T04:59:00Z")

</div>

Thx Danny…but if you had read my first post, im not using GLUT.  
BTW, im still getting flickering and not sure why double buffering would not be enabled. Ive looked through NeHe’s first tut and dont think im missing anything. Could there be something in my code that would prevent it?  
Thx…

---

<div class="post-metadata">

**Author:** ![JoeMac](https://community.khronos.org/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@JoeMac](https://community.khronos.org/u/JoeMac)\
**Post date:** [April 17, 2001, 5:16am UTC](https://community.khronos.org/t/double-buffering/27791/6 "2001-04-17T05:16:00Z")

</div>

You might actually be using double buffers correctly and still get flickering if you have some combination of:  
software rendering, a lot of polygons, slow computer, vsync disabled.  
Joe

---

<div class="post-metadata">

**Author:** ![Sanchis](https://community.khronos.org/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@Sanchis](https://community.khronos.org/u/Sanchis)\
**Post date:** [April 17, 2001, 5:16am UTC](https://community.khronos.org/t/double-buffering/27791/7 "2001-04-17T05:16:00Z")

</div>

Maybe  
glFinish();  
SwapBuffers(wglGetCurrentDC()); ?

---

<div class="post-metadata">

**Author:** ![DaViper](https://community.khronos.org/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@DaViper](https://community.khronos.org/u/DaViper)\
**Post date:** [April 17, 2001, 5:29am UTC](https://community.khronos.org/t/double-buffering/27791/8 "2001-04-17T05:29:00Z")

</div>

Are you sure that you are rendering to the backbuffer with using glDrawBuffer(GL\_BACK)

Chris

---

<div class="post-metadata">

**Author:** ![link19](https://community.khronos.org/letter_avatar_proxy/v4/letter/l/df705f/32.png) [@link19](https://community.khronos.org/u/link19)\
**Post date:** [April 17, 2001, 5:44am UTC](https://community.khronos.org/t/double-buffering/27791/9 "2001-04-17T05:44:00Z")

</div>

My computer is fast, but i am drawing a lot of polygons…so possibly youre right. However, I commented out the SwapBuffer() call and my rendering is still visible. Does this mean that i am not drawing to the back buffer? I made the call glDrawBuffer(GL\_BACK)right before i call my rendering function in OnPaint(). Is this the correct place for it?  
Thx for your sugggestions…

---

<div class="post-metadata">

**Author:** ![Deiussum](https://community.khronos.org/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@Deiussum](https://community.khronos.org/u/Deiussum)\
**Post date:** [April 17, 2001, 8:11am UTC](https://community.khronos.org/t/double-buffering/27791/10 "2001-04-17T08:11:00Z")

</div>

It sounds like you are using MFC. Try over-riding the OnEraseBkgnd() function and make it return TRUE.

---

<div class="post-metadata">

**Author:** ![link19](https://community.khronos.org/letter_avatar_proxy/v4/letter/l/df705f/32.png) [@link19](https://community.khronos.org/u/link19)\
**Post date:** [April 17, 2001, 8:16am UTC](https://community.khronos.org/t/double-buffering/27791/11 "2001-04-17T08:16:00Z")

</div>

Deiussum,  
Yep, using MFC. Already implemented returning true in OnEraseBkgnd(). My call to glDrawBuffer(GL\_BACK), is it in the correct place? as i mentioned in my earlier post…  
TIA

[This message has been edited by link19 (edited 04-17-2001).]

---

<div class="post-metadata">

**Author:** ![Deiussum](https://community.khronos.org/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@Deiussum](https://community.khronos.org/u/Deiussum)\
**Post date:** [April 17, 2001, 10:15am UTC](https://community.khronos.org/t/double-buffering/27791/12 "2001-04-17T10:15:00Z")

</div>

If your scene is still displaying even without the SwapBuffers, you are likely not getting a double-buffered context. Maybe you could try posting the function where you initialize the pixelformat.

---

<div class="post-metadata">

**Author:** ![link19](https://community.khronos.org/letter_avatar_proxy/v4/letter/l/df705f/32.png) [@link19](https://community.khronos.org/u/link19)\
**Post date:** [April 17, 2001, 10:23am UTC](https://community.khronos.org/t/double-buffering/27791/13 "2001-04-17T10:23:00Z")

</div>

Okay. But i think the only thing to notice wrt double buffering here is the flag PFD\_DOUBLEBUFFER. So im posting more than that:

> ```auto
> 
> ```

> BOOL CMyMesh::SetWindowPixelFormat(HDC hDC)  
> {  
> PIXELFORMATDESCRIPTOR pixelDesc;
> 
> ```
> pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
> pixelDesc.nVersion = 1;
> 
> pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | 
> PFD_SUPPORT_OPENGL | 
> PFD_SUPPORT_GDI | 
> PFD_DOUBLEBUFFER;
> 
> pixelDesc.iPixelType = PFD_TYPE_RGBA;
> pixelDesc.cColorBits = 32;
> pixelDesc.cRedBits = 8;
> pixelDesc.cRedShift = 16;
> pixelDesc.cGreenBits = 8;
> pixelDesc.cGreenShift = 8;
> pixelDesc.cBlueBits = 8;
> pixelDesc.cBlueShift = 0;
> pixelDesc.cAlphaBits = 0;
> pixelDesc.cAlphaShift = 0;
> pixelDesc.cAccumBits = 64;    
> pixelDesc.cAccumRedBits = 16;
> pixelDesc.cAccumGreenBits = 16;
> pixelDesc.cAccumBlueBits = 16;
> pixelDesc.cAccumAlphaBits = 0;
> pixelDesc.cDepthBits = 32;
> pixelDesc.cStencilBits = 8;
> pixelDesc.cAuxBuffers = 0;
> pixelDesc.iLayerType = PFD_MAIN_PLANE;
> pixelDesc.bReserved = 0;
> pixelDesc.dwLayerMask = 0;
> pixelDesc.dwVisibleMask = 0;
> pixelDesc.dwDamageMask = 0;
> 
> m_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc);
> if (m_GLPixelIndex==0) 
> {
> m_GLPixelIndex = 1;    
> if (DescribePixelFormat(hDC, m_GLPixelIndex,
> sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc)==0)
> {
> return FALSE;
> }
> }
> 
> if (SetPixelFormat(hDC, m_GLPixelIndex, &pixelDesc)==FALSE)
> {
> return FALSE;
> }
> 
> return TRUE;
> 
> ```
> 
> }
> 
> void CMyMesh::OnPaint()  
> {  
> CPaintDC dc(this);  
> glClear(GL\_COLOR\_BUFFER\_BIT | GL\_DEPTH\_BUFFER\_BIT);  
> glDrawBuffer(GL\_BACK);  
> RenderScene();
> 
> glPushMatrix();  
> …  
> glCallList(m\_meshList);  
> …  
> glPopMatrix();
> 
> ```
> glFinish();
> 
> ```
> 
> SwapBuffers(dc.GetSafeHdc());  
> }

> ```auto
> 
> ```

[This message has been edited by link19 (edited 04-17-2001).]

---

<div class="post-metadata">

**Author:** ![JoeMac](https://community.khronos.org/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@JoeMac](https://community.khronos.org/u/JoeMac)\
**Post date:** [April 17, 2001, 10:41am UTC](https://community.khronos.org/t/double-buffering/27791/14 "2001-04-17T10:41:00Z")

</div>

Out of curiosity, what does RenderScene() do since you are drawing outside of it? Does it also contain a call to swapbuffers()?

---

<div class="post-metadata">

**Author:** ![link19](https://community.khronos.org/letter_avatar_proxy/v4/letter/l/df705f/32.png) [@link19](https://community.khronos.org/u/link19)\
**Post date:** [April 17, 2001, 10:47am UTC](https://community.khronos.org/t/double-buffering/27791/15 "2001-04-17T10:47:00Z")

</div>

No i dont make a call to SwapBuffers in my RenderScene() function. It contains my display lists, m\_meshList for instance being one of them.

---

<div class="post-metadata">

**Author:** ![system](https://community.khronos.org/uploads/default/original/2X/b/ba89cb49313a0e869198c8be354afde8bcae027e.png) [@system](https://community.khronos.org/u/system)\
**Post date:** [October 26, 2003, 8:14pm UTC](https://community.khronos.org/t/double-buffering/27791/16 "2003-10-26T20:14:00Z")

</div>

PFD\_SUPPORT\_GDI  
and  
PFD\_DOUBLEBUFFER  
are mutually exclusive

---

<div class="post-metadata">

**Author:** ![mikael\_aronsson](https://community.khronos.org/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mikael\_aronsson](https://community.khronos.org/u/mikael_aronsson)\
**Post date:** [October 26, 2003, 11:22pm UTC](https://community.khronos.org/t/double-buffering/27791/17 "2003-10-26T23:22:00Z")

</div>

Yes, the PFD\_SUPPORT\_GDI allows you to use ordinary GDI calls to mix with OpenGL rendering, this prevents you from using doublebuffering (it would erase anything done with gdi calls), so just remove the PFD\_SUPPORT\_GDI and it should work fine.

---

<div class="post-metadata">

**Author:** ![Relic](https://community.khronos.org/user_avatar/community.khronos.org/relic/32/49_2.png) [@Relic](https://community.khronos.org/u/Relic)\
**Post date:** [October 28, 2003, 1:14am UTC](https://community.khronos.org/t/double-buffering/27791/18 "2003-10-28T01:14:00Z")

</div>

This is what the Microsoft implementation says, but other implementations can draw with GDI into OpenGL windows, but only into the front buffer, of course. (But you don’t really want to do that!)

---

<div class="post-metadata">

**Author:** ![roffe](https://community.khronos.org/letter_avatar_proxy/v4/letter/r/3be4f8/32.png) [@roffe](https://community.khronos.org/u/roffe)\
**Post date:** [October 28, 2003, 8:48am UTC](https://community.khronos.org/t/double-buffering/27791/19 "2003-10-28T08:48:00Z")

</div>

> Originally posted by link19:  
> My call to glDrawBuffer(GL\_BACK), is it in the correct place? as i mentioned in my earlier post…

For single buffering, GL\_FRONT is set as the default draw buffer. For double buffering GL\_BACK is set. So unless you want to render to both back and front you should never have to call glDrawBuffer explicitly.Calling glDrawBuffer(GL\_BACK) when using single buffering will result in an invalid operation error from glGetError(). If you requested double buffering and you’re not sure you got it, this would be a simple(but primitive) way of checking it.
