Multitexturing.cpp
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:7k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. #include "zfxd3dUtility.h"
  2. #include <stdio.h>
  3. // 自定义顶点结构体
  4. typedef struct tagCUSTOMVERTEX{
  5. D3DXVECTOR3 position; // The position of the vertex.
  6. FLOAT rhw;
  7. FLOAT tu1, tv1;    // The texture coordinate
  8. FLOAT tu2, tv2;    // The texture coordinate
  9. }CUSTOMVERTEX;
  10. // 自定义顶点格式
  11. #define D3DFVF_CUSTOMVERTEX  D3DFVF_XYZRHW|D3DFVF_TEX2
  12. // D3D对象接口
  13. IDirect3D9* g_pd3d;
  14. // D3D设备接口
  15. IDirect3DDevice9* g_pd3dDevice;
  16. // 字体
  17. ID3DXFont* g_pFont = NULL;
  18. // 顶点缓冲
  19. IDirect3DVertexBuffer9* g_pVB = NULL;
  20. // 纹理接口
  21. IDirect3DTexture9* g_pTex1 = NULL;
  22. IDirect3DTexture9* g_pTex2 = NULL;
  23. // 建立渲染环境和初始化
  24. bool Setup()
  25. {
  26. // 顶点缓冲初始化
  27. CUSTOMVERTEX vertices[4] = {
  28. { D3DXVECTOR3( 100, 100, 0), 1, 0, 0, 0, 0, },
  29. { D3DXVECTOR3( 100, 400, 0), 1, 0, 1, 0, 1, },
  30. { D3DXVECTOR3( 400, 100, 0), 1, 1, 0, 1, 0, },
  31. { D3DXVECTOR3( 400, 400, 0), 1, 1, 1, 1, 1, },
  32. };
  33. if( FAILED(g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
  34. , D3DPOOL_DEFAULT, &g_pVB, NULL)))
  35. return FALSE;
  36. void* pData;
  37. g_pVB->Lock(0, 4*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
  38. memcpy(pData, vertices, 4*sizeof(CUSTOMVERTEX));
  39. g_pVB->Unlock();
  40. // 设定渲染状态
  41. g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  42. g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
  43. // 加载纹理
  44. HRESULT hr;
  45. hr = D3DXCreateTextureFromFile(g_pd3dDevice, "wall.bmp", &g_pTex1);
  46. if( FAILED(hr))
  47. {
  48. MessageBox(0, "Failed to create texture from file", 0, 0);
  49. return false;
  50. }
  51. else
  52. g_pd3dDevice->SetTexture(0, g_pTex1);
  53. hr = D3DXCreateTextureFromFile(g_pd3dDevice, "lightmap.bmp", &g_pTex2);
  54. if( FAILED(hr))
  55. {
  56. MessageBox(0, "Failed to create texture from file", 0, 0);
  57. return false;
  58. }
  59. else
  60. g_pd3dDevice->SetTexture(1, g_pTex2);
  61. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  62. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
  63. return true;
  64. }
  65. bool DrawTextX()
  66. {
  67. //用于渲染侦FPS
  68. static int tmLastTime = GetTickCount();
  69. int tmNow = GetTickCount();
  70. //用于计算渲染FPS
  71. static int nFrameCount = 0;
  72. static int nFPS = 0;
  73. if(tmNow - tmLastTime > 1000)
  74. {
  75. //计算FPS
  76. tmLastTime = tmNow;
  77. nFPS = nFrameCount;
  78. nFrameCount = 0;
  79. }
  80. nFrameCount ++;
  81. char str[500];
  82. RECT rect;
  83. rect.left = 10;
  84. rect.top = 10;
  85. rect.bottom = rect.top + 30;
  86. rect.right = rect.left + 360;
  87. sprintf(str, "Current FPS:%d, please press 1~5 to enable a different multi-texturing mode", nFPS);
  88. g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
  89. return true;
  90. }
  91. bool Display()
  92. {
  93. // 绘制场景
  94. g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
  95. g_pd3dDevice->BeginScene();
  96. // 输出顶点流
  97. g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
  98. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  99. g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
  100. // 在屏幕上写字
  101. if(g_pFont)
  102. DrawTextX();
  103. g_pd3dDevice->EndScene();
  104. g_pd3dDevice->Present(0, 0, 0, 0);
  105. return true;
  106. }
  107. bool CleanUp()
  108. {
  109. SAFE_RELEASE(g_pd3dDevice);
  110. SAFE_RELEASE(g_pd3d);
  111. SAFE_RELEASE(g_pTex1);
  112. SAFE_RELEASE(g_pTex2);
  113. SAFE_RELEASE(g_pVB);
  114. return true;
  115. }
  116. int WINAPI WinMain(HINSTANCE hInstance,
  117.    HINSTANCE prevInstance, 
  118.    PSTR cmdLine,
  119.    int showCmd)
  120. {
  121. // 应用程序初始化
  122. if(!InitD3DApp(hInstance, true/*true代表窗口模式*/, 
  123. D3DDEVTYPE_HAL, &g_pd3d, &g_pd3dDevice, "MultiTexturing"/*窗口标题*/, 500, 500))
  124. {
  125. MessageBoxW(0, L"应用程序初始化失败", 0, 0);
  126. return 0;
  127. }
  128. // 创建字体
  129. if(!InitDXFont(g_pd3dDevice, &g_pFont, "Times New Roman"))
  130. g_pFont = NULL;
  131. if(!Setup())
  132. {
  133. MessageBox(0, "SetUp() failed!", 0, 0);
  134. return 0;
  135. }
  136. // 进入消息循环
  137. MSG msg;
  138. ZeroMemory(&msg, sizeof(msg));
  139. while(msg.message != WM_QUIT)
  140. {
  141. if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  142. {
  143. TranslateMessage(&msg);
  144. DispatchMessage(&msg);
  145. }
  146. else
  147. {
  148. if(!Display())
  149. {
  150. MessageBox(0, "Display() failed!", 0, 0);
  151. return 0;
  152. }
  153. }
  154. }
  155. CleanUp();
  156. return 0;
  157. }
  158. // 消息循环函数
  159. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  160. {
  161. static bool bWireFrame = false;
  162. switch( msg )
  163. {
  164. case WM_DESTROY:
  165. ::PostQuitMessage(0);
  166. break;
  167. case WM_KEYDOWN:
  168. if( wParam == VK_ESCAPE )
  169. ::DestroyWindow(hwnd);
  170. break;
  171. case WM_CHAR:
  172. switch(wParam)
  173. {
  174. case 49:// 数字键1,两块纹理相乘
  175. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  176. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  177. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  178. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
  179. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  180. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  181. break;
  182. case 50:// 数字键2,两块纹理相加
  183. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  184. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  185. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  186. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
  187. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  188. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  189. break;
  190. case 51:// 数字键3,两块纹理相减
  191. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  192. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  193. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  194. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SUBTRACT);
  195. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
  196. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_TEXTURE);
  197. break;
  198. case 52:// 数字键4,两块纹理相乘再乘以2
  199. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  200. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  201. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  202. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE2X);
  203. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  204. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  205. break;
  206. case 53:// 数字键5,禁止纹理混合
  207. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  208. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  209. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  210. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
  211. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  212. g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  213. break;
  214. }
  215. break;
  216. }
  217. return ::DefWindowProc(hwnd, msg, wParam, lParam);
  218. }