1

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

这是生成 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) 因为它没有做任何事情。

4

1 回答 1

1

好的,我修好了。这不是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));
}
于 2015-12-12T10:04:18.557 回答