TestDynamicDraw.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "TestDynamicDraw.h"
  2. void TestModule::TestDynamicDraw::Init()
  3. {
  4. // 初始化顶点信息 坐标、 颜色、 TexCoord、 序号
  5. m_OriginVertexs[0] = { -1.5f, -0.5f, 0.0f, 0.1f, 0.6f, 0.9f, 1.0f, 0.0f, 0.0f, 0.0f };
  6. m_OriginVertexs[1] = { -0.5f, -0.5f, 0.0f, 0.1f, 0.6f, 0.9f, 1.0f, 1.0f, 0.0f, 0.0f };
  7. m_OriginVertexs[2] = { -0.5f, 0.5f, 0.0f, 0.1f, 0.6f, 0.9f, 1.0f, 1.0f, 1.0f, 0.0f };
  8. m_OriginVertexs[3] = { -1.5f, 0.5f, 0.0f, 0.1f, 0.6f, 0.9f, 1.0f, 0.0f, 1.0f, 0.0f };
  9. m_OriginVertexs[4] = { 0.5f, -0.5f, 0.0f, 0.9f, 0.6f, 0.1f, 1.0f, 0.0f, 0.0f, 1.0f };
  10. m_OriginVertexs[5] = { 1.5f, -0.5f, 0.0f, 0.9f, 0.6f, 0.1f, 1.0f, 1.0f, 0.0f, 1.0f };
  11. m_OriginVertexs[6] = { 1.5f, 0.5f, 0.0f, 0.9f, 0.6f, 0.1f, 1.0f, 1.0f, 1.0f, 1.0f };
  12. m_OriginVertexs[7] = { 0.5f, 0.5f, 0.0f, 0.9f, 0.6f, 0.1f, 1.0f, 0.0f, 1.0f, 1.0f };
  13. // 创建并绑定 VAO
  14. glCreateVertexArrays(1, &m_VAO);
  15. glBindVertexArray(m_VAO);
  16. // 创建并绑定 VBO
  17. glCreateBuffers(1, &m_VBO);
  18. glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
  19. glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 1000, nullptr, GL_DYNAMIC_DRAW); // 预先申请好 1000 个 Vertex 的内存地址 虽然可能用不到
  20. // 设置 VAO 内存结构
  21. GL_CALL(glEnableVertexArrayAttrib(m_VAO, 0));
  22. // 绑定坐标 序号为 0, 坐标是 3 个 GL_FLOAT, 不需要归一化,步长大小是 sizeof(Vertex) 位置偏移是 offsetof(Vertex, Position)
  23. GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, Position)));
  24. GL_CALL(glEnableVertexArrayAttrib(m_VAO, 1));
  25. GL_CALL(glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, Color)));
  26. GL_CALL(glEnableVertexArrayAttrib(m_VAO, 2));
  27. GL_CALL(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, TexCoord)));
  28. GL_CALL(glEnableVertexArrayAttrib(m_VAO, 3));
  29. GL_CALL(glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, TextureIndex)));
  30. // 顶点索引数组
  31. GLuint indices[] = {
  32. 0, 1, 2, 2, 3, 0,
  33. 4, 5, 6, 6, 7, 4,
  34. };
  35. GL_CALL(glCreateBuffers(1, &m_IB));
  36. GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IB));
  37. GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW));
  38. m_T1.Init("res/textures/ChernoLogo.png");
  39. m_T2.Init("res/textures/HazelLogo.png");
  40. m_Shader.Init("res/shader/Vertex2.vert", "res/shader/Fragment2.frag");
  41. GL_CALL(glBindVertexArray(0));
  42. GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
  43. GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
  44. m_T1.Unbind();
  45. m_T2.Unbind();
  46. m_Shader.Unbind();
  47. }
  48. void TestModule::TestDynamicDraw::Exit()
  49. {
  50. }
  51. void TestModule::TestDynamicDraw::OnUpdate(float deltaTime)
  52. {
  53. const int BlueCount = 4;
  54. const int RedCount = 4;
  55. for (int i = 0; i < BlueCount; i++)
  56. {
  57. m_Vertexs[i] = m_OriginVertexs[i];
  58. m_Vertexs[i].Position[0] += m_BlueTransition.x;
  59. m_Vertexs[i].Position[1] += m_BlueTransition.y;
  60. }
  61. for (int i = RedCount; i < BlueCount + RedCount; i++)
  62. {
  63. m_Vertexs[i] = m_OriginVertexs[i];
  64. m_Vertexs[i].Position[0] += m_RedTransition.x;
  65. m_Vertexs[i].Position[1] += m_RedTransition.y;
  66. }
  67. // 设置动态缓冲区
  68. glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
  69. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(m_Vertexs), m_Vertexs);
  70. }
  71. void TestModule::TestDynamicDraw::OnRender()
  72. {
  73. static glm::mat4 mvp = glm::ortho(-2.0f, 2.0f, -1.5f, 1.5f, -1.0f, 1.0f);
  74. glClear(GL_COLOR_BUFFER_BIT);
  75. GL_CALL(glBindVertexArray(m_VAO));
  76. GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IB));
  77. m_Shader.Bind();
  78. m_Shader.SetUniformMat4f("u_MVP", mvp);
  79. GL_CALL(glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, nullptr));
  80. }
  81. void TestModule::TestDynamicDraw::OnImGuiRender()
  82. {
  83. ImGui::Begin("ClearColor");
  84. ImGui::SliderFloat2("Blue Block", &m_BlueTransition.x, -2.0f, 2.0f);
  85. ImGui::SliderFloat2("Red Block", &m_RedTransition.x, -2.0f, 2.0f);
  86. ImGui::End();
  87. }