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

OpenGL

开发平台:

Visual C++

  1. #include "Gut.h"
  2. #include "render_data.h"
  3. bool InitResourceDX9(void)
  4. {
  5. // `获得Direct3D9设备`
  6. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  7. // `计算出一个可以转换到镜头坐标系的矩阵`
  8. Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
  9. Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(60.0f, 1.0f, 1.0f, 100.0f);
  10. // `设置视角转换矩阵`
  11. device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
  12. // `设置镜头转换矩阵`
  13. device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
  14. // `关闭光照`
  15. device->SetRenderState(D3DRS_LIGHTING, FALSE);
  16. return true;
  17. }
  18. bool ReleaseResourceDX9(void)
  19. {
  20. return true;
  21. }
  22. // `使用Direct3D9来绘图`
  23. void RenderFrameDX9(void)
  24. {
  25. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  26. device->Clear(
  27. 0, NULL, // `清除整个画面 `
  28. D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, // `清除颜色和Z Buffer`
  29. D3DCOLOR_ARGB(0, 0, 0, 0), // `设置要把颜色清成黑色`
  30. 1.0f, // `设置要把Z值清为1, 也就是离镜头最远.`
  31. 0 // `设置要把Stencil buffer清为0, 在这是否设置没有区别.`
  32. );
  33. // `开始下绘图指令`
  34. device->BeginScene(); 
  35. // `设置数据格式`
  36. device->SetFVF(D3DFVF_XYZ); 
  37. // `4个金字塔的基本位置`
  38. Vector4 pos[4] = 
  39. {
  40. Vector4(-1.0f, -1.0f, 0.0f),
  41. Vector4( 1.0f, -1.0f, 0.0f),
  42. Vector4(-1.0f,  1.0f, 0.0f),
  43. Vector4( 1.0f,  1.0f, 0.0f),
  44. };
  45. Matrix4x4 world_matrix;
  46. for ( int i=0; i<4; i++ )
  47. {
  48. // `设置转换矩阵`
  49. world_matrix.Translate_Replace(pos[i]);
  50. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix);
  51. // `画出金字塔的8条边线`
  52. device->DrawPrimitiveUP(D3DPT_LINELIST, 8, g_vertices, sizeof(Vector4)); 
  53. }
  54. // `声明所有的绘图指令都下完了`
  55. device->EndScene(); 
  56. // `把背景backbuffer的画面显示出来`
  57.     device->Present( NULL, NULL, NULL, NULL );
  58. }