有以下一段代码可不知为什么就是在视图中不显示图形。头文件、库都已加入。而且还不报错以下是主要CPP文件。// LinView.cpp : implementation of the CLinView class
//#include "stdafx.h"
#include "Lin.h"#include "LinDoc.h"
#include "LinView.h"
#include "gl/gl.h"
#include "gl/glu.h"
#include "gl/glaux.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CLinViewIMPLEMENT_DYNCREATE(CLinView, CView)BEGIN_MESSAGE_MAP(CLinView, CView)
//{{AFX_MSG_MAP(CLinView)
ON_WM_SIZE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CLinView construction/destructionCLinView::CLinView()
{
// TODO: add construction code here m_pDC=NULL;
}CLinView::~CLinView()
{
}BOOL CLinView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
}/////////////////////////////////////////////////////////////////////////////
// CLinView drawingvoid CLinView::OnDraw(CDC* pDC)
{
CLinDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
DrawScene(); // TODO: add draw code for native data here
}/////////////////////////////////////////////////////////////////////////////
// CLinView printingBOOL CLinView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}void CLinView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}void CLinView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}/////////////////////////////////////////////////////////////////////////////
// CLinView diagnostics#ifdef _DEBUG
void CLinView::AssertValid() const
{
CView::AssertValid();
}void CLinView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}CLinDoc* CLinView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLinDoc)));
return (CLinDoc*)m_pDocument;
}
#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CLinView message handlersBOOL CLinView::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 CLinView::Init()
{
PIXELFORMATDESCRIPTOR pfd;
HGLRC hrc;
int n;
m_pDC = new CClientDC (this);
ASSERT(m_pDC != NULL);
if(!bSetupPixelFormat())
return;
n=::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(),n,sizeof(pfd),&pfd);
hrc=wglCreateContext(m_pDC->GetSafeHdc()); glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
glLineWidth(2.0);
glShadeModel(GL_FLAT);
glClearColor(0.0,0.0,0.0,0.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void CLinView::OnSize(UINT nType, int cx, int cy) 
{
CView::OnSize(nType, cx, cy);

int w=cx;
int h=cy;
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); gluPerspective(45.0,(GLfloat)w/(GLfloat)h,3.0,5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-4.0);
glRotatef(15.0,1.0,1.0,0.0);
}void CLinView::DrawScene(void)
{
glClearColor(0.0,1,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor4f(0.0,0.6,0.5,1.0); auxWireOctahedron(1.0);
glFinish();
SwapBuffers(wglGetCurrentDC());
}

解决方案 »

  1.   

    wglMakeCurrent 函数调用了吗?在调用其他 OpenGL 函数前必须调用 wglMakeCurrent 设置当前rc,就像以下示例:HDC    hdc; 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  iPixelFormat; 
     
    // get the best available match of pixel format for the device context  
    iPixelFormat = ChoosePixelFormat(hdc, &pfd); 
     
    // make that the pixel format of the device context 
    SetPixelFormat(hdc, iPixelFormat, &pfd); // create a rendering context 
    HGLRC hglrc = wglCreateContext (hdc); 
     
    // make it the calling thread's current rendering context
    wglMakeCurrent (hdc, hglrc);
     
    // call OpenGL APIs as desired ...
     
    // when the rendering context is no longer needed ...  
     
    // make the rendering context not current 
    wglMakeCurrent (NULL, NULL) ; 
     
    // delete the rendering context 
    wglDeleteContext (hglrc); 
      

  2.   

    估计问题在onsize()和drawscene(),好好调调里面的参数,记住物体一定要在你的视景体里面才能看得到
    void CLinView::OnSize(UINT nType, int cx, int cy) 
    {
    CView::OnSize(nType, cx, cy);

    int w=cx;
    int h=cy;
    if(h==0)
    h=1;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); gluPerspective(45.0,(GLfloat)w/(GLfloat)h,3.0,5.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0,0.0,-4.0);
    glRotatef(15.0,1.0,1.0,0.0);
    }
    void CLinView::DrawScene(void)
    {
    glClearColor(0.0,1,0,0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor4f(0.0,0.6,0.5,1.0); auxWireOctahedron(1.0);
    glFinish();
    SwapBuffers(wglGetCurrentDC());
    }
      

  3.   

    void CLinView::OnSize(UINT nType, int cx, int cy) 
    {
    CView::OnSize(nType, cx, cy);

    int w=cx;
    int h=cy;
    if(h==0)
    h=1;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); gluPerspective(45.0,(GLfloat)w/(GLfloat)h,3.0,5.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    }void CLinView::DrawScene(void)
    {
    glClearColor(0.0,1,0,0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor4f(0.0,0.6,0.5,1.0);
    //////////////这样不更好吗///////////////////
             glTranslatef(0.0,0.0,-4.0);//你可以试试这个的参数
    glRotatef(15.0,1.0,1.0,0.0);
    ///////////////////////////////////
    auxWireOctahedron(1.0);
    glFinish();
    SwapBuffers(wglGetCurrentDC());
    }