Click here to Skip to main content
15,892,737 members
Articles / Multimedia / OpenGL

OAG Library (OpenGL) Part 2.3 - Drawing 2D Textures Using the Mouse and Programatically

Rate me:
Please Sign up or sign in to vote.
4.29/5 (6 votes)
22 Oct 2010CPOL3 min read 34.1K   1.3K   17  
This tutorial shows library code for 2D Textures and how to draw them programatically using the mouse in an MFC application.
// 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\\RasterImageRectTool.h"
#include "CadTools\\RasterImageTriangleTool.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_DRAW_2DTEXT, &COAGMFCView::OnDraw2dtext)
	ON_COMMAND(ID_INSERT_RASTERIMAGERECTANGLE, &COAGMFCView::OnInsertRasterimagerectangle)
	ON_COMMAND(ID_INSERT_RASTERIMAGETRIANGLE, &COAGMFCView::OnInsertRasterimagetriangle)
END_MESSAGE_MAP()

// COAGMFCView construction/destruction

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

COAGMFCView::~COAGMFCView()
{
	UnloadData();
}

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

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

void COAGMFCView::UnloadData()
{
	if( m_pRender )
	{
		delete m_pRender;
		m_pRender = NULL;
	}

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

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

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 point
	oag::OAGPrimitives* pPoint = new oag::OAGPrimitives();
	pPoint->SetGeometryType(GL_POINTS);
	GetDocument()->m_pScene->AddObject( pPoint );

	pPoint->SetArraySize( 1 );
	pPoint->AddVertex(450, 100, 0 );
	pPoint->SetColor(255, 0, 0);

	//Creating a point
	oag::OAGPrimitives* pPoint2 = new oag::OAGPrimitives();
	pPoint2->SetGeometryType(GL_POINTS);
	GetDocument()->m_pScene->AddObject( pPoint2 );

	pPoint2->SetArraySize( 1 );
	pPoint2->AddVertex(450, 200, 0 );
	pPoint2->SetColor(255, 0, 0);

	
	//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::OnDraw2dtext()
{
	if ( m_pTool )
		delete m_pTool;

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

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

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

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

	m_pTool = new CRasterImageTriangleTool();
	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