render_dx9.cpp
Upload User: gzqinmao
Upload Date: 2022-07-13
Package Size: 472k
Code Size: 2k
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 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. device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  17. //device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT); // 关闭内插
  18. //device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); // 使用画边线模式
  19. return true;
  20. }
  21. bool ReleaseResourceDX9(void)
  22. {
  23. return true;
  24. }
  25. // 使用DirectX 9来绘图
  26. void RenderFrameDX9(void)
  27. {
  28. LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
  29. device->Clear(
  30. 0, NULL, // 清除整个画面 
  31. D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, // 清除颜色和Z buffer 
  32. D3DCOLOR_ARGB(0, 0, 0, 0), // 设置要把颜色清成黑色
  33. 1.0f, // 设置要把Z值清为1, 也就是离镜头最远
  34. 0 // 设置要把Stencil buffer清为0, 在这是否设置没有区别.
  35. );
  36. static float angle = 0.0f;
  37. angle += 0.01f;
  38. // 开始下绘图指令
  39. device->BeginScene(); 
  40. // 设置数据格式
  41. // D3DFVF_XYZ = 使用3个浮点数来记录位置
  42. // D3DFVF_DIFFUSE = 使用32bits整数类型来记录BGRA颜色
  43. device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE); 
  44. // 设置转换矩阵
  45. Matrix4x4 world_matrix;
  46. world_matrix.RotateZ_Replace(angle); // 生成旋转矩阵
  47. device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix);
  48. // 画出金字塔的8条边线
  49. device->DrawIndexedPrimitiveUP(
  50. D3DPT_TRIANGLELIST, // 指定所要画的基本图形种类 
  51. 0, // 会使用的最小顶点编号, 事实上没太大用处
  52. 5, // 顶点数组里有几个顶点
  53. 6, // 要画出几个基本图形
  54. g_indices, // 索引数组
  55. D3DFMT_INDEX16, // 索引数组的类型
  56. g_vertices, // 顶点数组
  57. sizeof(Vertex_VC) // 顶点数组里每个顶点的大小
  58. ); 
  59. // 声明所有的绘图指令都下完了
  60. device->EndScene(); 
  61. // 把背景backbuffer的画面显示出来
  62.     device->Present( NULL, NULL, NULL, NULL );
  63. }