当前位置:   article > 正文

跟着cherno手搓游戏引擎【14】封装opengl

跟着cherno手搓游戏引擎【14】封装opengl

本节先把代码粘上,后续会慢慢把注释都给加上,先看代码了解个大概(待更新)

前置:

RendererAPI.h:

  1. #pragma once
  2. namespace YOTO {
  3. enum class RendererAPI {
  4. None = 0,
  5. OpenGL=1
  6. };
  7. class Renderer {
  8. public:
  9. inline static RendererAPI GetAPI() {
  10. return s_RendererAPI;
  11. }
  12. static RendererAPI s_RendererAPI;
  13. };
  14. }

 RendererAPI.cpp:

  1. #include"ytpch.h"
  2. #include"Renderer.h"
  3. namespace YOTO {
  4. RendererAPI Renderer::s_RendererAPI = RendererAPI::OpenGL;
  5. }

抽象:

buffer.h:

  1. #pragma once
  2. #include <YOTO/Log.h>
  3. namespace YOTO {
  4. enum class ShaderDataType{
  5. None=0,
  6. Float,Float2,Float3,Float4,
  7. Mat3,Mat4,
  8. Int,Int2,Int3,Int4,
  9. Bool,
  10. };
  11. static uint32_t ShaderDataTypeSize(ShaderDataType type) {
  12. switch (type)
  13. {
  14. case YOTO::ShaderDataType::Float:
  15. return 4;
  16. break;
  17. case YOTO::ShaderDataType::Float2:
  18. return 4*2;
  19. break;
  20. case YOTO::ShaderDataType::Float3:
  21. return 4*3;
  22. break;
  23. case YOTO::ShaderDataType::Float4:
  24. return 4*4;
  25. break;
  26. case YOTO::ShaderDataType::Mat3:
  27. return 4*3*3;
  28. break;
  29. case YOTO::ShaderDataType::Mat4:
  30. return 4*4*4;
  31. break;
  32. case YOTO::ShaderDataType::Int:
  33. return 4;
  34. break;
  35. case YOTO::ShaderDataType::Int2:
  36. return 4*2;
  37. break;
  38. case YOTO::ShaderDataType::Int3:
  39. return 4*3;
  40. break;
  41. case YOTO::ShaderDataType::Int4:
  42. return 4*4;
  43. break;
  44. case YOTO::ShaderDataType::Bool:
  45. return 1;
  46. break;
  47. }
  48. YT_CORE_ASSERT(false, "未知的ShaderDataType!");
  49. return 0;
  50. }
  51. struct BufferElement {
  52. std::string Name;
  53. ShaderDataType Type;
  54. uint32_t Size;
  55. uint32_t Offset;
  56. bool Normalized;
  57. BufferElement(){}
  58. BufferElement(ShaderDataType type, const std::string& name,bool normalized=false)
  59. :Name(name), Type(type), Size(ShaderDataTypeSize(type)), Offset(0), Normalized(normalized){}
  60. uint32_t GetComponentCount() const{
  61. switch (Type)
  62. {
  63. case YOTO::ShaderDataType::Float:
  64. return 1;
  65. break;
  66. case YOTO::ShaderDataType::Float2:
  67. return 2;
  68. break;
  69. case YOTO::ShaderDataType::Float3:
  70. return 3;
  71. break;
  72. case YOTO::ShaderDataType::Float4:
  73. return 4;
  74. break;
  75. case YOTO::ShaderDataType::Mat3:
  76. return 3*3;
  77. break;
  78. case YOTO::ShaderDataType::Mat4:
  79. return 4*4;
  80. break;
  81. case YOTO::ShaderDataType::Int:
  82. return 1;
  83. break;
  84. case YOTO::ShaderDataType::Int2:
  85. return 2;
  86. break;
  87. case YOTO::ShaderDataType::Int3:
  88. return 3;
  89. break;
  90. case YOTO::ShaderDataType::Int4:
  91. return 4;
  92. break;
  93. case YOTO::ShaderDataType::Bool:
  94. return 1;
  95. break;
  96. default:
  97. break;
  98. }
  99. YT_CORE_ASSERT(false, "未知的ShaderDataType!");
  100. return 0;
  101. }
  102. };
  103. class BufferLayout {
  104. public:
  105. BufferLayout(){}
  106. BufferLayout(const std::initializer_list<BufferElement>elements)
  107. :m_Elements(elements)
  108. {
  109. CalculateOffsetAndStride();
  110. }
  111. inline uint32_t GetStride()const { return m_Stride; }
  112. inline const std::vector<BufferElement>& GetElements()const {
  113. return m_Elements;
  114. }
  115. std::vector<BufferElement>::iterator begin() { return m_Elements.begin(); }
  116. std::vector<BufferElement>::iterator end() { return m_Elements.end(); }
  117. std::vector<BufferElement>::const_iterator begin() const { return m_Elements.begin(); }
  118. std::vector<BufferElement>::const_iterator end() const { return m_Elements.end(); }
  119. private:
  120. void CalculateOffsetAndStride() {
  121. uint32_t offset = 0;
  122. m_Stride = 0;
  123. for (auto& element : m_Elements) {
  124. element.Offset = offset;
  125. offset += element.Size;
  126. m_Stride += element.Size;
  127. }
  128. }
  129. private:
  130. std::vector<BufferElement> m_Elements;
  131. uint32_t m_Stride = 0;
  132. };
  133. class VertexBuffer {
  134. public:
  135. virtual~VertexBuffer() {}
  136. virtual void Bind() const = 0;
  137. virtual void UnBind() const = 0;
  138. virtual void SetLayout(const BufferLayout& layout) = 0;
  139. virtual const BufferLayout& GetLayout()const = 0;
  140. static VertexBuffer* Create(float* vertices, uint32_t size);
  141. };
  142. class IndexBuffer {
  143. public:
  144. virtual~IndexBuffer(){}
  145. virtual void Bind() const = 0;
  146. virtual void UnBind() const = 0;
  147. virtual uint32_t GetCount() const = 0;
  148. static IndexBuffer* Create(uint32_t* indices, uint32_t size);
  149. };
  150. }

