建立OpenGL MFC开发环境

1.附加依赖性:    opengl32.lib glu32.lib glaux.lib

2.StdAfx.h 中添加:     #include <gl/GL.h>
               #include <gl/GLU.h>
              #include <gl/GLAux.h>
              #include <gl/glut.h>

3.添加成员函数及变量  还有消息相应函数:     

public:
 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
public:
 afx_msg void OnDestroy();
public:
 afx_msg void OnSize(UINT nType, int cx, int cy);
public:
 afx_msg void OnTimer(UINT_PTR nIDEvent);
public:
 bool RenderScene(void);
public:
 bool SetupPixelFormat(void);
public:
 void SetLogicalPalette(void);
public:
 bool InitializeOpenGL(CDC* pDC);
public:
 HGLRC m_hRC;
public:
 HPALETTE m_hPalette;
public:
 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 CGraphicFormView::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 CGraphicFormView::InitializeOpenGL(CDC* pDC)
{
 m_pDC = pDC;
 SetupPixelFormat();
 m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
 ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
 return TRUE;
}

11.设置像素格式:

bool CGraphicFormView::SetupPixelFormat(void)
{
 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.画图,在OnDraw中实现。

 
阅读更多
上一篇vc 中的Ivalidate()
下一篇用GDI函数画圆。可确定圆心的
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