Directx11教程37 紋理對映 7

2021-09-08 00:14:04 字數 1344 閱讀 5733

本章是在教程35、36的基礎上來實現乙個光照紋理結合的程式,就是把場景中旋轉的cube加上紋理。

lighttex.vs中頂點的結構現在為:

struct vertexinputtype ;

//紋理座標

output.tex = input.tex;

紋理座標不做任何變化,只是單純的從vs輸出到ps中。

lighttex.ps中,現在多了2個變數:

texture2d shadertexture;

samplerstate sampletype;

分別表示紋理和紋理取樣狀態。

紋理的顏色可以用hlsl函式float4 texturecolor = shadertexture.sample(sampletype, input.tex);計算得到,最後我們把光照計算得到顏色和紋理取樣的顏色用模式調製的方式(就是相乘)調和在一起,得到最終顏色。 finalcolor = finalcolor * texturecolor;

現在對cubemodelclass類做一些變化:

struct vertextype ;

定義乙個私有成員變數textureclass* m_texture;用它來讀取紋理,產生需要的紋理資源檢視資料。在初始化函式中,我們裝入紋理資料:

bool cubemodelclass::initialize(id3d11device* device, char* modelfilename,wchar* texturefilename)

// 裝入紋理資料.

result = loadtexture(device, texturefilename);

if(!result)

// 初始化頂點緩衝和頂點索引緩衝.

result = initializebuffers(device);

if(!result)

return true; }

接下來,我們就是新寫乙個lighttexshaderclass,用來處理紋理光照混合的shader處理,這個類和lightshaderclass的區別是多了乙個取樣狀態的變數。

//取樣狀態

id3d11samplerstate* m_samplestate;

這個狀態設定後,會被傳入ps中

//設定取樣狀態

devicecontext->pssetsamplers(0, 1, &m_samplestate);

程式執行後介面如下:

完整的**請參考:

工程檔案mytutoriald3d11_30

Directx11教程38 紋理對映 8

上篇日誌中,我們用紋理和光照顏色調製的方式得到最終顏色,本章我們嘗試用紋理取樣的顏色,直接做為材質的漫反射係數kd,並用它來做光照計算,最後再做個gamma校正,如果不做的話,效果會偏亮。lighttex.ps主要改動 float4 texturecolor shadertexture.sample...

Directx11教程39 紋理對映 9

在mytutoriald3d11 32中,我們在planemodelclass中增加乙個紋理textureclass m texture 讀入乙個grass的紋理,程式執行後的效果如下 完整的 請參考 工程檔案mytutoriald3d11 32 在mytutoriald3d11 33中,我們移去了...

DirectX11渲染過程

網易作業deadline 要涼了555 對directx初始化完成後,就可以進行渲染了。struct vs input 輸入資料的格式 struct vs output 輸出資料的格式 vs output main vs input input 編譯之後生成cso檔案,使用readfiletoblo...