Click here to Skip to main content
15,896,730 members
Articles / Desktop Programming / MFC

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

Rate me:
Please Sign up or sign in to vote.
4.42/5 (11 votes)
13 Aug 2011CPOL3 min read 64.5K   45  
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.
// OAGMFCView.cpp : implementation of the COAGMFCView class
//

#include "stdafx.h"
#include "OAGMFC.h"

#include "OAGMFCDoc.h"
#include "OAGMFCView.h"

#include "CadTools\\Tool.h"
#include "CadTools\\LineTool.h"
#include "CadTools\\PolyLineTool.h"
#include "CadTools\\RectangleTool.h"
#include "CadTools\\TriangleTool.h"
#include "CadTools\\RasterImageTool.h"
#include "CadTools\\Text2DTool.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// COAGMFCView

IMPLEMENT_DYNCREATE(COAGMFCView, CView)

BEGIN_MESSAGE_MAP(COAGMFCView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &COAGMFCView::OnFilePrintPreview)
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_COMMAND(ID_DRAW_LINE, &COAGMFCView::OnDrawLine)
	ON_COMMAND(ID_DRAW_POLYLINE, &COAGMFCView::OnDrawPolyline)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_KEYDOWN()
	ON_COMMAND(ID_DRAW_RECTANGLE, &COAGMFCView::OnDrawRectangle)
	ON_COMMAND(ID_DRAW_TRIANGLE, &COAGMFCView::OnDrawTriangle)
	ON_COMMAND(ID_INSERT_RASTERIMAGE, &COAGMFCView::OnInsertRasterimage)
	ON_COMMAND(ID_DRAW_2DTEXT, &COAGMFCView::OnDraw2dtext)
END_MESSAGE_MAP()

// COAGMFCView construction/destruction

COAGMFCView::COAGMFCView()
:m_pRender(NULL)
,m_pWinGraphicContext(NULL)
,m_pTool(NULL)
{
	
}

COAGMFCView::~COAGMFCView()
{
	if( m_pRender )
		delete m_pRender;

	if( m_pWinGraphicContext )
		delete m_pWinGraphicContext;

	if ( m_pTool )
		delete m_pTool;
}

BOOL COAGMFCView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// COAGMFCView drawing

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

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


// COAGMFCView printing


void COAGMFCView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL COAGMFCView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void COAGMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void COAGMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void COAGMFCView::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void COAGMFCView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// COAGMFCView diagnostics

#ifdef _DEBUG
void COAGMFCView::AssertValid() const
{
	CView::AssertValid();
}

void COAGMFCView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

COAGMFCDoc* COAGMFCView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COAGMFCDoc)));
	return (COAGMFCDoc*)m_pDocument;
}
#endif //_DEBUG


// COAGMFCView message handlers

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

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

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

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

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

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

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

	m_pRender->ScreenToWorld(&pt, &center, 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);
}

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, &center, 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);
}

void COAGMFCView::OnLButtonUp(UINT nFlags, CPoint point)
{
	if ( m_pTool && m_pTool->IsFinished() )
		Invalidate(FALSE);

	ReleaseCapture();

	CView::OnLButtonUp(nFlags, point);
}

void COAGMFCView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if( nChar == 27 || GetKeyState(VK_ESCAPE) & 0x80 )
	{
		if( m_pTool )
		{
			m_pTool->OnFinalize();
			delete m_pTool;
			m_pTool = NULL;
		}
	}

	Invalidate(FALSE);

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void COAGMFCView::OnInsertGeometries( UINT nID )
{
	GetDocument()->CreateNewGeometry2D( nID );
}

void COAGMFCView::OnDrawLine()
{
	if ( m_pTool )
		delete m_pTool;
	
	OnInsertGeometries(1);

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

void COAGMFCView::OnDrawPolyline()
{
	if ( m_pTool )
		delete m_pTool;

	OnInsertGeometries(2);

	m_pTool = new CPolylineTool();
	m_pTool->SetScene( GetDocument()->m_pScene );
}



void COAGMFCView::OnDrawRectangle()
{
	if ( m_pTool )
		delete m_pTool;

	OnInsertGeometries(3);

	m_pTool = new CRectangleTool();
	m_pTool->SetScene( GetDocument()->m_pScene );
	
}

void COAGMFCView::OnDrawTriangle()
{
	if ( m_pTool )
		delete m_pTool;

	OnInsertGeometries(4);

	m_pTool = new CTriangleTool();
	m_pTool->SetScene( GetDocument()->m_pScene );	
}

void COAGMFCView::OnInsertRasterimage()
{
	if ( m_pTool )
		delete m_pTool;

	m_pTool = new CRasterImageTool();
	m_pTool->SetScene( GetDocument()->m_pScene );
	m_pTool->SetWinGraphicContext( m_pWinGraphicContext );
}

void COAGMFCView::OnDraw2dtext()
{
	if ( m_pTool )
		delete m_pTool;

	m_pTool = new CText2DTool();
	m_pTool->SetScene( GetDocument()->m_pScene );
	m_pTool->SetWinGraphicContext( m_pWinGraphicContext );
}

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