Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: VBO not working correctly?

  1. #1
    Newbie Newbie
    Join Date
    Aug 2014
    Posts
    2

    VBO not working correctly?

    I'm having a strange problem, and I don't know what could be causing it. My current code is identical to how I've done this before. I'm trying to render a rectangle using VBO and orthographic projection.

    Click image for larger version. 

Name:	Uxln8.jpg 
Views:	19 
Size:	6.9 KB 
ID:	1403

    Vbo code

    Code :
    /*
        Vertex Buffer
    */
    typedef struct _Vertex {
        float x, y, z, w;
        float u, v;
    } Vertex;
     
    void createVertexBuffer(VertexBuffer *vertexBuffer, Vertex *vertices) {
        glGenBuffers(1, &vertexBuffer->bufferObj);
        glGenVertexArrays(1, &vertexBuffer->vertexArray);
        glBindVertexArray(vertexBuffer->vertexArray);
     
        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->bufferObj);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 6, &vertices[0], GL_STATIC_DRAW);
     
        const unsigned int uvOffset = sizeof(float) * 4;
     
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)uvOffset);
     
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
     
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }
     
    void destroyVertexBuffer(VertexBuffer *vertexBuffer) {
        glDeleteBuffers(1, &vertexBuffer->bufferObj);
        glDeleteVertexArrays(1, &vertexBuffer->vertexArray);
    }
     
    void bindVertexBuffer(VertexBuffer *vertexBuffer) {
        glBindVertexArray(vertexBuffer->vertexArray);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->bufferObj);
    }
     
    void drawVertexBufferMode(GLenum mode) {
        glDrawArrays(mode, 0, 6);
    }
     
    void drawVertexBuffer() {
        drawVertexBufferMode(GL_TRIANGLES);
    }
     
    void unbindVertexBuffer() {
        glBindVertexArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    Setup the rectangle

    Code :
        Vertex rectangle[6];
        VertexBuffer vbo;
        rectangle[0] = (Vertex){0.f, 0.f, 0.f, 1.f, 0.f, 0.f}; // Top left
        rectangle[1] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Top right
        rectangle[2] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
        rectangle[3] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Top left
        rectangle[4] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
        rectangle[5] = (Vertex){3.f, 3.f, 0.f, 1.f, 1.f, 1.f}; // Bottom right
     
        createVertexBuffer(&vbo, &rectangle);

  2. #2
    Intern Newbie
    Join Date
    Apr 2014
    Posts
    47
    Code :
        Vertex rectangle[6];
        VertexBuffer vbo;
        rectangle[0] = (Vertex){0.f, 0.f, 0.f, 1.f, 0.f, 0.f}; // Top left
        rectangle[1] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Top right
        rectangle[2] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
        rectangle[3] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Top left
        rectangle[4] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
        rectangle[5] = (Vertex){3.f, 3.f, 0.f, 1.f, 1.f, 1.f}; // Bottom right
     
        createVertexBuffer(&vbo, &rectangle);
    Your first triangle is wound counter clockwise and your second is wound clockwise. If you enable face culling this is probably the issue because one of the two will be culled.

    Try:
    Code :
        rectangle[0] = (Vertex){0.f, 0.f, 0.f, 1.f, 0.f, 0.f}; // Bottom left
        rectangle[1] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Bottom right
        rectangle[2] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Top left
        rectangle[5] = (Vertex){3.f, 3.f, 0.f, 1.f, 1.f, 1.f}; // Top right
        rectangle[3] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Bottom right
        rectangle[4] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Top left

    On an unrelated issue, if you don't supply a w-coordinate, openGL will assume 1.

  3. #3
    Newbie Newbie
    Join Date
    Aug 2014
    Posts
    2
    Thanks for the suggestion, unfortunately it doesn't seem to have an effect. I tried reordering the vertices such that they are wound similarly, but that didn't work. I also tried to glDisable(GL_CULL_FACE) with no effect. I also have depth testing disabled.

    Code :
        rectangle[0] = (Vertex){0.f, 0.f, 0.f, 1.f, 0.f, 0.f}; // Top left
        rectangle[1] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Top right
        rectangle[2] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
        rectangle[3] = (Vertex){3.f, 3.f, 0.f, 1.f, 1.f, 0.f}; // Bottom right
        rectangle[4] = (Vertex){3.f, 0.f, 0.f, 1.f, 0.f, 1.f}; // Top left
        rectangle[5] = (Vertex){0.f, 3.f, 0.f, 1.f, 1.f, 1.f}; // Bottom left

    or

    Code :
        rectangle[0] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 0.f}; // Top right
        rectangle[1] = (Vertex){0.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Top left
        rectangle[2] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
        rectangle[3] = (Vertex){3.f, 3.f, 0.f, 1.f, 1.f, 0.f}; // Bottom right
        rectangle[4] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
        rectangle[5] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 1.f}; // Top right

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •