render_dx9.cpp
Upload User: gzqinmao
Upload Date: 2022-07-13
Package Size: 472k
Code Size: 3k
Category:

OpenGL program

Development Platform:

Visual C++

  1. #include "Gut.h"
  2. #include "render_data.h"
  3. bool InitResourceDX9(void)
  4. {
  5. // 获得Direct3D 9设备
  6. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  7. // 设置视角转换矩阵
  8. Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, 1.0f, 0.1f, 100.0f);
  9. device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
  10. // 关闭光照
  11. device->SetRenderState(D3DRS_LIGHTING, FALSE);
  12. // 改变三角形正面的法线
  13. device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  14. return true;
  15. }
  16. bool ReleaseResourceDX9(void)
  17. {
  18. return true;
  19. }
  20. void ResizeWindowDX9(int width, int height)
  21. {
  22. // 获得Direct3D 9设备
  23. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  24. D3DPRESENT_PARAMETERS d3dpresent;
  25.     
  26. ZeroMemory( &d3dpresent, sizeof(d3dpresent) );
  27.     d3dpresent.Windowed = TRUE; // 使用窗口模式
  28.     d3dpresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
  29.     d3dpresent.BackBufferFormat = D3DFMT_UNKNOWN; // 使用窗口模式可以不去设置
  30. d3dpresent.BackBufferCount = 1; // 提供一块backbuffer
  31. d3dpresent.EnableAutoDepthStencil = TRUE; // 自动打开DepthStencil Buffer
  32. d3dpresent.AutoDepthStencilFormat = D3DFMT_D24S8; // DepthStencil Buffer模式
  33. device->Reset(&d3dpresent);
  34. // 投影矩阵, 重设水平和垂直方向的视角.
  35. float aspect = (float) height / (float) width;
  36. Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, aspect, 0.1f, 100.0f);
  37. device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
  38. // 关闭光照
  39. device->SetRenderState(D3DRS_LIGHTING, FALSE);
  40. // 改变三角形正面的法线
  41. device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  42. }
  43. // `使用Direct3D9来绘图`
  44. void RenderFrameDX9(void)
  45. {
  46. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  47. // `清除画面`
  48. device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
  49. // `开始下绘图指令`
  50. device->BeginScene(); 
  51. // `计算出一个可以转换到镜头坐标系的矩阵`
  52. Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
  53. device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
  54. // `设置数据格式`
  55. // D3DFVF_XYZ = `使用3个浮点数来记录位置`
  56. // D3DFVF_DIFFUSE = `使用32bits整数类型来记录BGRA颜色`
  57. device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE); 
  58. // `太阳`
  59. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_sun_matrix);
  60. device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, 
  61. g_pSphereIndices, D3DFMT_INDEX16, g_pSunVertices, sizeof(Vertex_VC) );
  62. // `地球`
  63. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_earth_matrix);
  64. device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, 
  65. g_pSphereIndices, D3DFMT_INDEX16, g_pEarthVertices, sizeof(Vertex_VC) );
  66. // `月亮`
  67. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_moon_matrix);
  68. device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, 
  69. g_pSphereIndices, D3DFMT_INDEX16, g_pMoonVertices, sizeof(Vertex_VC) );
  70. // `声明所有的绘图指令都下完了`
  71. device->EndScene(); 
  72. // `把背景backbuffer的画面显示出来`
  73.     device->Present( NULL, NULL, NULL, NULL );
  74. }