Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

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

4.40/5 (11 votes)
7 Aug 2011CPOL3 min read 57.7K   82  
OAG is a library written in C++. With this library, you can create OpenGL based applications.

Introduction

This library has been written for creating OpenGL based applications. In this tutorial, we will setup the library.

About the Library

The library works with plug-ins, and you can save and open the objects in XML format. In this tutorial, we are going to work with some classes to draw Geometry 2D. The library has plug-ins for Geometries 2D, textures, and text (True Type Font). The file OAG_2D_Demo.zip shows this. The library is available for download at SourceForge.

Sample

This sample demonstrates how to setup the library with MFC, setup the directories for directories, and setup the directories for libraries. For the sample, the project name is RenderGraphicMFC. This project tutorial has been created with Visual Studio 2008.

NewProject.jpg

Setting up the directories for includes for the project: The folder OAGLibrary is the folder where the library is stored. The folder OpenGL stores the classes for the OpenGL applications.

Image 2

Setting up the directories for libraries for the project: the bin folder stores the libraries. The folder VC9 stores the libraries with Visual Studio 2008.

Image 3

Setting up the View Class

The class oag::Win32Renderer is a renderer, and the class oag::WinGraphicContext sets up OpenGL in an MFC application.

C++
oag::WinRenderer*		m_pRender;
oag::WinGraphicContext*		m_pWinGraphicContext;

Now we will create the method OnInitialUpdate to initialize the library and the method EraseBkgnd. So OpenGL will be initialized and prepared to draw objects on the screen.

C++
void COAGMFCView::OnInitialUpdate()
{
    CView::OnInitialUpdate();

   //Delete the current m_pWinGraphicContext, 
   //m_pRender and tool if a new document is called
   UnloadData();

   m_pWinGraphicContext = new oag::WinGraphicContext(this);
   m_pRender = new oag::WinRenderer(m_pWinGraphicContext, false);
   m_pRender->SetBackGroundColor(0, 0, 0);

   if ( m_pWinGraphicContext->MakeCurrent() )
   {
      GetDocument()->m_pObjectsMappingTable->LoadOpenGLExtensions();

      m_pWinGraphicContext->DeleteCurrent();
   }
}

BOOL COAGMFCView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	return FALSE;
}

Now we will create the method OnSize to update the window size and the objects on the screen when the window size is changed. We need call the method RenderScene used by the renderer in the function OnDraw to draw the objects on the screen.

C++
void COAGMFCView::OnSize(UINT nType, int cx, int cy)
{
    CView::OnSize(nType, cx, cy);

    if ( m_pWinGraphicContext )
	    m_pWinGraphicContext->OnSize(cx, cy);
}

void COAGMFCDoc::OnDraw(CDC* pDC)
{
  COAGMFCDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);
  if (!pDoc)
   return;

  if( m_pRender )
  {
    m_pRender->SetScene( pDoc->m_pScene );
    m_pRender->RenderScene();
  }
}

Setting up the Document Class

The class oag::OAGScene stores the objects and draws them on the screen.

C++
oag::OAGScene* m_pScene; 
oag::ObjectsMappingTable* m_pObjectsMappingTable;

Implementing the constructor and destructor:

C++
COAGMFCDoc::COAGMFCDoc()
:m_pScene(NULL)
,m_pObjectsMappingTable(NULL)
{
  CreateLibraryObjects();
}

Deleting Objects

We have two ways to delete objects. You can either call m_pScene->DeleteAllObjects(), or delete the objects in the class where the user is adding the objects. For the demo, I use m_pScene->DeleteAllObjects() because the objects are being adding to the scene only.

C++
COAGMFCDoc::~COAGMFCDoc()
{
   UnloadLibraryObjects();
}

void COAGMFCDoc::CreateLibraryObjects()
{
  if( m_pObjectsMappingTable == NULL)
     m_pObjectsMappingTable = new oag::ObjectsMappingTable();

  if( m_pScene == NULL )
  {
     m_pScene = new oag::OAGScene();
     m_pScene->SetFontMappingTable(  m_pObjectsMappingTable->GetFontMappingTable() );
     m_pScene->SetTextureMappingTable( m_pObjectsMappingTable->GetTextureMappingTable() );
  }
}

void COAGMFCDoc::UnloadLibraryObjects()
{
   //Deletes the scene and all objects from the memory
   if ( m_pScene )
   {
	   m_pScene->DeleteAllObjects();

     delete m_pScene;
     m_pScene = NULL;
   }

   if ( m_pObjectsMappingTable )
   {
	   delete m_pObjectsMappingTable;
      m_pObjectsMappingTable = NULL;
   }
}

Compile the application, and you will see a window with a black background:

Image 4

Showing Objects on the Screen

The first thing to do is to change the function OnInitialUpdate on the view. The snippet below shows all the lines of the function. We just need to add a line for CreateObjects().

C++
void COAGMFCView::OnInitialUpdate()
{
  CView::OnInitialUpdate();

  m_pWinGraphicContext = new oag::WinGraphicContext(this);
  m_pRender = new oag::WinRenderer(m_pWinGraphicContext, false);
  m_pRender->SetBackGroundColor(0, 0, 0);

   if ( m_pWinGraphicContext->MakeCurrent() )
   {
      GetDocument()->m_pObjectsMappingTable->LoadOpenGLExtensions();

      m_pWinGraphicContext->DeleteCurrent();
   }

  CreateObjects();
}

The second thing to is to create the function CreateObjects that creates objects and adds them to the scene. So the scene draws the objects on the screen.

C++
void COAGMFCView::CreateObjects()
{
   //Creating a lines
   oag::OAGPrimitives* pLine = new oag::OAGPrimitives();
   pLine->SetGeometryType(GL_LINES);
   GetDocument()->m_pScene->AddObject( pLine );

   pLine->SetArraySize( 4 );
   pLine->AddVertex(450, 100, 0 );
   pLine->AddVertex(450, 200, 0 );
   pLine->AddVertex(400, 150, 0 );
   pLine->AddVertex(500, 150, 0 );

   //Creating a rectangle
   oag::OAGPrimitives* pQuad = new oag::OAGPrimitives();
   pQuad->SetGeometryType(GL_QUADS);
   GetDocument()->m_pScene->AddObject( pQuad );

   pQuad->SetArraySize( 4 );
   pQuad->AddVertex( 100, 100, 0);
   pQuad->AddVertex( 100, 200, 0);
   pQuad->AddVertex( 200, 200, 0);
   pQuad->AddVertex( 200, 100, 0);
}

Image 5

Building Geometries by Command Line

To build geometries using the library, you need to create a new instance of OAGPrimitives. With the class, you can draw primitives supported by OpenGL such as line, polylines, rectangles, and triangles. After creating a new instance of OAGPrimitives, you must use the command SetGeometryType to change the primitive type that you want to draw. The supported types by SetGeometryType are GL_LINES, GL_LINE_STRIP, GL_QUADS, and GL_TRIANGLES. The CreateNewGeometry2D function below shows how to create primitives and add them to the scene. The function CreateNewGeometry2D is not shown in this tutorial.

C++
void COAGMFCDoc::CreateNewGeometry2D(int nType)
{
	switch( nType )
	{
	case 1: //Line
		{
           oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
           pGeo->SetGeometryType( GL_LINES );
           m_pScene->AddObject( pGeo );
		}

		break;
	case 2: //Polyline
		{
          oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
          pGeo->SetGeometryType( GL_LINE_STRIP );
          m_pScene->AddObject( pGeo );
		}
		break;
	case 3: //Rectangle
		{
          oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
          pGeo->SetGeometryType( GL_QUADS );
          m_pScene->AddObject( pGeo );
		}
		break;
	case 4: //Triangle
		{
          oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
          pGeo->SetGeometryType( GL_TRIANGLES );
          m_pScene->AddObject( pGeo );
		}
		break;
	}
}

History

  • 5th June, 2010
    • Initial version
  • 11th June, 2010
    • Added plug-ins for images (BMP, JPG, TGA, PCX)
  • 19th June, 2010
    • Added plug-ins for True Type font
    • Added XML for texture and font
  • 23rd July, 2010
    • Changes in textures
    • Added Texture Mapping (RectangleMapping and TriangleMapping)
    • Added object table for texture and font
  • 15th August, 2010
    • Added simple objects 3D (Cube, Pyramid, and Quadrics)
    • Added Texture Mapping (CubeMapping and PyramidMapping) and Textures for Quadrics
  • 20th October, 2010
    • Added Filters and Wrap for texture
    • Added Multitexture
    • Added Reflection and Scene for Reflection
    • Added Multiple scene for the renderer
  • 1st August, 2011
    • Added plug-in for image PNG
    • Add Transformations using Matrix. The commands SetTranslation, SetScale, SetRotation were replace by SetTransform.
    • For Primitives, the function AddVertex was removed. This command was replaced by SetArrayVertices that uses a list of vector as parameter.
    • Added Billboard (oag::OAGBillboard , OAGBillboardTextureItems, OAGBillboardGroup). The Billboard has support for Texture for the classes RectangleMapping and TriangleMapping.

License

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