int COpenGLWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
int pixelformat;
m_pDC = new CClientDC(this);//在客户区作图
ASSERT(m_pDC != NULL);
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), //固定值
1, //固定值
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_TYPE_RGBA, // RGBA模式,不用调色板
16, //程序在16位色彩下运行
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
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
if ( (pixelformat = ChoosePixelFormat
(m_pDC- >GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("在该DC上找不到与PFD接近的位图结构");
return -1;
}
if (SetPixelFormat(m_pDC- >
GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox("无法在该DC上设置位图结构");
return -1;
}
m_hrc = wglCreateContext(m_pDC- >GetSafeHdc());
wglMakeCurrent(m_pDC- >GetSafeHdc(), m_hrc);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
return 0;//OpenGL窗口构造成功
}
void COpenGLWnd::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if(cy > 0)
{
glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (cx < = cy)
glOrtho(-3.0,3.0,-3.0 * (GLfloat)cx/(GLfloat)cy,
3.0 * (GLfloat)cx/(GLfloat)cy,-3.0,3.0);
else
glOrtho(-3.0,3.0,-3.0 * (GLfloat)cy/(GLfloat)cx,
3.0 * (GLfloat)cy/(GLfloat)cx,-3.0,3.0);
glMatrixMode(GL_MODELVIEW);
}
}
void COpenGLWnd::OnDestroy()
{
CFrameWnd::OnDestroy();
::wglMakeCurrent(NULL, NULL);
if (m_hrc)
::wglDeleteContext(m_hrc);
if (m_pDC)
delete m_pDC;
// TODO: Add your message handler code here
}
BOOL COpenGLWnd::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message
handler code here and/or call default
return TRUE;
//return CFrameWnd::OnEraseBkgnd(pDC);
}
void COpenGLWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
GLfloat light_position[]={2.0f,0.0f,4.0f,0.0f};
// TODO: Add your message handler code here
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);