U E D R , A S I H C RSS

상협/3DLibrary



1. 3D Library

1.1. Matrix

1.1.1. Matrix.h

~cpp 
//Matrix.h
#ifndef _MATRIX_H_
#define _MATRIX_H_
const float pi = float(3.141592653589793);
#include <vector>

float AngleToPi(float angle)
{
	return pi/(180/angle);
}

class Vector
{
private:
	float x,y,z;
	float mag;
	void set_mag();
public:
	Vector();
	Vector(float n1,float n2, float n3);
	void set(float n1, float n2,float n3);
	~Vector();
	float Getx() {return x;}
	float Gety() {return y;}
	float Getz() {return z;}
	float magval() const {return mag;}

//	void Translate(const Matrix m);

	//연산자 재정의 
	Vector operator+(const Vector &b) const;
	Vector operator-(const Vector &b) const;
	Vector operator*(float n) const;
	Vector operator-() const;

	float operator*(const Vector &b) const;
	Vector operator^(const Vector &b) const;
	
	void Clear();
	void Normalize();

		//프렌드
	friend Vector operator *(float n,const Vector &a);
 
//	friend ostream& operator << (ostream& os, const Vector& v); 
//	friend ostream& operator << (ostream &os,const Vector &v);



};



class Matrix {
private:
//	int matrix[4][4];
	bool m_vector;
public:
	float matrix[4][4];
	Matrix();
	Matrix(float in[4][4]);
	Matrix(float in[4]);
//	Matrix(int in1[4][4], int in2[4][4]);
	void SetMatrix(float in[4][4]);
	void SetVector(float in[4]);
//	void GetMatrix(int *out[4]);
	void PrintMatrix();
	void Rotation(float theta, char xyz);
	//연산자 재정의
	
	Matrix operator+(const Matrix &b) const ;
	Matrix operator-(const Matrix &b) const;
	Matrix operator-() const;
	Matrix operator*(float n) const;
	Matrix operator*(const Matrix &b) const;

	Vector operator*(const Vector &v) const;
	
//	void Translate(const Vector &v);
	void Rotation(const Vector &v);
	int Inverse();
	void ToIdenty();

	friend Matrix operator*(float n,const Matrix &a);
//	friend ostream& operator<<(ostream &os, const Matrix &m);

};

#endif

1.1.2. Matrix.cpp

~cpp 
//Matrix.cpp
#include "Matrix.h"
#include <iostream>
using namespace std;
#include <cmath>

Matrix::Matrix()
{
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			matrix[j][i]=0;
	}
}

Matrix::Matrix(float in[4][4])
{
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			matrix[j][i] = in[j][i];
	}
}

Matrix::Matrix(float in[4])
{
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			matrix[i][j] = 0;
			matrix[0][j] = in[j];
		}
	}
}
void Matrix::SetMatrix(float in[4][4])
{
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			matrix[j][i] = in[j][i];
	}
}
void Matrix::PrintMatrix()
{

	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			cout<<matrix[i][j]<<"  ";
		cout<<"\n";
	}
}

void Matrix::Rotation(float theta, char xyz)
{
	 float cosTheta = cos(AngleToPi(theta));
	 float sinTheta = sin(AngleToPi(theta));
     Matrix rotationX,rotationY,rotationZ;
	 rotationX.matrix[0][0] = 1;
	 rotationY.matrix[0][0] = rotationZ.matrix[0][0]=cosTheta;
	 rotationY.matrix[1][1] = 1;
	 rotationX.matrix[1][1] = rotationZ.matrix[1][1]=cosTheta;
	 rotationX.matrix[1][2] = rotationY.matrix[2][0]
		 =rotationZ.matrix[0][1]=sinTheta;
	 rotationX.matrix[2][1] = rotationY.matrix[0][2]
		 =rotationZ.matrix[1][0]=-sinTheta;
	 rotationZ.matrix[2][2] = 1;
	 rotationX.matrix[2][2] = rotationY.matrix[2][2]=cosTheta;
	 rotationX.matrix[3][3]=rotationY.matrix[3][3]
		 =rotationZ.matrix[3][3]=1;
	 switch(xyz)
	 {
	 case 'X':
	 case 'x':
		 *this = rotationX * *this;
		 break;
	 case 'Y':
	 case 'y':
		 *this = rotationY * *this;
		 break;
	 case 'Z':
	 case 'z':
		 *this = rotationZ * *this;
		 break;
	 }
}



