2)StdAfx.h 中添加:#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/GLAux.h>
#include <gl/glut.h>
注意:一定要添加在已有的包含文件之后。(我刚开始的时候添加错了)
3)添加成员函数及变量 还有消息相应函数:
BOOL RenderScence();
BOOL SetupPixelFormat(void);
BOOL InitializeOpenGL(CDC* pDC);
void SetLogicalPalette(void);
HGLRC m_hRC;
HPALETTE m_hPalette;
CDC* m_pDC;
4)在view类的PreCreateWindow函数中添加代码:
cs.style |=WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
5)VIEW类的OnCreate函数中添加代码:
m_pDC = new CClientDC(this);
SetTimer(1, 20, NULL);
InitializeOpenGL(m_pDC);
6)view类OnDestroy中添加:
::wglMakeCurrent(0, 0);
::wglDeleteContext(m_hRC);
if (m_hPalette)
{
DeleteObject(m_hPalette);
}
if (m_pDC)
{
delete m_pDC;
}
7)在view类的OnSize中添加:
glViewport(0, 0, cx, cy);
8)在view类的OnTimer中添加:
Invalidate(FALSE);
9)设置逻辑调色板,在view类中添加函数:
void CSOpenGlView::SetLogicalPalette(void)
{
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
}logicPalette = {0x300, 256};
BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE blues[] = {0, 85, 170, 255};
for (int colorNum = 0; colorNum < 256; ++colorNum)
{
logicPalette.aEntries[colorNum].peRed = reds[colorNum & 0x07];
logicPalette.aEntries[colorNum].peGreen = greens[(colorNum >> 0x03) & 0x07];
logicPalette.aEntries[colorNum].peBlue = blues[(colorNum >> 0x06) & 0x03];logicPalette.aEntries[colorNum].peFlags = 0;
}
m_hPalette = CreatePalette((LOGPALETTE*) &logicPalette);
}
10)添加初始化场景函数:
BOOL CSOpenGlView::InitializeOpenGL(CDC *pDC)
{
m_pDC = pDC;
SetupPixelFormat();
m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
return TRUE;
}
11)设置像素格式:
BOOL CSOpenGlView::SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixelformat;
pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);
::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd);
if (pfd.dwFlags & PFD_NEED_PALETTE)
{
SetLogicalPalette();
}
return TRUE;
}
12)在view类的OnDraw中添加
RenderScence();
13)场景绘制和渲染
BOOL CSOpenGlView::RenderScence()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.2f,0.6f,1.0f);
auxWireSphere(0.1);
::SwapBuffers(m_pDC->GetSafeHdc());
return TRUE;
}