[OpenGL]绘制三角形

文章目录[x]
  1. 1:扩展TestBed
  2. 2:编写沙盒类

扩展TestBed


#ifndef TESTDELEGATE_H
#define TESTDELEGATE_H

#include <string>
#include <iostream>

#include "RenderSystem/Renderer.h"
#include "RenderSystem/VertexArray.h"
#include "RenderSystem/VertexBufferLayout.h"
#include "RenderSystem/VertexBuffer.h"
#include "RenderSystem/IndexBuffer.h"
#include "RenderSystem/Texture.h"

#include "../Src/RenderSystem/Shader.h"

class SubTestBed
{
protected:
    Shader* m_Shader;
    VertexArray* m_VA;
    VertexBuffer* m_VB;
    IndexBuffer* m_IB;
    Renderer m_Renderer;
    
public:
    SubTestBed(const std::string shaderPath)
    {
        m_Shader = new Shader(shaderPath);
    }

    virtual void Setup()
    {
        float vertices[] = {
            -0.5f, -0.5f, 0,
            +0.0f, +0.5f, 0,
            +0.5f, -0.5f, 0
        };

        unsigned int indices[] = {
                0, 1, 2
        };

        m_VA = new VertexArray();
        m_VB = new VertexBuffer(vertices, sizeof(vertices));
        VertexBufferLayout layout;
        layout.Push<float>(3);
        m_VA->AddBuffer(*m_VB, layout);
        m_IB = new IndexBuffer(indices, 3);

        m_VB->UnBind();
        m_VA->UnBind();
        m_IB->UnBind();
    }

    virtual void Render()
    {
        m_Renderer.Draw(*m_VA, *m_IB, *m_Shader);
    }

    void Clear()
    {
        m_Renderer.Clear(0.2f, 0.3f, 0.3f, 1);
    }

    virtual void Destroy()
    {
        delete m_IB;
        delete m_VA;
        delete m_VB;
        delete m_Shader;
    };
};

#endif // TESTDELEGATE_H

 

编写沙盒类


练习题1 添加更多顶点到数据中,尝试绘制两个彼此相连的三角形
#ifndef HELLOTRIANGLE_H
#define HELLOTRIANGLE_H

#include "../TestBed.h"

class HelloTriangle : public TestBed
{
private:
    class IBOTriangle : public SubTestBed
    {
    public:
        IBOTriangle(const std::string shaderPath) : SubTestBed(shaderPath) { }

        virtual void Setup() override
        {
            float vertices[] = {
                -0.5f, -0.5f, 0,
                +0.5f, -0.5f, 0,
                +0.5f, +0.5f, 0,
                -0.5f, +0.5f, 0,
            };

            unsigned int indices[] = {
                0, 1, 2,
                2, 3, 0
            };


            m_VA = new VertexArray();
            m_VB = new VertexBuffer(vertices, sizeof(vertices));
            VertexBufferLayout layout;
            layout.Push<float>(3);
            m_VA->AddBuffer(*m_VB, layout);

            m_IB = new IndexBuffer(indices, 6);
            m_VB->UnBind();
            m_VA->UnBind();
            m_IB->UnBind();
        }

        virtual void Render() override
        {
            m_VA->Bind();
            m_VB->Bind();
            m_IB->Bind();
            m_Shader->Bind();
            glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
        }

    };

public:
    HelloTriangle()
    {
        this->subTest = new DiffrentVAOandVBOTrianlge(GetShaderPath("Basic.shader"));
    }

private:
    virtual const std::string GetShaderPath(const std::string shaderName) const override
    {
        return "SandBox/02_HelloTriangle/" + shaderName;
    }

};

#endif // !HELLOTRIANGLE_H
// Basic.shader
#Shader Vertex
#version 330 core  

layout(location = 0) in vec4 a_position;  

void main()                                
{                                          
   gl_Position = a_position;               
}


#Shader Fragment
#version 330 core   

out vec4 fragColor;  

void main()                                
{                                          
   fragColor = vec4(1.0, 0.5, 0.2, 1.0);
};
练习题2 创建相同的两个三角形,但对它们的数据使用不同的VAO和VBO
class DiffrentVAOandVBOTrianlge : public SubTestBed
{
    VertexArray* m_VA1;
    VertexBuffer* m_VB1;
    IndexBuffer* m_IB1;
public:
    DiffrentVAOandVBOTrianlge(const std::string shaderPath) : SubTestBed(shaderPath) { }

