http://msdn.microsoft.com/library/en-us/vcsample98/html/_sample_mfc_cube.asp

解决方案 »

  1.   

    把这三个库文件加入你的过程中:opengl32.lib glu32.lib glaux.lib 
    VIEW中再包括以下头文件:
    #include "gl\gl.h"
    #include "gl\glu.h"
    #include "gl\glaux.h"
    就可以最VIEW中使用OpenGL了。请看下面的例子:int CAtlas98GLView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CScrollView::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here
    InitPixelFormat();
    InitProjection();
    return 0;
    }void CAtlas98GLView::InitPixelFormat()
    {
    HGLRC hrc;
        m_pDC = new CClientDC(this);
        ASSERT(m_pDC != NULL);
        if (!bSetupPixelFormat())
            return;
        hrc = wglCreateContext(m_pDC->GetSafeHdc());
        wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
    }BOOL CAtlas98GLView::bSetupPixelFormat()
    {
        static PIXELFORMATDESCRIPTOR pfd = 
    {
            sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
            1,                              // version number
            PFD_DRAW_TO_WINDOW |            // support window
              PFD_SUPPORT_OPENGL |          // support OpenGL
              PFD_DOUBLEBUFFER,             // double buffered
            PFD_TYPE_RGBA,                  // RGBA type
            24,                             // 24-bit color depth
            0, 0, 0, 0, 0, 0,               // color bits ignored
            0,                              // no alpha buffer
            0,                              // shift bit ignored
            0,                              // no accumulation buffer
            0, 0, 0, 0,                     // accum bits ignored
            32,                             // 32-bit z-buffer
            0,                              // no stencil buffer
            0,                              // no auxiliary buffer
            PFD_MAIN_PLANE,                 // main layer
            0,                              // reserved
            0, 0, 0                         // layer masks ignored
        };
        int pixelformat;    if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
        {
            MessageBox("ChoosePixelFormat failed");
            return FALSE;
        }    if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
        {
            MessageBox("SetPixelFormat failed");
            return FALSE;
        }    return TRUE;
    }
    void CAtlas98GLView::InitProjection()
    {
    GLfloat fMaxObjSize, fAspect;
    GLfloat fNearPlane, fFarPlane;    GetClientRect(&m_oldRect);
    if(m_oldRect.bottom == 0)
    m_oldRect.bottom = 1;
        
    glViewport(0,0,m_oldRect.right,m_oldRect.bottom); glClearDepth(1.0f);
        glEnable(GL_DEPTH_TEST); fAspect = (GLfloat)m_oldRect.right/m_oldRect.bottom;
    fNearPlane = 0.001f;
    // fFarPlane = 20.0f;
    // fMaxObjSize = 20.0f;
    fFarPlane = 10.0f;
    fMaxObjSize = 10.0f;
    m_dRadius = fNearPlane + fMaxObjSize/2.0f;    glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, fAspect, fNearPlane, fFarPlane);
        glMatrixMode(GL_MODELVIEW);
    }