Click here to Skip to main content
Click here to Skip to main content

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

, 13 Aug 2011 CPOL
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.

Introduction

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

Primline.jpg
Line Polyline Rectangle Triangle

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 );
pQuad->SetPolygonMode(GL_FILL);
 
//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 );
pTri->SetPolygonMode(GL_FILL);
 
//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 )
    return;
 
::glPushMatrix();
::glLoadIdentity();
 
::glColor4ubv( m_Color.GetColor4ubv() );
 
::glPolygonMode(m_PolygonFace, m_PolygonMode);
 
switch( m_GeometryType )
{
 case GL_LINES:
 case GL_LINE_LOOP: 
 case GL_LINE_STRIP:
 case GL_QUADS:
		{
        ::glEnableClientState(GL_VERTEX_ARRAY);
        ::glVertexPointer(3, GL_FLOAT, 0, m_GeometryData);
        ::glDrawArrays( m_GeometryType, 0, m_ArraySize );
        ::glDisableClientState(GL_VERTEX_ARRAY);
       }
		break;
 case GL_TRIANGLES:
 case GL_TRIANGLE_FAN:
 case GL_TRIANGLE_STRIP:
	{
       ::glEnableClientState(GL_VERTEX_ARRAY);
       ::glVertexPointer(3, GL_FLOAT, 0, m_GeometryData);
       ::glDrawElements(m_GeometryType, GetVertexCount(), GL_UNSIGNED_BYTE, m_nIndices);
       ::glDisableClientState(GL_VERTEX_ARRAY);
 	}
    break;
}
 
	::glPopMatrix();
} 

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();
	
 m_pWinGraphicContext->MakeCurrent();
 font2d->Initialize();
 m_pWinGraphicContext->DeleteCurrent();
 
 font2d->SetText("Example of 2DText");
 font2d->SetFontSize(20);
 
 //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 )
   return;
 
   ::glPushMatrix();
   ::glLoadIdentity();
 
   ::glColor4ubv( m_Color.GetColor4ubv() );
 
   ::glPolygonMode(m_PolygonFace, m_PolygonMode);
 
   ::glLoadMatrixf( m_ObjectMatrix.GetData() );
 
   m_pFont->Render(m_strText.c_str());
 
   ::glPopMatrix();
 
}

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;
	
   OnInsertGeometries(1);

  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 );
    Invalidate(FALSE);		
  }

  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 );

    Invalidate(FALSE);
 }

  CView::OnMouseMove(nFlags, point);
} 

License

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

Share

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.

Comments and Discussions

 
QuestionCannot open include file: 'OAGScene.h': No such file or directory Pinmembermasoudrad22-Oct-12 10:58 
Questionlibrary down page not found. Pinmemberwaterharbin9-Aug-11 16:02 
AnswerRe: library down page not found. [modified] PinmemberEduardo Tucci12-Aug-11 8:29 
GeneralTriangle tool redundant PinmemberSharjith25-Oct-10 12:22 
GeneralRe: Triangle tool redundant PinmemberEduardo Tucci26-Oct-10 6:35 
GeneralRe: very good. PinmemberEduardo Tucci16-Jun-10 8:15 
GeneralAllocationg objects on the heap PinmemberNemanja Trifunovic15-Jun-10 6:08 
GeneralRe: Allocationg objects on the heap PinmemberEduardo Tucci15-Jun-10 15:47 
GeneralA transparent back ground Pinmemberkzhao66@yahoo.com15-Jun-10 5:47 
GeneralRe: A transparent back ground PinmemberEduardo Tucci15-Jun-10 15:14 
GeneralRe: A transparent back ground Pinmemberkzhao66@yahoo.com16-Jun-10 5:48 
GeneralRe: A transparent back ground PinmemberEduardo Tucci16-Jun-10 5:58 
GeneralRe: A transparent back ground Pinmembersanthosh4gCode20-Jun-10 15:48 

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

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

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