⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myd3d.cpp

📁 极限赛车CarGameDemo DirectX9
💻 CPP
字号:
#include "StdAfx.h"
#include "myd3d.h"

const DWORD d3d::Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
//-----------------------------------------------------------------------------
// 名称:InitDirectionalLight()
// 功能:创建方向光源
//-----------------------------------------------------------------------------
D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)
{
	D3DLIGHT9 light;
	::ZeroMemory(&light, sizeof(light));

	light.Type      = D3DLIGHT_DIRECTIONAL;
	light.Ambient   = *color * 0.4f;
	light.Diffuse   = *color;
	light.Specular  = *color * 0.6f;
	light.Direction = *direction;

	return light;
}

//-----------------------------------------------------------------------------
// 名称:InitPointLight()
// 功能:创建点光源
//-----------------------------------------------------------------------------
D3DLIGHT9 d3d::InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
{
	D3DLIGHT9 light;
	::ZeroMemory(&light, sizeof(light));

	light.Type      = D3DLIGHT_POINT;
	light.Ambient   = *color * 0.4f;
	light.Diffuse   = *color;
	light.Specular  = *color * 0.6f;
	light.Position  = *position;
	light.Range        = 1000.0f;
	light.Falloff      = 1.0f;
	light.Attenuation0 = 1.0f;
	light.Attenuation1 = 0.0f;
	light.Attenuation2 = 0.0f;

	return light;
}
//-----------------------------------------------------------------------------
// 名称:InitSpotLight()
// 功能:聚光灯
//-----------------------------------------------------------------------------
D3DLIGHT9 d3d::InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
{
	D3DLIGHT9 light;
	::ZeroMemory(&light, sizeof(light));

	light.Type      = D3DLIGHT_SPOT;
	light.Ambient   = *color * 0.4f;
	light.Diffuse   = *color;
	light.Specular  = *color * 0.6f;
	light.Position  = *position;
	light.Direction = *direction;
	light.Range        = 1000.0f;
	light.Falloff      = 1.0f;
	light.Attenuation0 = 1.0f;
	light.Attenuation1 = 0.0f;
	light.Attenuation2 = 0.0f;
	light.Theta        = 0.5f;
	light.Phi          = 0.7f;

	return light;
}

//-----------------------------------------------------------------------------
// 名称:InitMtrl()
// 功能:创建材质
//-----------------------------------------------------------------------------
D3DMATERIAL9 d3d::InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
{
	D3DMATERIAL9 mtrl;
	mtrl.Ambient  = a;
	mtrl.Diffuse  = d;
	mtrl.Specular = s;
	mtrl.Emissive = e;
	mtrl.Power    = p;
	return mtrl;
}

d3d::BoundingBox::BoundingBox()
{
	_min.x = d3d::INFINITY;
	_min.y = d3d::INFINITY;
	_min.z = d3d::INFINITY;

	_max.x = -d3d::INFINITY;
	_max.y = -d3d::INFINITY;
	_max.z = -d3d::INFINITY;
	_xzlmax = 0.0f;

}
//-----------------------------------------------------------------------------
// 名称:BoundingBox::isPointInside()
// 功能:测试点是否在边界盒内(这里p只关注在xz平面的 )
//-----------------------------------------------------------------------------
bool d3d::BoundingBox::isPointInside(D3DXVECTOR3& p)
{
	d3d::LineXZ line[4];
	for(int i=0; i<4; i++)
		line[i]._noResult = false;
	d3d::getLine(_Nvxz[0], _Nvxz[1], line[0]);
	d3d::getLine(_Nvxz[3], _Nvxz[2], line[1]);
	d3d::getLine(_Nvxz[0], _Nvxz[3], line[2]);
	d3d::getLine(_Nvxz[1], _Nvxz[2], line[3]);
	
	if( (d3d::lAndP(line[0], p)*d3d::lAndP(line[1], p) <= 0)&&
		(d3d::lAndP(line[2], p)*d3d::lAndP(line[3], p) <= 0) )
		return true;
	else
		return false;
	
}


d3d::BoundingSphere::BoundingSphere()
{
	_center.x = 0.0f;
	_center.y = 0.0f;
	_center.z = 0.0f;

	_radius = 0.0f;
}

bool d3d::BoundingSphere::isPointInside(D3DXVECTOR3& p)
{
	if( D3DXVec3Length(&(_Ncenter - p)) > _radius )
		return false;
	else
		return true;
}
d3d::LineXZ::LineXZ(){_noResult = false;}
d3d::LineXZ::LineXZ(float k, float c, bool b){_k = k; _c = c; _noResult = b;}

//-----------------------------------------------------------------------------
// 名称:getLine()
// 功能:求x,z平面上不同的两点确定的线:z=kx+c
//-----------------------------------------------------------------------------
void d3d::getLine(D3DXVECTOR3 p1, D3DXVECTOR3 p2 ,d3d::LineXZ &l)
{
	if( ((p1.x - p2.x) >= -d3d::EPSILON) &&((p1.x - p2.x) <= d3d::EPSILON) )//确定是否垂直与x轴
	{
		l._noResult = true;
		l._k = 1.0f;
		l._c = p1.x;//l._c保存与x轴交点
	}
	else
	{
		l._k = (p1.z - p2.z)/(p1.x - p2.x);
		l._c = p1.z - l._k*p1.x;
	}
}

//-----------------------------------------------------------------------------
// 名称:lAndP()
// 功能:x,z平面上点和线的关系,点在线上侧/左侧还回1,在线下侧/右侧还回-1,线上换回0
//-----------------------------------------------------------------------------
int d3d::lAndP(const d3d::LineXZ &l ,const D3DXVECTOR3 &p)
{
	//若线垂直于x轴,采用不同方法判断
	if( l._noResult )
	{
		if(p.x == l._c)
			return 0;
		else
		{
			if(p.x > l._c)
				return -1;
			else
				return 1;
		}
	}

	float temp = l._k*p.x+l._c-p.z;
	if(temp > -d3d::EPSILON && temp < d3d::EPSILON)
		return 0;
	else
	{
		if(temp > d3d::EPSILON)
			return -1;
		else
			return 1;
	}
	
}
//-----------------------------------------------------------------------------
// 名称:pOnSLine()
// 功能:x,z平面上点和线段的关系,
//-----------------------------------------------------------------------------
bool d3d::pOnSLine( const D3DXVECTOR3 &p1,  const D3DXVECTOR3 &p2, const D3DXVECTOR3 &p)
{
	d3d::LineXZ l;
	d3d::getLine(p1, p2,l);
	if( d3d::lAndP(l, p) != 0 )
		return false;
	else
	{
		if( l._noResult )
		{//垂直于x轴则比较z坐标
			if( (p1.z-p.z)*(p2.z-p.z) > d3d::EPSILON ) 
				return false;
			else 
				return true;
		}
		else
		{//比较x坐标
			if( (p1.x-p.x)*(p2.x-p.x) > d3d::EPSILON ) 
				return false;
			else 
				return true;
		}
	}
}
//-----------------------------------------------------------------------------
// 名称:FtoDw()
// 功能:float到DWORLD的类型转换
//-----------------------------------------------------------------------------
DWORD d3d::FtoDw(float f)
{
	return *((DWORD*)&f);
}

float d3d::getRandomMinMax( float fMin, float fMax )
{
	float fRandNum = (float)rand () / RAND_MAX;
	return fMin + (fMax - fMin) * fRandNum;
}

//-----------------------------------------------------------------------------
// 名称:getRandomVector()
// 功能:随机获取空间一顶点
//-----------------------------------------------------------------------------
D3DXVECTOR3 d3d::getRandomVector( void )
{
	D3DXVECTOR3 vVector;
	vVector.z = getRandomMinMax( -1.0f, 1.0f );
    
	float radius = (float)sqrt(1 - vVector.z * vVector.z);
    
	float t = getRandomMinMax( -D3DX_PI, D3DX_PI );

	vVector.x = (float)cosf(t) * radius;
	vVector.y = (float)sinf(t) * radius;

	return vVector;
}
//-----------------------------------------------------------------------------
// 名称:getPosOnTrack()
// 功能:在赛道上获得随机顶点
//-----------------------------------------------------------------------------
D3DXVECTOR3 d3d::getPosOnTrack()
{
	float radius = getRandomMinMax( 140.0f, 190.f );
	float t = getRandomMinMax( -D3DX_PI, D3DX_PI*0.5f );
	D3DXVECTOR3 vVector;
	vVector.y = 1.0f;
	vVector.x = (float)cosf(t) * radius;
	vVector.z = (float)sinf(t) * radius;

	return vVector;
}
//-----------------------------------------------------------------------------
// 名称:Gtime::Gtime()
// 功能:格式化显示时间
//-----------------------------------------------------------------------------
d3d:: Gtime::Gtime(float time)
{
	_minute = (int) time / 60;
	_second = (int) time % 60;
}

d3d:: Gtime::~Gtime()
{

}






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -