对上次所用的DirectX 3D基本框架进行一定的扩充,修改如下
d3dUtility.h中
1.增加了一些常用颜色的常量。
2.增加了3个光照函数。分别是初始化点光源,方向光及聚光灯。
3.增加了初始化材质函数及一些常见的材质常量。
d3dUtility.cpp中
实现了.h文件中声明的新增的4个函数。
代码清单:
- //
- //
- // File: d3dUtility.h
- // by tianzhihen
- // 2008.10.9
- // MSVC++ 8.0
- //
- #ifndef __d3dUtilityH
- #define __d3dUtilityH
- #include <d3dx9.h>
- namespace d3d
- {
- bool InitD3D(
- HINSTANCE hInstance, //应用程序实例
- int width, int height, //后备缓冲宽/高
- bool windowed, //显示模式(窗口/全屏)
- D3DDEVTYPE deviceType, //设备类型(HAL/REF)
- IDirect3DDevice9** device //指向设备(显卡)接口地址的指针
- );
- //进入消息循环
- int EnterMsgLoop(
- bool (*ptr_display)(float timeDelta));
- //回调函数
- LRESULT CALLBACK WndProc(
- HWND hwnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam
- );
- template<class T> void Release(T t)
- {
- if (t)
- {
- t->Release();
- t = 0;
- }
- }
- template<class T> void Delete(T t)
- {
- if (t)
- {
- delete t;
- t = 0;
- }
- }
- //
- // 颜色常量
- //
- const D3DXCOLOR WHITE( D3DCOLOR_XRGB(255, 255, 255) );
- const D3DXCOLOR BLACK( D3DCOLOR_XRGB( 0, 0, 0) );
- const D3DXCOLOR RED( D3DCOLOR_XRGB(255, 0, 0) );
- const D3DXCOLOR GREEN( D3DCOLOR_XRGB( 0, 255, 0) );
- const D3DXCOLOR BLUE( D3DCOLOR_XRGB( 0, 0, 255) );
- const D3DXCOLOR YELLOW( D3DCOLOR_XRGB(255, 255, 0) );
- const D3DXCOLOR CYAN( D3DCOLOR_XRGB( 0, 255, 255) );
- const D3DXCOLOR MAGENTA( D3DCOLOR_XRGB(255, 0, 255) );
- //
- // 光照函数
- //
- D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction,D3DXCOLOR* color);
- D3DLIGHT9 InitPointLight(D3DXVECTOR3* position,D3DXCOLOR* color);
- D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position,D3DXVECTOR3* direction,D3DXCOLOR* color);
- //
- // 材质 函数/常量
- //
- D3DMATERIAL9 InitMtrl(D3DXCOLOR a,D3DXCOLOR d,D3DXCOLOR s,D3DXCOLOR e,float p);
- const D3DMATERIAL9 WHITE_MTRL = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);
- const D3DMATERIAL9 RED_MTRL = InitMtrl(RED, RED, RED, BLACK, 2.0f);
- const D3DMATERIAL9 GREEN_MTRL = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);
- const D3DMATERIAL9 BLUE_MTRL = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
- const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);
- }
- #endif
- //
- //
- // File: d3dUtility.cpp
- // by tianzhihen
- // 2008.10.9
- // MSVC++ 8.0
- //
- #include "d3dUtility.h"
- bool d3d::InitD3D(HINSTANCE hInstance,
- int width, int height,
- bool windowed,
- D3DDEVTYPE deviceType,
- IDirect3DDevice9** device )
- {
- //
- //创造应用程序窗口
- //
- //初始化windows窗口类
- WNDCLASS wc;
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC)d3d::WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(0, IDI_APPLICATION);
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = 0;
- wc.lpszClassName = "Direct3D9App";
- //注册窗口类
- if (!RegisterClass(&wc))
- {
- ::MessageBox(0,"RegisterClass() - FAILED",0,0);
- return false;
- }
- //创建窗口并返回其句柄
- HWND hwnd = 0;
- hwnd = ::CreateWindow("Direct3D9App", "Direct3D9App",
- WS_EX_TOPMOST,
- 0, 0, width, height,
- 0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/);
- if( !hwnd )
- {
- ::MessageBox(0, "CreateWindow() - FAILED", 0, 0);
- return false;
- }
- //显示/更新 窗口
- ::ShowWindow(hwnd,SW_SHOW);
- ::UpdateWindow(hwnd);
- //
- // Init D3D
- //
- HRESULT hr = 0;
- //获取IDirect3D9 的指针(第1步)
- IDirect3D9* d3d9 = 0;
- d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
- if (!d3d9)
- {
- ::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
- return false;
- }
- //校验顶点运算(第2步)
- //获取设备性能
- D3DCAPS9 caps;
- d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, //指定默认显示卡
- deviceType, //指定设备类型
- &caps /*返回已初始化的设备性能结构实例*/);
- int vp = 0;
- if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
- vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; //支持硬件顶点运算
- else
- vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; //不支持硬件顶点运算
- //填充D3DPRESENT_PARAMETERS 结构(第3步)
- D3DPRESENT_PARAMETERS d3dpp;
- d3dpp.BackBufferWidth = width;
- d3dpp.BackBufferHeight = height;
- d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
- d3dpp.BackBufferCount = 1;
- d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
- d3dpp.MultiSampleQuality = 0;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //指定交换链中的缓存的页面置换方式
- d3dpp.hDeviceWindow = hwnd;
- d3dpp.Windowed = windowed;
- d3dpp.EnableAutoDepthStencil = true;
- d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
- d3dpp.Flags = 0;
- d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; //默认刷新频率
- d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //立即提交(后备缓冲)
- //创建设备(第4步)
- hr = d3d9->CreateDevice(
- D3DADAPTER_DEFAULT, //默认显示卡
- deviceType, //设备类型(HAL/REF)
- hwnd, //窗口句柄
- vp, //指定顶点运算方式
- &d3dpp, //present parameters
- device //返回创建的设备接口的地址
- );
- if(FAILED(hr))
- {
- //如果创建失败,重设深度缓存的像素格式,再创建一次。
- d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- hr = d3d9->CreateDevice(
- D3DADAPTER_DEFAULT, //默认显示卡
- deviceType, //设备类型(HAL/REF)
- hwnd, //窗口句柄
- vp, //指定顶点运算方式
- &d3dpp, //present parameters
- device //返回创建的设备接口的地址
- );
- if (FAILED(hr))
- {
- d3d9->Release();
- ::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
- return false;
- }
- }
- d3d9->Release();
- return true;
- }
- int d3d::EnterMsgLoop(bool (*ptr_display)(float timeDelta))
- {
- //
- //消息循环
- //
- MSG msg;
- ::ZeroMemory(&msg,sizeof(MSG));
- static float lastTime = (float)timeGetTime();
- while (msg.message!=WM_QUIT)
- {
- if (::PeekMessage(&msg,0,0,0,PM_REMOVE))
- {
- ::TranslateMessage(&msg);
- ::DispatchMessage(&msg);
- }
- else
- {
- float currTime = (float)timeGetTime();
- float timeDelta = (currTime - lastTime)*0.001f;
- ptr_display(timeDelta);
- lastTime = currTime;
- }
- }
- return (int)msg.wParam;
- }
- D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction,D3DXCOLOR* color)
- {
- //
- // 初始化方向光
- //
- D3DLIGHT9 light;
- ::ZeroMemory(&light, sizeof(light));
- light.Type = D3DLIGHT_DIRECTIONAL;
- light.Ambient = *color * 0.6f;
- light.Diffuse = *color;
- light.Specular = *color * 0.3f;
- light.Direction = *direction;
- return light;
- }
- D3DLIGHT9 d3d::InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
- {
- //
- // 初始化点光源
- //
- D3DLIGHT9 light;
- ::ZeroMemory(&light, sizeof(light));
- light.Type = D3DLIGHT_POINT;
- light.Ambient = *color * 0.6f;
- light.Diffuse = *color;
- light.Specular = *color * 0.6f;
- light.Position = *position;
- light.Range = 1000.0f; //最大光程
- light.Attenuation0 = 1.0f; //衰减系数
- light.Attenuation1 = 0.0f;
- light.Attenuation2 = 0.0f;
- return light;
- }
- D3DLIGHT9 d3d::InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
- {
- //
- // 初始化聚光灯
- //
- D3DLIGHT9 light;
- ::ZeroMemory(&light, sizeof(light));
- light.Type = D3DLIGHT_SPOT;
- light.Ambient = *color * 0.0f;
- 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.4f; //内锥形的圆锥角
- light.Phi = 0.9f; //外锥形的圆锥角
- return light;
- }
- 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;
- }