render_dx10.cpp
上传用户:btjssb159
上传日期:2022-07-13
资源大小:107k
文件大小:4k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. #ifdef _ENABLE_DX10_
  2. #include "Gut.h"
  3. #include "render_data.h"
  4. static ID3D10Device *g_pDevice = NULL;
  5. static ID3D10InputLayout *g_pVertexLayout = NULL;
  6. static ID3D10Buffer *g_pVertexBuffer = NULL;
  7. static ID3D10Buffer *g_pConstantBuffer = NULL;
  8. static ID3D10VertexShader *g_pVertexShader = NULL;
  9. static ID3D10PixelShader *g_pPixelShader = NULL;
  10. bool InitResourceDX10(void)
  11. {
  12. g_pDevice = GutGetGraphicsDeviceDX10();
  13. ID3D10Blob *pVSCode = NULL;
  14. // `载入Vertex Shader.`
  15. g_pVertexShader = GutLoadVertexShaderDX10_HLSL("../../shaders/vertex_transform_dx10.shader", "VS", "vs_4_0", &pVSCode);
  16. if ( NULL==g_pVertexShader )
  17. return false;
  18. // `载入Pixel Shader.`
  19. g_pPixelShader = GutLoadPixelShaderDX10_HLSL("../../shaders/vertex_transform_dx10.shader", "PS", "ps_4_0");
  20. if ( NULL==g_pPixelShader )
  21. return false;
  22. // `设置Vertex数据格式.`
  23. D3D10_INPUT_ELEMENT_DESC layout[] =
  24. {
  25. { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0 },
  26. };
  27. if ( D3D_OK != g_pDevice->CreateInputLayout( layout, 1, pVSCode->GetBufferPointer(), pVSCode->GetBufferSize(), &g_pVertexLayout ) )
  28. return false;
  29. SAFE_RELEASE(pVSCode);
  30. // `设置一块可以用来放Vertex的内存.`
  31. D3D10_BUFFER_DESC cbDesc;
  32. cbDesc.ByteWidth = sizeof(g_vertices);
  33. cbDesc.Usage = D3D10_USAGE_DYNAMIC;
  34. cbDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  35. cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  36. cbDesc.MiscFlags = 0;
  37. // `分配一块可以存放Vertex的内存, 也就是Vertex Buffer.`
  38. if ( D3D_OK != g_pDevice->CreateBuffer( &cbDesc, NULL, &g_pVertexBuffer ) )
  39. return false;
  40. // `把数据拷贝到显示适配器内存的Vertex Buffer.`
  41. Vector4 *pVertexBuffer;
  42. g_pVertexBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pVertexBuffer );
  43. memcpy(pVertexBuffer, g_vertices, sizeof(g_vertices));
  44. g_pVertexBuffer->Unmap();
  45. // `分配Shader读取参数的内存空间.`
  46. cbDesc.ByteWidth = sizeof(Matrix4x4);
  47. cbDesc.Usage = D3D10_USAGE_DYNAMIC;
  48. cbDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
  49. cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  50. cbDesc.MiscFlags = 0;
  51. if ( D3D_OK != g_pDevice->CreateBuffer( &cbDesc, NULL, &g_pConstantBuffer ) )
  52. return false;
  53. // `计算出一个可以转换到镜头坐标系的矩阵.`
  54. Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
  55. Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, 1.0f, 1.0f, 100.0f);
  56. Matrix4x4 view_projection_matrix = view_matrix * projection_matrix;
  57. // `把矩阵拷贝到shader参数区的内存空间.`
  58. Matrix4x4 *pConstData;
  59. g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData );
  60. *pConstData = view_projection_matrix;
  61. g_pConstantBuffer->Unmap();
  62. return true;
  63. }
  64. bool ReleaseResourceDX10(void)
  65. {
  66.   SAFE_RELEASE(g_pVertexLayout);
  67.   SAFE_RELEASE(g_pVertexBuffer);
  68.   SAFE_RELEASE(g_pConstantBuffer);
  69.   SAFE_RELEASE(g_pVertexShader);
  70.   SAFE_RELEASE(g_pPixelShader);
  71. return true;
  72. }
  73. void RenderFrameDX10(void)
  74. {
  75. Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  76. UINT stride = sizeof(Vector4);
  77. UINT offset = 0;
  78. // `获得调用GutCreateGraphicsDeviceDX10时所生成的D3D10对象`
  79. ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer
  80. ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer
  81. IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer
  82. // `清除颜色`
  83. g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor);
  84. // `清除Depth/Stencil buffer`
  85. g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0);
  86. // `设置vertex shader`
  87. g_pDevice->VSSetShader(g_pVertexShader);
  88. // `设置pixel shader`
  89. g_pDevice->PSSetShader(g_pPixelShader);
  90. // `设置vertex shader读取参数的内存位罝`
  91. g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer);
  92. // `设置vertex数据格式`
  93. g_pDevice->IASetInputLayout(g_pVertexLayout);
  94. // `设置vertex buffer`
  95. g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);
  96.            // `设置要画线`
  97. g_pDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_LINELIST );
  98. // `画线`
  99. g_pDevice->Draw(16, 0);
  100. // `更新画面`
  101. pSwapChain->Present(0, 0);
  102. }
  103. #endif // _ENABLE_DX10_