Hazel Game Engine (052) 2DTransform

If there are errors in the code, terminology, etc. in the text, please correct me

Article Directory

foreword

  • What this section does 1: Control the position and size of graphics on the screen
    1. In Section 051, the DrawQuad method of the Renderer2D rendering class has two parameters, position and size
    2. These two parameters control the position and scaling of the drawing graphics , and what we need to do in this section is to implement them.
  • What this section does 2: Add pure virtual functions to the Shader class
    1. The shader class in the Renderer2D rendering class needs a dynamic pointer to be forced into a subclass (because the subclass has functions that the parent class does not have)
    2. So you need to add the corresponding pure virtual function to the parent class and let the subclass implement it
    3. In this way, when the base class type pointer points to the subclass and calls the subclass function, there is no need to force the dynamic pointer, and the function of the subclass can be executed through dynamic polymorphism

Effect

Please add a picture description

the code

  • Sandbox2D.cpp

    Hazel::Renderer2D::BeginScene(m_CameraController.GetCamera());
    Hazel::Renderer2D::DrawQuad({
          
          -1.0f, 0.0f}, {
          
          0.8f,0.8f}, m_FlatColor);
    Hazel::Renderer2D::DrawQuad({
          
           0.5f, -0.5f }, {
          
           0.5f, 0.8f }, {
          
          0.2f, 0.8f, 0.9f, 1.0f});
    Hazel::Renderer2D::EndScene();
    
  • Renderer2D.cpp

    void Hazel::Renderer2D::BeginScene(const OrthographicCamera& camera)
    {
          
          
      // 上传矩阵数据到glsl
      s_Data->FlatColorShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());
    }
    
    void Hazel::Renderer2D::EndScene()
    {
          
          
    }
    
    void Hazel::Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color)
    {
          
          
      DrawQuad({
          
           position.x, position.y, 0.0f }, size, color);
    }
    
    void Hazel::Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color)
    {
          
          
      s_Data->QuadVertexArray->Bind();		// 绑定顶点数组
      s_Data->FlatColorShader->Bind();		// 绑定shader
      // 设置transform
      ///
      // 这里
      ///
      glm::mat4 tranform = glm::translate(glm::mat4(1.0f), position) *
       glm::scale(glm::mat4(1.0f), {
          
           size.x, size.y, 1.0f });
      // shader类不再需要动态指针强转成子类
      // std::dynamic_pointer_cast<OpenGLShader>(s_Data->FlatColorShader)->UploadUniformFloat4("u_Color", color);
      s_Data->FlatColorShader->SetMat4("u_Transform", tranform);// 控制图形在屏幕的位置和大小
      s_Data->FlatColorShader->SetFloat4("u_Color", color);
      RenderCommand::DrawIndexed(s_Data->QuadVertexArray);
    }
    
  • Shader.cpp

    Add functions only available to subclasses to this parent class, and let subclasses implement them as pure virtual functions, so that when subclass functions are called, dynamic pointers do not need to be forced to subclass pointers

    virtual void SetFloat3(const std::string& name, const glm::vec3& value) = 0;
    virtual void SetFloat4(const std::string& name, const glm::vec4& value) = 0;
    virtual void SetMat4(const std::string& name, const glm::mat4& value) = 0;
    
  • OpenGLShader.cpp

    To implement the virtual function of the parent class, but the essence is to call the previously designed UploadUniformFloat3 and 4 functions

    void OpenGLShader::SetFloat3(const std::string& name, const glm::vec3& value)
    {
          
          
        UploadUniformFloat3(name, value);
    }
    void OpenGLShader::SetFloat4(const std::string& name, const glm::vec4& value)
    {
          
          
        UploadUniformFloat4(name, value);
    }
    void OpenGLShader::SetMat4(const std::string& name, const glm::mat4& value)
    {
          
          
        UploadUniformMat4(name, value);
    }
    // 原本有的
    void OpenGLShader::UploadUniformMat3(const std::string& name, const glm::mat3& matrix)
    {
          
          
        GLint location = glGetUniformLocation(m_RendererID, name.c_str());
        glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
    }
    void OpenGLShader::UploadUniformMat4(const std::string& name, const glm::mat4& matrix)
    {
          
          
        GLint location = glGetUniformLocation(m_RendererID, name.c_str());
        glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
    }
    
  • Corresponding shader code: FlatColor.glsl

    #type vertex
    #version 330 core
    
    layout(location = 0) in vec3 a_Position;
    
    uniform mat4 u_ViewProjection;
    uniform mat4 u_Transform;
    
    void main() {
    	gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0);
    }
    
    #type fragment
    #version 330 core
    
    layout(location = 0) out vec4 color;
    
    uniform vec4 u_Color;
    
    void main() {
    	color = u_Color;	
    }
    

Guess you like

Origin blog.csdn.net/qq_34060370/article/details/131882789