buffer.cpp:

  1. #include"ytpch.h"
  2. #include"Buffer.h"
  3. #include "Renderer.h"
  4. #include "Platform/OpenGL/OpenGLBuffer.h"
  5. namespace YOTO {
  6. VertexBuffer* VertexBuffer::Create(float* vertices, uint32_t size)
  7. {
  8. switch (Renderer::GetAPI())
  9. {
  10. case RendererAPI::None:
  11. YT_CORE_ASSERT(false,"Buffer:API为None不支持");
  12. return nullptr;
  13. case RendererAPI::OpenGL:
  14. return new OpenGLVertexBuffer(vertices,size);
  15. }
  16. YT_CORE_ASSERT(false,"Buffer:未知API");
  17. return nullptr;
  18. }
  19. IndexBuffer* IndexBuffer::Create(uint32_t* indices, uint32_t size)
  20. {
  21. switch (Renderer::GetAPI())
  22. {
  23. case RendererAPI::None:
  24. YT_CORE_ASSERT(false, "Buffer:API为None不支持");
  25. return nullptr;
  26. case RendererAPI::OpenGL:
  27. return new OpenGLIndexBuffer(indices, size);
  28. }
  29. YT_CORE_ASSERT(false, "Buffer:未知API");
  30. return nullptr;
  31. }
  32. }

 VertexArray.h:

  1. #pragma once
  2. #include"YOTO/Renderer/Buffer.h"
  3. namespace YOTO {
  4. class VertexArray {
  5. public:
  6. virtual~VertexArray() {}
  7. virtual void Bind() const = 0;
  8. virtual void UnBind() const = 0;
  9. virtual void AddVertexBuffer(const std::shared_ptr<VertexBuffer>&vertexBuffer) = 0;
  10. virtual void AddIndexBuffer(const std::shared_ptr<IndexBuffer>& indexBuffer) = 0;
  11. virtual const std::vector<std::shared_ptr<VertexBuffer>>& GetVertexBuffers()const = 0;
  12. virtual const std::shared_ptr<IndexBuffer>& GetIndexBuffer()const = 0;
  13. static VertexArray* Create();
  14. };
  15. }

 VertexArray.cpp:

  1. #include "ytpch.h"
  2. #include "VertexArray.h"
  3. #include"Renderer.h"
  4. #include "Platform/OpenGL/OpenGLVertexArray.h"
  5. namespace YOTO {
  6. VertexArray* VertexArray::Create()
  7. {
  8. switch (Renderer::GetAPI())
  9. {
  10. case RendererAPI::None:
  11. YT_CORE_ASSERT(false, "Buffer:API为None不支持");
  12. return nullptr;
  13. case RendererAPI::OpenGL:
  14. return new OpenGLVertexArray();
  15. }
  16. YT_CORE_ASSERT(false, "Buffer:未知API");
  17. return nullptr;
  18. }
  19. }

