OpenGL:两个顶点数组 + 两个索引数组



我正在尝试通过一个简单的弹簧质量系统的动画开始学习现代openGL。

我班上有一个弹簧和一个质量,每个都使用索引数组来绘制,当我单独使用它们时,它们都可以完美地工作。当我将两者绘制在一起时,顶点位置都是错误的。我猜这与我如何对顶点缓冲区和索引数组等进行编号有关,但我对内部工作的理解不足以弄清楚。

#include "Spring.hpp"
#include "ShaderPaths.hpp"
#include "atlasglShader.hpp"
#include "atlascoreMacros.hpp"
#include <atlas/utils/Geometry.hpp>
const int NUM_VERTICES_PER_LINE = 3;
const int NUMFLOATSPERVERTICES = 6;
const int VERTEX_BYTE_SIZE = NUMFLOATSPERVERTICES * sizeof(float);
GLint numSpringIndices, numMassIndices;
#define NUM_ARRAY_ELEMENTS(a) sizeof(a) / sizeof(*a);
Spring::Spring() : anchorPosition{ 0.0f, 1.0f, 0.0f }, mPosition{0.0f, 0.0f, 0.0f} {
    USING_ATLAS_GL_NS; //Short for atlas GL namespace
    USING_ATLAS_MATH_NS;
    glGenVertexArrays(1, &mVertexArrayObject);
    glBindVertexArray(mVertexArrayObject);

    ShapeData Spring = ObjectGenerator::makeSpring(anchorPosition, stretch, d);
    ShapeData Mass = ObjectGenerator::makeMass(calculateConnectionPoint(anchorPosition), massWidth, massHeight);
    ShapeData Triangle = ObjectGenerator::makeTriangle();
    numSpringIndices = Spring.numIndices;
    numMassIndices = Mass.numIndices;
    //======= Spring Buffer ======//
    glGenBuffers(1, &mSpringBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, mSpringBuffer);
    glBufferData(GL_ARRAY_BUFFER, Spring.vertexBufferSize(), Spring.vertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (char*)(sizeof(float) * 3));
    glGenBuffers(1, &springIndexBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, springIndexBufferID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, Spring.indexBufferSize(), Spring.indices, GL_STATIC_DRAW);
    //======= Mass Buffer ======//
    glGenBuffers(1, &mMassBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, mMassBuffer);
    glBufferData(GL_ARRAY_BUFFER, Mass.vertexBufferSize(), Mass.vertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (char*)(sizeof(float) * 3));
    glGenBuffers(1, &massIndexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, massIndexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, Mass.indexBufferSize(), Mass.indices, GL_STATIC_DRAW);
    //======= Shaders ======//
    std::string shaderDir = generated::ShaderPaths::getShaderDirectory();
    std::vector<ShaderInfo> springShaders
    {
        ShaderInfo{ GL_VERTEX_SHADER, shaderDir + "Spring.vs.glsl" },
        ShaderInfo{ GL_FRAGMENT_SHADER, shaderDir + "Spring.fs.glsl" }
    };
    mShaders.push_back(ShaderPointer(new Shader));
    mShaders[0]->compileShaders(springShaders);
    mShaders[0]->linkShaders();
    //===== Clean ups to prevent memory leaks =====//
    Spring.cleanup();
    Mass.cleanup();
}
Spring::~Spring()
{
    glDeleteVertexArrays(1, &mVertexArrayObject);
    glDeleteBuffers(1, &mSpringBuffer);
}
void Spring::renderGeometry(atlas::math::Matrix4 projection, atlas::math::Matrix4 view) {
    // To avoid warnings from unused variables, you can use the 
    //UNUSED macro.
    UNUSED(projection);
    UNUSED(view);
    mShaders[0]->enableShaders();
    glBindVertexArray(mVertexArrayObject);
    GLint dominatingColorUniformLocation = mShaders[0]->getUniformVariable("dominatingColor");
    //========== Draw Spring ===============//
    glm::vec3 springColor(0.0f, 1.0f, 0.0f);
    glUniform3fv(dominatingColorUniformLocation, 1, &springColor[0]); //Send the location of the first float
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, springIndexBufferID);
    glDrawElements(GL_LINES, numSpringIndices, GL_UNSIGNED_SHORT, 0);
    //======================================//
    //========= Draw Mass =================//
    glm::vec3 massColor(1.0f, 0.0f, 0.0f);
    glUniform3fv(dominatingColorUniformLocation, 1, &massColor[0]); //Send the location of the first float
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, massIndexBuffer);
    glDrawElements(GL_TRIANGLES, numMassIndices, GL_UNSIGNED_SHORT, 0);
    //======================================//
    mShaders[0]->disableShaders();
}
void Spring::updateGeometry(atlas::utils::Time const& t) {
    mModel = glm::translate(Matrix4(1.0f), mPosition);
}
glm::vec3 Spring::calculateConnectionPoint(glm::vec3 anchorPosition) {
    glm::vec3 temp = glm::vec3(anchorPosition.x, anchorPosition.y - (18 * d), 0.0f);
    return temp;
}

ObjectGenerator类看起来像这样,适合任何好奇的人

