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

DirextX编程

开发平台:

Visual C++

  1. #include "zfxd3dUtility.h"
  2. #include <stdio.h>
  3. // D3D对象接口
  4. IDirect3D9* g_pd3d;
  5. // D3D设备接口
  6. IDirect3DDevice9* g_pd3dDevice;
  7. // 自定义顶点结构体
  8. typedef struct tagCUSTOMVERTEX{
  9. D3DXVECTOR3 position; // The position of the vertex.
  10. D3DCOLOR    color;    // The color
  11. }CUSTOMVERTEX;
  12. // 自定义顶点格式
  13. #define D3DFVF_CUSTOMVERTEX  D3DFVF_XYZ|D3DFVF_DIFFUSE
  14. IDirect3DVertexBuffer9* g_pVB1;          //Vertex Buffer, 圆柱体的侧面
  15. IDirect3DVertexBuffer9* g_pVB2;          //Vertex Buffer, 圆柱体的上表面
  16. IDirect3DVertexBuffer9* g_pVB3;          //Vertex Buffer, 圆柱体的上表面
  17. BOOL CreateVertexBuf1( IDirect3DVertexBuffer9* & g_pVB);
  18. BOOL CreateVertexBuf2( IDirect3DVertexBuffer9* & g_pVB);
  19. BOOL CreateVertexBuf3( IDirect3DVertexBuffer9* & g_pVB);
  20. // 字体
  21. ID3DXFont* g_pFont = NULL;
  22. D3DXMATRIX g_proj;
  23. LPDIRECT3DVERTEXSHADER9 vertexShader;
  24. // 建立渲染环境
  25. bool Setup()
  26. {
  27. HRESULT hr;
  28. if( !CreateVertexBuf1(g_pVB1))
  29. {
  30. MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
  31. return false;
  32. }
  33. if( !CreateVertexBuf2(g_pVB2))
  34. {
  35. MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
  36. return false;
  37. }
  38. if( !CreateVertexBuf3(g_pVB3))
  39. {
  40. MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
  41. return false;
  42. }
  43. // 设定渲染状态
  44. g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  45. // 设置投影矩阵
  46. D3DXMatrixPerspectiveFovLH(
  47. &g_proj,
  48. D3DX_PI * 0.25f, // 45 - degree
  49. 1,
  50. 1.0f,
  51. 1000.0f);
  52. LPD3DXBUFFER pShaderBuffer, pErrorBuffer;
  53. hr = D3DXAssembleShaderFromFile("vertex.vsd", 0, 0, D3DXSHADER_DEBUG, &pShaderBuffer, &pErrorBuffer);
  54. hr = g_pd3dDevice->CreateVertexShader((DWORD*)(pShaderBuffer->GetBufferPointer()), &vertexShader);
  55. return true;
  56. }
  57. bool DrawTextX()
  58. {
  59. //用于渲染侦FPS
  60. static int tmLastTime = GetTickCount();
  61. int tmNow = GetTickCount();
  62. //用于计算渲染FPS
  63. static int nFrameCount = 0;
  64. static int nFPS = 0;
  65. if(tmNow - tmLastTime > 1000)
  66. {
  67. //计算FPS
  68. tmLastTime = tmNow;
  69. nFPS = nFrameCount;
  70. nFrameCount = 0;
  71. }
  72. nFrameCount ++;
  73. char str[500];
  74. RECT rect;
  75. rect.left = rect.top = 10;
  76. rect.bottom = rect.top + 30;
  77. rect.right = rect.left + 260;
  78. sprintf(str, "Current FPS:%d, please press Q to toggle WireFrame mode, 
  79. and press LButton to enable a different Light source", nFPS);
  80. g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
  81. return true;
  82. }
  83. bool Display()
  84. {
  85. // 获取两次进入函数入口的时间间隔
  86. static float lastTime = (float)timeGetTime();
  87. float curTime  = (float)timeGetTime();
  88. float timeDelta = (curTime - lastTime)*0.001f;
  89. lastTime = curTime;
  90. // 设定视口矩阵,你可以通过按下上下左右键改变视角
  91. static float angle  = (3.0f * D3DX_PI) / 2.0f;
  92. static float height = 5.0f;
  93. if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
  94. angle -= 0.5f * timeDelta;
  95. if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
  96. angle += 0.5f * timeDelta;
  97. if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
  98. height += 1.5f * timeDelta;
  99. if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
  100. height -= 1.5f * timeDelta;
  101. D3DXVECTOR3 eyept( cosf(angle) * 5.0f, height, sinf(angle) * 5.0f );
  102. D3DXVECTOR3 lookAt(0.0f, 0.0f, 0.0f);
  103. D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  104. D3DXMATRIX view_mat;
  105. D3DXMatrixLookAtLH(&view_mat, &eyept, &lookAt, &up);
  106. D3DXMATRIX matWorld, matWorldViewProj, matT;
  107. D3DXMatrixIdentity(&matWorld);
  108. D3DXMatrixMultiply(&matWorldViewProj, &g_proj, &view_mat);
  109. matWorldViewProj = matWorld * view_mat * g_proj;
  110. D3DXMatrixTranspose(&matT, &matWorldViewProj);
  111. g_pd3dDevice->SetVertexShaderConstantF(0, (float*)&matT, 4);
  112. // 设定灯光
  113. g_pd3dDevice->SetVertexShader(vertexShader);
  114. // 绘制场景
  115. g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
  116. g_pd3dDevice->BeginScene();
  117. // 输出顶点流
  118. g_pd3dDevice->SetStreamSource(0, g_pVB1, 0, sizeof(CUSTOMVERTEX));
  119. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  120. g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 98);
  121. g_pd3dDevice->SetStreamSource(0, g_pVB2, 0, sizeof(CUSTOMVERTEX));
  122. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  123. g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 49);
  124. g_pd3dDevice->SetStreamSource(0, g_pVB3, 0, sizeof(CUSTOMVERTEX));
  125. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  126. g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 49);
  127. g_pd3dDevice->SetVertexShader(NULL);
  128. // 在屏幕上写字
  129. if(g_pFont)
  130. DrawTextX();
  131. g_pd3dDevice->EndScene();
  132. g_pd3dDevice->Present(0, 0, 0, 0);
  133. return true;
  134. }
  135. bool CleanUp()
  136. {
  137. g_pd3dDevice->SetVertexShader(NULL);
  138. SAFE_RELEASE(vertexShader);
  139. SAFE_RELEASE(g_pd3dDevice);
  140. SAFE_RELEASE(g_pd3d);
  141. return true;
  142. }
  143. int WINAPI WinMain(HINSTANCE hInstance,
  144.    HINSTANCE prevInstance, 
  145.    PSTR cmdLine,
  146.    int showCmd)
  147. {
  148. // 应用程序初始化
  149. if(!InitD3DApp(hInstance, true/*true代表窗口模式*/, 
  150. D3DDEVTYPE_HAL, &g_pd3d, &g_pd3dDevice, "light"/*窗口标题*/, 1024, 768))
  151. {
  152. MessageBoxW(0, L"应用程序初始化失败", 0, 0);
  153. return 0;
  154. }
  155. // 创建字体
  156. if(!InitDXFont(g_pd3dDevice, &g_pFont, "Times New Roman"))
  157. g_pFont = NULL;
  158. Setup();
  159. // 进入消息循环
  160. MSG msg;
  161. ZeroMemory(&msg, sizeof(msg));
  162. while(msg.message != WM_QUIT)
  163. {
  164. if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  165. {
  166. TranslateMessage(&msg);
  167. DispatchMessage(&msg);
  168. }
  169. else
  170. {
  171. Display();
  172. }
  173. }
  174. CleanUp();
  175. return 0;
  176. }
  177. // 消息循环函数
  178. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  179. {
  180. static bool bWireFrame = false;
  181. switch( msg )
  182. {
  183. case WM_DESTROY:
  184. ::PostQuitMessage(0);
  185. break;
  186. case WM_LBUTTONDOWN:
  187. // g_nlight_Enabled++;
  188. break;
  189. case WM_KEYDOWN:
  190. if( wParam == VK_ESCAPE )
  191. ::DestroyWindow(hwnd);
  192. break;
  193. case WM_CHAR:
  194. if(wParam == 'q')
  195. bWireFrame = !bWireFrame;
  196. if(bWireFrame)
  197. g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
  198. else
  199. g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
  200. break;
  201. }
  202. return ::DefWindowProc(hwnd, msg, wParam, lParam);
  203. }
  204. //100 Vertices created.........
  205. BOOL CreateVertexBuf1(IDirect3DVertexBuffer9* & g_pVB)
  206. {
  207. CUSTOMVERTEX pVertices[100];
  208. ZeroMemory( pVertices, 100*sizeof( CUSTOMVERTEX ) );
  209. for(int i=0;i<50;i++)
  210. {
  211. FLOAT alpha = (2*D3DX_PI*i)/49;
  212. pVertices[2*i].position   =  D3DXVECTOR3( sinf(alpha), 1.0f, cosf(alpha));
  213. pVertices[2*i].color      =  0x88ffff00;
  214. pVertices[2*i+1].position =  D3DXVECTOR3( sinf(alpha),-1.0f, cosf(alpha));
  215. pVertices[2*i+1].color      =  0x88ff00ff;
  216. }
  217. if( FAILED(g_pd3dDevice->CreateVertexBuffer( 100*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
  218. , D3DPOOL_DEFAULT, &g_pVB, NULL)))
  219. return FALSE;
  220. void* pData;
  221. g_pVB->Lock(0, 100*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
  222. memcpy(pData, pVertices, 100*sizeof(CUSTOMVERTEX));
  223. g_pVB->Unlock();
  224. return TRUE;
  225. }
  226. BOOL CreateVertexBuf2(IDirect3DVertexBuffer9* & g_pVB)
  227. {
  228. CUSTOMVERTEX pVertices[51];
  229. ZeroMemory( pVertices, 51*sizeof( CUSTOMVERTEX ) );
  230. pVertices[0].position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f);
  231. pVertices[0].color    = 0x8800ffff;
  232. for(int i=0;i<50;i++)
  233. {
  234. FLOAT alpha = (2*D3DX_PI*i)/49;
  235. pVertices[i+1].position   =  D3DXVECTOR3( sinf(alpha), 1.0f, cosf(alpha));
  236. pVertices[i+1].color      =  0x88ffffff;
  237. }
  238. if( FAILED(g_pd3dDevice->CreateVertexBuffer( 51*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
  239. , D3DPOOL_DEFAULT, &g_pVB, NULL)))
  240. return FALSE;
  241. void* pData;
  242. g_pVB->Lock(0, 51*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
  243. memcpy(pData, pVertices, 51*sizeof(CUSTOMVERTEX));
  244. g_pVB->Unlock();
  245. return TRUE;
  246. }
  247. BOOL CreateVertexBuf3(IDirect3DVertexBuffer9* & g_pVB)
  248. {
  249. CUSTOMVERTEX pVertices[51];
  250. ZeroMemory( pVertices, 51*sizeof( CUSTOMVERTEX ) );
  251. pVertices[0].position = D3DXVECTOR3( 0.0f, -1.0f, 0.0f);
  252. pVertices[0].color    = 0x88ffff00;
  253. for(int i=0;i<50;i++)
  254. {
  255. FLOAT alpha = (2*D3DX_PI*i)/49;
  256. pVertices[i+1].position   =  D3DXVECTOR3( sinf(alpha), -1.0f, cosf(alpha));
  257. pVertices[i+1].color      =  0x88ffffff;
  258. }
  259. if( FAILED(g_pd3dDevice->CreateVertexBuffer( 51*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
  260. , D3DPOOL_DEFAULT, &g_pVB, NULL)))
  261. return FALSE;
  262. void* pData;
  263. g_pVB->Lock(0, 51*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
  264. memcpy(pData, pVertices, 51*sizeof(CUSTOMVERTEX));
  265. g_pVB->Unlock();
  266. return TRUE;
  267. }