render_dx9.cpp
上传用户:gzqinmao
上传日期:2022-07-13
资源大小:472k
文件大小:5k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #include "Gut.h"
  2. #include "render_data.h"
  3. #include <d3dx9.h>
  4. #include <d3dx9math.h>
  5. bool InitResourceDX9(void)
  6. {
  7. // 获得Direct3D 9设备
  8. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  9. // 设置视角转换矩阵
  10. Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, 1.0f, 0.1f, 100.0f);
  11. device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
  12. // 关闭光照
  13. device->SetRenderState(D3DRS_LIGHTING, FALSE);
  14. // 改变三角形正面的法线
  15. device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  16. return true;
  17. }
  18. bool ReleaseResourceDX9(void)
  19. {
  20. return true;
  21. }
  22. void ResizeWindowDX9(int width, int height)
  23. {
  24. // 获得Direct3D 9设备
  25. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  26. D3DPRESENT_PARAMETERS d3dpresent;
  27.     
  28. ZeroMemory( &d3dpresent, sizeof(d3dpresent) );
  29.     d3dpresent.Windowed = TRUE; // 使用窗口模式
  30.     d3dpresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
  31.     d3dpresent.BackBufferFormat = D3DFMT_UNKNOWN; // 使用窗口模式可以不去设置
  32. d3dpresent.BackBufferCount = 1; // 提供一块backbuffer
  33. d3dpresent.EnableAutoDepthStencil = TRUE; // 自动打开DepthStencil Buffer
  34. d3dpresent.AutoDepthStencilFormat = D3DFMT_D24S8; // DepthStencil Buffer模式
  35. device->Reset(&d3dpresent);
  36. // 投影矩阵, 重设水平和垂直方向的视角.
  37. float aspect = (float) height / (float) width;
  38. Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, aspect, 0.1f, 100.0f);
  39. device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
  40. // 关闭光照
  41. device->SetRenderState(D3DRS_LIGHTING, FALSE);
  42. // 改变三角形正面的法线
  43. device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  44. }
  45. // 使用DirectX 9来绘图
  46. void RenderFrameDX9(void)
  47. {
  48. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  49. // `清除画面`
  50. device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
  51. // `开始下绘图指令`
  52. device->BeginScene(); 
  53. // `计算出一个可以转换到镜头坐标系的矩阵`
  54. Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
  55. device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
  56. // `设置数据格式`
  57. // D3DFVF_XYZ = `使用3个浮点数来记录位置`
  58. // D3DFVF_DIFFUSE = `使用32bits整数类型来记录BGRA颜色`
  59. device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE); 
  60. D3DXMATRIX temp_matrix;
  61. // `太阳, 代入鼠标的旋转控制`
  62. D3DXMATRIX sun_matrix, sun_ry, sun_rx;
  63. D3DXMatrixRotationX(&sun_rx, g_fRotate_X);
  64. D3DXMatrixRotationY(&sun_ry, g_fRotate_Y);
  65. D3DXMatrixMultiply(&sun_matrix, &sun_ry, &sun_rx);
  66. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &sun_matrix);
  67. device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, 
  68. g_pSphereIndices, D3DFMT_INDEX16, g_pSunVertices, sizeof(Vertex_VC) );
  69. // `水星`
  70. float mercury_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year_mercury); 
  71. D3DXMATRIX mercury_matrix, mercury_translate, mercury_rotate;
  72. D3DXMatrixRotationY(&mercury_rotate, mercury_rotate_y);
  73. D3DXMatrixTranslation(&mercury_translate, mercury_to_sun_distance, 0.0f, 0.0f);
  74. D3DXMatrixMultiply(&temp_matrix, &mercury_translate, &mercury_rotate);
  75. D3DXMatrixMultiply(&mercury_matrix, &temp_matrix, &sun_matrix);
  76. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &mercury_matrix);
  77. device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, g_pSphereIndices, D3DFMT_INDEX16, g_pMoonVertices, sizeof(Vertex_VC) );
  78. // `金星`
  79. float venus_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year_venus); 
  80. D3DXMATRIX venus_matrix, venus_rotate, venus_translate;
  81. D3DXMatrixRotationY(&venus_rotate, venus_rotate_y);
  82. D3DXMatrixTranslation(&venus_translate, venus_to_sun_distance, 0.0f, 0.0f);
  83. D3DXMatrixMultiply(&temp_matrix, &venus_translate, &venus_rotate);
  84. D3DXMatrixMultiply(&venus_matrix, &temp_matrix, &sun_matrix);
  85. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &venus_matrix);
  86. device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, g_pSphereIndices, D3DFMT_INDEX16, g_pMoonVertices, sizeof(Vertex_VC) );
  87. // `地球`
  88. float earth_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year); 
  89. D3DXMATRIX earth_matrix, earth_rotate, earth_translate;
  90. D3DXMatrixRotationY(&earth_rotate, earth_rotate_y);
  91. D3DXMatrixTranslation(&earth_translate, earth_to_sun_distance, 0.0f, 0.0f);
  92. D3DXMatrixMultiply(&temp_matrix, &earth_translate, &earth_rotate);
  93. D3DXMatrixMultiply(&earth_matrix, &temp_matrix, &sun_matrix);
  94. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &earth_matrix);
  95. device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, g_pSphereIndices, D3DFMT_INDEX16, g_pEarthVertices, sizeof(Vertex_VC) );
  96. // `月亮`
  97. float moon_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_month); 
  98. D3DXMATRIX moon_matrix, moon_rotate, moon_translate;
  99. D3DXMatrixRotationY(&moon_rotate, moon_rotate_y);
  100. D3DXMatrixTranslation(&moon_translate, moon_to_earth_distance, 0.0f, 0.0f);
  101. D3DXMatrixMultiply(&temp_matrix, &moon_translate, &moon_rotate);
  102. D3DXMatrixMultiply(&moon_matrix, &temp_matrix, &earth_matrix);
  103. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &moon_matrix);
  104. device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, g_pSphereIndices, D3DFMT_INDEX16, g_pMoonVertices, sizeof(Vertex_VC) );
  105. // `声明所有的绘图指令都下完了`
  106. device->EndScene(); 
  107. // `把背景backbuffer的画面显示出来`
  108.     device->Present( NULL, NULL, NULL, NULL );
  109. }