c++ - OpenGL在不同的VBO中绘制具有不同颜色、顶点位置和顶点颜色的点?

标签 c++ opengl glsl glm-math vbo

我想用不同的颜色绘制不同的点。我将顶点位置数据和颜色数据放在不同的VBO中,如下所示:

这是我的 C++ 代码:

    m_Points.push_back(glm::vec3(4, 0, 0));
    m_Points.push_back(glm::vec3(0, 2, 0));
    m_Points.push_back(glm::vec3(0, 0, 3));
    m_Points.push_back(glm::vec3(0, 0, 6));

    m_Colors.push_back(glm::vec3(0, 1.0, 0));
    m_Colors.push_back(glm::vec3(1.0, 0, 0));
    m_Colors.push_back(glm::vec3(0, 0, 1.0));
    m_Colors.push_back(glm::vec3(1.0, 1.0, 0));

    glEnable(GL_PROGRAM_POINT_SIZE);


    glGenVertexArrays(1, &m_VAO);
    glBindVertexArray(m_VAO);

    // the first VBO, it is the coordinates
    glGenBuffers(1, &m_VBO);
    glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * sizeof(m_Points), &m_Points[0][0], GL_DYNAMIC_DRAW);

    // the second VBO, the color
    glGenBuffers(1, &m_VBO_Color);
    glBindBuffer(GL_ARRAY_BUFFER, m_VBO_Color);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * sizeof(m_Colors), &m_Colors[0][0], GL_DYNAMIC_DRAW);

    glEnableVertexAttribArray(0);
    // in file axis.vs, there is a statement
    // layout (location = 0) in vec3 vertex;
    // the first argument 0 means (location = 0)
    glVertexAttribPointer(0,                 // location = 0 in vertics shader file
                          3,                 // the position has X,Y,Z 3 elements
                          GL_FLOAT,          // element type
                          GL_FALSE,          // do not normalize
                          sizeof(glm::vec3), // Stride
                          (void *) nullptr); // an offset of into the array, it is 0


    glEnableVertexAttribArray(1);
    // in file axis.vs, there is a statement
    // layout (location = 1) in vec3 vertex;
    // the first argument 1 means (location = 1)
    glVertexAttribPointer(1,                 // location = 1 in vertics shader file
                          3,                 // the position has R,G,B 3 elements
                          GL_FLOAT,          // element type
                          GL_FALSE,          // do not normalize
                          sizeof(glm::vec3), // Stride
                          (void *) nullptr); // an offset of into the array, it is 0


    glBindVertexArray(0);

m_Points 和 m_Colors 都是某种 std::vector<glm::vec3>

这是我的顶点着色器和帧着色器:

layout (location = 0) in vec3 vertex;
layout (location = 1) in vec3 color;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

out vec3 ObjectColor;

void main()
{
    gl_Position = projection * view * model * vec4(vertex, 1.0);
    gl_PointSize = 10.0;
    ObjectColor = color;

}
#version 330 core

in vec3 ObjectColor;

out vec4 FragColor;

void main()
{
    FragColor = vec4(ObjectColor, 1.0f);
}

绘制代码如下所示:

    shader.use();
    glBindVertexArray(m_VAO);
    glDrawArrays(GL_POINTS, 0, m_Points.size());

但我想通过 AddPoint() 动态添加一些点函数,所以这里是在 vector 中添加顶点和颜色元素的代码:

void AddPoint() 
{
    glm::vec3 p(5.0, 6.0, 0.0);    
    m_Points.push_back(p);
    glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * m_Points.size(), &m_Points[0][0], GL_DYNAMIC_DRAW);

    glm::vec3 b(1.0, 0.0, 0.0);
    m_Colors.push_back(b);
    glBindBuffer(GL_ARRAY_BUFFER, m_VBO_Color);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * m_Colors.size(), &m_Colors[0][0], GL_DYNAMIC_DRAW);
}

问题是:最初的 4 个点以 4 种不同的颜色正确显示,但 AddPoint() 永远不起作用,我没有看到任何添加和渲染的新点。

我知道如果我将顶点位置放在单个 VBO 中,

如:

日期数组可以是:

x, y, z, r, g, b
x, y, z, r, g, b
x, y, z, r, g, b
x, y, z, r, g, b

它可以正常工作,没有任何问题。

但是为什么如果我把位置和颜色放在不同的VBO中却不起作用呢?

有什么想法吗?谢谢。

这个问题( c++ - Storing different vertex attributes in different VBO's - Stack Overflow )是相关的,但我仍然不知道为什么我的问题仍然发生。

编辑 Rabbid76 指出glVertexAttribPointer()函数仅通过 glBindBuffer 附加(复制)来自先前绑定(bind) VBO 的数据。这表明我不能将顶点位置数组和顶点颜色数组放在不同的 VBO 中?

编辑2 是我的AddPoint()这里功能错误?

最佳答案

glVertexAttribPointer将绑定(bind)到 GL_ARRAY_BUFFER 目标的缓冲区对象关联到指定的属性。该规范存储在当前Vertex Array Object的状态 vector 中。 .
正确的缓冲区对象必须由 glBindBuffer 绑定(bind),之前 glVertexAttribPointer被调用:

// the first VBO, it is the coordinates
glGenBuffers(1, &m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * sizeof(m_Points), &m_Points[0][0], GL_DYNAMIC_DRAW);

// the second VBO, the color
glGenBuffers(1, &m_VBO_Color);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO_Color);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * sizeof(m_Colors), &m_Colors[0][0], GL_DYNAMIC_DRAW);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO); // <--- bind `m_VBO`

glVertexAttribPointer(0,                 // location = 0 in vertics shader file
                      3,                 // the position has X,Y,Z 3 elements
                      GL_FLOAT,          // element type
                      GL_FALSE,          // do not normalize
                      sizeof(glm::vec3), // Stride

glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO_Color); // <--- bind `m_VBO_Color`

glVertexAttribPointer(1,                 // location = 1 in vertics shader file
                      3,                 // the position has R,G,B 3 elements
                      GL_FLOAT,          // element type
                      GL_FALSE,          // do not normalize
                      sizeof(glm::vec3), // Stride
                      (void *) nullptr); // an offset of into the array, it is 0

关于c++ - OpenGL在不同的VBO中绘制具有不同颜色、顶点位置和顶点颜色的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63616715/

相关文章:

c++ - 未能构建 codeblocks smith wxWidget 项目

iphone - 在 objective-c 中编写一个简单的 c++ 函数

c++ - CreateWindowEx WS_POPUP 为什么要打边框?

c++ - Qt 5 的多个 OpenGL 视口(viewport)

opengl - 标准化值意味着什么?

javascript - 图像处理后更新制服或从片段着色器获取数据

C++派生类链构造函数错误

c++ - 如何像编译器一样获取每一个虚函数索引?

c++ - OpenGL:如果您无论如何都必须绑定(bind)目标,那么 "named"缓冲区函数有什么意义?

java - GLSL 移动点光源