当前位置:   article > 正文

D3D 纹理 例子程序_d3d9 多个bitmap数据渲染到同一个texture上

d3d9 多个bitmap数据渲染到同一个texture上

D3D 纹理 例子程序

程序目的

将纹理图片显示到图元表面。

程序实现

  1. #pragma once
  2. #pragma comment(lib,"winmm.lib")
  3. #pragma comment(lib,"d3d9.lib")
  4. #pragma comment(lib,"d3dx9.lib")
  5. #include<d3d9.h>
  6. #include<d3dx9.h>
  7. struct CUSTOMVERTEX
  8. {
  9. D3DXVECTOR3 position;
  10. FLOAT tu;
  11. FLOAT tv;
  12. };
  13. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)
  14. LRESULT CALLBACK MsgProc(
  15. HWND hWnd,
  16. UINT msg,
  17. WPARAM wParam,
  18. LPARAM lParam);
  19. HRESULT Init(HWND hWnd);
  20. HRESULT InitD3D(HWND hWnd);
  21. HRESULT InitGeometry();
  22. void SetUpMatrices();
  23. void SetUpTextures();
  24. void Render();
  25. void CleanUp();
  26. //global variables.
  27. LPDIRECT3D9 pd3d9=NULL;
  28. LPDIRECT3DDEVICE9 pd3dDevice9=NULL;
  29. LPDIRECT3DVERTEXBUFFER9 pd3dVB=NULL;
  30. LPDIRECT3DTEXTURE9 pd3dTexture=NULL;
  31. INT WINAPI wWinMain(
  32. HINSTANCE,
  33. HINSTANCE,
  34. LPWSTR,
  35. INT)
  36. {
  37. //wnd class.
  38. WNDCLASSEX wcex;
  39. ZeroMemory(&wcex,sizeof(wcex));
  40. wcex.cbSize=sizeof(wcex);
  41. wcex.hInstance=GetModuleHandle(NULL);
  42. wcex.lpfnWndProc=MsgProc;
  43. wcex.lpszClassName=L"DXTextures";
  44. wcex.style=CS_CLASSDC;
  45. //register class.
  46. RegisterClassEx(&wcex);
  47. //create window.
  48. HWND hWnd=CreateWindowEx(
  49. WS_EX_OVERLAPPEDWINDOW,
  50. L"DXTextures",
  51. L"DXTextures Window",
  52. WS_OVERLAPPEDWINDOW,
  53. 100,
  54. 100,
  55. 300,
  56. 300,
  57. NULL,
  58. NULL,
  59. wcex.hInstance,
  60. NULL);
  61. //show window.
  62. ShowWindow(hWnd,SW_SHOWDEFAULT);
  63. UpdateWindow(hWnd);
  64. //init global variables.
  65. if(FAILED(Init(hWnd)))
  66. {
  67. return -1;
  68. }
  69. //message loop
  70. MSG msg;
  71. ZeroMemory(&msg,sizeof(msg));
  72. while(msg.message != WM_QUIT)
  73. {
  74. if(PeekMessage(&msg,hWnd,0,0,PM_REMOVE))
  75. {
  76. TranslateMessage(&msg);
  77. DispatchMessage(&msg);
  78. }
  79. else
  80. {
  81. Render();
  82. }
  83. }
  84. return 0;
  85. }
  86. LRESULT CALLBACK MsgProc(
  87. HWND hWnd,
  88. UINT msg,
  89. WPARAM wParam,
  90. LPARAM lParam)
  91. {
  92. switch(msg)
  93. {
  94. case WM_DESTROY:
  95. CleanUp();
  96. PostQuitMessage(0);
  97. return 0;
  98. }
  99. return DefWindowProc(hWnd,msg,wParam,lParam);
  100. }
  101. void Render()
  102. {
  103. pd3dDevice9->Clear(
  104. 0,
  105. NULL,
  106. D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  107. D3DCOLOR_XRGB(0,0,255),
  108. 1.0f,
  109. 0);
  110. if(SUCCEEDED(pd3dDevice9->BeginScene()))
  111. {
  112. SetUpMatrices();
  113. SetUpTextures();
  114. pd3dDevice9->SetStreamSource( 0, pd3dVB, 0, sizeof( CUSTOMVERTEX ) );
  115. pd3dDevice9->SetFVF( D3DFVF_CUSTOMVERTEX );
  116. pd3dDevice9->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 * 50 - 2 );
  117. pd3dDevice9->EndScene();
  118. }
  119. pd3dDevice9->Present(NULL,NULL,NULL,NULL);
  120. }
  121. HRESULT InitD3D(HWND hWnd)
  122. {
  123. //create d3d.
  124. pd3d9=Direct3DCreate9(D3D_SDK_VERSION);
  125. if(pd3d9 == NULL)
  126. {
  127. return E_FAIL;
  128. }
  129. //init present param.
  130. D3DPRESENT_PARAMETERS d3dpp;
  131. ZeroMemory(&d3dpp,sizeof(d3dpp));
  132. d3dpp.Windowed=TRUE;
  133. d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
  134. d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
  135. d3dpp.EnableAutoDepthStencil=TRUE;
  136. d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
  137. //create device.
  138. if(FAILED(pd3d9->CreateDevice(
  139. D3DADAPTER_DEFAULT,
  140. D3DDEVTYPE_HAL,
  141. hWnd,
  142. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  143. &d3dpp,
  144. &pd3dDevice9)))
  145. {
  146. return E_FAIL;
  147. }
  148. return S_OK;
  149. }
  150. void CleanUp()
  151. {
  152. if(pd3dVB != NULL)
  153. {
  154. pd3dVB->Release();
  155. pd3dVB=NULL;
  156. }
  157. if(pd3dDevice9 != NULL)
  158. {
  159. pd3dDevice9->Release();
  160. pd3dDevice9=NULL;
  161. }
  162. if(pd3d9 != NULL)
  163. {
  164. pd3d9->Release();
  165. pd3d9=NULL;
  166. }
  167. }
  168. void SetUpMatrices()
  169. {
  170. //world matrix.
  171. D3DXMATRIXA16 worldMat;
  172. D3DXMatrixIdentity(&worldMat);
  173. D3DXMatrixRotationY(&worldMat,timeGetTime()/1000.0f);
  174. pd3dDevice9->SetTransform(D3DTS_WORLD,&worldMat);
  175. //view matrix.
  176. D3DXMATRIXA16 viewMat;
  177. D3DXVECTOR3 vEye(0.0f,2.0f,-5.0f);
  178. D3DXVECTOR3 vLookAt(0.0f,0.0f,0.0f);
  179. D3DXVECTOR3 vUp(0.0f,1.0f,0.0f);
  180. D3DXMatrixLookAtLH(&viewMat,&vEye,&vLookAt,&vUp);
  181. pd3dDevice9->SetTransform(D3DTS_VIEW,&viewMat);
  182. //projection matrix.
  183. D3DXMATRIXA16 projMat;
  184. D3DXMatrixPerspectiveFovLH(&projMat,D3DX_PI/4,1.0f,1.0f,1000.0f);
  185. pd3dDevice9->SetTransform(D3DTS_PROJECTION,&projMat);
  186. }
  187. HRESULT InitGeometry()
  188. {
  189. //create texture.
  190. if(FAILED(D3DXCreateTextureFromFile(
  191. pd3dDevice9,
  192. L"dxts.jpg",
  193. &pd3dTexture)))
  194. {
  195. return E_FAIL;
  196. }
  197. //create vertex buffer.
  198. if(FAILED(pd3dDevice9->CreateVertexBuffer(
  199. 50*2*sizeof(CUSTOMVERTEX),
  200. 0,
  201. D3DFVF_CUSTOMVERTEX,
  202. D3DPOOL_DEFAULT,
  203. &pd3dVB,
  204. NULL)))
  205. {
  206. return E_FAIL;
  207. }
  208. //assgin value.
  209. CUSTOMVERTEX* pVertices;
  210. if(FAILED(pd3dVB->Lock(
  211. 0,
  212. 50*2*sizeof(CUSTOMVERTEX),
  213. (void**)&pVertices,
  214. 0)))
  215. {
  216. return E_FAIL;
  217. }
  218. for( DWORD i = 0; i < 50; i++ )
  219. {
  220. FLOAT theta = ( 2 * D3DX_PI * i ) / ( 50 - 1 );
  221. pVertices[2 * i + 0].position = D3DXVECTOR3( cosf( theta ), -1.0f, sinf( theta ) );
  222. pVertices[2 * i + 0].tu=(2*(FLOAT)i)/49.0f;
  223. pVertices[2 * i + 0].tv=1.0f;
  224. pVertices[2 * i + 1].position = D3DXVECTOR3( cosf( theta ), 1.0f, sinf( theta ) );
  225. pVertices[2 * i + 1].tu=(2*(FLOAT)i)/49.0f;
  226. pVertices[2 * i + 1].tv=0.0f;
  227. }
  228. pd3dVB->Unlock();
  229. // Turn off culling
  230. pd3dDevice9->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  231. // Turn off D3D lighting
  232. pd3dDevice9->SetRenderState( D3DRS_LIGHTING, FALSE );
  233. // Turn on the zbuffer
  234. pd3dDevice9->SetRenderState( D3DRS_ZENABLE, TRUE );
  235. return S_OK;
  236. }
  237. void SetUpTextures()
  238. {
  239. pd3dDevice9->SetTexture(0,pd3dTexture);
  240. }
  241. HRESULT Init(HWND hWnd)
  242. {
  243. if(FAILED(InitD3D(hWnd)))
  244. {
  245. return E_FAIL;
  246. }
  247. if(FAILED(InitGeometry()))
  248. {
  249. return E_FAIL;
  250. }
  251. return S_OK;
  252. }

程序结果

总结

1.d3d学习至今,纹理卡的时间最长,里面涉及的参数太多。现在也不是很懂。

2.d3d大约的流程是:声明一种特性,特性初始化,设置这种特性,使用这种特性。

3.如果想运行这个程序,将DX路径配置好,寻找一个图片,大小2的幂数,修改名字为dxts.jpg加载到工程就可以运行了。

4.学习d3d看来是需要有知识基础的,数学里面的空间与几何是需要的,后面还将会用到矩阵,路漫漫...

5.上周去面试发现这些都没有什么用,唉,等到什么程度才能真的顶用呢?


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/883829
推荐阅读
相关标签
  

闽ICP备14008679号