Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / MFC

OAG Library (OpenGL) Part 1 - Setting Up the Library for an MFC Application

Rate me:
Please Sign up or sign in to vote.
4.40/5 (11 votes)
7 Aug 2011CPOL3 min read 56K   56  
OAG is a library written in C++. With this library, you can create OpenGL based applications.
#include "..\..\..\OpenGL\include\WindowsViewer\WinGraphicContext.h"

oag::WinGraphicContext::WinGraphicContext(void)
:m_bIsDone(FALSE)
,m_pDC(NULL)
,m_hRC(NULL)
,X(0)
,Y(0)
,m_WindowWidth(0)
,m_WindowHeight(0)
{

}

oag::WinGraphicContext::WinGraphicContext(CWnd* pWindow)
:m_bIsDone(FALSE)
,m_pDC(NULL)
,m_hRC(NULL)
,X(0)
,Y(0)
,m_WindowWidth(0)
,m_WindowHeight(0)
{
	m_pDC = new CClientDC(pWindow);

	if( m_pDC )
	{
		if (!SetDCPixelFormat(m_pDC->GetSafeHdc(), PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER )) 
			return;

		m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());

		CRect rect;
		pWindow->GetClientRect(rect);

		m_WindowWidth = rect.Width();
		m_WindowHeight = rect.Height();
	}
}

oag::WinGraphicContext::~WinGraphicContext(void)
{
	if (m_hRC != NULL)
	{
		::wglDeleteContext(m_hRC);	
		m_hRC = NULL;
	}

	if(m_pDC != NULL)
	{
		delete m_pDC;
		m_pDC = NULL;
	}
}

//Operations
bool oag::WinGraphicContext::SetDCPixelFormat(HDC hDC, DWORD dwFlags)
{
	PIXELFORMATDESCRIPTOR pixelDescriptor;
	
	pixelDescriptor.nSize			= sizeof(PIXELFORMATDESCRIPTOR);
	pixelDescriptor.nVersion		= 1;
	pixelDescriptor.dwFlags			= dwFlags;
	pixelDescriptor.iPixelType		= PFD_TYPE_RGBA;
	pixelDescriptor.cColorBits		= 16;
	pixelDescriptor.cRedBits = 8;
	pixelDescriptor.cRedShift = 16;
	pixelDescriptor.cGreenBits = 8;
	pixelDescriptor.cGreenShift = 8;
	pixelDescriptor.cBlueBits = 8;
	pixelDescriptor.cBlueShift = 0;
	pixelDescriptor.cAlphaBits = 0;
	pixelDescriptor.cAlphaShift = 0;
	pixelDescriptor.cAccumBits = 64;
	pixelDescriptor.cAccumRedBits = 16;
	pixelDescriptor.cAccumGreenBits = 16;
	pixelDescriptor.cAccumBlueBits = 16;
	pixelDescriptor.cAccumAlphaBits = 0;
	pixelDescriptor.cDepthBits = 32;
	pixelDescriptor.cStencilBits = 8;
	pixelDescriptor.cAuxBuffers = 0;
	pixelDescriptor.iLayerType = PFD_MAIN_PLANE;
	pixelDescriptor.bReserved = 0;
	pixelDescriptor.dwLayerMask = 0;
	pixelDescriptor.dwVisibleMask = 0;
	pixelDescriptor.dwDamageMask = 0;
	
	int nPixelIndex					= ::ChoosePixelFormat(hDC, &pixelDescriptor);
	
	if (nPixelIndex == 0) //*** Choose default
	{
		nPixelIndex = 1;
		
		if (::DescribePixelFormat(hDC, nPixelIndex, sizeof(PIXELFORMATDESCRIPTOR), &pixelDescriptor) == 0) return FALSE;
	}

	if (!::SetPixelFormat(hDC, nPixelIndex, &pixelDescriptor)) return FALSE;

	return true;
}

void oag::WinGraphicContext::OnSize(int width, int height)
{
	m_WindowWidth = width; m_WindowHeight = height;
}

BOOL oag::WinGraphicContext::MakeCurrent()
{
	BOOL bMake = false;

	if( m_hRC )
		bMake = ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); 

	return bMake;
}

void oag::WinGraphicContext::DeleteCurrent()
{
	::wglMakeCurrent(NULL, NULL);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Brazil Brazil
I live in Matão, a small city in Brazil. I studied as Programmer in a College for Software Development in Database.
After finishing the College I have been working with java, c# and Computer Graphics with searches for OpenGL.

Comments and Discussions