Matrix Matrix::operator +(const Matrix &b) const
{
	float sumOfMatrix[4][4];
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			sumOfMatrix[j][i] =b.matrix[j][i]+matrix[j][i];

	}
	return Matrix(sumOfMatrix);
}

Matrix Matrix::operator *(float n) const
{
	float MultipleOfMatrix[4][4];
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			MultipleOfMatrix[j][i] =matrix[j][i]*n;
	}	
	return Matrix(MultipleOfMatrix);

}

Matrix Matrix::operator -(const Matrix &b) const
{
	float SubtractOfMatrix[4][4];
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			SubtractOfMatrix[j][i] =b.matrix[j][i]-matrix[j][i];
	}
	return Matrix(SubtractOfMatrix);

}

Matrix Matrix::operator *(const Matrix &b) const
{

	float MultipleOfMatrix[4][4];
	for (int t=0;t<4;t++)
	{
		for(int a=0;a<4;a++)
			MultipleOfMatrix[t][a] = 0;
	}
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			for(int k=0;k<4;k++)
			{
				MultipleOfMatrix[i][j]+=matrix[i][k]*b.matrix[k][j];
			}
		}
	}
	return Matrix(MultipleOfMatrix);

}

Matrix Matrix::operator -() const
{
	float SubtractOfMatrix[4][4];
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			SubtractOfMatrix[j][i] = -matrix[j][i];
	}
	return Matrix(SubtractOfMatrix);
}

Vector Matrix::operator *(const Vector &v) const
{
	Vector b = v;
	float reVector[4];
	float bVector[4];
	bVector[0]=b.Getx();
	bVector[1]=b.Gety();
	bVector[2]=b.Getz();
	bVector[3]=1;
	reVector[0]=reVector[1]=reVector[2]=reVector[3]=0;
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			reVector[i]+=matrix[j][i]*bVector[j];
	}

	return Vector(reVector[0],reVector[1],reVector[2]);
}
/*
void Matrix::Translate(const Vector &v)
{
*/


void Matrix::Rotation(const Vector &v)
{



}
Matrix operator*(float n,const Matrix &a)
{
	return a*n;
}


///////Vector define///////////
//vector1.cpp
#include <iostream>
#include <cmath>
using namespace std;
#include "Matrix.h"
const float Rad_to_deg=float(57.2957795130823);
//개별 메서드
void Vector::set_mag()
{
	mag=sqrt(x*x+y*y+z*z);
}
//공용 메서드
Vector::Vector()
{
	x=y=z=mag=0;

}
Vector::Vector(float n1, float n2, float n3)
{
	set(n1,n2,n3);
}
void Vector::set(float n1,float n2,float n3)
{
	x=n1;
	y=n2;
	z=n3;
	set_mag();
}
Vector::~Vector()
{
}
/*
void Vector::Translate(const Matrix m)
{
}
*/

Vector Vector::operator +(const Vector &b) const
{
	return Vector(x+b.x,y+b.y,z+b.z);
}
Vector Vector::operator -(const Vector &b) const
{
	return Vector(x-b.x,y-b.y, z-b.z);
}
Vector Vector::operator -() const
{
	return Vector(-x,-y,-z);
}
Vector Vector::operator *(float n) const
{
	return Vector(n*x,n*y, n*z);
}

float Vector::operator*(const Vector &b) const
{
	return float(x*b.x+y*b.y+z*b.z);
}
Vector Vector::operator^(const Vector &b) const
{
	return Vector(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);
}

