glu32.lib glaux.lib opengl32.lib。OpenGLApp : CWinApp 與 OpenGLWin : CFrameWnd 類別。// OpenGLApp.cpp
OpenGLApp::InitInstance() {
OpenGLWin* pWnd = new OpenGLWin;
pWnd->ShowWindow(SW_SHOWNORMAL);
pWnd->UpdateWindow();
m_pMainWnd = pWnd;
return TRUE;
}
OpenGLApp app;
// OpenGLWin.h
class OpenGLWin : public CFrameWnd {
HGLRC m_hglrc;
afx_msg int OnCreate(LPCREATESTRUCT);
afx_msg void OnPaint();
afx_msg void OnDestroy();
void myinit(); void drawsphere(); void OnDrawSin();
DECLARE_MESSAGE_MAP()
};
// OpenGLWin.cpp
BEGIN_MESSAGE_MAP(OpenGLWin, CFrameWnd)
ON_WM_CREATE() ON_WM_PAINT() ON_WM_DESTROY()
END_MESSAGE_MAP()
OpenGLWin::OpenGLWin() { myinit(); Create(NULL, _T("OpenGL Demo")); }
void OpenGLWin::myinit() {
HDC hdc = ::GetDC(m_hWnd);
// 設定像素格式、建立 RC ...
m_hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, m_hglrc);
glClearColor(0,0,0,1);
::ReleaseDC(m_hWnd, hdc);
}
void OpenGLWin::drawsphere() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auxSolidSphere(1.0);
glFlush(); auxSwapBuffers();
}
void OpenGLWin::OnDrawSin() {
static float angle = 0;
angle += 0.05f;
glLoadIdentity();
gluLookAt(5*sin(angle), 0, 5*cos(angle), 0,0,0, 0,1,0); // 繞 Y 軸公轉
drawsphere();
}
void OpenGLWin::OnPaint() {
CPaintDC dc(this);
wglMakeCurrent(dc.m_hDC, m_hglrc);
OnDrawSin();
}
void OpenGLWin::OnDestroy() {
wglMakeCurrent(NULL, NULL);
wglDeleteContext(m_hglrc);
CFrameWnd::OnDestroy();
}
此範例展示最小化 MFC + OpenGL 架構,透過 gluLookAt 移動視點模擬動畫,適合作為進一步擴展的基礎。