我想在一个简单的MFC中绘制一些openGl:
流程:
我的onDraw():
void CMFCopenGLtestView::OnDraw(CDC* pDC){
ValidateRect(NULL);
CRect rect;
GetClientRect(&rect);
pDC->SetTextColor(RGB(255, 0, 0));
wglMakeCurrent(m_hDC, m_hRC);
//://
// HERE i WANT TO DRAW SOME OPENGL
//://
SwapBuffers(m_hDC);
wglMakeCurrent(m_hDC, NULL);
pDC->DrawText(_T("test"), 16, &rect, 1);
}
onCreate()
int CMFCopenGLtestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
InitializeOpenGL();
return 0;
}
BOOL CMFCopenGLtestView::InitializeOpenGL()
{
m_pDC = new CClientDC(this);
if (NULL == m_pDC) // failure to get DC
{
return FALSE;
}
if (!SetupPixelFormat(NULL))
{
return FALSE;
}
if (0 == (m_hRC =
::wglCreateContext(m_pDC->GetSafeHdc())))
{
return FALSE;
}
if (FALSE ==
::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC))
{
return FALSE;
}
// specify black as clear color
::glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// specify the back of the buffer as clear depth
::glClearDepth(1.0f);
// enable depth testing
::glEnable(GL_DEPTH_TEST);
return TRUE;
}
BOOL CMFCopenGLtestView::SetupPixelFormat(
PIXELFORMATDESCRIPTOR* pPFD)
{
// default pixel format for a single-buffered,
// OpenGL-supporting, hardware-accelerated,
// RGBA-mode format. Pass in a pointer to a different
// pixel format if you want something else
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),// size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL, // support OpenGL
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
16, // 16-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int pixelformat;
PIXELFORMATDESCRIPTOR* pPFDtoUse;
// let the user override the default pixel format
pPFDtoUse = (0 == pPFD) ? &pfd : pPFD;
if (0 == (pixelformat =
::ChoosePixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse)))
{
return FALSE;
}
if (FALSE == ::SetPixelFormat(m_pDC->GetSafeHdc(),
pixelformat, pPFDtoUse))
{
return FALSE;
}
return TRUE;
}
如果您对我做错了什么有所了解,我会把它们全部拿走!