PDA

View Full Version : VBO not working correctly?



rcapote
08-21-2014, 02:16 PM
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.

1403

Vbo 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



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);

MtRoad
08-21-2014, 04:59 PM
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:


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.

rcapote
08-23-2014, 10:30 AM
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.



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



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