实现:

OpenGLBuffer.h:

  1. #pragma once
  2. #include "YOTO/Renderer/Buffer.h"
  3. namespace YOTO {
  4. class OpenGLVertexBuffer : public VertexBuffer {
  5. public:
  6. OpenGLVertexBuffer(float* vertices, uint32_t size);
  7. virtual~OpenGLVertexBuffer();
  8. virtual void Bind()const override;
  9. virtual void UnBind()const override;
  10. virtual void SetLayout(const BufferLayout& layout) override {
  11. m_Layout = layout;
  12. }
  13. virtual const BufferLayout& GetLayout()const override {
  14. return m_Layout;
  15. }
  16. private:
  17. uint32_t m_RendererID;
  18. BufferLayout m_Layout;
  19. };
  20. class OpenGLIndexBuffer : public IndexBuffer {
  21. public:
  22. OpenGLIndexBuffer(uint32_t* indices, uint32_t count);
  23. virtual~OpenGLIndexBuffer();
  24. virtual void Bind()const;
  25. virtual void UnBind()const;
  26. virtual uint32_t GetCount() const {
  27. return m_Count;
  28. };
  29. private:
  30. uint32_t m_RendererID;
  31. uint32_t m_Count;
  32. };
  33. }

 OpenGLBuffer.cpp:

  1. #include"ytpch.h"
  2. #include"OpenGLBuffer.h"
  3. #include <glad/glad.h>
  4. namespace YOTO {
  5. // VertexBuffer
  6. OpenGLVertexBuffer::OpenGLVertexBuffer(float* vertices, uint32_t size)
  7. {
  8. glCreateBuffers(1, &m_RendererID);
  9. glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
  10. glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
  11. }
  12. OpenGLVertexBuffer::~OpenGLVertexBuffer()
  13. {
  14. glDeleteBuffers(1, &m_RendererID);
  15. }
  16. void OpenGLVertexBuffer::Bind() const
  17. {
  18. glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
  19. }
  20. void OpenGLVertexBuffer::UnBind() const
  21. {
  22. glBindBuffer(GL_ARRAY_BUFFER, 0);
  23. }
  24. // IndexBuffer /
  25. OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indices, uint32_t count)
  26. :m_Count(count)
  27. {
  28. glCreateBuffers(1, &m_RendererID);
  29. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
  30. glBufferData(GL_ELEMENT_ARRAY_BUFFER, count*sizeof(uint32_t), indices, GL_STATIC_DRAW);
  31. }
  32. OpenGLIndexBuffer::~OpenGLIndexBuffer()
  33. {
  34. glDeleteBuffers(1, &m_RendererID);
  35. }
  36. void OpenGLIndexBuffer::Bind() const
  37. {
  38. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
  39. }
  40. void OpenGLIndexBuffer::UnBind() const
  41. {
  42. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  43. }
  44. }

 OpenGLVertexArray.h:

  1. #pragma once
  2. #include"YOTO/Renderer/VertexArray.h"
  3. namespace YOTO {
  4. class OpenGLVertexArray:public VertexArray
  5. {
  6. public :
  7. OpenGLVertexArray();
  8. virtual ~OpenGLVertexArray();
  9. virtual void Bind() const override;
  10. virtual void UnBind() const override;
  11. virtual void AddVertexBuffer(const std::shared_ptr<VertexBuffer>& vertexBuffer)override;
  12. virtual void AddIndexBuffer(const std::shared_ptr<IndexBuffer>& indexBuffer)override;
  13. virtual const std::vector<std::shared_ptr<VertexBuffer>>& GetVertexBuffers()const {
  14. return m_VertexBuffers;
  15. }
  16. virtual const std::shared_ptr<IndexBuffer>& GetIndexBuffer()const {
  17. return m_IndexBuffers;
  18. }
  19. private:
  20. uint32_t m_RendererID;
  21. std::vector<std::shared_ptr<VertexBuffer>> m_VertexBuffers;
  22. std::shared_ptr<IndexBuffer> m_IndexBuffers;
  23. };
  24. }

  OpenGLVertexArray.cpp:

  1. #include "ytpch.h"
  2. #include "OpenGLVertexArray.h"
  3. #include <glad/glad.h>
  4. namespace YOTO {
  5. static GLenum ShaderDataTypeToOpenGLBaseType(ShaderDataType type) {
  6. switch (type)
  7. {
  8. case YOTO::ShaderDataType::Float:
  9. return GL_FLOAT;
  10. case YOTO::ShaderDataType::Float2:
  11. return GL_FLOAT;
  12. case YOTO::ShaderDataType::Float3:
  13. return GL_FLOAT;
  14. case YOTO::ShaderDataType::Float4:
  15. return GL_FLOAT;
  16. case YOTO::ShaderDataType::Mat3:
  17. return GL_FLOAT;
  18. case YOTO::ShaderDataType::Mat4:
  19. return GL_FLOAT;
  20. case YOTO::ShaderDataType::Int:
  21. return GL_INT;
  22. case YOTO::ShaderDataType::Int2:
  23. return GL_INT;
  24. case YOTO::ShaderDataType::Int3:
  25. return GL_INT;
  26. case YOTO::ShaderDataType::Int4:
  27. return GL_INT;
  28. case YOTO::ShaderDataType::Bool:
  29. return GL_BOOL;
  30. }
  31. return 0;
  32. }
  33. OpenGLVertexArray::OpenGLVertexArray()
  34. {
  35. glCreateVertexArrays(1, &m_RendererID);
  36. }
  37. OpenGLVertexArray::~OpenGLVertexArray()
  38. {
  39. glDeleteVertexArrays(1, &m_RendererID);
  40. }
  41. void OpenGLVertexArray::Bind() const
  42. {
  43. glBindVertexArray(m_RendererID);
  44. }
  45. void OpenGLVertexArray::UnBind() const
  46. {
  47. glBindVertexArray(0);
  48. }
  49. void OpenGLVertexArray::AddVertexBuffer(const std::shared_ptr<VertexBuffer>& vertexBuffer)
  50. {
  51. YT_CORE_ASSERT(vertexBuffer->GetLayout().GetElements().size(), "OpenGLVertexArray:VertexBuffer没有布局(Layout)")
  52. glBindVertexArray(m_RendererID);
  53. vertexBuffer->Bind();
  54. uint32_t index = 0;
  55. const auto& layout = vertexBuffer->GetLayout();
  56. for (const auto& element : layout) {
  57. glEnableVertexAttribArray(index);
  58. //设置缓冲区数据格式:缓冲区序号、顶点属性的大小、什么数据类型、会不会被归一化、
  59. glVertexAttribPointer(index,
  60. element.GetComponentCount(),
  61. ShaderDataTypeToOpenGLBaseType(element.Type),
  62. element.Normalized ? GL_TRUE : GL_FALSE,
  63. layout.GetStride(),
  64. (const void*)element.Offset);
  65. index++;
  66. }
  67. m_VertexBuffers.push_back(vertexBuffer);
  68. }
  69. void OpenGLVertexArray::AddIndexBuffer(const std::shared_ptr<IndexBuffer>& indexBuffer)
  70. {
  71. glBindVertexArray(m_RendererID);
  72. indexBuffer->Bind();
  73. m_IndexBuffers = indexBuffer;
  74. }
  75. }

