LunchMarble
LunchMarble

Reputation: 5141

How can I draw a triangle with a VBO?

I am trying to get my VBO to draw, but I can't see anything. I am attempting to draw a single triangle (seems to me that a single triangle is a good start in the right direction). Everything compiles and runs without breaking.

void initGraphics(int width, int height) {
    glViewport(0, 0, width, height);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, width, height, 0.0);
    glMatrixMode(GL_MODELVIEW);
}

GLuint vboId;

void initVbo(void) {
    GLsizei dataSize;
    GLfloat* vertices;
    int vCount = 3;

    dataSize = sizeof(GLfloat) * 3 * vCount;

    vertices = (GLfloat*)malloc(dataSize);

    vertices[0] = 0;
    vertices[1] = 0;
    vertices[2] = 0;

    vertices[3] = 100;
    vertices[4] = 0;
    vertices[5] = 0;

    vertices[6] = 100;
    vertices[7] = 100;
    vertices[8] = 0;

    glewInit();

    glGenBuffersARB(1, &vboId);

    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);

    glBufferDataARB(GL_ARRAY_BUFFER_ARB, dataSize, vertices, GL_STATIC_DRAW_ARB);

    free(vertices);

    //glDeleteBuffersARB(1, &vboId); // edit #1
}

unsigned int indices[] = { 0, 1, 2 }; // edit #3

void drawVbo(void) {

    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1.0, 1.0, 1.0, 1.0);

    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, 0);
//edit #1, #2
    glDrawElements(GL_TRIANGLES, 3 /*1*/, /*GL_UNSIGNED_BYTE*/ GL_UNSIGNED_INT, &indices);

    glDisableClientState(GL_VERTEX_ARRAY);

    glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); 
}

These are the functions that are getting called. I am running on windows and I am avoiding posting a bunch of windows related code...

EDIT #1

Modified the code to reflect genpfault's suggestions.

EDIT #2

Modified the code to reflect Nicol Bolas' suggestions.

COMMENT #1

The following code works (just to prove projections are set up correct):

glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(100.0, 0.0, 0.0);
glVertex3f(100.0, 100.0, 0.0);
glEnd();

EDIT #3

Modified the code to reflect Nicol Bolas' suggestions.

UPDATE #1

The modified code now works. Although I am curious how to get glDrawArrays to work properly...my implementation looked like:

glDrawArrays(GL_TRIANGLES, 0, 0);

This doesn't seem right to me, but the spec says:

mode: Specifies what kind of primitives to render. Symbolic constants GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_QUAD_STRIP, GL_QUADS, and GL_POLYGON are accepted.

first: Specifies the starting index in the enabled arrays.

count: Specifies the number of indices to be rendered.

Based on what Nicol Bolas was saying, since I shouldn't need indices, 0, 0 make sense as arguments. Right?

Upvotes: 1

Views: 1644

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 473407

 glDrawElements(GL_TRIANGLES, 1, GL_UNSIGNED_INT, &indices);

Triangles, as you may be aware, are made of three vertices. You are sending one (the second parameter). You can't draw a triangle from one position.

Upvotes: 2

genpfault
genpfault

Reputation: 52082

glGenBuffersARB(1, &vboId);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, dataSize, vertices, GL_STATIC_DRAW_ARB);

free(vertices);
glDeleteBuffersARB(1, &vboId);  // wat

After the glDeleteBuffersARB() call the pointed-to VBO ID(s) are invalidated and can't be used in glBindBufferARB().

Also:

glDrawElements(GL_TRIANGLES, 1, GL_UNSIGNED_BYTE, &indices);

You've defined indices as a unsigned int so you should use GL_UNSIGNED_INT instead of GL_UNSIGNED_BYTE.

Upvotes: 3

Related Questions