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

OpenGL

开发平台:

Visual C++

  1. #include <windows.h>
  2. // Standard OpenGL header
  3. #include <GL/gl.h>
  4. #include "Gut.h"
  5. #include "render_data.h"
  6. static Matrix4x4 g_view_matrix;
  7. bool InitResourceOpenGL(void)
  8. {
  9. // 计算出一个可以转换到镜头坐标系的矩阵
  10. g_view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
  11. // 投影矩阵
  12. Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_OpenGL(90.0f, 1.0f, 0.1f, 100.0f);
  13. // 设置视角转换矩阵
  14. glMatrixMode(GL_PROJECTION);
  15. glLoadMatrixf( (float *) &projection_matrix);
  16. glMatrixMode(GL_MODELVIEW);
  17. glEnable(GL_CULL_FACE);
  18. return true;
  19. }
  20. bool ReleaseResourceOpenGL(void)
  21. {
  22. // 没做任何事
  23. return true;
  24. }
  25. // 使用OpenGL来绘图
  26. void RenderFrameOpenGL(void)
  27. {
  28. // `清除画面`
  29. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  30. glEnable(GL_DEPTH_TEST);
  31. // `设置要用数组的方式传入顶点位置和颜色`
  32. glEnableClientState(GL_VERTEX_ARRAY);
  33. glEnableClientState(GL_COLOR_ARRAY);
  34. // `计算出一个可以转换到镜头坐标系的矩阵`
  35. Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
  36. Vector4 border(-15.0f, 0.0f, -15.0f);
  37. Vector4 grid_position = border;
  38. const int grids_x = 30;
  39. const int grids_z = 30;
  40. for ( int x=0; x<grids_x; x++ )
  41. {
  42. int grid_x = x & 0x07;
  43. grid_position[2] = border[2];
  44. for ( int z=0; z<grids_z; z++ )
  45. {
  46. int grid_z = z & 0x07;
  47. char c = g_map[grid_x][grid_z];
  48. // `设置转换矩阵`
  49. Matrix4x4 object_matrix; object_matrix.Identity();
  50. if ( c==0 )
  51. {
  52. object_matrix = view_matrix;
  53. object_matrix.Translate(grid_position);
  54. glLoadMatrixf( (float *) &object_matrix);
  55. // `设置GPU要去哪读取顶点坐标数据`
  56. glVertexPointer(3, GL_FLOAT, sizeof(Vertex_VC), &g_road_vertices[0].m_Position);
  57. // `设置GPU要去哪读取顶点颜色数据`
  58. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex_VC), &g_road_vertices[0].m_RGBA);
  59. // `画出地板`
  60. glDrawElements(
  61. GL_TRIANGLE_STRIP, // `指定所要画的基本图形种类`
  62. 4, // `有几个索引值`
  63. GL_UNSIGNED_SHORT, // `索引值的类型`
  64. g_road_trianglestrip_indices // `索引值数组`
  65. );
  66. }
  67. else
  68. {
  69. // `设置金字塔的高度`
  70. object_matrix.Scale_Replace(1.0f, (float) c, 1.0f);
  71. object_matrix[3] = grid_position;
  72. Matrix4x4 world_view_matrix = object_matrix * view_matrix;
  73. glLoadMatrixf( (float *) &world_view_matrix);
  74. // `设置GPU要去哪读取顶点坐标数据`
  75. glVertexPointer(3, GL_FLOAT, sizeof(Vertex_VC), &g_pyramid_vertices[0].m_Position);
  76. // `设置GPU要去哪读取顶点颜色数据`
  77. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex_VC), &g_pyramid_vertices[0].m_RGBA);
  78. // `画出金字塔`
  79. glDrawElements(
  80. GL_TRIANGLE_FAN, // `指定所要画的基本图形种类`
  81. 6, // `有几个索引值`
  82. GL_UNSIGNED_SHORT, // `索引值的类型`
  83. g_pyramid_trianglefan_indices // `索引值数组`
  84. );
  85. }
  86. grid_position[2] += 1.0f;
  87. }
  88. grid_position[0] += 1.0f;
  89. }
  90. // `把背景backbuffer的画面显示出来`
  91. GutSwapBuffersOpenGL();
  92. }