Why is the number of vt and v elements in blender.obj different?
Following the instructions in the tutorial https://www.youtube.com/watch?v=yc0b5GcYl3U (How to untie a UV sphere in Blender) I was able to create a textured sphere in blender.
Now I want this in my openGL C ++ program. To this end, I followed the tutorial http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Load_OBJexported to save the sphere as an .obj file (using the triangulation export option as mentioned in the mentioned tutorial) and happily found many 'v' results , 'vt' and 'f' as a result.
However, after parsing the file, I found 642 vertices (v), 561 "texture vertices" (vt) [and 1216 lines of elements (f) of the expected structure "fa / at b / bt c / ct"].
What puzzles me: my naive understanding of openGL tells me that every point on a textured object has a site in space (vertex) and a site on texture (UV point). So I would really expect the vs and vts matche numbers. But they don't: 642! = 561. How can this be?
source to share
Because OBJ and OpenGL use a different definition of "vertex" and handle indexes differently.
In the following explanation, I will call the vertex coordinates, which are values in v
OBJ records, "positions".
OBJ
The main characteristic of the OBJ vertex / index model is that it uses separate indices for different vertex attributes (positions, normals, texture coordinates).
This means that you can have independent lists of positions and texture coordinates of different sizes. The file only needs to specify each unique position and each unique pair of texture coordinates once.
The vertex is then defined by specifying indices of 3 : one each for position, texture coordinates, and normal.
Opengl
OpenGL, on the other hand, uses a single set of indices that refer to full vertices.
The vertex is defined by position, texture coordinates, and normal. Thus, a vertex is needed for every unique combination of position, texture coordinates, and normal.
Conversion
When you read an OBJ file for OpenGL rendering, you need to create a vertex for each unique combination of position, texture coordinates, and normal. Since they refer to indexes in records f
, you need to create an OpenGL vertex for each unique index triplet that you find in those f
records. For each of these vertices, you use the position, texture coordinates, and normal at the specified index as read from the OBJ file.
My older answer here contains pseudocode to illustrate the process: OpenGL - Difficulties with index buffers .
source to share