Click here to Skip to main content
13,097,225 members (47,434 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 13 Jun 2010

OAG Library (OpenGL) Part 2.1 - Drawing Objects 2D using the Mouse and Programatically

, 13 Aug 2011
Rate this:
Please Sign up or sign in to vote.
This tutorial shows Library Code for Geometries 2D how to draw them programatically and how draw to objects using the mouse in an application MFC.


In this tutorial, I have added classes to draw Geometries 2D, textures and True Type Fonts, so you can draw the objects easier on the screen.You must download the library to compile the sample. Click here to go to the download page. Using the OAGMFC, you can save and open XML files (*.oagxml). The XML files are an alternative for collada files.

The Geometries 2D

Line PolylineRectangleTriangle

Creating Geometries 2D

To build geometries using the library, you need to create a new instance for OAGPrimitives. With the class, you can draw primitives supported by opengl such as line, lines, polylines, rectangle and triangle. 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 (for lines), GL_LINE_STRIP (for polyline), GL_QUADS (for rectangle) and GL_TRIANGLES (for triangle). The example below shows how to create a line.

//Creating a line
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
peo->SetGeometryType( GL_LINES );

If you wish to draw a solid geometry, use the SetPolygonMode(GL_FILL). Using GL_FILL you tell OpenGL to draw Solid Polygons. Below is the code for drawing a solid triangle and solid rectangle.

//solid rectangle
oag::OAGPrimitives* pQuad = new oag::OAGPrimitives();
pQuad->SetGeometryType( GL_QUADS );
//Adding vertices for the rectangle
std::vector<:oagvector3f> lstQuadVector;

lstQuadVector.push_back( oag::OAGVector3f( 20.f, 132.f, 0.f ));
lstQuadVector.push_back( oag::OAGVector3f( 20.f, 145.f, 0.f ));
lstVector.push_back( oag::OAGVector3f( 127.45f, 64.f, 0.f ));
lstVector.push_back( oag::OAGVector3f( 127.45f, 132.f, 0.f ));

pQuad->SetArrayVertices( lstQuadVector );
m_pScene->AddObject( pQuad );
//Solid Triangle
oag::OAGPrimitives* pTri = new oag::OAGPrimitives();
pTri->SetGeometryType( GL_TRIANGLES );
//Adding vertices for the triangle
std::vector<:oagvector3f> lstTriVector;

lstTriVector.push_back( oag::OAGVector3f( 57.f, 39.f, 0.f ));
lstTriVector.push_back( oag::OAGVector3f( 112.f, 128.f, 0.f ));
lstTriVector.push_back( oag::OAGVector3f( 176.f, 64.f, 0.f ));

pTri->SetArrayVertices( lstTriVector );
m_pScene->AddObject( pTri );

The Library Code For Drawing the Primitives

Here an OnDraw function uses commands for Faster Vertex in OpenGL. The commands glDrawElements and glDrawArrays do it.

void oag::OAGPrimitives::OnDraw()
 if( m_ArraySize < 1 )
::glColor4ubv( m_Color.GetColor4ubv() );
::glPolygonMode(m_PolygonFace, m_PolygonMode);
switch( m_GeometryType )
 case GL_LINES:
 case GL_LINE_LOOP: 
 case GL_QUADS:
        ::glVertexPointer(3, GL_FLOAT, 0, m_GeometryData);
        ::glDrawArrays( m_GeometryType, 0, m_ArraySize );
       ::glVertexPointer(3, GL_FLOAT, 0, m_GeometryData);
       ::glDrawElements(m_GeometryType, GetVertexCount(), GL_UNSIGNED_BYTE, m_nIndices);

Creating Text

To create text using the library, you need to create a new instance of oag::OAGFont2D. With this class, you can draw draw 2D text in OpenGL using the libraries FTGL and FreeType. The example below shows how to create a 2D Text. The library and the demo use the file arial.ttf.

oag::OAGFont2D* font2d = new oag::OAGFont2D();


font2d->SetText("Example of 2DText");

//Font Position
oag::Matrix4x4 mt;
mt.SetTranslation(vec.m_X, vec.m_Y, vec.m_Z);

font2d->SetTransform( mt );

m_pScene->AddObject( font2d );

The Library Code for Drawing the Text

void oag::OAGFontMapping2D::OnDraw()
if ( m_pFont == NULL )
   ::glColor4ubv( m_Color.GetColor4ubv() );
   ::glPolygonMode(m_PolygonFace, m_PolygonMode);
   ::glLoadMatrixf( m_ObjectMatrix.GetData() );

The Sample

This tutorial shows how to draw geometries 2D using the mouse using classes. The classes written to draw are in the folder CadTools. This project tutorial has been created with Visual Studio 2008.

The Draw Menu for Drawing the Geometries

Drawing the Geometries

When you call one of the Draw Menu items, one tool is created for the primitive and the document is called to create the primitive type for the menu item. See the example when you call the command line in the Draw menu.

void COAGMFCView::OnDrawLine()
  if ( m_pTool )
    delete m_pTool;

  m_pTool = new CLineTool();
  m_pTool->SetScene( GetDocument()->m_pScene );

After clicking in line you have a primitive created in your scene. To start drawing, you click on the screen and move the mouse to see the primitive on the screen.

The primitive line has two points and when you click again on the screen, you set the second point for the line and the primitive is finished.

Now let's go to the polyline. Select the Draw the menu and click on Polyline and click on the screen to start drawing. For the polyline, you can click on the screen lot of times. When your polyline is done, you must press key ESC to finish the primitive.

The next primitive is the rectangle. Select the Draw the menu and click on Rectangle and click on the screen to start drawing. Move the mouse and see a rectangle drawing on the screen. When the rectangle is done, click on the screen to finish the primitive.

The last primitive is the triangle. Select the Draw the menu and click on Triangle and click on the screen to start drawing. Moving the mouse, you won't see a triangle because you have just two points for the triangle. When you click on the screen again, the second point is defined and you can see a triangle on the screen if you move the mouse. When the triangle is done, click on the screen again. The triangle is finished.

Mouse Events

When you click on the screen, the point is changed to world coordinates. After this, the point is added on the tool. The tool adds the coordinate to the primitive with the function MouseClick. If it is the first click for the primitive, the tool adds two points to the primitive.

void COAGMFCView::OnLButtonDown(UINT nFlags, CPoint point)
  oag::OAGVector3d center;

  oag::OAGVector3f pt(point.x, point.y, 0);

  m_pRender->ScreenToWorld(&pt, ¢er, 1);

  if( m_pTool && !m_pTool->IsFinished() )
    oag::OAGVector3f vec( (float)center.m_X, (float)center.m_Y, 0.f );
    m_pTool->OnMouseClick( vec );

  CView::OnLButtonDown(nFlags, point);

When you move the mouse, the last point is changed and you see the primitive in real time.

void COAGMFCView::OnMouseMove(UINT nFlags, CPoint point)
 if( m_pTool && m_pTool->IsStarted() && !m_pTool->IsFinished() )
    oag::OAGVector3d center;

    oag::OAGVector3f pt(point.x, point.y, 0);
    oag::OAGVector3f vec;

    m_pRender->ScreenToWorld(&pt, ¢er, 1);
    oag::OAGVector3f vecPoint( (float)center.m_X, (float)center.m_Y, 0.f );
    vec.m_X = vecPoint.m_X; vec.m_Y = vecPoint.m_Y;

    m_pTool->OnMouseMove( vec );


  CView::OnMouseMove(nFlags, point);


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


About the Author

Eduardo Tucci
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.

You may also be interested in...

Comments and Discussions

QuestionCannot open include file: 'OAGScene.h': No such file or directory Pin
masoudrad22-Oct-12 9:58
membermasoudrad22-Oct-12 9:58 
Questionlibrary down page not found. Pin
waterharbin9-Aug-11 15:02
memberwaterharbin9-Aug-11 15:02 
AnswerRe: library down page not found. [modified] Pin
Eduardo Tucci12-Aug-11 7:29
memberEduardo Tucci12-Aug-11 7:29 
GeneralTriangle tool redundant Pin
Sharjith25-Oct-10 11:22
memberSharjith25-Oct-10 11:22 
GeneralRe: Triangle tool redundant Pin
Eduardo Tucci26-Oct-10 5:35
memberEduardo Tucci26-Oct-10 5:35 

I Have made one tool for each 2D geometry. In this case the polyline tool and triangle tool are different.
With the polilyne tool you can draw lot of vertices and for triangle tool you draw three vertices to draw a triangle.

You are right. I should have used the polyline tool to draw the triangle. I did not think about this when the tools were created.I am just showing the library commands to draw the 2D Geometries.
GeneralRe: very good. Pin
Eduardo Tucci16-Jun-10 7:15
memberEduardo Tucci16-Jun-10 7:15 
GeneralAllocationg objects on the heap Pin
Nemanja Trifunovic15-Jun-10 5:08
memberNemanja Trifunovic15-Jun-10 5:08 
GeneralRe: Allocationg objects on the heap Pin
Eduardo Tucci15-Jun-10 14:47
memberEduardo Tucci15-Jun-10 14:47 
GeneralA transparent back ground Pin
kzhao66@yahoo.com15-Jun-10 4:47
memberkzhao66@yahoo.com15-Jun-10 4:47 
GeneralRe: A transparent back ground Pin
Eduardo Tucci15-Jun-10 14:14
memberEduardo Tucci15-Jun-10 14:14 
GeneralRe: A transparent back ground Pin
kzhao66@yahoo.com16-Jun-10 4:48
memberkzhao66@yahoo.com16-Jun-10 4:48 
GeneralRe: A transparent back ground Pin
Eduardo Tucci16-Jun-10 4:58
memberEduardo Tucci16-Jun-10 4:58 
GeneralRe: A transparent back ground Pin
santhosh4gCode20-Jun-10 14:48
membersanthosh4gCode20-Jun-10 14:48 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 13 Aug 2011
Article Copyright 2010 by Eduardo Tucci
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid