1
\$\begingroup\$

I'm going insane trying to work out why I unable to draw a triangle using VAO and indexing. I have a bunch of redundant vertexes so that i can switch my index array up to test the drawing.

std::vector<glm::vec3> vertices;

//bot left
vertices.push_back(glm::vec3(-0.75,0.9,1));     //0
vertices.push_back(glm::vec3(-0.5, 0.9, 1));    //1
vertices.push_back(glm::vec3(-0.25, 0.9, 1));   //2
vertices.push_back(glm::vec3(-0.0, 0.9, 1));    //3
//mid right
vertices.push_back(glm::vec3(0.75, 0, 1));  //4 
vertices.push_back(glm::vec3(0.5, 0, 1));   //5
vertices.push_back(glm::vec3(0.25, 0, 1));  //6
vertices.push_back(glm::vec3(0.0, 0, 1));   //7
//top left
vertices.push_back(glm::vec3(-0.75, -0.9, 1));  //8
vertices.push_back(glm::vec3(-0.5, -0.9, 1));   //9
vertices.push_back(glm::vec3(-0.25, -0.9, 1));  //10
vertices.push_back(glm::vec3(-0.0, -0.9, -1));  //11

The indices are simple:

 GLuint indices[] = {0,5,11};

Here is how I am initialising everything:

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ivbo);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
    &vertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(glm::vec3), (void*)0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ivbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

I am rendering with:

glUseProgram(program);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 0.0, 0.0, 1.0);//clear red

glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)0);

glutSwapBuffers();

Vertex shader is simple as they come:

#version 430 core
layout(location = 0) in vec3 in_position;

void main(void)
{
   gl_Position = vec4(in_position, 1);
}

Same with the fragment shader

#version 430 core
out vec4 color;

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

EDIT: Switching the z coordinate in the vertices to a negative value fixes the problem. What is going on?

\$\endgroup\$
4
  • \$\begingroup\$ Do you have backface culling on? \$\endgroup\$
    – Bálint
    Oct 21, 2016 at 14:25
  • \$\begingroup\$ Well if it was before it is not now. Unfortunately it has not fixed the issue. \$\endgroup\$
    – Zwander
    Oct 21, 2016 at 14:40
  • \$\begingroup\$ Did you fix sizeof(indices)? Also in bytes. \$\endgroup\$
    – 3Dave
    Oct 21, 2016 at 18:14
  • \$\begingroup\$ sizeof(indices) is now sizeof(GLuint)*indiciesSize \$\endgroup\$
    – Zwander
    Oct 21, 2016 at 22:29

1 Answer 1

2
\$\begingroup\$

First to ensure all vertices are rendered:

  • Ensure that backface culling is disabled with GlDisable(GL_CULL_FACE)
  • Disable depth testing glDisable(GL_DEPTH_TEST) OR ensure you are clearing the depth buffer bit before drawing glClear(GL_DEPTH_BUFFER_BIT)

Second

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),&vertices[0], GL_STATIC_DRAW);

is incorrect.

Second parameter to glBufferData is size of vertex array in bytes. Passing sizeof(vertices) will pass the 24 bytes (32 on debug) that indicate the pointer to the start location of the vector and the end location of the vector.

Instead you should use:

sizeof(glm::vec3) * vertices.size()

The same thing happens with the size you pass size to the ELEMENTS_BUFFER .


glBufferData's second argument is the size in Bytes of the array you are passing. When it say's size in Bytes it means the whole size of your vertices vector.

So in this specific scenario you have stored 12 positions. Every one of those elements is a float number as we can see and every position consists of three float numbers.

"Coding" this would be

sizeof(float) * 3 * vertices.size();
  • Normally sizeof(float) will return 4 which are the bytes for a float-number.
  • vertices.size() will return 12 since you have 12 positions stored.
  • and 3 is just a constant indicating that for every position you have 3 float-numbers.

So the size in bytes that you will have to pass as an argument to glBufferData is 4 * 3 * 12 = 144Bytes.

You need to do the same for the Elements buffer.

\$\endgroup\$
5
  • \$\begingroup\$ Hmmm... Nope. Not even the first triangle draws now. \$\endgroup\$
    – Zwander
    Oct 21, 2016 at 14:36
  • \$\begingroup\$ I'm pretty sure sizeof(std::vector<...>) doesn't do what you want. \$\endgroup\$
    – Quentin
    Oct 21, 2016 at 14:37
  • \$\begingroup\$ Yes you are right, i tried to give a quick one but as a said i am in a bit of hurry. I tried to give an example of what size should look like, your glBufferData size parameter is wrong anyways fix it and i think your problem should go away/.. \$\endgroup\$
    – mkanakis
    Oct 21, 2016 at 14:51
  • \$\begingroup\$ sizeof(std::vector<glm::vec3>) * vertices.size() should work but it doesnt... sizeof(std::vector<glm::vec3>)' returns 12 bytes as it should. Weirdly sizeof (vertices)` returns less bytes than sizeof(std::vector<glm::vec3>) * vertices.size(). Getting nothing being drawn. \$\endgroup\$
    – Zwander
    Oct 21, 2016 at 22:25
  • \$\begingroup\$ I am editing my answer, to cover your question. \$\endgroup\$
    – mkanakis
    Oct 22, 2016 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .