d3dUtility.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:3k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "dxstdafx.h"
- #include "d3dUtility.h"
- //using namespace myd3d;
- bool Initd3dDevice( IDirect3D9* pD3D, IDirect3DDevice9** ppd3dDevice, HWND hWnd)
- {
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory( &d3dpp, sizeof(d3dpp));
- d3dpp.Windowed = TRUE;
- // d3dpp.BackBufferWidth = 0;
- // d3dpp.BackBufferHeight = 0;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
- d3dpp.EnableAutoDepthStencil = TRUE;
- d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
- if( FAILED( pD3D -> CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
- D3DCREATE_SOFTWARE_VERTEXPROCESSING,
- &d3dpp, ppd3dDevice)))
- {
- ::MessageBoxA( 0, "Create d3d Device Failed!", "Init d3d Device", 0);
- return false;
- }
- return true;
- }
- D3DLIGHT9 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;
- }
- D3DLIGHT9 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;
- }
- D3DLIGHT9 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;
- }
- D3DMATERIAL9 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;
- }
- BoundingBox::BoundingBox()
- {
- _minPoint = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
- _maxPoint = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
- }
- BoundingBox::BoundingBox(D3DXVECTOR3 minPoint, D3DXVECTOR3 maxPoint)
- {
- _minPoint = minPoint;
- _maxPoint = maxPoint;
- }
- bool BoundingBox::isPointInside(D3DXVECTOR3 &p)
- {
- if( p.x >= _minPoint.x && p.y >= _minPoint.y && p.z >= _minPoint.z &&
- p.x <= _maxPoint.x && p.y <= _maxPoint.y && p.z <= _maxPoint.z)
- return true;
- else
- return false;
- }
- BoundingSphere::BoundingSphere(D3DXVECTOR3 center, float radius)
- {
- _center = center;
- _radius = radius;
- }
- float GetRandomFloat(float lowBound, float highBound)
- {
- if(lowBound > highBound)
- return lowBound;
- //generate a randon float number between 0 and 1
- float fNum = ( rand() % 10000 ) * 0.0001f;
- //get a number between lowBound and highBound
- return ( highBound - lowBound ) * fNum + lowBound ;
- }
- void GetRandomVector(D3DXVECTOR3* out, D3DXVECTOR3* min, D3DXVECTOR3* max)
- {
- out->x = GetRandomFloat( min->x, max->x);
- out->y = GetRandomFloat( min->y, max->y);
- out->z = GetRandomFloat( min->z, max->z);
- }
- DWORD FtoDw(float f)
- {
- return *((DWORD*)&f);
- }