Skip to main content
added 1866 characters in body
Source Link
akaltar
  • 1.7k
  • 14
  • 26

Some pseudocode for aligning as asked for:

float* verticess = new float[NumFaces * 9]; // vertices : input, verticess ouptut to VBO
float* normalss = new float[NumFaces * 9];  // normals: input, normalss outut to VBO
float* texcoordss = new float[NumFaces * 6];
for(int i = 0; i < NumFaces; i++)
{
    //For 3 vertices with 3(xyz coordinates) va - A vertex index, vb - B vertex ....
    verticess[i * 9] = vertices[faces[i].va-1].v[0]; // vertices[i].v is array of 3(xyz)
    verticess[i*9+1] = vertices[faces[i].va-1].v[1];
    verticess[i*9+2] = vertices[faces[i].va-1].v[2];

    verticess[i*9+3] = vertices[faces[i].vb-1].v[0];
    verticess[i*9+4] = vertices[faces[i].vb-1].v[1];
    verticess[i*9+5] = vertices[faces[i].vb-1].v[2];

    verticess[i*9+6] = vertices[faces[i].vc-1].v[0];
    verticess[i*9+7] = vertices[faces[i].vc-1].v[1];
    verticess[i*9+8] = vertices[faces[i].vc-1].v[2];

    //For normalss 3 normals with 3(xyz coordinates) na - A normal index...

    normalss[i * 9] = normals[faces[i].na-1].v[0];
    normalss[i*9+1] = normals[faces[i].na-1].v[1];
    normalss[i*9+2] = normals[faces[i].na-1].v[2];

    normalss[i*9+3] = vecnormals[faces[i].nb-1].v[0];
    normalss[i*9+4] = vecnormals[faces[i].nb-1].v[1];
    normalss[i*9+5] = vecnormals[faces[i].nb-1].v[2];

    normalss[i*9+6] = vecnormals[faces[i].nc-1].v[0];
    normalss[i*9+7] = vecnormals[faces[i].nc-1].v[1];
    normalss[i*9+8] = vecnormals[faces[i].nc-1].v[2];

    //for texcoordss 3 coords with 2(st coordinates) ta - A tex index ....

    texcoordss[i * 6] = texcoords[faces[i].ta-1].v[0];
    texcoordss[i*6+1] = texcoords[faces[i].ta-1].v[1];

    texcoordss[i*6+2] = texcoords[faces[i].tb-1].v[0];
    texcoordss[i*6+3] = texcoords[faces[i].tb-1].v[1];

    texcoordss[i*6+4] = texcoords[faces[i].tc-1].v[0];
    texcoordss[i*6+5] = texcoords[faces[i].tc-1].v[1];
}

Some pseudocode for aligning as asked for:

float* verticess = new float[NumFaces * 9]; // vertices : input, verticess ouptut to VBO
float* normalss = new float[NumFaces * 9];  // normals: input, normalss outut to VBO
float* texcoordss = new float[NumFaces * 6];
for(int i = 0; i < NumFaces; i++)
{
    //For 3 vertices with 3(xyz coordinates) va - A vertex index, vb - B vertex ....
    verticess[i * 9] = vertices[faces[i].va-1].v[0]; // vertices[i].v is array of 3(xyz)
    verticess[i*9+1] = vertices[faces[i].va-1].v[1];
    verticess[i*9+2] = vertices[faces[i].va-1].v[2];

    verticess[i*9+3] = vertices[faces[i].vb-1].v[0];
    verticess[i*9+4] = vertices[faces[i].vb-1].v[1];
    verticess[i*9+5] = vertices[faces[i].vb-1].v[2];

    verticess[i*9+6] = vertices[faces[i].vc-1].v[0];
    verticess[i*9+7] = vertices[faces[i].vc-1].v[1];
    verticess[i*9+8] = vertices[faces[i].vc-1].v[2];

    //For normalss 3 normals with 3(xyz coordinates) na - A normal index...

    normalss[i * 9] = normals[faces[i].na-1].v[0];
    normalss[i*9+1] = normals[faces[i].na-1].v[1];
    normalss[i*9+2] = normals[faces[i].na-1].v[2];

    normalss[i*9+3] = vecnormals[faces[i].nb-1].v[0];
    normalss[i*9+4] = vecnormals[faces[i].nb-1].v[1];
    normalss[i*9+5] = vecnormals[faces[i].nb-1].v[2];

    normalss[i*9+6] = vecnormals[faces[i].nc-1].v[0];
    normalss[i*9+7] = vecnormals[faces[i].nc-1].v[1];
    normalss[i*9+8] = vecnormals[faces[i].nc-1].v[2];

    //for texcoordss 3 coords with 2(st coordinates) ta - A tex index ....

    texcoordss[i * 6] = texcoords[faces[i].ta-1].v[0];
    texcoordss[i*6+1] = texcoords[faces[i].ta-1].v[1];

    texcoordss[i*6+2] = texcoords[faces[i].tb-1].v[0];
    texcoordss[i*6+3] = texcoords[faces[i].tb-1].v[1];

    texcoordss[i*6+4] = texcoords[faces[i].tc-1].v[0];
    texcoordss[i*6+5] = texcoords[faces[i].tc-1].v[1];
}
Source Link
akaltar
  • 1.7k
  • 14
  • 26

When using index buffers, you are indexing into all of the buffers at the same time. That means that if you have a point which can have multiple texture coordinates(If there is a seam at that point) or normals(for example if faceted) then you need to duplicate the given points vertex coordinates with all possible combinations.


If you want to have indexing with texture coordinates and normals, then you have 2 options:

  • Export the .obj so that it has all the duplicates already(a.k.a. baking)(I believe you can set this up in 3ds max GW obj exporter by disabling "optimize")
  • Do this duplicating of vertices in your own code.

I would recommend either leaving this system as-is and worry about performance later or use Assimp, which not only does this all for you, it supports more formats with consistency, and will save you some headaches later with its tangent generation and other useful preprocessing.