Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / MFC
Tip/Trick

OpenGL MFC/CDialog - Basic

Rate me:
Please Sign up or sign in to vote.
4.86/5 (6 votes)
6 Apr 2013CPOL1 min read 24.1K   2.3K   4   3
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.

Image 1

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:

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Praisegreat example! Pin
Southmountain12-Feb-21 8:36
Southmountain12-Feb-21 8:36 
PraiseGreat little example with a dialog in MFC Pin
ninpo18-May-20 10:17
ninpo18-May-20 10:17 
QuestionMy vote of 5 Pin
manujcm11-Nov-15 18:50
manujcm11-Nov-15 18:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.