    virtual void Setup() override
    {
        float vertices1[] = {
            -0.8f, -0.5f, 0.0f,
            +0.0f, -0.5f, 0.0f,
            -0.4f, +0.5f, 0.0f,
        };

        float vertices2[] = {
            0.8f, -0.5f, 0.0f,
            0.0f, -0.5f, 0.0f,
            0.4f, +0.5f, 0.0f,
        };

        unsigned int indices[] = {
            0, 1, 2
        };

        m_VA = new VertexArray();
        m_VB = new VertexBuffer(vertices1, sizeof(vertices1));
        VertexBufferLayout layout;
        layout.Push<float>(3);
        m_VA->AddBuffer(*m_VB, layout);
        m_IB = new IndexBuffer(indices, 3);

        m_VA1 = new VertexArray();
        m_VB1 = new VertexBuffer(vertices2, sizeof(vertices2));
        m_VA1->AddBuffer(*m_VB1, layout);
        m_IB1 = new IndexBuffer(indices, 3);

        m_VB->UnBind();
        m_VA->UnBind();
        m_IB->UnBind();
        m_VB1->UnBind();
        m_VA1->UnBind();
        m_IB1->UnBind();
    }

    virtual void Render() override
    {
        m_VA->Bind();
        m_VB->Bind();
        m_IB->Bind();
        m_Shader->Bind();
        GLCall(glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr));
        m_VA1->Bind();
        m_VB1->Bind();
        m_IB1->Bind();
        GLCall(glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr));
    }

    virtual void Destroy() override
    {
        SubTestBed::Destroy();
        delete m_VA1;
        delete m_VB1;
        delete m_IB1;
    }
};

// Shader同上

练习题3 创建两个着色器程序,第二个程序使用一个不同的片段着色器,输出黄色, 再次绘制这两个三角形,让其中一个输出为黄色
class DiffrentShaderTrianlge : public SubTestBed
{
    VertexArray* m_VA1;
    VertexBuffer* m_VB1;
    IndexBuffer* m_IB1;
    Shader* m_Shader1;
public:
    DiffrentShaderTrianlge(const std::string shaderPath, const std::string shaderPath1) : SubTestBed(shaderPath) 
    {
       m_Shader1 = new Shader(shaderPath1);
    }

    virtual void Setup() override
    {
        float vertices1[] = {
            -0.8f, -0.5f, 0.0f,
            +0.0f, -0.5f, 0.0f,
            -0.4f, +0.5f, 0.0f,
        };

        float vertices2[] = {
            0.8f, -0.5f, 0.0f,
            0.0f, -0.5f, 0.0f,
            0.4f, +0.5f, 0.0f,
        };

        unsigned int indices[] = {
            0, 1, 2
        };

        m_VA = new VertexArray();
        m_VB = new VertexBuffer(vertices1, sizeof(vertices1));
        VertexBufferLayout layout;
        layout.Push<float>(3);
        m_VA->AddBuffer(*m_VB, layout);
        m_IB = new IndexBuffer(indices, 3);

        m_VA1 = new VertexArray();
        m_VB1 = new VertexBuffer(vertices2, sizeof(vertices2));
        m_VA1->AddBuffer(*m_VB1, layout);
        m_IB1 = new IndexBuffer(indices, 3);

        m_VB->UnBind();
        m_VA->UnBind();
        m_IB->UnBind();
        m_VB1->UnBind();
        m_VA1->UnBind();
        m_IB1->UnBind();
    }

    virtual void Render() override
    {
        m_VA->Bind();
        m_VB->Bind();
        m_IB->Bind();
        m_Shader->Bind();
        GLCall(glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr));
        m_VA1->Bind();
        m_VB1->Bind();
        m_IB1->Bind();
        m_Shader1->Bind();
        GLCall(glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr));
    }

    virtual void Destroy() override
    {
        SubTestBed::Destroy();
        delete m_VA1;
        delete m_VB1;
        delete m_IB1;
        delete m_Shader1;
    }
};
// Yellow.shader
#Shader Vertex
#version 330 core  

layout(location = 0) in vec4 a_position;  

void main()                                
{                                          
   gl_Position = a_position;               
}


#Shader Fragment
#version 330 core   

out vec4 fragColor;  

void main()                                
{                                          
   fragColor = vec4(1.0, 1.0, 0.0, 1.0);
};

点赞

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像