I create a MFC single document application using the VC 2012 MFC template.
Then I add the opengl support on it as follows:
In the View class:
#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/glut.h>
class CImageDeblurringView : public CView
{
protected: // create from serialization only
CImageDeblurringView();
DECLARE_DYNCREATE(CImageDeblurringView)
// Attributes
public:
CImageDeblurringDoc* GetDocument() const;
// Operations
public:
HGLRCm_hrc;
...
};
Int the view implementation, I add the message function on WM_PAINT:
#include "stdafx.h"
#ifndef SHARED_HANDLERS
#include "ImageDeblurring.h"
#endif
#include "ImageDeblurringDoc.h"
#include "ImageDeblurringView.h"
int CImageDeblurringView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
PIXELFORMATDESCRIPTOR pfd ;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_SUPPORT_GDI;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
//得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)
HWND hWnd = this->GetSafeHwnd();
//GetDC该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄
//以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。
HDC hDC = ::GetDC(hWnd);
int nPixelFormat = ChoosePixelFormat(hDC, &pfd);
if (nPixelFormat == 0)
return false;
BOOL bResult = SetPixelFormat (hDC, nPixelFormat, &pfd);
if (!bResult)
return false;
// --- OpenGL 3.x ---
m_hrc = wglCreateContext(hDC);
if(m_hrc==0)
{
AfxMessageBox(_T("wglCreateContext failed!"));
return 0;
}
if(wglMakeCurrent(hDC, m_hrc) == FALSE)
{
AfxMessageBox(_T("wglMakeCurrent failed!"));
return 0;
}
//set default opengl environment parameters.
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0,0.0,0.0,0.0);// 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
return 0;
}
BOOL CImageDeblurringView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);//openGL在MDI中必需
return CView::PreCreateWindow(cs);
}
void CImageDeblurringView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
GLsizei width,height;
width = cx;
height = cy;
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);//透视投影
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
void CImageDeblurringView::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRgn rgnA;
rgnA.CreateRoundRectRgn( 50, 50, 150, 150, 30, 30 );
CBrush brA, brB, brC;
brA.CreateSolidBrush( RGB(255, 0, 0) );
dc.FillRgn( &rgnA, &brA);
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CView::OnPaint()
// Here's Where We Do All The Drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f);//物体左移1.5,向内移6,相当于移动镜头一样,让物体进入镜头中
glBegin(GL_TRIANGLES); // 绘制三角形
glColor3f(255.0f,0.0f,0.0f);
glVertex3f( 0.0f, 255.0f, 0.0f); // 上顶点
glColor3f(0.0f,255.0f,0.0f);
glVertex3f(-255.0f,-255.0f, 0.0f); // 左下
glColor3f(0.0f,0.0f,255.0f);
glVertex3f( 1.0f,-1.0f, 0.0f); // 右下
glEnd(); // 三角形绘制结束
glTranslatef(3.0f,0.0f,0.0f);
glBegin(GL_QUADS); // 绘制正方形
glColor3f(255.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f); // 左上
glColor3f(0.0f,255.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f); // 右上
glColor3f(0.0f,0.0f,255.0f);
glVertex3f( 1.0f,-1.0f, 0.0f); // 左下
glColor3f(255.255f,255.0f,255.0f);
glVertex3f(-1.0f,-1.0f, 0.0f); // 右下
glEnd(); // 正方形绘制结束
glFlush();
SwapBuffers(dc.m_hDC);
}
The problem is:
When I build the program and run it, only the following code display a round corner square,
CRgn rgnA;
rgnA.CreateRoundRectRgn( 50, 50, 150, 150, 30, 30 );
CBrush brA, brB, brC;
brA.CreateSolidBrush( RGB(255, 0, 0) );
dc.FillRgn( &rgnA, &brA);
All the OpenGL related code give NO display on the view.
What is missed?