C++ Opengl-渲染不同的顶点格式

C++ Opengl-渲染不同的顶点格式,c++,opengl,vertex-attributes,C++,Opengl,Vertex Attributes,我正在寻找一种很好的方法来渲染具有不同顶点布局的网格对象,而无需花费大量精力(例如,为每个顶点布局定义一个渲染器类)。下面可以看到一些不同顶点格式的示例 enum EVertexFormat { VERTEX_FORMAT_UNDEFINED = -1, VERTEX_FORMAT_P1 = 0, VERTEX_FORMAT_P1N1, VERTEX_FORMAT_P1N1UV, VERTEX_FORMAT_P1N1C1, VERTEX_FORMA

我正在寻找一种很好的方法来渲染具有不同顶点布局的网格对象,而无需花费大量精力(例如,为每个顶点布局定义一个渲染器类)。下面可以看到一些不同顶点格式的示例

enum EVertexFormat
{
    VERTEX_FORMAT_UNDEFINED = -1,
    VERTEX_FORMAT_P1 = 0,
    VERTEX_FORMAT_P1N1,
    VERTEX_FORMAT_P1N1UV,
    VERTEX_FORMAT_P1N1C1,
    VERTEX_FORMAT_P1N1UVC1,
};

// the simplest possible vertex -- position only 
struct SVertexP1
{
    math::Vector3D m_position;      // position of the vertex
};

struct SVertexP1N1
{
    math::Vector3D m_position;      // position of the vertex
    math::Vector3D m_normal;        // normal of the vertex
};

// a typical vertex format with position, vertex normal
// and one set of texture coordinates
struct SVertexP1N1UV
{
    math::Vector3D m_position;      // position of the vertex
    math::Vector3D m_normal;        // normal of the vertex
    math::Vector2D m_uv;            // (u,v) texture coordinate
};

struct SVertexP1N1C1
{
    math::Vector3D m_position;      // position of the vertex
    math::Vector3D m_normal;        // normal of the vertex
    uint32_t m_color_u32;           // color of the vertex
};

struct SVertexP1N1UVC1
{
    math::Vector3D m_position;      // position of the vertex
    math::Vector3D m_normal;        // normal of the vertex
    math::Vector2D m_uv;            // (u,v) texture coordinate
    uint32_t m_color_u32;           // color of the vertex
};

背景是,我想渲染不同的对象。其中一些是不拥有纹理坐标或法线的基本体(例如平面、球体)。另一方面,我想渲染具有法线、纹理坐标等的更复杂对象。是否有一种聪明的方法或设计来避免编程多个渲染器类,而是使用单个渲染器类?我知道,这也会影响着色器。

您可以为每个顶点结构提供一个静态方法,可能称为
EnableVertexAttributeArray
或类似方法。在此静态方法中,假设绑定了正确的数组缓冲区,则在
GL\u ARRAY\u BUFFER
中设置顶点布局

struct SVertexP1N1
{
    math::Vector3D m_position;      // position of the vertex
    math::Vector3D m_normal;        // normal of the vertex
    static void EnableVertexAttribArray()
    {
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SVertexP1N1), (const GLvoid*)offsetof(SVertexP1N1, m_position));
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(SVertexP1N1), (const GLvoid*)offsetof(SVertexP1N1, m_normal));
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
    }
};
然后,可以基于顶点结构创建顶点缓冲区的模板类。比如说,

template <class VertexType> class vertex_buffer
{
public:
    typedef VertexType vertex_type;

    vertex_buffer()
    {
        glGenVertexArrays(1, &m_vao);
        glGenBuffers(1, &m_vbo);
        glGenBuffers(1, &m_ibo);
        glBindVertexArray(m_vao);
        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
        vertex_type::EnableVertexAttribArray(); // <--------
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }

    ~vertex_buffer()
    {
        glDeleteVertexArrays(1, &m_vao);
        glDeleteBuffers(1, &m_vbo);
        glDeleteBuffers(1, &m_ibo);
    }

    // ...

    void draw()
    {
        glBindVertexArray(m_vao);
        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
        glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, NULL);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }

private:
    GLuint m_vao;
    GLuint m_vbo;
    GLuint m_ibo;
    std::vector<vertex_type> m_vertices;
    std::vector<GLuint> m_indices;
}
模板类顶点\u缓冲区
{
公众:
typedef VertexType vertex_type;
顶点缓冲区()
{
Glgenvertexarray(1和m_vao);
glGenBuffers(1和m_vbo);
glGenBuffers(1和m_ibo);
glBindVertexArray(m_vao);
glBindBuffer(GL_数组_BUFFER,m_vbo);
glBindBuffer(GL_元素数组缓冲区,m_ibo);
顶点类型::EnableVertexAttributeArray()//