www.pudn.com > RayTrace1.1.rar > Vector3.h


#if !defined(AFX_VECTOR3_H__353A4FBF_52F9_4DC3_AFEE_4D2A748A2065__INCLUDED_) 
#define AFX_VECTOR3_H__353A4FBF_52F9_4DC3_AFEE_4D2A748A2065__INCLUDED_ 
 
#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
// Vector3.h : header file 
// 
 
#include "math.h" 
#include "stdlib.h" 
 
typedef unsigned int Pixel; 
 
inline float Rand( float a_Range ) { return ((float)rand() / RAND_MAX) * a_Range; } 
#define DOT(A,B)		(A.x*B.x+A.y*B.y+A.z*B.z) 
#define NORMALIZE(A)	{float l=1/sqrtf(A.x*A.x+A.y*A.y+A.z*A.z);A.x*=l;A.y*=l;A.z*=l;} 
#define LENGTH(A)		(sqrtf(A.x*A.x+A.y*A.y+A.z*A.z)) 
#define SQRLENGTH(A)	(A.x*A.x+A.y*A.y+A.z*A.z) 
#define SQRDISTANCE(A,B) ((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z)) 
 
#define EPSILON			0.001f 
#define TRACEDEPTH		6 
 
#define PI				3.141592653589793238462f 
#define HIT		 1		// Ray hit primitive 
#define MISS	 0		// Ray missed primitive 
#define INPRIM	-1		// Ray started inside primitive 
#define SCRWIDTH	800 
#define SCRHEIGHT	600 
///////////////////////////////////////////////////////////////////////////// 
// CVector3 dialog 
 
class CVector3  
{ 
// Construction 
public: 
 
	CVector3() : x( 0.0f ), y( 0.0f ), z( 0.0f ) {}; 
	CVector3(CVector3& vector3){x=vector3.x;y=vector3.y;z=vector3.z; } 
	CVector3( float a_X, float a_Y, float a_Z ) : x( a_X ), y( a_Y ), z( a_Z ) {}; 
	void Set( float a_X, float a_Y, float a_Z ) { x = a_X; y = a_Y; z = a_Z; } 
	void Normalize() { float l = 1.0f / Length(); x *= l; y *= l; z *= l; } 
	float Length() { return (float)sqrt( x * x + y * y + z * z ); } 
	float SqrLength() { return x * x + y * y + z * z; } 
	float Dot( CVector3 a_V ) { return x * a_V.x + y * a_V.y + z * a_V.z; } 
	CVector3 Cross( CVector3 b ) { return CVector3( y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x ); } 
	void operator += ( CVector3& a_V ) { x += a_V.x; y += a_V.y; z += a_V.z; } 
	void operator = ( CVector3& a_V ) { x = a_V.x; y = a_V.y; z = a_V.z; } 
	void operator += ( CVector3* a_V ) { x += a_V->x; y += a_V->y; z += a_V->z; } 
	void operator -= ( CVector3& a_V ) { x -= a_V.x; y -= a_V.y; z -= a_V.z; } 
	void operator -= ( CVector3* a_V ) { x -= a_V->x; y -= a_V->y; z -= a_V->z; } 
	void operator *= ( float f ) { x *= f; y *= f; z *= f; } 
	void operator *= ( CVector3& a_V ) { x *= a_V.x; y *= a_V.y; z *= a_V.z; } 
	void operator *= ( CVector3* a_V ) { x *= a_V->x; y *= a_V->y; z *= a_V->z; } 
	CVector3 operator- () const { return CVector3( -x, -y, -z ); } 
	friend CVector3 operator + ( const CVector3& v1, const CVector3& v2 ) { return CVector3( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z ); } 
	friend CVector3 operator - ( const CVector3& v1, const CVector3& v2 ) { return CVector3( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z ); } 
	friend CVector3 operator + ( const CVector3& v1, CVector3* v2 ) { return CVector3( v1.x + v2->x, v1.y + v2->y, v1.z + v2->z ); } 
	friend CVector3 operator - ( const CVector3& v1, CVector3* v2 ) { return CVector3( v1.x - v2->x, v1.y - v2->y, v1.z - v2->z ); } 
	friend CVector3 operator * ( const CVector3& v, float f ) { return CVector3( v.x * f, v.y * f, v.z * f ); } 
	friend CVector3 operator * ( const CVector3& v1, CVector3& v2 ) { return CVector3( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z ); } 
	friend CVector3 operator * ( float f, const CVector3& v ) { return CVector3( v.x * f, v.y * f, v.z * f ); } 
	union 
	{ 
		struct { float x, y, z; }; 
		struct { float r, g, b; }; 
		struct { float cell[3]; }; 
	}; 
 
 
}; 
typedef CVector3 Color; 
 
class CPlane  
{ 
 
 
public: 
	CPlane() : N( 0, 0, 0 ), D( 0 ) {}; 
	CPlane( CVector3 a_Normal, float a_D ) 
	{ 
		N=a_Normal; 
		D=a_D; 
	} 
	 
	union 
	{ 
		struct 
		{ 
			CVector3 N; 
			float D; 
		}; 
		float cell[4]; 
	}; 
 
}; 
class CRay  
{ 
 
public: 
	CRay() : m_Origin( CVector3( 0, 0, 0 ) ), m_Direction( CVector3( 0, 0, 0 ) ) {}; 
	CRay( CVector3& a_Origin, CVector3& a_Dir ):	m_Origin( a_Origin ),  
	m_Direction( a_Dir ) 
	{} 
	void SetOrigin( CVector3& a_Origin ) { m_Origin = a_Origin; } 
	void SetDirection( CVector3& a_Direction ) { m_Direction = a_Direction; } 
	CVector3& GetOrigin() { return m_Origin; } 
	CVector3& GetDirection() { return m_Direction; } 
private: 
	CVector3 m_Origin; 
	CVector3 m_Direction; 
 
}; 
class CMaterial 
{ 
 
public: 
	CMaterial() 
	{} 
	void SetColor( Color& a_Color ) { m_Color = a_Color; } 
	Color GetColor() { return m_Color; } 
	void SetDiffuse( float a_Diff ) { m_Diff = a_Diff; } 
	void SetSpecular( float a_Spec ) { m_Spec = a_Spec; } 
	void SetReflection( float a_Refl ) { m_Refl = a_Refl; } 
	void SetRefraction( float a_Refr ) { m_Refr = a_Refr; } 
	float GetSpecular() { return m_Spec; } 
	float GetDiffuse() { return m_Diff; } 
	float GetReflection() { return m_Refl; } 
	float GetRefraction() { return m_Refr; } 
	void SetRefrIndex( float a_Refr ) { m_RIndex = a_Refr; } 
	float GetRefrIndex() { return m_RIndex; } 
	void operator = ( CMaterial& a_V )  
	{  m_Color= a_V.m_Color; m_Refl = a_V.m_Refl; m_Refr = a_V.m_Refr;m_Diff=a_V.m_Diff;m_Spec=a_V.m_Spec;m_RIndex=a_V.m_RIndex; } 
private: 
	Color m_Color; 
	float m_Refl, m_Refr; 
	float m_Diff, m_Spec; 
	float m_RIndex; 
 
}; 
class CPrimitive  
{ 
 
	public: 
	enum 
	{ 
		SPHERE = 1, 
		PLANE 
	}; 
	CPrimitive() : m_Name( 0 ), m_Light( false ) {}; 
	CMaterial* GetMaterial() { return &m_Material; } 
	void SetMaterial( CMaterial& a_Mat ) { m_Material = a_Mat; } 
	virtual int GetType() = 0; 
	virtual int Intersect( CRay& a_Ray, float& a_Dist ) = 0; 
	virtual CVector3 GetNormal( CVector3& a_Pos ) = 0; 
	virtual Color GetColor() { return m_Material.GetColor(); } 
	virtual void Light( bool a_Light ) { m_Light = a_Light; } 
	bool IsLight() { return m_Light; } 
	void SetName( char* a_Name ); 
	char* GetName() { return m_Name; } 
protected: 
	CMaterial m_Material; 
	char* m_Name; 
	bool m_Light; 
 
}; 
 
