MFC下OpenGL扩展调用失败,而glut则没有问题
力为
博客专家认证 2009-02-13 05:39:00 MFC下OpenGL扩展调用失败,而glut则没有问题。
如何解决呢?
以下是代码。
// GLEWTest2005View.cpp : implementation of the CGLEWTest2005View class
//
#include "stdafx.h"
#include "GLEWTest2005.h"
#include "GLEWTest2005Doc.h"
#include "GLEWTest2005View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CGLEWTest2005View
IMPLEMENT_DYNCREATE(CGLEWTest2005View, CView)
BEGIN_MESSAGE_MAP(CGLEWTest2005View, CView)
// 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()
// CGLEWTest2005View construction/destruction
CGLEWTest2005View::CGLEWTest2005View()
: m_hGLRC(NULL)
, m_pDC(NULL)
{
// TODO: add construction code here
}
CGLEWTest2005View::~CGLEWTest2005View()
{
delete m_pDC;
}
BOOL CGLEWTest2005View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// CGLEWTest2005View drawing
void CGLEWTest2005View::OnDraw(CDC* /*pDC*/)
{
CGLEWTest2005Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
wglMakeCurrent(m_pDC->GetSafeHdc(), m_hGLRC);
// TODO: add draw code for native data here
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
CRect rect;
GetClientRect(&rect);
glViewport(rect.left, rect.top, rect.Width(), rect.Height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, rect.Width()/rect.Height(), 0, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 200, 0,0,0, 0,1,0);
glBegin(GL_LINE);
glColor3f(1.0,0,0);
glVertex3f(0,0,0);
glVertex3f(100, 0, 0);
glColor3f(0.0,1,0);
glVertex3f(0,0,0);
glVertex3f(0, 100, 0);
glColor3f(0.0,0,1);
glVertex3f(0,0,0);
glVertex3f(0, 0, 100);
glEnd();
static GLuint id = 0;
glIsBufferARB(id); // Crash
SwapBuffers(m_pDC->GetSafeHdc());
//wglMakeCurrent(NULL, NULL);
}
// CGLEWTest2005View printing
BOOL CGLEWTest2005View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGLEWTest2005View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGLEWTest2005View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
// CGLEWTest2005View diagnostics
#ifdef _DEBUG
void CGLEWTest2005View::AssertValid() const
{
CView::AssertValid();
}
void CGLEWTest2005View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGLEWTest2005Doc* CGLEWTest2005View::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGLEWTest2005Doc)));
return (CGLEWTest2005Doc*)m_pDocument;
}
#endif //_DEBUG
// CGLEWTest2005View message handlers
void CGLEWTest2005View::OnInitialUpdate()
{
CView::OnInitialUpdate();
GLuint PixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // pfd结构的大小
1, // 版本号
PFD_DRAW_TO_WINDOW | // 支持在窗口中绘图
PFD_SUPPORT_OPENGL | // 支持 OpenGL
PFD_DOUBLEBUFFER, // 双缓存模式
PFD_TYPE_RGBA, // RGBA 颜色模式
24, // 24 位颜色深度
0, 0, 0, 0, 0, 0, // 忽略颜色位
1, // 没有非透明度缓存
0, // 忽略移位位
0, // 无累加缓存
0, 0, 0, 0, // 忽略累加位
32, // 32 位深度缓存
8, // 无模板缓存 //改动阴影
0, // 无辅助缓存
PFD_MAIN_PLANE, // 主层
0, // 保留
0, 0, 0 // 忽略层,可见性和损毁掩模
};
HWND hWND = this->GetSafeHwnd();
m_pDC = new CClientDC(this);
HDC hDC = m_pDC->GetSafeHdc();
// 选择合适的Device Context像素点格式
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
::MessageBox(NULL,_T("Can't Find A Suitable PixelFormat."),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION );
return ; // Return FALSE
}
// 设置新的Device Context像素点格式
if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
::MessageBox(NULL,_T("Can't Set The PixelFormat."),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
return ; // Return FALSE
}
// 根据当前的Device Context得到rending context
if (!(this->m_hGLRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
//KillGLWindow(); // Reset The Display
::MessageBox(NULL,_T("Can't Create A GL Rendering Context."),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
return ; // Return FALSE
}
// 设置当前Device context 的rendering context
if(!wglMakeCurrent(hDC,this->m_hGLRC)) // Try To Activate The Rendering Context
{
//KillGLWindow(); // Reset The Display
::MessageBox(NULL,_T("Can't Activate The GL Rendering Context."),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
return ; // Return FALSE
}
glewInit();
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.5f,0.6f,0.8f,1.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
//wglMakeCurrent(NULL, NULL);
}