Clarity on the specific relationship between Indice value & Vertex in ofMesh()

Hello,

I’m new to working with graphics in 3-D and openGL. I’m trying to get my head around the relationship between vertices and indices in oFMesh. I’ve been looking at Josh’s openGL tutorial (http://www.openframeworks.cc/tutorials/graphics/opengl.html) and the Programming Interactivity Book (http://shop.oreilly.com/product/0636920021735.do) Chapter 13: Graphics and openGL (along with a few other resources).

“A vertex is where a point in the mesh is located in 3-D space. An index is where that point is within the mesh” (Book)

"It’s (the index?) just a way of saying: I want this vertex to be used at this location. The mesh goes through the list of indices connected each set of three indices, each of which represents a vertex in the vertex array, to another vertex. It’s pretty rad and it saves you having to make and store more indices than necessary. A more typical usage is something like the following: (site)

  
ofMesh mesh;  
for (int y = 0; y < height; y++){  
  for (int x = 0; x<width; x++){  
    mesh.addVertex(ofPoint(x,y,0)); // make a new vertex  
    mesh.addColor(ofFloatColor(0,0,0));  // add a color at that vertex  
  }  
}  
// now it's important to make sure that each vertex is correctly connected with the  
// other vertices around it. This is done using indices, which you can set up like so:    
for (int y = 0; y<height-1; y++){  
  for (int x=0; x<width-1; x++){  
    mesh.addIndex(x+y*width);       // 0  
    mesh.addIndex((x+1)+y*width);     // 1  
    mesh.addIndex(x+(y+1)*width);     // 10  
   
    mesh.addIndex((x+1)+y*width);     // 1  
    mesh.addIndex((x+1)+(y+1)*width);   // 11  
    mesh.addIndex(x+(y+1)*width);     // 10  
  }  
}  
  

I was thinking of the indices as a collection of all of the triangle points, per triangle, including the duplicate points (places where the triangles connect). I see the vertex as the specific location in space. But, I can’t get that to cognitively map correctly between the above code (addVertex & addMesh supplied values).

I was also looking at the isocahedron code example, and still puzzled.

  
#pragma once  
#include "ofMain.h"  
   
const int X = 158;   
const int Z = 256;  
   
//This is the data for the vertices, which keeps the data as simple as possible:  
static GLfloat vdata[12][3] = {  
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},  
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},  
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0} };  
   
//data for the indices, representing the index of the vertices   
//that are to be connected into the triangle.   
//You’ll notice that for 12 vertices you need 20 indices of 3 vertices each:  
static GLint indices[20][3] = {  
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1}, {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3}, {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6}, {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}  
};  
   
class icosahedron : public ofBaseApp{   
   
public:  
  float ang;   
  ofMesh mesh;  
  void setup();  
  void update();   
  void draw();  
};  

Hmm…Can anyone shed some light on this? It’s still a bit foggy to me. Cheers!

the index is the position of the vertex in the collection of vertices that there is in the mesh. since the mesh it’s drawn as triangles the indices define in which order those vertices form triangles. so if you have for example:

vertices = {0,0} {20,0} {20,20} {0,20}
indices = 0 1 2 2 3 0

that will result in a square formed by 2 triangles starting from the top left corner and going through them clockwise

That definitely helps! Thanks!

So, if the square defined above was turned into a cube, would this mapping be correct? (working clockwise)

front vertices = {0, 0, 0}, { 20, 0, 0}, {20, 20, 0}, {0, 20, 0}
right vertices = {20, 0, 0}, {20, 0, 1}, {20, 20, 1}, {20, 20, 0}
back vertices = {0, 0, 1}, {20, 0, 1}, {20, 20, 1}, {0, 20, 1}
left vertices = {0, 0, 1}, {0, 0, 0}, {0, 20, 0}, {20, 20, 1}

vertices = {0, 0, 0} {20, 0, 0}, {20, 20, 0}, {0, 20, 0}, {20, 0, 1}, {20, 20, 1}, {0, 0, 1}, {0, 20, 1},
indices = 0 1 2 2 3 0 1 4 5 5 2 1 6 4 5 5 7 6 6 0 3 3 5 6

That’s under the assumption that vertices would be assigned clockwise per face/plane. I understand that vertices need to be passed in a particular order, based on the GL shape/mode called in oF/openGL. I just want to make sure that I’m thinking about the concept correctly.

yes that’s it, i haven’t gone through the exact indices but the idea is correct. there’s several indices in the cube that are repeated so instead of writing them 3 times you put all the vertices of the cube and then use the indices to define the triangles that form the cube.

Perfect! Got it!

Thanks, mate! Helps a bunch!