Click here to Skip to main content
Click here to Skip to main content
Go to top

OpenGL MFC/CDialog - Basic

, 6 Apr 2013
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ReturnVoid
Student
Australia Australia
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 7 Apr 2013
Article Copyright 2013 by ReturnVoid
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid