TexFiltering.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 tu, tv;    // The color
  7. }CUSTOMVERTEX;
  8. // 自定义顶点格式
  9. #define D3DFVF_CUSTOMVERTEX  D3DFVF_XYZ|D3DFVF_TEX1
  10. typedef enum tagTEXTUREFILTERTYPE{
  11. SAMPLE_POINT = 0,
  12. SAMPLE_LINEAR = 1,
  13. SAMPLE_ANISOTROPIC = 2,
  14. }TEXTUREFILTERTYPE;
  15. // D3D对象接口
  16. IDirect3D9* g_pd3d;
  17. // D3D设备接口
  18. IDirect3DDevice9* g_pd3dDevice;
  19. // 字体
  20. ID3DXFont* g_pFont = NULL;
  21. // 顶点缓冲和纹理接口
  22. IDirect3DVertexBuffer9* g_pVB = NULL;
  23. IDirect3DTexture9* g_pTex = NULL;
  24. // 设备可支持的最大各向异性滤波值
  25. int g_maxAnisotropic;
  26. // 枚举变量,纹理过滤模式
  27. TEXTUREFILTERTYPE g_filtertype = SAMPLE_POINT;
  28. // 布尔变量,是否开启明细纹理过滤功能
  29. bool g_mipmap = false;
  30. // 建立渲染环境和初始化
  31. bool Setup()
  32. {
  33. // 得到最大的各向异性值
  34. D3DCAPS9 caps;
  35. g_pd3dDevice->GetDeviceCaps(&caps);
  36. g_maxAnisotropic = caps.MaxAnisotropy;
  37. // 加载纹理
  38. HRESULT hr;
  39. hr = D3DXCreateTextureFromFile(g_pd3dDevice, "texture.bmp", &g_pTex);
  40. if( FAILED(hr))
  41. {
  42. MessageBox(0, "Failed to create texture from file", 0, 0);
  43. return false;
  44. }
  45. else
  46. g_pd3dDevice->SetTexture(0, g_pTex);
  47. // 顶点缓冲初始化
  48. CUSTOMVERTEX vertices[4] = {
  49. { D3DXVECTOR3(-100, 0, -100), 0, 0, },
  50. { D3DXVECTOR3( 100, 0, -100), 11, 0, },
  51. { D3DXVECTOR3(-100, 0,  100), 0, 11, },
  52. { D3DXVECTOR3( 100, 0,  100), 11, 11, },
  53. };
  54. if( FAILED(g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
  55. , D3DPOOL_DEFAULT, &g_pVB, NULL)))
  56. return FALSE;
  57. void* pData;
  58. g_pVB->Lock(0, 4*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
  59. memcpy(pData, vertices, 4*sizeof(CUSTOMVERTEX));
  60. g_pVB->Unlock();
  61. // 设定渲染状态
  62. g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  63. g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
  64. // 设置投影矩阵
  65. D3DXMATRIX proj;
  66. D3DXMatrixPerspectiveFovLH(
  67. &proj,
  68. D3DX_PI * 0.25f, // 45 - degree
  69. 1,
  70. 1.0f,
  71. 1000.0f);
  72. g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
  73. return true;
  74. }
  75. bool DrawTextX()
  76. {
  77. //用于渲染侦FPS
  78. static int tmLastTime = GetTickCount();
  79. int tmNow = GetTickCount();
  80. //用于计算渲染FPS
  81. static int nFrameCount = 0;
  82. static int nFPS = 0;
  83. if(tmNow - tmLastTime > 1000)
  84. {
  85. //计算FPS
  86. tmLastTime = tmNow;
  87. nFPS = nFrameCount;
  88. nFrameCount = 0;
  89. }
  90. nFrameCount ++;
  91. char str[500];
  92. RECT rect;
  93. rect.left = 10;
  94. rect.top = 10;
  95. rect.bottom = rect.top + 30;
  96. rect.right = rect.left + 360;
  97. sprintf(str, "Current FPS:%d, please press 1~5 to enable a different texture filtering mode", nFPS);
  98. g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
  99. switch(g_filtertype)
  100. {
  101. case SAMPLE_POINT:
  102. sprintf(str, "filter type: nearest-point sampling");
  103. break;
  104. case SAMPLE_LINEAR:
  105. sprintf(str, "filter type: linear texture filtering");
  106. break;
  107. case SAMPLE_ANISOTROPIC:
  108. sprintf(str, "filter type: anisotropic texture filtering");
  109. break;
  110. }
  111. rect.left = 10;
  112. rect.top = 50;
  113. rect.bottom = rect.top + 10;
  114. rect.right = rect.left + 360;
  115. g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
  116. if(g_mipmap)
  117. sprintf(str, "mipmap texturing on");
  118. else
  119. sprintf(str, "mipmap texturing off");
  120. rect.left = 10;
  121. rect.top = 65;
  122. rect.bottom = rect.top + 10;
  123. rect.right = rect.left + 360;
  124. g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
  125. return true;
  126. }
  127. bool Display()
  128. {
  129. // 设定视口矩阵
  130. float angle  = (3.0f * D3DX_PI) / 2.0f;
  131. float height = 5.0f;
  132. D3DXVECTOR3 eyept( cosf(angle) * 50.0f, height, sinf(angle) * 50.0f );
  133. D3DXVECTOR3 lookAt(0.0f, 0.0f, 0.0f);
  134. D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  135. D3DXMATRIX view_mat;
  136. D3DXMatrixLookAtLH(&view_mat, &eyept, &lookAt, &up);
  137. g_pd3dDevice->SetTransform(D3DTS_VIEW, &view_mat);
  138. // 绘制场景
  139. g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
  140. g_pd3dDevice->BeginScene();
  141. // 输出顶点流
  142. g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
  143. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  144. g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
  145. // 在屏幕上写字
  146. if(g_pFont)
  147. DrawTextX();
  148. g_pd3dDevice->EndScene();
  149. g_pd3dDevice->Present(0, 0, 0, 0);
  150. return true;
  151. }
  152. bool CleanUp()
  153. {
  154. SAFE_RELEASE(g_pd3dDevice);
  155. SAFE_RELEASE(g_pd3d);
  156. SAFE_RELEASE(g_pTex);
  157. SAFE_RELEASE(g_pVB);
  158. return true;
  159. }
  160. int WINAPI WinMain(HINSTANCE hInstance,
  161.    HINSTANCE prevInstance, 
  162.    PSTR cmdLine,
  163.    int showCmd)
  164. {
  165. // 应用程序初始化
  166. if(!InitD3DApp(hInstance, true/*true代表窗口模式*/, 
  167. D3DDEVTYPE_HAL, &g_pd3d, &g_pd3dDevice, "Texture Filtering"/*窗口标题*/, 400, 400))
  168. {
  169. MessageBoxW(0, L"应用程序初始化失败", 0, 0);
  170. return 0;
  171. }
  172. // 创建字体
  173. if(!InitDXFont(g_pd3dDevice, &g_pFont, "Times New Roman"))
  174. g_pFont = NULL;
  175. if(!Setup())
  176. {
  177. MessageBox(0, "SetUp() failed!", 0, 0);
  178. return 0;
  179. }
  180. // 进入消息循环
  181. MSG msg;
  182. ZeroMemory(&msg, sizeof(msg));
  183. while(msg.message != WM_QUIT)
  184. {
  185. if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  186. {
  187. TranslateMessage(&msg);
  188. DispatchMessage(&msg);
  189. }
  190. else
  191. {
  192. if(!Display())
  193. {
  194. MessageBox(0, "Display() failed!", 0, 0);
  195. return 0;
  196. }
  197. }
  198. }
  199. CleanUp();
  200. return 0;
  201. }
  202. // 消息循环函数
  203. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  204. {
  205. static bool bWireFrame = false;
  206. switch( msg )
  207. {
  208. case WM_DESTROY:
  209. ::PostQuitMessage(0);
  210. break;
  211. case WM_KEYDOWN:
  212. if( wParam == VK_ESCAPE )
  213. ::DestroyWindow(hwnd);
  214. break;
  215. case WM_CHAR:
  216. if(wParam == 49)// 数字键1
  217. {
  218. // 开启最近点采样
  219. g_filtertype = SAMPLE_POINT;
  220. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
  221. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
  222. }
  223. else if(wParam == 50)// 数字键2
  224. {
  225. // 开启线性纹理采样
  226. g_filtertype = SAMPLE_LINEAR;
  227. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  228. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  229. }
  230. else if(wParam == 51)// 数字键3
  231. {
  232. // 开启各向异性纹理采样
  233. g_filtertype = SAMPLE_ANISOTROPIC;
  234. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
  235. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
  236. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 4);
  237. }
  238. else if(wParam == 52)// 数字键4
  239. {
  240. // 开启明细纹理
  241. g_mipmap = true;
  242. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
  243. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 1);
  244. }
  245. else if(wParam == 53)// 数字键5
  246. {
  247. // 禁用明细纹理
  248. g_mipmap = false;
  249. g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
  250. }
  251. break;
  252. }
  253. return ::DefWindowProc(hwnd, msg, wParam, lParam);
  254. }