繁体   English   中英

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

[英]OpenGL: Two Vertex Arrays + Two Index Arrays

我试图从一个简单的弹簧质量系统的动画开始学习现代的openGL。

在我的课堂上,我有一个弹簧和一个质量,每个都使用一个索引数组进行绘制,当我分别进行操作时,它们都可以正常工作。 当我将两者绘制在一起时,顶点位置都是错误的。 我猜想这与我对顶点缓冲区和索引数组的编号方式有关,但是我不了解内部的工作原理以至于无法弄清楚。

#include "Spring.hpp"
#include "ShaderPaths.hpp"
#include "atlas\gl\Shader.hpp"
#include "atlas\core\Macros.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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM