Before drawing a cube I set its vertices:

GLfloat vertices[] = { 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, }; 

Now we need to determine the indexes, but I do not know how
Please tell me what are the ways?

  • For those who are not familiar with OpenGL closely, explain what indexes are meant. - Cerbo
  • @Cerbo most likely in view of the array indices vertices forming the faces of the cube with the help of 2 triangles and 4 vertices. - ampawd
  • @Cerbo exactly as ampawd wrote - andrew
  • I can be wrong. but. it is necessary to index the points having the same coordinates and lying on the same plane. And in your mesh, I did not find such. hence there is simply nothing to index. - perfect
  • @perfect he has the cube tops - everything is corny indexed there - ampawd

1 answer 1

what you ask for is called the index buffer, since you have 8 vertices defined for the cube, you will have to draw with the help of array indexes vertices , and for example the mode GL_TRIANGLE_STRIP

each 4th row of indices defines one face of the cube with two triangles for which two vertices will be common.

the cube has 6 faces, therefore 4*6 = 24 indices are necessary.

enter image description here

depending on the order in which the positions of each vertex are written in the vertices array, the indices in the indices array will depend, ie for your case there will be something like

 GLushort indices[] = { 0, 3, 1, 2, // нижняя грань 2, 6, 3, 7, // левая грань //... остальные 4 попытайтесь сами определить // обход против часовой стрелки по дефолту }; //... // далее в цикле отрисовки glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, 0); 
  • Good, but I didn’t understand how to know indices - andrew
  • @andrew draw a cube on a piece of paper, number the vertices counting in the order in which you have them in the array vertices further choose 4 indices for each facet - what exactly is not clear - ampawd
  • @andrew added a picture for better understanding - ampawd 5:14
  • thanks, so much clearer - andrew
  • GL_TRIANGLE_STRIP rather complicated decision for such a case. Better and easier GL_TRIANGLES - Kromster