Search code examples
openglvbovao

Opengl: first vertex always drawn at origin


I got the problem that the first vertex is always drawn at (0,0,0) no matter where I want it to be (all other positions are correct). I think it is a mistake in my 'init' function. Here are the positions I use:

positions.push_back( glm::vec3(-0.5f , -0.5f , 0.0f ) );
positions.push_back( glm::vec3( 0.0f ,  0.5f , 0.0f ) );
positions.push_back( glm::vec3( 0.5f , -0.5f , 0.0f ) );

My 'init' function:

// generate buffers
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);

glBindVertexArray(this->VAO);

// fill buffers with data
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * this->positions.size(), &this->positions[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * this->indices.size(), &this->indices[0], GL_STATIC_DRAW);

// linking vertex attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

// unbind (only) VAO
glBindVertexArray(0);

And my 'render' function (just to render the VAO, no shaders):

glBindVertexArray(this->VAO);
glDrawElements(GL_TRIANGLES, this->numElements, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

I also tried to adjust the 'stride' and 'pointer' values in the 'glVertexAttribPointer' function but it just didn't work.


Solution

  • Ok thanks to Anton Angelov I found the mistake... The indices of the mesh need to start from 0 (not from 1)! Thanks again for your comments