• 跟着cherno手搓游戏引擎【27】升级2DRenderer(添加旋转)


    水节,添加了旋转的DrawQuad:

    Renderer2D.h: 

    1. #pragma once
    2. #include "OrthographicCamera.h"
    3. #include"Texture.h"
    4. namespace YOTO {
    5. class Renderer2D
    6. {
    7. public:
    8. //为什么渲染器是静态的:
    9. static void Init();
    10. static void ShutDown();
    11. static void BeginScene(const OrthographicCamera& camera);
    12. static void EndScene();
    13. static void DrawQuad(const glm::vec2& position, const glm::vec2& size ,const glm::vec4& color);
    14. static void DrawQuad(const glm::vec3& position, const glm::vec2& size ,const glm::vec4& color);
    15. static void DrawQuad(const glm::vec2& position, const glm::vec2& size ,const Ref texture,float tilingFactor=1.0f,const glm::vec4& tintColor=glm::vec4(1.0f));
    16. static void DrawQuad(const glm::vec3& position, const glm::vec2& size ,const Ref texture,float tilingFactor=1.0f,const glm::vec4& tintColor=glm::vec4(1.0f));
    17. static void DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation,const glm::vec4& color);
    18. static void DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation,const glm::vec4& color);
    19. static void DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation,const Ref texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f));
    20. static void DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation,const Ref texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f));
    21. };
    22. }

    Renderer2D.cpp:  

    1. #include "ytpch.h"
    2. #include "Renderer2D.h"
    3. #include"VertexArray.h"
    4. #include"Shader.h"
    5. //#include "Platform/OpenGL/OpenGLShader.h"
    6. #include
    7. #include "RenderCommand.h"
    8. namespace YOTO {
    9. struct Renderer2DStorage {
    10. Ref QuadVertexArray;
    11. //Ref FlatColorShader;
    12. Ref TextureShader;
    13. Ref WhiteTexture;
    14. };
    15. static Renderer2DStorage* s_Data;
    16. void Renderer2D::Init()
    17. {
    18. YT_PROFILE_FUNCTION();
    19. s_Data = new Renderer2DStorage();
    20. s_Data->QuadVertexArray = VertexArray::Create();
    21. float squareVertices[5 * 4] = {
    22. -0.5f,-0.5f,0.0f,0.0f,0.0f,
    23. 0.5f,-0.5f,0.0f,1.0f,0.0f,
    24. 0.5f,0.5f,0.0f,1.0f,1.0f,
    25. -0.5f,0.5f,0.0f,0.0f,1.0f,
    26. };
    27. Ref squareVB;
    28. squareVB.reset(VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
    29. squareVB->SetLayout({
    30. {ShaderDataType::Float3,"a_Position"},
    31. {ShaderDataType::Float2,"a_TexCoord"}
    32. });
    33. s_Data->QuadVertexArray->AddVertexBuffer(squareVB);
    34. uint32_t squareIndices[6] = { 0,1,2,2,3,0 };
    35. Ref squareIB;
    36. squareIB.reset((IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))));
    37. s_Data->QuadVertexArray->AddIndexBuffer(squareIB);
    38. s_Data->WhiteTexture = Texture2D::Create(1, 1);
    39. uint32_t whiteTextureData = 0xffffffff;
    40. s_Data->WhiteTexture->SetData(&whiteTextureData,sizeof(uint32_t));
    41. //s_Data->FlatColorShader =Shader::Create("assets/shaders/FlatColor.glsl");
    42. s_Data->TextureShader= Shader::Create("assets/shaders/Texture.glsl");
    43. s_Data->TextureShader->Bind();
    44. s_Data->TextureShader->SetInt("u_Texture", 0);
    45. }
    46. void Renderer2D::ShutDown()
    47. {
    48. YT_PROFILE_FUNCTION();
    49. delete s_Data;
    50. }
    51. void Renderer2D::BeginScene(const OrthographicCamera& camera)
    52. {
    53. YT_PROFILE_FUNCTION();
    54. /*s_Data->FlatColorShader->Bind();
    55. s_Data->FlatColorShader->SetMat4("u_ViewProjection",camera.GetViewProjectionMatrix());*/
    56. s_Data->TextureShader->Bind();
    57. s_Data->TextureShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());
    58. }
    59. void Renderer2D::EndScene()
    60. {
    61. YT_PROFILE_FUNCTION();
    62. }
    63. void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color)
    64. {
    65. DrawQuad({ position.x,position.y,0.0f }, size, color);
    66. }
    67. void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color)
    68. {
    69. YT_PROFILE_FUNCTION();
    70. //s_Data->FlatColorShader->Bind();
    71. //s_Data->FlatColorShader->SetFloat4("u_Color", color);
    72. //s_Data->TextureShader->Bind();
    73. s_Data->TextureShader->SetFloat4("u_Color", color);
    74. s_Data->TextureShader->SetFloat("m_TilingFactor", 1.0f);
    75. s_Data->WhiteTexture->Bind();
    76. glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) /**rotation*/ * glm::scale(glm::mat4(1.0f), {size.x,size.y,1.0f});
    77. s_Data->TextureShader->SetMat4("u_Transform", transform);
    78. s_Data->QuadVertexArray->Bind();
    79. RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
    80. }
    81. void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref texture, float tilingFactor, const glm::vec4& tintColor)
    82. {
    83. DrawQuad({ position.x,position.y,0.0f }, size, texture, tilingFactor, tintColor);
    84. }
    85. void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref texture, float tilingFactor, const glm::vec4& tintColor)
    86. {
    87. YT_PROFILE_FUNCTION();
    88. //s_Data->TextureShader->Bind();
    89. s_Data->TextureShader->SetFloat4("u_Color", tintColor);
    90. s_Data->TextureShader->SetFloat("m_TilingFactor",tilingFactor);
    91. texture->Bind();
    92. glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) /**rotation*/ * glm::scale(glm::mat4(1.0f), { size.x,size.y,1.0f });
    93. s_Data->TextureShader->SetMat4("u_Transform", transform);
    94. s_Data->QuadVertexArray->Bind();
    95. RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
    96. }
    97. void Renderer2D::DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const glm::vec4& color)
    98. {
    99. DrawRotatedQuad({ position.x,position.y,0.0f }, size, rotation,color);
    100. }
    101. void Renderer2D::DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const glm::vec4& color)
    102. {
    103. YT_PROFILE_FUNCTION();
    104. s_Data->TextureShader->SetFloat4("u_Color", color);
    105. s_Data->TextureShader->SetFloat("m_TilingFactor", 1.0f);
    106. s_Data->WhiteTexture->Bind();
    107. glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::rotate(glm::mat4(1.0f), rotation, {0.0f,0.0f,1.0f}) * glm::scale(glm::mat4(1.0f), { size.x,size.y,1.0f });
    108. s_Data->TextureShader->SetMat4("u_Transform", transform);
    109. s_Data->QuadVertexArray->Bind();
    110. RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
    111. }
    112. void Renderer2D::DrawRotatedQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const Ref texture, float tilingFactor, const glm::vec4& tintColor)
    113. {
    114. DrawRotatedQuad({ position.x,position.y,0.0f }, size, rotation, texture, tilingFactor, tintColor);
    115. }
    116. void Renderer2D::DrawRotatedQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const Ref texture, float tilingFactor, const glm::vec4& tintColor)
    117. {
    118. YT_PROFILE_FUNCTION();
    119. //s_Data->TextureShader->Bind();
    120. s_Data->TextureShader->SetFloat4("u_Color", tintColor);
    121. s_Data->TextureShader->SetFloat("m_TilingFactor", tilingFactor);
    122. texture->Bind();
    123. glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::rotate(glm::mat4(1.0f), rotation, { 0.0f,0.0f,1.0f }) * glm::scale(glm::mat4(1.0f), { size.x,size.y,1.0f });
    124. s_Data->TextureShader->SetMat4("u_Transform", transform);
    125. s_Data->QuadVertexArray->Bind();
    126. RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
    127. }
    128. }

    OpenGLShader.h:添加SetFloat(父类Shader.h也要添加):

    1. void OpenGLShader::SetFloat(const std::string& name, float value)
    2. {
    3. YT_PROFILE_FUNCTION();
    4. UploadUniformFloat(name, value);
    5. }

     修改测试:

    Texture.glsl:

    1. #type vertex
    2. #version 330 core
    3. layout(location = 0) in vec3 a_Position;
    4. layout(location = 1) in vec2 a_TexCoord;
    5. uniform mat4 u_ViewProjection;
    6. uniform mat4 u_Transform;
    7. out vec2 v_TexCoord;
    8. out vec3 v_Position;
    9. void main(){
    10. v_TexCoord=a_TexCoord;
    11. v_Position=a_Position;
    12. gl_Position =u_ViewProjection*u_Transform*vec4( a_Position,1.0);
    13. }
    14. #type fragment
    15. #version 330 core
    16. layout(location = 0) out vec4 color;
    17. in vec3 v_Position;
    18. in vec2 v_TexCoord;
    19. uniform vec4 u_Color ;
    20. uniform float m_TilingFactor;
    21. uniform sampler2D u_Texture ;
    22. void main(){
    23. color = texture(u_Texture, v_TexCoord*m_TilingFactor)*u_Color;
    24. }

    Sandbox2D.cpp: 

    1. #include "Sandbox2D.h"
    2. #include
    3. #include
    4. //#include
    5. #include
    6. #include
    7. #include
    8. template<typename Fn>
    9. class Timer {
    10. public:
    11. Timer(const char* name, Fn&&func)
    12. :m_Name(name),m_Func(func),m_Stopped(false)
    13. {
    14. m_StartTimepoint = std::chrono::high_resolution_clock::now();
    15. }
    16. ~Timer() {
    17. if (!m_Stopped) {
    18. Stop();
    19. }
    20. }
    21. void Stop() {
    22. auto endTimepoint= std::chrono::high_resolution_clock::now();
    23. long long start = std::chrono::time_point_cast(m_StartTimepoint).time_since_epoch().count();
    24. long long end = std::chrono::time_point_cast(endTimepoint).time_since_epoch().count();
    25. m_Stopped = true;
    26. float duration = (end - start)*0.001f;
    27. m_Func({m_Name,duration});
    28. //std::cout << "Timer:"<< m_Name << "时差:" << duration << "ms" << std::endl;
    29. }
    30. private:
    31. const char* m_Name;
    32. std::chrono::time_pointm_StartTimepoint;
    33. bool m_Stopped;
    34. Fn m_Func;
    35. };
    36. //未找到匹配的重载:auto的问题,改回原来的类型就好了
    37. #define PROFILE_SCOPE(name) Timer timer##__LINE__(name,[&](ProfileResult profileResult) {m_ProfileResults.push_back(profileResult);})
    38. Sandbox2D::Sandbox2D()
    39. :Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true)
    40. {
    41. }
    42. void Sandbox2D::OnAttach()
    43. {
    44. YT_PROFILE_FUNCTION();
    45. m_CheckerboardTexture = YOTO::Texture2D::Create("assets/textures/Checkerboard.png");
    46. }
    47. void Sandbox2D::OnDetach()
    48. {
    49. YT_PROFILE_FUNCTION();
    50. }
    51. void Sandbox2D::OnUpdate(YOTO::Timestep ts)
    52. {
    53. YT_PROFILE_FUNCTION();
    54. //update
    55. m_CameraController.OnUpdate(ts);
    56. {
    57. YT_PROFILE_SCOPE("Sandbox2D::Renderer Prep");
    58. //Render
    59. YOTO::RenderCommand::SetClearColor({ 0.2f, 0.2f, 0.2f, 1.0f });
    60. YOTO::RenderCommand::Clear();
    61. }
    62. {
    63. YT_PROFILE_SCOPE("Sandbox2D::Renderer Draw");
    64. YOTO::Renderer2D::BeginScene(m_CameraController.GetCamera());
    65. {
    66. static glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
    67. glm::vec4 redColor(0.8f, 0.3f, 0.3f, 1.0f);
    68. glm::vec4 blueColor(0.2f, 0.3f, 0.8f, 1.0f);
    69. /*std::dynamic_pointer_cast(m_FlatColorShader)->Bind();
    70. std::dynamic_pointer_cast(m_FlatColorShader)->UploadUniformFloat4("u_Color", m_SquareColor);
    71. YOTO::Renderer::Submit(m_FlatColorShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f)));*/
    72. YOTO::Renderer2D::DrawRotatedQuad({ -1.0f,0.0f }, { 0.8f,0.8f }, glm::radians(45.0f),{ 0.8f,0.2f,0.3f,1.0f });
    73. YOTO::Renderer2D::DrawQuad({ 0.5f,-0.5f }, { 0.5f,0.75f }, { 0.2f,0.3f,0.8f,1.0f });
    74. YOTO::Renderer2D::DrawQuad({ 0.0f,0.0f,-0.1f }, { 10.0f,10.0f }, m_CheckerboardTexture,10.0f,glm::vec4(1.0f,0.9f,0.9f,1.0f));
    75. YOTO::Renderer2D::EndScene();
    76. }
    77. }
    78. }
    79. void Sandbox2D::OnImGuiRender()
    80. {
    81. YT_PROFILE_FUNCTION();
    82. ImGui::Begin("Setting");
    83. ImGui::ColorEdit4("Color", glm::value_ptr(m_SquareColor));
    84. for (auto& res : m_ProfileResults) {
    85. char lable[50];
    86. strcpy(lable, "%.3fms ");
    87. strcat(lable, res.Name);
    88. ImGui::Text(lable, res.Time);
    89. }
    90. m_ProfileResults.clear();
    91. ImGui::End();
    92. }
    93. void Sandbox2D::OnEvent(YOTO::Event& e)
    94. {
    95. YT_PROFILE_FUNCTION();
    96. m_CameraController.OnEvent(e);
    97. }

  • 相关阅读:
    LeetCode 494.目标和 (动态规划 + 性能优化)二维数组 压缩成 一维数组
    奥拉帕尼人血清白蛋白HSA纳米粒|陶扎色替卵清白蛋白OVA纳米粒|来他替尼小鼠血清白蛋白MSA纳米粒(试剂)
    pyqt环境搭建
    可以通过电脑远程控制安卓设备的软件
    骨传导耳机哪个牌子好?5款高口碑骨传导耳机横评,看谁最值得入手!
    《Effective Objective-C 2.0》读书笔记——对象、消息、运行期
    介绍kafka核心原理及底层刷盘机制,集群分片机制,消息丢失和重复消费有对应的线上解决方案
    高性能Spark_transformation性能
    Git入门
    二叉搜素树(BSTree)详解—— C++ 数据结构
  • 原文地址:https://blog.csdn.net/weixin_61943345/article/details/136354410