0

I am trying to experiment and learn how to use multiple VBOs to draw very different objects in OpenGL. The first VBO is just a text renderer, and for now the shader to be used with the main VBO is just a basic vertex shader

attribute vec3 vPosition;

void main()
{
  gl_Position = vec4(vPosition, 1.0);
}

Test Fragment Shader:

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

The code renders the text fine, but as soon as I call UseProgram and bind the VAO, I get a black screen.

Here's my initialization of said VAO:

//random polygon

  points[0]= vec3(-0.5, 0.5, 0.3);
  points[1]= vec3(-0.5, -0.5, 0.2);
  points[2]= vec3(0.5, -0.5, 0.3);
  points[3]= vec3(0.5, 0.5, 0.4);

  //Returns a uInt name for a shader program
  shader = InitShader("vshader.glsl", "fshader.glsl");

  glGenVertexArrays(1, &vao);
  glGenBuffers(1, &buffer);
  glBindVertexArray(vao);
  glBindBuffer(GL_ARRAY_BUFFER, buffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

  GLuint loc = glGetAttribLocation(shader, "vPosition");
  glEnableVertexAttribArray(loc);

  glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), BUFFER_OFFSET(0));
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindVertexArray(0);

  glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white background

Where vao and buffer are previously uninitialized GLuints, and after debugging they do not return -1.

And here is my code to draw

  glUseProgram(shader);
  glBindBuffer(GL_ARRAY_BUFFER, buffer);
  glBindVertexArray(vao);
  glDrawArrays(GL_POLYGON, 0, 4);
  glBindVertexArray(0);
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  • try to cross check with [complete GL+GLSL+VAO/VBO C++ example](https://stackoverflow.com/a/31913542/2521214). Look for `vao_init,vao_exit,vao_draw` in there. – Spektre Dec 03 '20 at 11:30
  • Are you checking the shaders are linking and compiling correctly with shader error logging? – jackw11111 Dec 04 '20 at 01:01

1 Answers1

0

Thanks for all the help everyone, I figured out that I needed to call glFlush() before binding my second VBO and VAO and drawing them.