15,035,866 members
Articles / Desktop Programming / MFC
Article
Posted 13 Jun 2010

58.8K views
44 bookmarked

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

Rate me:
4.13/5 (10 votes)
13 Aug 2011CPOL3 min read
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

 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.

C++
```//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.

C++
```//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.

C++
```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.

C++
```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

C++
```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.

## 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.

C++
```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.

C++
```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.

C++
```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)

## About the Author

 Software Developer 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

 First Prev Next
 need help Member 1469824415-Jul-20 13:50 Member 14698244 15-Jul-20 13:50
 Cannot open include file: 'OAGScene.h': No such file or directory masoudrad22-Oct-12 9:58 masoudrad 22-Oct-12 9:58
 library down page not found. waterharbin9-Aug-11 15:02 waterharbin 9-Aug-11 15:02
 Hello,Eduardo.I can not find your libraries.Can you update your download page again?
 Re: library down page not found. [modified] Eduardo Tucci12-Aug-11 7:29 Eduardo Tucci 12-Aug-11 7:29
 Triangle tool redundant Sharjith25-Oct-10 11:22 Sharjith 25-Oct-10 11:22
 Re: Triangle tool redundant Eduardo Tucci26-Oct-10 5:35 Eduardo Tucci 26-Oct-10 5:35
 Re: very good. Eduardo Tucci16-Jun-10 7:15 Eduardo Tucci 16-Jun-10 7:15
 Allocationg objects on the heap Nemanja Trifunovic15-Jun-10 5:08 Nemanja Trifunovic 15-Jun-10 5:08
 Re: Allocationg objects on the heap Eduardo Tucci15-Jun-10 14:47 Eduardo Tucci 15-Jun-10 14:47
 A transparent back ground kzhao66@yahoo.com15-Jun-10 4:47 kzhao66@yahoo.com 15-Jun-10 4:47
 Re: A transparent back ground Eduardo Tucci15-Jun-10 14:14 Eduardo Tucci 15-Jun-10 14:14
 Re: A transparent back ground kzhao66@yahoo.com16-Jun-10 4:48 kzhao66@yahoo.com 16-Jun-10 4:48
 Re: A transparent back ground Eduardo Tucci16-Jun-10 4:58 Eduardo Tucci 16-Jun-10 4:58
 Re: A transparent back ground Santhosh G_20-Jun-10 14:48 Santhosh G_ 20-Jun-10 14:48
 Last Visit: 31-Dec-99 18:00     Last Update: 22-Sep-21 20:54 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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