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 2 of 2

Thread: Nothing visible when I try to render a triangle with a vertex buffer.

  1. #1
    Newbie Newbie
    Join Date
    May 2014
    Posts
    1

    Nothing visible when I try to render a triangle with a vertex buffer.

    I have trying to render a vertex buffer to the screen to draw a triangle, but nothing is visible. I have a fragment and a vertex shader loaded without problem.

    Code for creating vertex buffers:
    Code :
    	// Create the vertex array.
    	glGenVertexArrays(1, &vertexArray);
    	glBindVertexArray(vertexArray);
     
    	// Create the vertex buffer.
    	glGenBuffers(1, &vertexBuffer);
     
    	// Set up the color buffers.
    	glGenVertexArrays(1, &colorArray);
    	glBindVertexArray(colorArray);
     
    	glGenBuffers(1, &colorBuffer);

    Code for rendering vertex buffer:
    Code :
    	// Vertex data.
    	glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    	glBufferData(GL_ARRAY_BUFFER, BUFFER_SIZE * 2 * sizeof(GLfloat), vertexData, GL_STATIC_DRAW);
    	glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    	glEnableVertexAttribArray(0);
     
    	// Color data.
    	glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
    	glBufferData(GL_ARRAY_BUFFER, BUFFER_SIZE * 2 * sizeof(GLfloat), colorData, GL_STATIC_DRAW);
    	glVertexAttribPointer((GLuint)1, 4, GL_FLOAT, GL_FALSE, 0, 0);
    	glEnableVertexAttribArray(0);
     
    	// See what kind of drawing the user wants to do.
    	int mode;
    	switch (type)
    	{
    	case TYPE_TRIANGLE:
    		mode = GL_TRIANGLES;
    		break;
    	case TYPE_LINE:
    		mode = GL_LINE;
    		break;
    	case TYPE_POINTS:
    		mode = GL_POINTS;
    		break;
    	default:
    		mode = GL_TRIANGLES;
    		break;
    	}
     
    	// Now we can draw the buffer to the screen.
    	glBindVertexArray(vertexArray);
    	glEnable(GL_BLEND);
    	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    	glDrawArrays(mode, 0, BUFFER_SIZE);
    	glBindVertexArray(0);

    I am some what new to OpenGL having only used DirectX 9 in the past, any ideas on what is going on?

  2. #2
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    You may want to find a good example on using vertex array objects (VAO) and vertex buffer objects (VBO). I'll try and walk though some of the problems with your code, but I may not cover everything in full detail:

    • In your setup code, you create two VAOs (glGenVertexArray calls), and two VBOs (glGenBuffers). For what you're trying to do, you need one VAO, and two VBOs. A VAO encodes the entire vertex state for a draw call (which buffer each attribute comes from, the type and number of components for each attribute, strides, etc). You need one of those to capture all your state. The VBOs are the containers for the actual vertex data. You could store your positions and colors interleaved in the same VBO, but for now it's probably simpler if you create one VBO for the positions, and one VBO for he colors.
    • In your glBufferData call for the positions, you multiply by 2 to calculate the size, suggesting that you have two coordinates for your positions. But then you pass 3 as the second argument to glVertexAttribPointer, suggesting that you have 3 coordinates. Those two values have to be the same, and match your position data.
    • In your glBufferData call for the colors, you multiply by 2 to calculate the size, suggesting that you have two components for your colors. But then you pass 4 as the second argument to glVertexAttribPointer, suggesting that you have 4 components. Those two values have to be the same, and match your color data.
    • You call glEnableVertexAttribArray with the argument 0 twice. So you enable attribute 0 twice, but never enable attribute 1.
    • Just before you draw, you call glBindVertexArray(vertexArray). This wipes out all the state you just set up, and replaces it with the state in vertexArray. You want to have one VAO, and bind it before you start setting up your state.

    You can in fact set up all this state during setup, and then when you want to render, you only have to bind the VAO again, and make your draw call.

Posting Permissions

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