class CSphere : public CPrimitive 
{ 
public: 
	int GetType() { return SPHERE; } 
	CSphere( CVector3& a_Centre, float a_Radius ) :  
		m_Centre( a_Centre ), m_SqRadius( a_Radius * a_Radius ),  
		m_Radius( a_Radius ), m_RRadius( 1.0f / a_Radius ) {}; 
	CVector3& GetCentre() { return m_Centre; } 
	float GetSqRadius() { return m_SqRadius; } 
	float GetRadius(){return m_Radius;} 
	int Intersect( CRay& a_Ray, float& a_Dist ); 
	CVector3 GetNormal( CVector3& a_Pos ) { return (a_Pos - m_Centre) * m_RRadius; } 
private: 
	CVector3 m_Centre; 
	float m_SqRadius, m_Radius, m_RRadius; 
}; 
 
class CPlanePrim : public CPrimitive 
{ 
public: 
	int GetType() { return PLANE; } 
	CPlanePrim( CVector3& a_Normal, float a_D ) : m_Plane( CPlane( a_Normal, a_D ) ) {}; 
	CVector3& GetNormal() { return m_Plane.N; } 
	float GetD() { return m_Plane.D; } 
	int Intersect( CRay& a_Ray, float& a_Dist ); 
	CVector3 GetNormal( CVector3& a_Pos ); 
private: 
	CPlane m_Plane; 
}; 
 
class CScene 
{ 
public: 
	void DeleteLastObject(); 
	void AddScene(int type); 
	CScene() : m_Primitives( 0 ), m_Primitive( 0 ) {}; 
	~CScene(); 
	void SphereTree( int& a_Prim, float a_Radius, CVector3 a_Pos, int a_Depth ); 
	void InitScene(); 
	int GetNrPrimitives() { return m_Primitives; } 
	CPrimitive* GetPrimitive( int a_Idx ) { return m_Primitive[a_Idx]; } 
private: 
	int m_Primitives; 
	CPrimitive** m_Primitive; 
}; 
class CSurface 
{ 
	enum 
	{ 
		OWNER = 1 
	}; 
 
public: 
	// constructor / destructors 
	CSurface( int a_Width, int a_Height ); 
	~CSurface(); 
 
	// member data access 
    RGBQUAD** GetBuffer() { return m_Buffer; } 
	int GetWidth() { return m_Width; } 
	int GetHeight() { return m_Height; } 
 
	// Special operations 
	void Clear(  ); 
 
private: 
	// Attributes 
	RGBQUAD** m_Buffer;	 
	int m_Width, m_Height;	 
	 
	// Static attributes for the buildin font 
		 
	int s_Transl[256];		 
}; 
 
class CRayTraceEngine 
{ 
public: 
	void GetViewPoint(CVector3 ViewPoint); 
	CRayTraceEngine(); 
	~CRayTraceEngine(); 
	void SetTarget( RGBQUAD** a_Dest, int a_Width, int a_Height ); 
	CScene* GetScene() { return m_Scene; } 
	CPrimitive* Raytrace( CRay& a_Ray, Color& a_Acc, int a_Depth, float a_RIndex, float& a_Dist ); 
	void InitRender(); 
	bool Render(); 
protected: 
	// renderer data 
	float m_WX1, m_WY1, m_WX2, m_WY2, m_DX, m_DY, m_SX, m_SY; 
	CScene* m_Scene; 
	RGBQUAD** m_Dest; 
	int m_Width, m_Height, m_CurrLine, m_PPos; 
	CPrimitive** m_LastRow; 
	CVector3 o; 
}; 
 
#endif // !defined(AFX_VECTOR3_H__353A4FBF_52F9_4DC3_AFEE_4D2A748A2065__INCLUDED_)