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

📄 litpyramid.cpp

📁 d3d编程中的场景中的光照效果的实现的一个简单DEMO
💻 CPP
字号:
#include "d3dUtility.h"

IDirect3DDevice9* Device = 0; 
const int Width  = 640;
const int Height = 480;
IDirect3DVertexBuffer9* Pyramid = 0;

struct Vertex//顶点结构
{
	Vertex(){}
	Vertex(float x, float y, float z, float nx, float ny, float nz)
	{
		_x  = x;  _y  = y;	_z  = z;
		_nx = nx; _ny = ny; _nz = nz;
	}
	float  _x,  _y,  _z;//位置
	float _nx, _ny, _nz;//法线
	static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL;//fvf

bool Setup()
{
	Device->SetRenderState(D3DRS_LIGHTING, true);	//开启灯光,默认开启
	Device->CreateVertexBuffer(
		12 * sizeof(Vertex), 
		D3DUSAGE_WRITEONLY,
		Vertex::FVF,
		D3DPOOL_MANAGED,
		&Pyramid,
		0);
	Vertex* v;
	Pyramid->Lock(0, 0, (void**)&v, 0);
	// front face
	v[0] = Vertex(-1.0f, 0.0f, -1.0f, 0.0f, 0.707f, -0.707f);
	v[1] = Vertex( 0.0f, 1.0f,  0.0f, 0.0f, 0.707f, -0.707f);
	v[2] = Vertex( 1.0f, 0.0f, -1.0f, 0.0f, 0.707f, -0.707f);
	// left face
	v[3] = Vertex(-1.0f, 0.0f,  1.0f, -0.707f, 0.707f, 0.0f);
	v[4] = Vertex( 0.0f, 1.0f,  0.0f, -0.707f, 0.707f, 0.0f);
	v[5] = Vertex(-1.0f, 0.0f, -1.0f, -0.707f, 0.707f, 0.0f);
	// right face
	v[6] = Vertex( 1.0f, 0.0f, -1.0f, 0.707f, 0.707f, 0.0f);
	v[7] = Vertex( 0.0f, 1.0f,  0.0f, 0.707f, 0.707f, 0.0f);
	v[8] = Vertex( 1.0f, 0.0f,  1.0f, 0.707f, 0.707f, 0.0f);
	// back face
	v[9]  = Vertex( 1.0f, 0.0f,  1.0f, 0.0f, 0.707f, 0.707f);
	v[10] = Vertex( 0.0f, 1.0f,  0.0f, 0.0f, 0.707f, 0.707f);
	v[11] = Vertex(-1.0f, 0.0f,  1.0f, 0.0f, 0.707f, 0.707f);
	Pyramid->Unlock();
/*
	D3DMATERIAL9 mtrl;		//定义一个材质
	mtrl.Ambient  = d3d::BLUE;//对环境光反射蓝色
	mtrl.Diffuse  = d3d::WHITE;//对漫射光反射白色
	mtrl.Specular = d3d::WHITE;//对镜面光反射白色
	mtrl.Emissive = d3d::BLACK;//对自发光设置为黑色
	mtrl.Power    = 5.0f;//高光点强度
	Device->SetMaterial(&mtrl);	//设置材质

	D3DLIGHT9 dir;		//	定义一个光照
	::ZeroMemory(&dir, sizeof(dir));	
	dir.Type      = D3DLIGHT_DIRECTIONAL;	//类型为方向光
	dir.Diffuse   = d3d::WHITE;						//	发出白色漫射光
	dir.Specular  = d3d::WHITE * 0.3f;			//	发出白色镜面光
	dir.Ambient   = d3d::WHITE * 0.6f;			//	发出白色环境光
	dir.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f);	//方向为x正方向,即水平向右照射
	Device->SetLight(0, &dir);	//	设置光照dir为第0号光源
	Device->LightEnable(0, true);	//	开启第0号光源
  */

	D3DMATERIAL9 mtrl;		//定义一个材质
	mtrl.Ambient  = d3d::WHITE;//对环境光反射蓝色
	mtrl.Diffuse  = d3d::WHITE;//对漫射光反射白色
	mtrl.Specular = d3d::WHITE;//对镜面光反射白色
	mtrl.Emissive =d3d::AAA;//对自发光设置为黑色
	mtrl.Power    = 0.2f;//高光点强度
	Device->SetMaterial(&mtrl);	//设置材质

	D3DLIGHT9 dir;		//	定义一个光照
	::ZeroMemory(&dir, sizeof(dir));	
	dir.Type      = D3DLIGHT_DIRECTIONAL;	//类型为方向光
	dir.Diffuse   = d3d::BLACK;						//	发出白色漫射光
	dir.Specular  = d3d::BLACK * 0.5f;			//	发出白色镜面光
	dir.Ambient   = d3d::BLACK * 0.8f;			//	发出白色环境光
	dir.Direction = D3DXVECTOR3(-1.0f,-1.0f, 0.0f);	//方向为x正方向,即水平向右照射
	Device->SetLight(0, &dir);	//	设置光照dir为第0号光源
	Device->LightEnable(0, true);	//	开启第0号光源




	D3DXVECTOR3 pos(0.0f, 1.0f, -3.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &pos, &target, &up);
	Device->SetTransform(D3DTS_VIEW, &V);

	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
			&proj,
			D3DX_PI * 0.5f, // 90 - degree
			(float)Width / (float)Height,
			1.0f,
			1000.0f);
	Device->SetTransform(D3DTS_PROJECTION, &proj);
	return true;
}

void Cleanup()
{
	d3d::Release<IDirect3DVertexBuffer9*>(Pyramid);
}

bool Display(float timeDelta)
{
	if( Device )
	{
		D3DXMATRIX yRot;
		static float y = 0.0f;
		D3DXMatrixRotationY(&yRot, y);
		y += timeDelta;
		if( y >= 6.28f )
			y = 0.0f;
		Device->SetTransform(D3DTS_WORLD, &yRot);
		Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
		Device->BeginScene();
		Device->SetStreamSource(0, Pyramid, 0, sizeof(Vertex));
		Device->SetFVF(Vertex::FVF);
		Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);
		Device->EndScene();
		Device->Present(0, 0, 0, 0);
	}
	return true;
}

LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;
		
	case WM_KEYDOWN:
		if( wParam == VK_ESCAPE )
			::DestroyWindow(hwnd);
		break;
	}
	return ::DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hinstance,
				   HINSTANCE prevInstance, 
				   PSTR cmdLine,
				   int showCmd)
{
	if(!d3d::InitD3D(hinstance,
		Width, Height, true, D3DDEVTYPE_HAL, &Device))
	{
		::MessageBox(0, "InitD3D() - FAILED", 0, 0);
		return 0;
	}
	if(!Setup())
	{
		::MessageBox(0, "Setup() - FAILED", 0, 0);
		return 0;
	}
	d3d::EnterMsgLoop( Display );
	Cleanup();
	Device->Release();
	return 0;
}

⌨️ 快捷键说明

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