OpenGL- glDrawElements只绘制第一个元素

时间:2015-12-11 23:48:00

标签: c++ opengl vbo vao

大家好,我正在为我的游戏制作一个小型3D引擎,我为顶点数组和VBO创建了一个类。它一直在工作,直到我试图向顶点数组添加索引。现在它只绘制索引的第一个元素!

这是函数build,它生成VAO和VBO。

void build() {
vboID = new GLuint[vboS];

if (vertexIndexing) {
    for (int n = 0, m = 0, i = 0; n < vertexes.size(); m++) {
        if (i + m >= vertexes.size()) {
            break;
        }
        indices.push_back((GLushort)(i+m));
        if (m >= s) {
            m = 0;
            n++;
            i+=s/2;
            std::cout << "\n";
        }
        std::cout << i + m << " , ";
    }
    std::cout << "\n";
}

shader->use();

glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glGenBuffers(vboS, vboID);

if (vertexIndexing) {
    glGenBuffers(1, &IBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort), &indices[0], GL_STATIC_DRAW);
}

int n = 0;

if (options.vertex) {
    shader->addAttribute("vertex");
    glBindBuffer(GL_ARRAY_BUFFER, vboID[n]);
    glBufferData(GL_ARRAY_BUFFER, pos.size() * sizeof(GLfloat), &pos[0], GL_STATIC_DRAW);
    glVertexAttribPointer(shader->attribute("vertex"), 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(shader->attribute("vertex"));
    n++;
}

if (options.color) {
    shader->addAttribute("color");
    glBindBuffer(GL_ARRAY_BUFFER, vboID[n]);
    glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(GLfloat), &colors[0], GL_STATIC_DRAW);
    glVertexAttribPointer(shader->attribute("color"), 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(shader->attribute("color"));
    n++;
}

if (options.normal) {
    shader->addAttribute("normal");
    glBindBuffer(GL_ARRAY_BUFFER, vboID[n]);
    glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(GLfloat), &normals[0], GL_STATIC_DRAW);
    glVertexAttribPointer(shader->attribute("normal"), 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(shader->attribute("normal"));
    n++;
}

if (options.texcoord) {
    shader->addAttribute("texcoord");
    glBindBuffer(GL_ARRAY_BUFFER, vboID[n]);
    glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(GLfloat), &texcoords[0], GL_STATIC_DRAW);
    glVertexAttribPointer(shader->attribute("texcoord"), 2, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(shader->attribute("texcoord"));
    n++;
}
glBindVertexArray(0);
shader->disable();

}

这是绘制VAO的函数draw。

void draw() {
glBindVertexArray(vaoID);
if (vertexIndexing) {
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    shader->use();
    glDrawElements(mode, indices.size(), GL_UNSIGNED_SHORT, 0);
    shader->disable();
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
else {
    shader->use();
    glDrawArrays(mode, 0, vertexes.size());
    shader->disable();
}
glBindVertexArray(0);

}

注意:我删除了glEnableClientState(GL_VERTEX_ARRAY),因为它没有做任何事情。

1 个答案:

答案 0 :(得分:0)

好的,我修好了。这不是openGL的问题。 这是指数的产生。

新指数产生:

for (int n = 0, m = 0, i = 0; n < vertexes.size(); m++) {
    if (i + m >= vertexes.size()) {
        break;
    }
    if (m >= s) {
        m = 0;
        n++;
        i+=s/2;
    }
    indices.push_back((GLushort)(i + m));
}