Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

OpenGL MFC/CDialog - Basic

0.00/5 (No votes)
6 Apr 2013 1  
Working example: OpenGL CDialog Multiple Context

Introduction   

This tip contains a basic working example of OpenGL with MFC/CDialog, without the deprecated glBegin() and glEnd(). The project contains code that enables multiple contexts MFC/CDialog-style.

Background

The basic idea is to get a simple window or two to draw simple graphics in a MFC/CDialog-style window. This is geared more towards beginners who, like myself, struggle to get something like this to work. A basic understanding of C++/OpenGL would be helpful. The code contains no colour mapping or shaders, just basic shape using vao/vbo.

Using the Code

The code was written in VS 2010 as MFC project. It makes use of GLEW & its library.

How to use:

  • Unzip, Open Solution & compile to see example work.
  • Make sure to include glew32.dll in the debug/release folder, as I have not included it.

The following snippet of code highlights main link/binding:

CStatic m_PictCtrl_Left;
OpenGLRenderer m_OGL_Window_Left;

m_PictCtrl_Left.MoveWindow
(iPosOffSetX,iPosOffSetY,iWidth,iHeight,TRUE); //position window & size first 
m_PictCtrl_Left.GetWindowRect(rectLeft);
ScreenToClient(rectLeft);

m_OGL_Window_Left.CreateGLContext(rectLeft, this);

bool OpenGLRenderer::CreateGLContext(CRect rect, CWnd *parent)
{
	CString className = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | 
	CS_OWNDC, NULL, (HBRUSH)GetStockObject(WHITE_BRUSH), 
	NULL); //default background colour
	CreateEx(0, className, _T("OpenGL with MFC/CDialog"), 
	WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, rect, parent, 0);

	SetViewPortSize(rect.Width(), rect.Height());

	if(!InitContext() ) //InitContext() sets PIXELFORMATDESCRIPTOR & context
	{
		MessageBox(_T("ERROR Creating InitContext"));
		return false;
	};
	return true;
} 

Points of Interest

I searched, Googled & read a lot, but failed to come across a complete example that displayed the above without using glBegin()/glEnd() and/or int main()/winmain() as the entry point. Very frustrating.

History

I don't plan on maintaining this, unless something serious warrants updating. There is probably more efficient ways to implement this. I just wanted to share this as I struggled to find something similar that contained a full yet basic working example.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here