void Vector::Clear()
{
	Vector();
}

void Vector::Normalize()
{
	Vector(x/mag,y/mag,z/mag);
}

//프렌드 메서드
Vector operator *(float n,const Vector &a)
{
	return a*n;
}

/// 사용하는예는 아래에 나왔음.
int main()
{
	float test1[4][4] = 
	{{1,2,3,4}
	,{1,2,3,4}
	,{1,2,3,4}
	,{1,2,3,4}};
	float test2[4] = {1,2,3,4};
	Matrix te2(test1),tesum;
	te2.Rotation(24,'z');
	cout<<"te2\n";
	te2.PrintMatrix();

	Vector ex;
	return 0;
}

1.2. DrawQuad1

  • 그림을 사각형에 씌울때 사용. 점과 법선 백터만 주면 알아서 사각형을 하나 만들어 준다. 이 함수 처음 만들때 실수때문에 삽질 많이 함.. ㅠㅜ
  • void DrawQuad(float inx,float iny,float inz, Matrix normal) : inx,iny,inz는 각 성분의 크기, normal은 법선 벡터
  • 사용예

~cpp 
	glBindTexture(GL_TEXTURE_2D, tex[0]);
	normal.matrix[0][2] = -1;
	DrawQuad(1,1,1,normal);
  • 실제 코드

~cpp 
void DrawQuad(float inx,float iny,float inz, Matrix normal)
{
	float x,y,z;
	char select;
	if(normal.matrix[0][0]!=0)
		select='x';
	else if(normal.matrix[0][1]!=0)
		select='y';
	else if(normal.matrix[0][2]!=0)
		select='z';
	switch(select)
	{
	case 'x':
		{
			x = normal.matrix[0][0];
			y=1;
			if(x>0)
				z=1;
			else
				z=-1;
			glBegin(GL_QUADS);
				glNormal3f(normal.matrix[0][0],normal.matrix[0][1],normal.matrix[0][2]);
				glTexCoord2f(0.0f,0.0f); glVertex3f(x*inx,y*iny,z*inz);
				glTexCoord2f(0.0f,1.0f); glVertex3f(x*inx,-y*iny,z*inz);
				glTexCoord2f(1.0f,1.0f); glVertex3f(x*inx,-y*iny,-z*inz);
				glTexCoord2f(1.0f,0.0f); glVertex3f(x*inx,y*iny,-z*inz);
			glEnd();
		}
		break;
	case 'y':
		{
			y = normal.matrix[0][1];
			x=-1;
			if(y>0)
				z=-1;
			else
				z=1;
			glBegin(GL_QUADS);
				glNormal3f(normal.matrix[0][0],normal.matrix[0][1],normal.matrix[0][2]);
				glTexCoord2f(0.0f,0.0f); glVertex3f(x*inx,y*iny,z*inz);
				glTexCoord2f(0.0f,1.0f); glVertex3f(x*inx,y*iny,-z*inz);
				glTexCoord2f(1.0f,1.0f); glVertex3f(-x*inx,y*iny,-z*inz);
				glTexCoord2f(1.0f,0.0f); glVertex3f(-x*inx,y*iny,z*inz);
			glEnd();

		}
		break;
	case 'z':
		{
			z = normal.matrix[0][2];
			x=1;
			if(z>0)
				y=-1;
			else
				y=1;
			glBegin(GL_QUADS);
				glNormal3f(normal.matrix[0][0],normal.matrix[0][1],normal.matrix[0][2]);
				glTexCoord2f(0.0f,0.0f); glVertex3f(x*inx,y*iny,z*inz);
				glTexCoord2f(0.0f,1.0f); glVertex3f(x*inx,-y*iny,z*inz);
				glTexCoord2f(1.0f,1.0f); glVertex3f(-x*inx,-y*iny,z*inz);
				glTexCoord2f(1.0f,0.0f); glVertex3f(-x*inx,y*iny,z*inz);
			glEnd();

		}	
		break;
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0116 sec