DirectX基礎學習系列4 顏色和光照

2022-03-06 03:13:50 字數 3136 閱讀 2420

4.1顏色表示

rgb顏色:d3dcolor  可以用巨集d3dcolor_argb(a,r,g,b)  d3dcolor_xrgb(255,r,g,b)

另外一種浮點表示:d3dcolorvalue,浮點型別,最小為0 最大為1

4.2頂點顏色

struct colorvetex

float x, y,z;

d3dcolor color;

static const dword fvf;

const dword colorvetex::fvf = d3dfvf_xyz | d3dfvf_diffuse ;

4.3著色

兩種著色方式:shading mode

1flat shading 平面著色:每個圖元的畫素都被賦予該圖元的第乙個頂點的顏色

2gourand shading :各畫素的顏色由著色的三個頂點顏色插值決定、

設定著色模式:device->setrenderstate(d3drs_shademode, d3dshade_flat);

5 光照

5.1光照的組成

1環境光 

2漫射光:特定方向,達到表面後均勻反射

3鏡面光 :特定方向,達到表面後嚴格向另外乙個方向反射,形成在一定範圍內可看的高亮區域,計算量很大

可以控制開關:device->setrenderstate(d3drs_specularenable, true);

5.2材質

材質允許定義對各種顏色光的反射比

typedef struct d3dmaterial9  d3dmaterial9, *lpd3dmaterial9;
設定材質屬性:hresultsetmaterial(const d3dmaterial9 *pmaterial);

5.3頂點法線

struct colorvetex

float x, y,z;

float _nx,_ny,_nz ;

static const dword fvf;

const dword colorvetex::fvf = d3dfvf_xyz | d3dfvf_normal;

注意頂點向量的規範化:device->setrenderstate(d3drs_normalizeenable, true);

5.4 光源

dx支援的三種光源:點光源,方向光,聚光燈

typedef struct d3dlight9  d3dlight9, *lpd3dlight;
光源設定完之後 需要註冊,dx維護了乙個光源列表

device->setlight(0,&light);

註冊完之後 可以進行控制

device->lightenable();

5.5場景新增光源的方法:

1啟用光照

2建立材質,設定材質

3建立光源,開啟光源

4啟用其餘光源

**:

#include "d3dutility.h"
//
// globals
//
idirect3ddevice9* device = 0;
const

int width = 640;

const

int height = 480;

idirect3dvertexbuffer9* pyramid = 0;
//
// classes and structures
//
struct vertex
vertex(float x, float y, float z, float nx, float ny, float nz)
float  _x,  _y,  _z;
float _nx, _ny, _nz;
static

const dword fvf;

};
const dword vertex::fvf = d3dfvf_xyz | d3dfvf_normal;
//
// framework functions
//
bool setup()
void cleanup()
bool display(float timedelta)
return

true;

}
//
// wndproc
//
lresult callback d3d::wndproc(hwnd hwnd, uint msg, wparam wparam, lparam lparam)
return ::defwindowproc(hwnd, msg, wparam, lparam);
}
//
// winmain
//
int winapi winmain(hinstance hinstance,
hinstance previnstance,
pstr cmdline,
int showcmd)
if(!setup())
d3d::entermsgloop( display );
cleanup();
device->release();
return 0;
}

DirectX基礎學習系列5 融合技術

7.1融合方程 1概念融合技術將當前光柵化畫素的顏色與以前已光柵化並處於同乙個位置的畫素顏色進行合成,即將當前要進行光柵化的三角形單元與已寫入後台的畫素進行融合 2需要遵循的原則 1 先繪製不需要融合的物體 2 需要融合的物品按照攝像機的深度值進行排序 3融合方程 color rgbsrc ksrc...

DirectX 基礎學習系列6 字型

directx9自帶id3dxfont類 內部呼叫gdi的介面,效率一般,但能夠處理一些複雜的字型 hresult d3dxcreatefontindirect lpdirect3ddevice9 pdevice,const d3dxfont desc pdesc,lpd3dxfont ppfont...

DirectX學習 數學基礎 1

向量的基本運用 建立3d向量 typedef struct d3dxvector3 public d3dvector d3dxvector3 const float d3dxvector3 const d3dvector d3dxvector3 const d3dxfloat16 d3dxvecto...