SAMPLE: MFCOGL a Generic MFC OpenGL Code Sample

Last reviewed: October 10, 1997
Article ID: Q127071
4.00    | 3.50 3.51 2.00 2.10
WINDOWS | WINDOWS NT kbgraphic kbcode kbfile kbwebcontent

The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    
  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ 32-bit Edition, versions 2.0 and 2.1
    

SUMMARY

Microsoft Windows NT's OpenGL can be used with the Microsoft Foundation Class (MFC) library. This article gives you the steps to follow to enable MFC applications to use OpenGL.

The companion sample (MFCOGL) is a generic sample that demonstrates using OpenGL with MFC. Download MFCOGL.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download MFCOGL.EXE (size: 31139 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the SOFTLIB\MSLFILES directory
          Get MFCOGL.EXE (size: 31139 bytes) 
    

MORE INFORMATION

Step-by-Step Example to Use OpenGL in MFC Application

  1. Include the necessary header files to use OpenGL including:

        - "gl\gl.h" for all core OpenGL library fuctions. These functions
          have the "gl" prefix such as glBegin().
    

        - "gl\glu.h" for all OpenGL utility library functions. These
          functions have the "glu" prefix such as gluLookAt().
    

        - "gl\glaux.h" for all Windows NT OpenGL auxilary library functions.
          These functions have the "aux" prefix such as auxSphere().
    

    You don't need to add a header file for functions with the "wgl" prefix.

  2. Add necessary library modules to the link project settings. These library modules include OPENGL32.LIB, GLU32.LIB, and GLAUX.LIB.

  3. Add implementations for OnPaletteChanged() and OnQueryNewPalette() in CMainFrame class for palette-aware applications.

       void CMainFrame::OnPaletteChanged(CWnd* pFocusWnd)
       {
           CFrameWnd::OnPaletteChanged(pFocusWnd);
    
           if (pFocusWnd != this)
               OnQueryNewPalette();
    
       }
    
       BOOL CMainFrame::OnQueryNewPalette()
       {
           WORD   i;
           CPalette   *pOldPal;
           CMfcOglView *pView = (CMfcOglView *)GetActiveView();
           CClientDC   dc(pView);
    
    
           pOldPal = dc.SelectPalette(&pView->m_cPalette, FALSE);
           i = dc.RealizePalette();
           dc.SelectPalette(pOldPal, FALSE);
    
           if (i > 0)
               InvalidateRect(NULL);
    
           return CFrameWnd::OnQueryNewPalette();
       }
    
    

  4. Use the one or more of the following classes derived from CWnd, including view classes, that will use OpenGL for rendering onto:

        - Implement PreCreateWindow() and add WS_CLIPSIBLINGS and
          WS_CLIPCHILDREN to the windows styles:
    

          BOOL CMfcOglView::PreCreateWindow(CREATESTRUCT& cs)
          {
    
              cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
    
              return CView::PreCreateWindow(cs);
          }
    
        - Implement OnCreate() to initialize a rendering context and make
          it current. Also, initialize any OpenGL states here:
    
          int CMfcOglView::OnCreate(LPCREATESTRUCT lpCreateStruct)
          {
              if (CView::OnCreate(lpCreateStruct) == -1)
                  return -1;
    
              Init(); // initialize OpenGL
    
              return 0;
          }
    
        - Implement OnSize() if the window is sizeable:
    
          void CMfcOglView::OnSize(UINT nType, int cx, int cy)
          {
              CView::OnSize(nType, cx, cy);
    
              if (cy > 0)
              {
                  glViewport(0, 0, cx, cy);
    
                  if ((m_oldRect.right > cx) || (m_oldRect.bottom > cy))
                      RedrawWindow();
    
                  m_oldRect.right = cx;
                  m_oldRect.bottom = cy;
    
                  glMatrixMode(GL_PROJECTION);
                  glLoadIdentity();
                  gluPerspective(45.0f, (GLdouble)cx / cy, 3.0f, 7.0f);
                  glMatrixMode(GL_MODELVIEW);
              }
          }
    
        - Implement OpenGL rendering code. This can be done in OnDraw() or
          other application-specific places such as OnTimer().
    
        - Implement clean-up code, which is typically done in OnDestroy():
    
          void CMfcOglView::OnDestroy()
          {
              HGLRC   hrc;
    
              if (m_nTimerID)
                  KillTimer(m_nTimerID);
    
              hrc = ::wglGetCurrentContext();
    
              ::wglMakeCurrent(NULL,  NULL);
    
              if (hrc)
                  ::wglDeleteContext(hrc);
    
              CPalette    palDefault;
    
              // Select our palette out of the dc
              palDefault.CreateStockObject(DEFAULT_PALETTE);
              m_pDC->SelectPalette(&palDefault, FALSE);
    
              if (m_pDC)
                  delete m_pDC;
    
              CView::OnDestroy();
          }
    


Additional reference words: 3.50 3.51 2.00 2.10 3.00 3.10 4.00 95 graphics
KBCategory: kbgraphic kbcode kbfile kbwebcontent
KBSubcategory: GdiOpenGL
Keywords : GdiOpenGL kbcode kbfile kbgraphic kbwebcontent
Technology : kbMfc
Version : 4.00 | 3.50 3.51 2.00 2.10
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.