OpenGL - VAO, VBO, IBO, glDrawElements not displaying

我的未来我决定 提交于 2021-02-07 13:44:59

问题


I am having trouble getting my level data to appear on the screen. I have my shader in use rendering a cube correctly but not the level.

Here is the setup for my VBO, VAO and IBO:

void ZoneMesh::buildData()
{
    // Create the VBO for this mesh
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // Create the IBO
    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, numPoly * 3 * sizeof(short), indices, GL_STATIC_DRAW);

    // Create the VAO
    glGenVertexArraysAPPLE(1, &vao);
    glBindVertexArrayAPPLE(vao);

    // Bind the VBO to the buffer and set up the attributes
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float)*3));
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float)*5));

    //Bind the IBO to the VAO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
}

Here is my vertex structure:

struct Vertex
{
    float x;
    float y;
    float z;
    float u;
    float v;
    float normX;
    float normY;
    float normZ;
};

Here are the relevant data items in the ZoneMesh class:

Vertex* vertices;
short* indices;

GLuint vbo;
GLuint vao;
GLuint ibo;

Vertex shader:

#version 120

attribute vec3 position;

uniform mat4 camera;

void main()
{
    gl_Position = camera * vec4(position, 1.0f);
}

Fragment shader:

#version 120

void main(void)
{
    gl_FragColor = vec4(0.0, 0.6, 0.7, 1.0);
}

Rendering:

    shader.Use();

    // Testing - render the first 50 meshes
    for(int i = 0; i < 50; i++)
    {

        glUniformMatrix4fv(shader("camera"), 1, GL_FALSE, glm::value_ptr(MVPMatrix));

        glEnableVertexAttribArray(shader["position"]);
        glBindVertexArrayAPPLE(zone.getVAO(i));
        glDrawElements(GL_TRIANGLES, 500, GL_UNSIGNED_SHORT, NULL);

    }

    shader.UnUse();

The rendering/shader use is not the problem. The MVPMatrix is correct. I have a cube rendering correctly above it. The zone does not render though.


回答1:


GL_LINE is not a valid primitive for glDrawElements, you want GL_LINES.

Use glGetError() in your code to find these kinds of problems!




回答2:


I had the same problem... Have a look there: what is the role of glbindvertexarrays... following step by steps all the Gen/Bind/Data make it work. Further-more, it solved the confusion between the various dialects introduced by several versions of OpenGL...



来源:https://stackoverflow.com/questions/11662030/opengl-vao-vbo-ibo-gldrawelements-not-displaying

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!