opengl rendering

I have an application which uses OpenGL. In that application I have a function which will use gl* functions to render. The rendering is done only when the size of the view,

orientation, position of the object in the view and/or camera changes. Otherwise I rely on the swapbuffer mechanism to display the correct image, hoping that the backbuffer is always

ok. But this didn’t work for all hardware. Currently I have been fixing the problem by rendering at OnDraw() message. But this fix is not a proper solution for my application as I have

lots of object with so many polygons.

I also have set PFD_SWAP_COPY while choosing PIXELFORMATDESCRIPTOR for open gl, so that the back buffer will always have the correct rendered image. But this also in vain, as I

came to know that PFD_SWAP_COPY is just a hint to hardware driver.

Currently I want to try this logic:

  1. render using gl functions
  2. capture the back buffer
  3. use the captured back buffer to draw using windows GDI from OnDraw

I cannot think about any solution so any help or reference will be highly appreciated.

Following is the code snippet which I want to use:

// gl functions to render
void COGLView::Draw3DObject()
wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);

// gl functions

SwapBuffer(m_pDC->GetSafeHdc(), m_hRC);


void COGLView::OnDraw(CDC* pDC)
wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);

// swap the buffer for allowing windows to draw in this view



void COGLView::OnSize(UINT nType, int cx, int cy)
CView::OnSize(nType, cx, cy);

if(cx <= 0 || cy <= 0)

wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);

glViewport(0, 0, cx, cy);



wglMakeCurrent(NULL, NULL);



void COGLView::OnMouseMove(UINT nFlags, CPoint point)
BOOL bRefresh = TRUE;

// free rotation in opengl x and y axes
m_cCamera3D.RotateX(0.5 * (m_pntCursor.x - point.x));
m_cCamera3D.RotateY(0.5 * (m_pntCursor.y - point.y));

if(bRefresh == TRUE)


But have been fixing by calling Draw3DObject() at OnDraw().

What about this?

Alternatively, save the back buffer to a texture and redraw it. Don’t mix GDI and OpenGL functions (will be slower anyway))!

Thanks Zengar for the reply…

I will have a look at the link provided.

Does “Don’t mix GDI and OpenGL” mean i shouldn’t have GDI and drawings in same application? What do you think about the process of capturing the image drawn in back buffer? Cause I want to cature the image drawn in back buffer and draw the captured image using windows gdi at OnDraw() or OnPaint() just eliminating the SwapBuffers().

Thanks in advance.

You should not mix GDI and OpenGL commands in the same window, I mean. If you don’t want to use the buffer region etension, you could copy the backbuffer to a texture and then draw the texture (thus bypassing the GDI calls) — it will be much faster then a GDI path.

Thanks Zengar…

>you could copy the backbuffer to a texture and then draw the

I don’t think I can use texture drawing cause it will have the same problem when using SwapBuffers() at OnDraw().

>You should not mix GDI and OpenGL commands in the same window,
>I mean.

Yes I will not be mix GDI and OpenGL commands on the same window. Currently my progress is I have been able to capture the back and draw at OnDraw() using SetDIBitsToDevice() and my OnDraw() function doesn’t have any other gl functions. It looks like it have worked on my NVIDIA hardware, have to try on some ATI hardware. Also do you have any kind of code snippets for buffer region extension or something. I think it will be good to use extensions that capture and drawing.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.