调用:

Application.h:

  1. #pragma once
  2. #include"Core.h"
  3. #include"Event/Event.h"
  4. #include"Event/ApplicationEvent.h"
  5. #include "YOTO/Window.h"
  6. #include"YOTO/LayerStack.h"
  7. #include"YOTO/ImGui/ImGuiLayer.h"
  8. #include <YOTO/Renderer/Shader.h>
  9. #include <YOTO/Renderer/Buffer.h>
  10. #include <YOTO/Renderer/VertexArray.h>
  11. namespace YOTO {
  12. class YOTO_API Application
  13. {
  14. public:
  15. Application();
  16. virtual ~Application();
  17. void Run();
  18. void OnEvent(Event &e);
  19. void PushLayer(Layer* layer);
  20. void PushOverlay(Layer* layer);
  21. inline static Application& Get() {return * s_Instance;}
  22. inline Window& GetWindow() { return *m_Window; }
  23. private:
  24. bool OnWindowClosed(WindowCloseEvent& e);
  25. std::unique_ptr<Window> m_Window;
  26. ImGuiLayer * m_ImGuiLayer;
  27. bool m_Running = true;
  28. LayerStack m_LayerStack;
  29. //unsigned int m_VertexArray;
  30. std::shared_ptr<Shader> m_Shader;
  31. std::shared_ptr<VertexArray> m_VertexArray;
  32. std::shared_ptr<Shader> m_BlueShader;
  33. std::shared_ptr<VertexArray> m_SquareVA;
  34. static Application* s_Instance;
  35. };
  36. //在客户端定义
  37. Application* CreateApplication();
  38. }

 Application.cpp:

  1. #include"ytpch.h"
  2. #include "Application.h"
  3. #include"Log.h"
  4. #include<glad/glad.h>
  5. #include"Input.h"
  6. namespace YOTO {
  7. #define BIND_EVENT_FN(x) std::bind(&x, this, std::placeholders::_1)
  8. Application* Application::s_Instance = nullptr;
  9. Application::Application() {
  10. YT_CORE_ASSERT(!s_Instance, "Application需要为空!")
  11. s_Instance = this;
  12. //智能指针
  13. m_Window = std::unique_ptr<Window>(Window::Creat());
  14. //设置回调函数
  15. m_Window->SetEventCallback(BIND_EVENT_FN(Application::OnEvent));
  16. //new一个Layer,放在最后层进行渲染
  17. m_ImGuiLayer = new ImGuiLayer();
  18. PushOverlay(m_ImGuiLayer);
  19. //unsigned int id;
  20. //glGenBuffers(1, &id);
  21. uint32_t indices[3] = { 0,1,2 };
  22. float vertices[3 * 7] = {
  23. -0.5f,-0.5f,0.0f, 0.8f,0.2f,0.8f,1.0f,
  24. 0.5f,-0.5f,0.0f, 0.2f,0.3f,0.8f,1.0f,
  25. 0.0f,0.5f,0.0f, 0.8f,0.8f,0.2f,1.0f,
  26. };
  27. m_VertexArray.reset(VertexArray::Create());
  28. //顶点数组:
  29. //glGenVertexArrays(1, &m_VertexArray);
  30. //glBindVertexArray(m_VertexArray);
  31. //顶点缓冲区
  32. //glGenBuffers(1, &m_VertexBuffer);
  33. //glBindBuffer(GL_ARRAY_BUFFER,m_VertexBuffer);
  34. //把数据传送给gpu,GL_STATIC_DRAW不断的用新数据刷新数组。告诉opengl这个缓冲区的数据布局
  35. //glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  36. std::shared_ptr<VertexBuffer> m_VertexBuffer;
  37. m_VertexBuffer.reset(VertexBuffer::Create(vertices, sizeof(vertices)));
  38. //启用数据的索引0
  39. //glEnableVertexAttribArray(0);
  40. //设置缓冲区数据格式:缓冲区序号、顶点属性的大小、什么数据类型、会不会被归一化、
  41. //glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),nullptr);
  42. {
  43. BufferLayout setlayout = {
  44. {ShaderDataType::Float3,"a_Position"},
  45. {ShaderDataType::Float4,"a_Color"}
  46. };
  47. m_VertexBuffer->SetLayout(setlayout);
  48. }
  49. //uint32_t index = 0;
  50. //const auto& layout = m_VertexBuffer->GetLayout();
  51. //for (const auto& element : layout) {
  52. // glEnableVertexAttribArray(index);
  53. // //设置缓冲区数据格式:缓冲区序号、顶点属性的大小、什么数据类型、会不会被归一化、
  54. // glVertexAttribPointer(index,
  55. // element.GetComponentCount(),
  56. // ShaderDataTypeToOpenGLBaseType(element.Type),
  57. // element.Normalized?GL_TRUE:GL_FALSE,
  58. // layout.GetStride() ,
  59. // (const void *)element.Offset);
  60. // index++;
  61. //}
  62. m_VertexArray->AddVertexBuffer(m_VertexBuffer);
  63. //创建索引缓冲区
  64. //glGenBuffers(1, &m_IndexBuffer);
  65. //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer);
  66. //设置缓冲区格式
  67. //glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  68. std::shared_ptr<IndexBuffer>m_IndexBuffer;
  69. m_IndexBuffer.reset(IndexBuffer::Create(indices, sizeof(indices)/sizeof(uint32_t)));
  70. m_VertexArray->AddIndexBuffer(m_IndexBuffer);
  71. //着色器
  72. //顶点布局
  73. std::string vertexSource = R"(
  74. #version 330 core
  75. layout(location = 0) in vec3 a_Position;
  76. layout(location = 1) in vec4 a_Color;
  77. out vec3 v_Position;
  78. out vec4 v_Color;
  79. void main(){
  80. v_Position=a_Position;
  81. v_Color=a_Color;
  82. gl_Position =vec4( a_Position+0.5,1.0);
  83. }
  84. )";
  85. //绘制颜色
  86. std::string fragmentSource = R"(
  87. #version 330 core
  88. layout(location = 0) out vec4 color;
  89. in vec3 v_Position;
  90. in vec4 v_Color;
  91. void main(){
  92. color=vec4(v_Color);
  93. }
  94. )";
  95. m_Shader.reset(new Shader(vertexSource, fragmentSource));
  96. ///测试/
  97. m_SquareVA.reset(VertexArray::Create());
  98. float squareVertices[3 * 4] = {
  99. -0.5f,-0.5f,0.0f,
  100. 0.5f,-0.5f,0.0f,
  101. 0.5f,0.5f,0.0f,
  102. -0.5f,0.5f,0.0f
  103. };
  104. std::shared_ptr<VertexBuffer> squareVB;
  105. squareVB.reset(VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
  106. squareVB->SetLayout({
  107. {ShaderDataType::Float3,"a_Position"}
  108. });
  109. m_SquareVA->AddVertexBuffer(squareVB);
  110. uint32_t squareIndices[6] = { 0,1,2,2,3,0 };
  111. std::shared_ptr<IndexBuffer> squareIB;
  112. squareIB.reset((IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))));
  113. m_SquareVA->AddIndexBuffer(squareIB);
  114. //测试:
  115. std::string BlueShaderVertexSource = R"(
  116. #version 330 core
  117. layout(location = 0) in vec3 a_Position;
  118. out vec3 v_Position;
  119. void main(){
  120. v_Position=a_Position;
  121. gl_Position =vec4( a_Position,1.0);
  122. }
  123. )";
  124. //绘制颜色
  125. std::string BlueShaderFragmentSource = R"(
  126. #version 330 core
  127. layout(location = 0) out vec4 color;
  128. in vec3 v_Position;
  129. void main(){
  130. color=vec4(0.2,0.3,0.8,1.0);
  131. }
  132. )";
  133. m_BlueShader.reset(new Shader(BlueShaderVertexSource, BlueShaderFragmentSource));
  134. //shader
  135. }
  136. Application::~Application() {
  137. }
  138. /// <summary>
  139. /// 所有的Window事件都会在这触发,作为参数e
  140. /// </summary>
  141. /// <param name="e"></param>
  142. void Application::OnEvent(Event& e) {
  143. //根据事件类型绑定对应事件
  144. EventDispatcher dispatcher(e);
  145. dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FN(Application::OnWindowClosed));
  146. //输出事件信息
  147. YT_CORE_INFO("Application:{0}",e);
  148. for (auto it = m_LayerStack.end(); it != m_LayerStack.begin();) {
  149. (*--it)->OnEvent(e);
  150. if (e.m_Handled)
  151. break;
  152. }
  153. }
  154. bool Application::OnWindowClosed(WindowCloseEvent& e) {
  155. m_Running = false;
  156. return true;
  157. }
  158. void Application::Run() {
  159. WindowResizeEvent e(1280, 720);
  160. if (e.IsInCategory(EventCategoryApplication)) {
  161. YT_CORE_TRACE(e);
  162. }
  163. if (e.IsInCategory(EventCategoryInput)) {
  164. YT_CORE_ERROR(e);
  165. }
  166. while (m_Running)
  167. {
  168. glClearColor(0.2f, 0.2f, 0.2f,1);
  169. glClear(GL_COLOR_BUFFER_BIT);
  170. m_BlueShader->Bind();
  171. m_SquareVA->Bind();
  172. glDrawElements(GL_TRIANGLES, m_SquareVA->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr);
  173. //glBindVertexArray(m_VertexArray);
  174. m_Shader->Bind();
  175. m_VertexArray->Bind();
  176. glDrawElements(GL_TRIANGLES,m_VertexArray->GetIndexBuffer()->GetCount(),GL_UNSIGNED_INT,nullptr);
  177. for (Layer* layer : m_LayerStack) {
  178. layer->OnUpdate();
  179. }
  180. //将ImGui的刷新放到APP中,与Update分开
  181. m_ImGuiLayer->Begin();
  182. for (Layer* layer : m_LayerStack) {
  183. layer->OnImGuiRender();
  184. }
  185. m_ImGuiLayer->End();
  186. m_Window->OnUpdate();
  187. }
  188. }
  189. void Application::PushLayer(Layer* layer) {
  190. m_LayerStack.PushLayer(layer);
  191. layer->OnAttach();
  192. }
  193. void Application::PushOverlay(Layer* layer) {
  194. m_LayerStack.PushOverlay(layer);
  195. layer->OnAttach();
  196. }
  197. }

 测试:

shader就不改了,随便一测试画出来个矩形就对了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/48482
推荐阅读
相关标签
  

闽ICP备14008679号