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:
- render using gl functions
- capture the back buffer
- 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
gluLookAt(....);
glBegin(....);
//......
//......
//......
glEnd();
glFinish();
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
SwapBuffers(m_pDC->GetSafeHdc());
glFinish();
}
void COGLView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if(cx <= 0 || cy <= 0)
{
return;
}
wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
glViewport(0, 0, cx, cy);
SetProjection();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
wglMakeCurrent(NULL, NULL);
Draw3DObject();
}
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)
{
Draw3DObject();
}
}
But have been fixing by calling Draw3DObject() at OnDraw().