Click here to Skip to main content
15,892,697 members
Articles / Desktop Programming / MFC

OAG Library (OpenGL) Part 1 - Setting Up the Library for an MFC Application

Rate me:
Please Sign up or sign in to vote.
4.40/5 (11 votes)
7 Aug 2011CPOL3 min read 56.2K   56  
OAG is a library written in C++. With this library, you can create OpenGL based applications.
////////////////////////// [ MATH2D ] ////////////////////////////////


#pragma once
#include "OAGVector3d.h"
#include "OAGVector3f.h"
#include "MathDefinitions.h"
#include <math.h>

////////////////////////// [START INLINE FUNCTIONS ] ////////////////////////////////

//Used for checking collisions in filled polygons
inline double Angle2D(double x1, double y1, double x2, double y2)
{
   double dtheta,theta1,theta2;

   theta1 = atan2(y1,x1);
   theta2 = atan2(y2,x2);
   dtheta = theta2 - theta1;
   while (dtheta > _PI)
	   dtheta -= _2PI;
   while (dtheta < - _PI)
      dtheta += _2PI;

   return(dtheta);
}

//Used for checking collisions in lineIntersection
inline bool LineIntersection(oag::OAGVector3d* a, oag::OAGVector3d* b, oag::OAGVector3d* c, oag::OAGVector3d* d)
{
	double r, s;

	r =	( ((a->m_Y - c->m_Y)*(d->m_X - c->m_X)) - ((a->m_X - c->m_X)*(d->m_Y - c->m_Y)) )/
		( ((b->m_X - a->m_X)*(d->m_Y - c->m_Y)) - ((b->m_Y - a->m_Y)*(d->m_X - c->m_X)) );	


	s = ( ( (a->m_Y - c->m_Y)*(b->m_X - a->m_X) ) - ( (a->m_X - c->m_X)*(b->m_Y - a->m_Y) ) ) /
		( ( (b->m_X - a->m_X)*(d->m_Y - c->m_Y) ) - ( (b->m_Y - a->m_Y)*(d->m_X - c->m_X) ) );


	if ( (r>=0 && r<=1) && (s>=0 && s<=1) )
		return true;

	return false;
}

inline bool LineIntersection(oag::OAGVector3f* a, oag::OAGVector3f* b, oag::OAGVector3f* c, oag::OAGVector3f* d)
{
	float r, s;

	r =	( ((a->m_Y - c->m_Y)*(d->m_X - c->m_X)) - ((a->m_X - c->m_X)*(d->m_Y - c->m_Y)) )/
		( ((b->m_X - a->m_X)*(d->m_Y - c->m_Y)) - ((b->m_Y - a->m_Y)*(d->m_X - c->m_X)) );	


	s = ( ( (a->m_Y - c->m_Y)*(b->m_X - a->m_X) ) - ( (a->m_X - c->m_X)*(b->m_Y - a->m_Y) ) ) /
		( ( (b->m_X - a->m_X)*(d->m_Y - c->m_Y) ) - ( (b->m_Y - a->m_Y)*(d->m_X - c->m_X) ) );


	if ( (r>=0 && r<=1) && (s>=0 && s<=1) )
		return true;

	return false;
}

//Used for testing whether if a point is inside of a triangle
inline bool PointInsideTriangle(oag::OAGVector3d* p1, oag::OAGVector3d* p2, oag::OAGVector3d* p3, oag::OAGVector3d clickPoint)
{
	double fAB = (clickPoint.m_Y-p1->m_Y)*(p2->m_X-p1->m_X) - (clickPoint.m_X-p1->m_X)*(p2->m_Y-p1->m_Y);
	double fCA = (clickPoint.m_Y-p3->m_Y)*(p1->m_X-p3->m_X) - (clickPoint.m_X-p3->m_X)*(p1->m_Y-p3->m_Y);
	double fBC = (clickPoint.m_Y-p2->m_Y)*(p3->m_X-p2->m_X) - (clickPoint.m_X-p2->m_X)*(p3->m_Y-p2->m_Y);

	return (fAB*fBC>0 && fBC*fCA>0) ? true : false;		
}

//inline bool PointInsideTriangle(oag::OAGVector3d* p1, oag::OAGVector3d* p2, oag::OAGVector3d* p3, oag::OAGVector3d clickPoint)
//{
//	float fAB = (clickPoint.m_Y-p1->m_Y)*(p2->m_X-p1->m_X) - (clickPoint.m_X-p1->m_X)*(p2->m_Y-p1->m_Y);
//	float fCA = (clickPoint.m_Y-p3->m_Y)*(p1->m_X-p3->m_X) - (clickPoint.m_X-p3->m_X)*(p1->m_Y-p3->m_Y);
//	float fBC = (clickPoint.m_Y-p2->m_Y)*(p3->m_X-p2->m_X) - (clickPoint.m_X-p2->m_X)*(p3->m_Y-p2->m_Y);
//
//	return (fAB*fBC>0 && fBC*fCA>0) ? true : false;		
//}

//Used for testing whether a point is inside of a rectangle
inline bool PointInsideRectangle(oag::OAGVector3d* lowerPoint, oag::OAGVector3d* higherPoint, oag::OAGVector3d clickPoint)
{
	return clickPoint.m_X>=lowerPoint->m_X && clickPoint.m_X<=higherPoint->m_X && clickPoint.m_Y >=lowerPoint->m_Y && clickPoint.m_Y <= higherPoint->m_Y;
}

//inline bool PointInsideRectangle(oag::OAGVector3d* lowerPoint, oag::OAGVector3d* higherPoint, oag::OAGVector3d clickPoint)
//{
//	return clickPoint.m_X>=lowerPoint->m_X && clickPoint.m_X<=higherPoint->m_X && clickPoint.m_Y >=lowerPoint->m_Y && clickPoint.m_Y <= higherPoint->m_Y;
//}
//Used for testing whether a number is power of two
inline bool IsPowerOf2(int number)
{
	int count=0;

	for(int i=15;i>=0;i--)
	{
		if((1<<i) & number)
			count++;
	}

	if(count == 1)
		return true;

	return false;
}


////////////////////////// [END INLINE FUNCTIONS ] ////////////////////////////////


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