Click here to Skip to main content
15,896,486 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.
#ifndef OAG_OAGCUBE_H
#define OAG_OAGCUBE_H

#include "OAGObject.h"
#include "OAGPrimitives.h"

namespace oag
{
	class OAGCube : public oag::OAGObject
	{
	public:
		OAGCube(void);
		OAGCube(float nWidth, float nHeight, float nDepth);
		virtual ~OAGCube(void);

	//Attributes
	protected:
		float				m_nWidth, m_nHeight, m_nDepth;
		oag::OAGPrimitives	leftFace, rightFace, frontFace, backFace, topFace, bottomFace;

	//Operations
	protected:

		void CreateNodeColor( oag::OAGColor &color, CXmlNode* pNodeColor)
		{
			//Color node
			char buffer[_CVTBUFSIZE];			

			_gcvt_s(buffer, _CVTBUFSIZE, color.GetRed()/255.f, 2 );	pNodeColor->AddAttribute("Red", buffer);
			_gcvt_s(buffer, _CVTBUFSIZE, color.GetGreen()/255.f, 2 ); pNodeColor->AddAttribute("Green", buffer);
			_gcvt_s(buffer, _CVTBUFSIZE, color.GetBlue()/255.f, 2 );  pNodeColor->AddAttribute("Blue", buffer);
			_gcvt_s(buffer, _CVTBUFSIZE, color.GetAlpha()/255.f, 2 ); pNodeColor->AddAttribute("Alpha", buffer);
		}

	public:
		void CreateCube();
		float GetDepth()							{ return m_nDepth; };
		float GetHeight()							{ return m_nHeight; };
		float GetWidth()							{ return m_nWidth; };
		void SetDepth(float nDepth)					{ m_nDepth = nDepth; };
		void SetHeight(float nHeight)				{ m_nHeight = nHeight; };
		void SetWidth(float nWidth)					{ m_nWidth = nWidth; };
		
		void SetLeftFaceColor(BYTE red, BYTE green, BYTE blue, BYTE alpha = 255)
		{
			leftFace.SetColor(red, green, blue, alpha);
		}

		void SetRightFaceColor(BYTE red, BYTE green, BYTE blue, BYTE alpha = 255)
		{
			rightFace.SetColor(red, green, blue, alpha);
		}

		void SetFrontFaceColor(BYTE red, BYTE green, BYTE blue, BYTE alpha = 255)
		{
			frontFace.SetColor(red, green, blue, alpha);
		}

		void SetBackFaceColor(BYTE red, BYTE green, BYTE blue, BYTE alpha = 255)
		{
			backFace.SetColor(red, green, blue, alpha);
		}

		void SetTopFaceColor(BYTE red, BYTE green, BYTE blue, BYTE alpha = 255)
		{
			topFace.SetColor(red, green, blue, alpha);
		}

		void SetBottomFaceColor(BYTE red, BYTE green, BYTE blue, BYTE alpha = 255)
		{
			bottomFace.SetColor(red, green, blue, alpha);
		}

	//Virtual
	public:
		 virtual void OnDraw();
		 virtual void ReadNodeXML(CXmlNode* pNode);
		 virtual void SaveNodeXML(CXmlNode* pNode);

		//Sets the color for the object
		void SetColor(BYTE red, BYTE green, BYTE blue, BYTE alpha = 255)
		{ 
			SetFrontFaceColor(red, green, blue, alpha);
			SetBackFaceColor(red, green, blue, alpha);
			SetRightFaceColor(red, green, blue, alpha);
			SetLeftFaceColor(red, green, blue, alpha);
			SetTopFaceColor(red, green, blue, alpha);
			SetBottomFaceColor(red, green, blue, alpha);
		}
		
		//Selects a polygon rasterization mode.
		void SetPolygonMode(GLenum mode, GLenum face = GL_FRONT_AND_BACK)
		{ 
			frontFace.SetPolygonMode(mode, face);
			backFace.SetPolygonMode(mode, face);
			rightFace.SetPolygonMode(mode, face);
			leftFace.SetPolygonMode(mode, face);
			topFace.SetPolygonMode(mode, face);
			bottomFace.SetPolygonMode(mode, face);
		}

		//Sets the position for the object or translate the object.
		void SetPosition(float x, float y, float z)
		{ 
			oag::OAGObject::SetPosition(x, y, z);

			frontFace.SetPosition(x, y, z);
			rightFace.SetPosition(x, y, z);
			backFace.SetPosition(x, y, z);
			leftFace.SetPosition(x, y, z);
			bottomFace.SetPosition(x, y, z);
			topFace.SetPosition(x, y, z);
		}
	};
};

#endif //OAG_OAGCUBE_H

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