Chinaunix首页 | 论坛 | 博客
  • 博客访问: 94321
  • 博文数量: 50
  • 博客积分: 1086
  • 博客等级: 少尉
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-25 16:16
文章分类
文章存档

2011年(50)

我的朋友

分类: C/C++

2011-11-15 18:59:48

基于MFC对话框picture控件的OpenGL绘图建立一个基于对话框的工程(名称:OpenGL)
并且在设置的Link里加入库opengl32.lib glu32.lib glaux.lib

为对话框添加picture control 控件,ID:IDC_RENDER
建立好框架后替换代码:

  1. 1、将以下代码替换OpenGLDlg.cpp中代码
  2. 复制代码

  3. // OpenGLDlg.cpp : implementation file
  4. //
  5. #include "stdafx.h"
  6. #include "OpenGL.h"
  7. #include "OpenGLDlg.h"

  8. #include <gl\gl.h>
  9. #include <gl\glu.h>
  10. #include <gl\glaux.h>

  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CAboutDlg dialog used for App About
  18. class CAboutDlg : public CDialog
  19. {
  20. public:
  21.  CAboutDlg();

  22. // Dialog Data
  23.  //{{AFX_DATA(CAboutDlg)
  24.  enum { IDD = IDD_ABOUTBOX };
  25.  //}}AFX_DATA
  26.  // ClassWizard generated virtual function overrides
  27.  //{{AFX_VIRTUAL(CAboutDlg)
  28.  protected:
  29.  virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  30.  //}}AFX_VIRTUAL
  31. // Implementation
  32. protected:
  33.  //{{AFX_MSG(CAboutDlg)
  34.  //}}AFX_MSG
  35.  DECLARE_MESSAGE_MAP()
  36. };
  37. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  38. {
  39.  //{{AFX_DATA_INIT(CAboutDlg)
  40.  //}}AFX_DATA_INIT
  41. }
  42. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44.  CDialog::DoDataExchange(pDX);
  45.  //{{AFX_DATA_MAP(CAboutDlg)
  46.  //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  49.  //{{AFX_MSG_MAP(CAboutDlg)
  50.   // No message handlers
  51.  //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // COpenGLDlg dialog
  55. COpenGLDlg::COpenGLDlg(CWnd* pParent /*=NULL*/)
  56.  : CDialog(COpenGLDlg::IDD, pParent)
  57. {
  58.  //{{AFX_DATA_INIT(COpenGLDlg)
  59.   // NOTE: the ClassWizard will add member initialization here
  60.  //}}AFX_DATA_INIT
  61.  // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  62.   PixelFormat=0;
  63.      m_yRotate = 0;
  64.  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  65. }
  66. void COpenGLDlg::DoDataExchange(CDataExchange* pDX)
  67. {
  68.  CDialog::DoDataExchange(pDX);
  69.  //{{AFX_DATA_MAP(COpenGLDlg)
  70.   // NOTE: the ClassWizard will add DDX and DDV calls here
  71.  //}}AFX_DATA_MAP
  72. }
  73. BEGIN_MESSAGE_MAP(COpenGLDlg, CDialog)
  74.  //{{AFX_MSG_MAP(COpenGLDlg)
  75.  ON_WM_SYSCOMMAND()
  76.  ON_WM_PAINT()
  77.  ON_WM_QUERYDRAGICON()
  78.  ON_WM_TIMER()
  79.  //}}AFX_MSG_MAP
  80. END_MESSAGE_MAP()
  81. /////////////////////////////////////////////////////////////////////////////
  82. // COpenGLDlg message handlers
  83. BOOL COpenGLDlg::OnInitDialog()
  84. {
  85.  CDialog::OnInitDialog();
  86.  // Add "About..." menu item to system menu.
  87.  // IDM_ABOUTBOX must be in the system command range.
  88.  ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  89.  ASSERT(IDM_ABOUTBOX < 0xF000);
  90.  CMenu* pSysMenu = GetSystemMenu(FALSE);
  91.  if (pSysMenu != NULL)
  92.  {
  93.   CString strAboutMenu;
  94.   strAboutMenu.LoadString(IDS_ABOUTBOX);
  95.   if (!strAboutMenu.IsEmpty())
  96.   {
  97.    pSysMenu->AppendMenu(MF_SEPARATOR);
  98.    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  99.   }
  100.  }
  101.  // Set the icon for this dialog. The framework does this automatically
  102.  // when the application's main window is not a dialog
  103.  SetIcon(m_hIcon, TRUE); // Set big icon
  104.  SetIcon(m_hIcon, FALSE); // Set small icon

  105. ///////////////////////OPENGL INIT/////////////////////////
  106.   CWnd *wnd=GetDlgItem(IDC_RENDER);
  107.     hrenderDC=::GetDC(wnd->m_hWnd);
  108.  if(SetWindowPixelFormat(hrenderDC)==FALSE)
  109.   return 0;

  110.  if(CreateViewGLContext(hrenderDC)==FALSE)
  111.   return 0;
  112.  glPolygonMode(GL_FRONT,GL_FILL);
  113.  glPolygonMode(GL_BACK,GL_FILL);
  114. ///////////////////////////////////////////
  115.  glEnable(GL_TEXTURE_2D);
  116.  glShadeModel(GL_SMOOTH);
  117.  glViewport(0,0,259,231);
  118.  glMatrixMode(GL_PROJECTION);
  119.  glLoadIdentity();
  120.  gluPerspective(45,1,0.1,100.0);
  121.  glMatrixMode(GL_MODELVIEW);
  122.  glLoadIdentity();
  123.  glShadeModel(GL_SMOOTH); // Enable Smooth Shading
  124.  glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
  125.  glClearDepth(1.0f); // Depth Buffer Setup
  126.  glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  127.  glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
  128.  /////////////////////////////////////////////////////////////////////////
  129.  glEnableClientState(GL_VERTEX_ARRAY);
  130.  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  131.  SetTimer(1,10,0);
  132. ////////////////////////////////////////////////////////////////
  133.  // TODO: Add extra initialization here

  134.  return TRUE; // return TRUE unless you set the focus to a control
  135. }
  136. void COpenGLDlg::OnSysCommand(UINT nID, LPARAM lParam)
  137. {
  138.  if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  139.  {
  140.   CAboutDlg dlgAbout;
  141.   dlgAbout.DoModal();
  142.  }
  143.  else
  144.  {
  145.   CDialog::OnSysCommand(nID, lParam);
  146.  }
  147. }
  148. // If you add a minimize button to your dialog, you will need the code below
  149. // to draw the icon. For MFC applications using the document/view model,
  150. // this is automatically done for you by the framework.
  151. void COpenGLDlg::OnPaint()
  152. {
  153.  if (IsIconic())
  154.  {
  155.   CPaintDC dc(this); // device context for painting
  156.   SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  157.   // Center icon in client rectangle
  158.   int cxIcon = GetSystemMetrics(SM_CXICON);
  159.   int cyIcon = GetSystemMetrics(SM_CYICON);
  160.   CRect rect;
  161.   GetClientRect(&rect);
  162.   int x = (rect.Width() - cxIcon + 1) / 2;
  163.   int y = (rect.Height() - cyIcon + 1) / 2;
  164.   // Draw the icon
  165.   dc.DrawIcon(x, y, m_hIcon);
  166.  }
  167.  else
  168.  {
  169.   CDialog::OnPaint();
  170.  }
  171. }
  172. // The system calls this to obtain the cursor to display while the user drags
  173. // the minimized window.
  174. HCURSOR COpenGLDlg::OnQueryDragIcon()
  175. {
  176.  return (HCURSOR) m_hIcon;
  177. }
  178. BOOL COpenGLDlg::SetWindowPixelFormat(HDC hDC)
  179. {
  180. PIXELFORMATDESCRIPTOR pixelDesc;
  181. pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  182. pixelDesc.nVersion = 1;
  183. pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
  184.      PFD_SUPPORT_OPENGL |
  185.      PFD_DOUBLEBUFFER |
  186.      PFD_TYPE_RGBA;
  187. pixelDesc.iPixelType = PFD_TYPE_RGBA;
  188. pixelDesc.cColorBits = 32;
  189. pixelDesc.cRedBits = 0;
  190. pixelDesc.cRedShift = 0;
  191. pixelDesc.cGreenBits = 0;
  192. pixelDesc.cGreenShift = 0;
  193. pixelDesc.cBlueBits = 0;
  194. pixelDesc.cBlueShift = 0;
  195. pixelDesc.cAlphaBits = 0;
  196. pixelDesc.cAlphaShift = 0;
  197. pixelDesc.cAccumBits = 0;
  198. pixelDesc.cAccumRedBits = 0;
  199. pixelDesc.cAccumGreenBits = 0;
  200. pixelDesc.cAccumBlueBits = 0;
  201. pixelDesc.cAccumAlphaBits = 0;
  202. pixelDesc.cDepthBits = 0;
  203. pixelDesc.cStencilBits = 1;
  204. pixelDesc.cAuxBuffers = 0;
  205. pixelDesc.iLayerType = PFD_MAIN_PLANE;
  206. pixelDesc.bReserved = 0;
  207. pixelDesc.dwLayerMask = 0;
  208. pixelDesc.dwVisibleMask = 0;
  209. pixelDesc.dwDamageMask = 0;
  210. PixelFormat = ChoosePixelFormat(hDC,&pixelDesc);
  211. if(PixelFormat==0) // Choose default
  212. {
  213.  PixelFormat = 1;
  214.  if(DescribePixelFormat(hDC,PixelFormat,
  215.   sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
  216.  {
  217.   return FALSE;
  218.  }
  219. }
  220. if(SetPixelFormat(hDC,PixelFormat,&pixelDesc)==FALSE)
  221. {
  222.  return FALSE;
  223. }
  224. return TRUE;
  225. }

  226. BOOL COpenGLDlg::CreateViewGLContext(HDC hDC)
  227. {
  228. hrenderRC = wglCreateContext(hDC);
  229. if(hrenderRC==NULL)
  230.  return FALSE;
  231. if(wglMakeCurrent(hDC,hrenderRC)==FALSE)
  232.  return FALSE;
  233. return TRUE;
  234. }
  235. void COpenGLDlg::RenderScene()
  236. {

  237.  /////////////////////////////////////////////////
  238.  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  239.  glLoadIdentity();
  240.  glTranslatef(0.0f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
  241.  glRotated(m_yRotate, 0.0, 1.0, 0.0);
  242.  glBegin(GL_TRIANGLES); // Drawing Using Triangles

  243.   glVertex3f( 0.0f, 1.0f, 0.0f); // Top
  244.   glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
  245.   glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
  246.  glEnd(); // Finished Drawing The Triangle
  247.  SwapBuffers(hrenderDC);
  248. }
  249. void COpenGLDlg::OnTimer(UINT nIDEvent) //实时绘制场景
  250. {
  251.  // TODO: Add your message handler code here and/or call default
  252.  RenderScene();
  253.  m_yRotate +=3;
  254.  CDialog::OnTimer(nIDEvent);
  255. }



  256.  2、替换OpenGLDlg.h代码
  257. 复制代码

  258. // OpenGLDlg.h : header file
  259. //
  260. #if !defined(AFX_OPENGLDLG_H__8E962FCE_4DD3_4AE0_BA13_D93DE3FBA4A1__INCLUDED_)
  261. #define AFX_OPENGLDLG_H__8E962FCE_4DD3_4AE0_BA13_D93DE3FBA4A1__INCLUDED_
  262. #if _MSC_VER > 1000
  263. #pragma once
  264. #endif // _MSC_VER > 1000
  265. /////////////////////////////////////////////////////////////////////////////
  266. // COpenGLDlg dialog
  267. class COpenGLDlg : public CDialog
  268. {
  269. // Construction
  270. public:
  271.  COpenGLDlg(CWnd* pParent = NULL); // standard constructor

  272.   BOOL SetWindowPixelFormat(HDC hDC); //设定象素格式
  273.  BOOL CreateViewGLContext(HDC hDC); //View GL Context
  274.     void RenderScene(); //绘制场景
  275.  HDC hrenderDC; //DC
  276.  HGLRC hrenderRC; //RC
  277.  float m_yRotate; //转速
  278.  int PixelFormat;
  279. // Dialog Data
  280.  //{{AFX_DATA(COpenGLDlg)
  281.  enum { IDD = IDD_OPENGL_DIALOG };
  282.   // NOTE: the ClassWizard will add data members here
  283.  //}}AFX_DATA
  284.  // ClassWizard generated virtual function overrides
  285.  //{{AFX_VIRTUAL(COpenGLDlg)
  286.  protected:
  287.  virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  288.  //}}AFX_VIRTUAL
  289. // Implementation
  290. protected:
  291.  HICON m_hIcon;
  292.  // Generated message map functions
  293.  //{{AFX_MSG(COpenGLDlg)
  294.  virtual BOOL OnInitDialog();
  295.  afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
  296.  afx_msg void OnPaint();
  297.  afx_msg HCURSOR OnQueryDragIcon();
  298.  afx_msg void OnTimer(UINT nIDEvent);
  299.  //}}AFX_MSG
  300.  DECLARE_MESSAGE_MAP()
  301. };
  302. //{{AFX_INSERT_LOCATION}}
  303. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  304. #endif // !defined(AFX_OPENGLDLG_H__8E962FCE_4DD3_4AE0_BA13_D93DE3FBA4A1__INCLUDED_)
阅读(3845) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~