#include "ObjectGenerator.h"
#define NUM_ARRAY_ELEMENTS(a) sizeof(a) / sizeof(*a);
ShapeData ObjectGenerator::makeSpring(glm::vec3 anchorPosition, GLfloat stretch, GLfloat d) {
    ShapeData ret;
    static const Vertex vertices[] = {
        glm::vec3( anchorPosition.x, anchorPosition.y, 0.0f ), // 0
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x, anchorPosition.y - d,0.0f ), // 1
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x + stretch, anchorPosition.y - (2 * d), 0.0f ), // 2
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x - stretch, anchorPosition.y - (4 * d), 0.0f ), // 3
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x + stretch, anchorPosition.y - (6 * d), 0.0f ), // 4
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x - stretch, anchorPosition.y - (8 * d), 0.0f ), // 5
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x + stretch, anchorPosition.y - (10 * d), 0.0f ), // 6
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x - stretch, anchorPosition.y - (12 * d), 0.0f ), // 7
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x + stretch, anchorPosition.y - (14 * d), 0.0f ), // 8
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x - stretch, anchorPosition.y - (16 * d), 0.0f ), // 9
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x, anchorPosition.y - (17 * d), 0.0f ), // 10
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
        glm::vec3( anchorPosition.x, anchorPosition.y - (18 * d), 0.0f ), // 11
        glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
    };
    ret.numVertices = NUM_ARRAY_ELEMENTS(vertices);
    ret.vertices = new Vertex[ret.numVertices];
    memcpy(ret.vertices, vertices, sizeof(vertices)); //memcpy(dest, source, size);
    GLushort indices[] = { 0,1 ,1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,9, 9,10, 10,11 };
    ret.numIndices = NUM_ARRAY_ELEMENTS(indices);
    ret.indices = new GLushort[ret.numIndices];
    memcpy(ret.indices, indices, sizeof(indices));
    return ret;
}
ShapeData ObjectGenerator::makeMass(glm::vec3 connectionPoint, GLfloat width, GLfloat height) {
    ShapeData ret;
    static const Vertex vertices[] = {
        //=================Mass==============//
        glm::vec3( connectionPoint.x - width, connectionPoint.y, 0.0f ), //top Left 0
        glm::vec3( 0.0f, 1.0f, 0.0f ), // Mass Color
        glm::vec3(connectionPoint.x + width, connectionPoint.y, 0.0f), //top Right 1
        glm::vec3(0.0f, 1.0f, 0.0f), // Mass Color
        glm::vec3(connectionPoint.x + width, connectionPoint.y - height, 0.0f), // bottom right 2
        glm::vec3(0.0f, 1.0f, 0.0f), // Mass Color
        glm::vec3( connectionPoint.x - width, connectionPoint.y - height, 0.0f ), // bottom left 3
        glm::vec3( 0.0f, 1.0f, 0.0f ), // Mass Color
    };
    ret.numVertices = NUM_ARRAY_ELEMENTS(vertices);
    ret.vertices = new Vertex[ret.numVertices];
    memcpy(ret.vertices, vertices, sizeof(vertices)); //memcpy(dest, source, size);
    GLushort indices[] = {0,1,3, 1,2,3  };
    ret.numIndices = NUM_ARRAY_ELEMENTS(indices);
    ret.indices = new GLushort[ret.numIndices];
    memcpy(ret.indices, indices, sizeof(indices));
    return ret;
}

您的代码中有几个相当基本的问题,这些问题似乎是基于对 OpenGL 状态管理如何工作以及如何连接不同类型的对象的误解。

说明代码中一个基本问题的最清晰方法是代码中的此序列(省略部分):

//======= Spring Buffer ======//
...
glBindBuffer(GL_ARRAY_BUFFER, mSpringBuffer);
...
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
//======= Mass Buffer ======//
...
glBindBuffer(GL_ARRAY_BUFFER, mMassBuffer);
...
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);

如果您查看这两个glVertexAttribPointer()调用,它们都设置了相同的顶点属性(0)。由于它们都设置了相同的状态,因此第二个"获胜",覆盖您在第一次调用时设置的状态。调用的参数是相同的,但glVertexAttribPointer()也隐式获取当前绑定的GL_ARRAY_BUFFER,数据将来自该。因此,属性 0 将使用来自 mMassBuffer 的数据,而不是来自 mSpringBuffer 的数据。

解决此问题的最佳方法是使用两个顶点数组对象 (VAO)。VAO 跟踪使用 glVertexAttribPointer() 设置的状态。因此,如果您使用两个不同的VAO,其中一个可以跟踪用于弹簧的状态,另一个可以跟踪用于质量的状态。删除当前 VAO 创建/绑定代码后,请执行以下操作:

//======= Spring Buffer ======//
glGenVertexArrays(1, &mSpringVao);
glBindVertexArray(mSpringVao);
...
//======= Spring Buffer ======//
glGenVertexArrays(1, &mMassVao);
glBindVertexArray(mMassVao);
...

然后,在绘制代码中,您不必再绑定任何缓冲区,因为属性的所有状态设置都在相应的 VAO 中跟踪。相反,请在每次绘制调用之前绑定相应的 VAO:

glBindVertexArray(mSpringVao);
glDrawElements(GL_LINES, numSpringIndices, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(mMassVao);
glDrawElements(GL_LINES, numMassIndices, GL_UNSIGNED_SHORT, 0);

当然,您仍然需要设置制服等。

这里的设置代码中还有另一个小但很重要的问题:

glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
glEnableVertexAttribArray(3);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (char*)(sizeof(float) * 3));

此处的两个glEnableVertexAttribArray()调用的值是错误的。它们需要匹配所有其他调用中使用的顶点属性的位置:

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (char*)(sizeof(float) * 3));

在设置相应的绘图ELEMENT_ARRAY_BUFFER之前,代码似乎没有绑定ARRAY_BUFFER。例如,缺少以下内容

glBindBuffer(GL_ARRAY_BUFFER, mMassBuffer); 

最新更新