Directx11教程39 紋理對映 9

2021-09-08 00:14:04 字數 2067 閱讀 9020

在mytutoriald3d11_32中,我們在planemodelclass中增加乙個紋理textureclass* m_texture;讀入乙個grass的紋理,程式執行後的效果如下:

完整的**請參考:

工程檔案mytutoriald3d11_32

在mytutoriald3d11_33中,我們移去了model類中的紋理資源檢視成員變數,同時也移去了textureclass類,而用乙個紋理管理類texmanagerclass管理所有的紋理資源,這樣的話,可以更方便的管理紋理,之前把紋理資源放在每個model 類中,這樣每個模型只能對應乙個紋理,很不方便。

紋理管理類很簡單,就是用乙個vector儲存所有的紋理資源檢視std::vectorm_texturervs;資源管理類會儲存資源的名字,以及資源本身,通過createtex函式返回紋理資源,如果紋理資源不存在,建立紋理資源。

id3d11shaderresourceview* texmanagerclass::createtex(id3d11device* device,string filename)

m_texturenames.push_back(filename);

m_texturervs.push_back(rv);

return rv; }

在graphicsclass中,我們定義成員變數texmanagerclass*  m_texmanager;在渲染函式中,最後乙個引數直接用m_texmanager->createtex(m_d3d->getdevice(),string("grass.dds"))這樣的方式得到紋理資源。

//把plane頂點和索引資料放入緩衝區,準備渲染

m_planemodel->render(m_d3d->getdevicecontext());

//用light shader texture渲染

result = m_lighttexshader->render(m_d3d->getdevicecontext(), m_planemodel->getindexcount(), worldmatrix, viewmatrix, projectionmatrix,

light, material, camera,m_texmanager->createtex(m_d3d->getdevice(),string("grass.dds")));

if(!result)

//執行平移操作,得到最終的模型世界矩陣

d3dxmatrixrotationx(&worldmatrix1, -1.57); //pai/2

d3dxmatrixtranslation(&worldmatrix2, 0.0, 0.0, 8.0);

d3dxmatrixmultiply(&worldmatrix3, &worldmatrix1, &worldmatrix2);

result = m_lighttexshader->render(m_d3d->getdevicecontext(), m_planemodel->getindexcount(), worldmatrix3, viewmatrix, projectionmatrix,

light, material, camera,m_texmanager->createtex(m_d3d->getdevice(),string("stone01.dds")));

if(!result)

程式執行後介面如下,因為兩個平面使用的是同乙個model,漫反射係數一樣,所以正面的牆也有綠色。

在mytutoriald3d11_34中,我們改變ps**,直接使用紋理做為kd,這樣效果就好很多。

完整的**請參考:

工程檔案mytutoriald3d11_33

工程檔案mytutoriald3d11_34

Directx11教程37 紋理對映 7

本章是在教程35 36的基礎上來實現乙個光照紋理結合的程式,就是把場景中旋轉的cube加上紋理。lighttex.vs中頂點的結構現在為 struct vertexinputtype 紋理座標 output.tex input.tex 紋理座標不做任何變化,只是單純的從vs輸出到ps中。lightt...

Directx11教程38 紋理對映 8

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

DirectX11渲染過程

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