I have a vector with triangles. And the triangles coincide vertices in some. I need to create two vectors

using VertexBuffer = std::vector<Vector>; using IndexBuffer = std::vector<VertexIndex>; 

and write vertices to the VertexBuffer vector so that they do not repeat. And then write indexes with a VertexBuffer to IndexBuffer. I started to do, and confused, and I can not do the task. what would not do everything wrong. Need your help. Example:

  //1 v1=(0, 0, 0) v2=(0, 1, 2) v3=(0, 3, 3) //2 v4=(0, 0, 0) v5=(4, 5, 6) v6=(0, 3, 3) //3 v7=(4, 5, 6) v8=(0, 0, 0) v9=(0, 3, 3) VertexBuffer 0.(0, 0, 0) 1.(0, 1, 2) 2.(0, 3, 3) 3.(4, 5, 6) IndexBuffer 0, 1, 2 0, 3, 2 3, 0, 2 

Code:

  std::pair<VertexBuffer, IndexBuffer> Optimize(const std::vector<Triangle>& triangles) { Triangle triangle; VertexBuffer vertexBuffer; IndexBuffer indexBuffer; for (int index = 0; index < triangles.size(); ++index) { triangle = triangles[index]; if (triangle != triangles[index]) vertexBuffer[index] = triangles[index]; for (int i = 0; i < 3; ++i) { indexBuffer[i] = vertexBuffer[index]; } } return std::pair<VertexBuffer, IndexBuffer>(); } 

    1 answer 1

    As far as I understand the task - to take a map, in which to enter pairs: (ΠΊΠ»ΡŽΡ‡ - ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹; Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ - цСлочислСнный индСкс) . If the coordinates are already in the map, then we use the index obtained from the map, otherwise we increase the counter of unique points and use it as the value of the new pair and as the index

     Ρ‚ΠΎΡ‡ΠΊΠ° ΠΊΠΎΠΎΡ€Π΄ индСкс v1 = (0, 0, 0); 0 v2 = (0, 1, 2); 1 v3 = (0, 3, 3); 2 v4 = (0, 0, 0); 0 v5 = (4, 5, 6); 3 v6 = (0, 3, 3); 2 

    If map is not possible, then the principle is similar, only the search will take longer, and the index along with the point should not be stored:

     для ΠΊΠ°ΠΆΠ΄ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ: iter = std:find Π² Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌ vertexBuffer indexbuffer[] = iter - begin Ссли iter = end //новая Ρ‚ΠΎΡ‡ΠΊΠ° push Ρ‚ΠΎΡ‡ΠΊΡƒ Π² vertexBuffer 
    • I need to do it through the vector. in the task is shown through the vector - dimaAf