Hazel游戏引擎(110)渲染Line和Rect

文中若有代码、术语等错误,欢迎指正

前言

  • 此节目的

    要给当前引擎添加能渲染线条方框功能。

    渲染方框是在渲染线条的基础上实现的

  • 为什么

    • 当前的物体包围盒需用线条、方框显示,提供友好的视觉功能
    • 以后使用引擎的人员可能会需要射线检测,在编辑场景时就要看到射线的效果,相当于gizmos。
  • 线条、Rect如何画

    • 先说quad怎么画

      OpenGL的API画Quad需要提供索引缓冲区,会有重复的索引被使用

    • 线条

      使用OpenGL的API画不需要提供索引缓冲区,只需要提供顶点数组就行。

      同quad一样,顶点位置可以重复利用,两个顶点位置确定一条线

    • Rect

      是用4条线组合而成的,一样可以重复利用顶点位置

      如下:封闭口子的最后一个位置并不需要新的顶点位置,只需要0起始点的顶点位置

  • 如何实现

    使用OpenGL的API:glDrawArrays(GL_LINES, 0, vertexCount);

  • 实现细节

    渲染线条需要融入当前的批处理中。

    渲染线条同样需要顶点数组(顶点布局)、顶点缓冲、shader。

    需开启线条光滑 glEnable(GL_LINE_SMOOTH)

    可以设置线条宽度glLineWidth(width);

将Quad绘制成Rect

  • Line的glsl

    // Basic Texture Shader
    #type vertex
    #version 450 core
    layout(location = 0) in vec3 a_Position;
    layout(location = 1) in vec4 a_Color;
    layout(location = 2) in int a_EntityID;
    layout(std140, binding = 0) uniform Camera{
    	mat4 u_ViewProjection;
    };
    struct VertexOutput{
    	vec4 Color;
    };
    layout(location = 0) out VertexOutput Output;
    layout(location = 1) out flat int v_EntityID;
    void main(){
    	Output.Color = a_Color;
    	v_EntityID = a_EntityID;
    
    	gl_Position = u_ViewProjection * vec4(a_Position, 1.0);
    }
    #type fragment
    #version 450 core
    layout(location = 0) out vec4 o_Color;
    layout(location = 1) out int o_EntityID;
    struct VertexOutput{
    	vec4 Color;
    };
    layout(location = 0) in VertexOutput Input;
    layout(location = 1) in flat int v_EntityID;
    void main(){
    	o_Color = Input.Color;
    	o_EntityID = v_EntityID;
    }
    
  • 实际绘制Line的opengl代码

    void OpenGLRendererAPI::DrawLines(const Ref<VertexArray>& vertexArray, uint32_t vertexCount){
        vertexArray->Bind();
        glDrawArrays(GL_LINES, 0, vertexCount);
    }
    void OpenGLRendererAPI::SetLineWidth(float width){
        glLineWidth(width);
    }
    
  • 批处理代码加上line的顶点数组等信息

    struct LineVertex {
        glm::vec3 Position;
        glm::vec4 Color;
        int EntityID;
    };
    // Line
    Ref<VertexArray> LineVertexArray;
    Ref<VertexBuffer> LineVertexBuffer;
    Ref<Shader> LineShader;
    // Line
    uint32_t LineVertexCount = 0;// 只需要提供顶点数量
    LineVertex* LineVertexBufferBase = nullptr;
    LineVertex* LineVertexBufferPtr = nullptr;
    
    // Line//
    // 0.在CPU开辟存储s_Data.MaxVertices个的QuadVertex的内存
    s_Data.LineVertexBufferBase = new LineVertex[s_Data.MaxVertices];
    // 1.创建顶点数组
    s_Data.LineVertexArray = VertexArray::Create();
    
    // 2.创建顶点缓冲区,先在GPU开辟一块s_Data.MaxVertices * sizeof(QuadVertex)大小的内存
    // 与cpu对应大,是为了传输顶点数据
    s_Data.LineVertexBuffer = VertexBuffer::Create(s_Data.MaxVertices * sizeof(LineVertex));
    
    // 2.1设置顶点缓冲区布局
    s_Data.LineVertexBuffer->SetLayout({
        {ShaderDataType::Float3, "a_Position"},
        {ShaderDataType::Float4, "a_Color"},
        {ShaderDataType::Int, "a_EntityID"}
    });
    
    // 1.1设置顶点数组使用的缓冲区,并且在这个缓冲区中设置布局
    s_Data.LineVertexArray->AddVertexBuffer(s_Data.LineVertexBuffer);
    
    // 3.索引缓冲-Line不需要索引缓冲区
    s_Data.LineShader = Shader::Create("assets/shaders/Renderer2D_Line.glsl");
    void Renderer2D::Flush(){
        if (s_Data.LineVertexCount) {
            // 计算当前绘制需要多少个顶点数据
            uint32_t dataSize = (uint8_t*)s_Data.LineVertexBufferPtr - (uint8_t*)s_Data.LineVertexBufferBase;
            // 截取部分CPU的顶点数据上传OpenGL
            s_Data.LineVertexBuffer->SetData(s_Data.LineVertexBufferBase, dataSize);
    
            s_Data.LineShader->Bind();
            // 新增的:设置线条宽度
            RenderCommand::SetLineWidth(s_Data.LineWidth);
            // 调用绘画命令
            RenderCommand::DrawLines(s_Data.LineVertexArray, s_Data.LineVertexCount);
            s_Data.Stats.DrawCalls++;
        }
    }
    void Renderer2D::DrawLine(const glm::vec3& p0, glm::vec3& p1, const glm::vec4& color, int entityID){
        s_Data.LineVertexBufferPtr->Position = p0;
        s_Data.LineVertexBufferPtr->Color = color;
        s_Data.LineVertexBufferPtr->EntityID = entityID;
        s_Data.LineVertexBufferPtr++;
    
        s_Data.LineVertexBufferPtr->Position = p1;
        s_Data.LineVertexBufferPtr->Color = color;
        s_Data.LineVertexBufferPtr->EntityID = entityID;
        s_Data.LineVertexBufferPtr++;
    
        s_Data.LineVertexCount += 2;
    }
    // 根据一点中心位置确定4个点的位置绘制rect
    void Renderer2D::DrawRect(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color, int entityID){
        // position是中心位置
        glm::vec3 p0 = glm::vec3(position.x - size.x * 0.5f, position.y - size.y * 0.5f, position.z);// 左下角
        glm::vec3 p1 = glm::vec3(position.x + size.x * 0.5f, position.y - size.y * 0.5f, position.z);// 右下角
        glm::vec3 p2 = glm::vec3(position.x + size.x * 0.5f, position.y + size.y * 0.5f, position.z);// 右上角
        glm::vec3 p3 = glm::vec3(position.x - size.x * 0.5f, position.y + size.y * 0.5f, position.z);// 左上角
    
        DrawLine(p0, p1, color);
        DrawLine(p1, p2, color);
        DrawLine(p2, p3, color);
        DrawLine(p3, p0, color);
    }
    // 根据实体的transform确定顶点位置再绘制
    void Renderer2D::DrawRect(const glm::mat4& transform, const glm::vec4& color, int entityID){
        glm::vec3 lineVertices[4];
        for (size_t i = 0; i < 4; i++)
        {
            lineVertices[i] = transform * s_Data.QuadVertexPosition[i]; // quad的顶点位置正好是rect的顶点位置
        }
        DrawLine(lineVertices[0], lineVertices[1], color);
        DrawLine(lineVertices[1], lineVertices[2], color);
        DrawLine(lineVertices[2], lineVertices[3], color);
        DrawLine(lineVertices[3], lineVertices[0], color);
    }
    
  • Scene场景遍历具有SpriteRendere组件的实体,遍历调用Renderer2D::DrawRect函数,并传入自身的transform确定rect的顶点位置从而绘制4个线条组成Rect

    void Scene::OnUpdateEditor(Timestep ts, EditorCamera& camera)
    {
        Renderer2D::BeginScene(camera);
        // 绘画sprite
        {
            auto group = m_Registry.group<TransformComponent>(entt::get<SpriteRendererComponent>);
            for (auto entity : group) {
                // get返回的tuple里本是引用
                auto [tfc, sprite] = group.get<TransformComponent, SpriteRendererComponent>(entity);
                //Renderer2D::DrawSprite(tfc.GetTransform(), sprite, (int)entity);
                //
                Renderer2D::DrawRect(tfc.GetTransform(), glm::vec4(1.0f), (int)entity);
                //
            }
        }
        // 绘画circles
        {
            auto view = m_Registry.view<TransformComponent, CircleRendererComponent>();
            for (auto entity : view) {
                // get返回的tuple里本是引用
                auto [tfc, circle] = view.get<TransformComponent, CircleRendererComponent>(entity);
                Renderer2D::DrawCircle(tfc.GetTransform(), circle.Color, circle.Thickness, circle.Fade, (int)entity);
            }
        }
        Renderer2D::DrawLine(glm::vec3(2.0f), glm::vec3(5.0f), glm::vec4(1, 0, 1, 1));
        Renderer2D::DrawRect(glm::vec3(0.0f), glm::vec3(1.0f), glm::vec4(1, 0, 1, 1));
        Renderer2D::EndScene();
    }
    

效果

设置宽度的效果

将Quad渲染成Rect的效果

请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘建杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值