Im making a custom Wavefront .obj loader and the images loads but the texture is flipped. What is the problem? here is code and a image.

enter image description here

std::vector<UINT> vertexIndices, uvIndices, normalIndices;
std::vector<UINT> indices(4119);
std::vector< XMFLOAT3> temp_vertices;
std::vector< XMFLOAT2 > temp_uvs;
std::vector< XMFLOAT3 > temp_normals;
std::vector< XMFLOAT3> out_vertices;
std::vector< XMFLOAT3> out_normals;
std::vector< XMFLOAT2> out_uvs;
std::vector<Vertex::Basic32> vertices( 4119);
const char *path = "C:\\Users\\terry\\Desktop\\untitled.txt";

FILE * file = fopen(path, "r");
if( file == NULL ){
    printf("Impossible to open the file !\n");

while( 1 ){

    char lineHeader[128];
    // read the first word of the line
    int res = fscanf(file, "%s", lineHeader);
    if (res == EOF)
        break; // EOF = End Of File. Quit the loop.

    // else : parse lineHeader

    if ( strcmp( lineHeader, "v" ) == 0 ){
    XMFLOAT3 vertex;
    fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );
    }else if ( strcmp( lineHeader, "vt" ) == 0 ){
    XMFLOAT2 uv;
    fscanf(file, "%f %f\n", &uv.x, &uv.y );
    }else if ( strcmp( lineHeader, "vn" ) == 0 ){
    XMFLOAT3 normal;
    fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );
    }else if ( strcmp( lineHeader, "f" ) == 0 ){
    std::string vertex1, vertex2, vertex3;
    unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
    int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2] );
    if (matches != 9){
        printf("File can't be read by our simple parser : ( Try exporting with other options\n");
    uvIndices    .push_back(uvIndex[0]);
    uvIndices    .push_back(uvIndex[1]);
    uvIndices    .push_back(uvIndex[2]);

 for( unsigned int i=0; i<4119; i++ ){
     unsigned int vertexIndex = vertexIndices[i];
     XMFLOAT3 vertex = temp_vertices[ vertexIndex-1 ];
     unsigned int normalIndex = normalIndices[i];
     XMFLOAT3 normal = temp_normals[ normalIndex-1 ];
     unsigned int uvIndex = uvIndices[i];
     XMFLOAT2 uv = temp_uvs[ uvIndex-1 ];


 for( unsigned int i=0; i<4119; i++ ){
     vertices[i].Pos = out_vertices[i];
     vertices[i].Normal = out_normals[i];
     vertices[i].Tex = out_uvs[i];
     vertices[i].Tex.y = vertices[i].Tex.y * -1.0f;
     indices[i] = i;
  • \$\begingroup\$ Im in Left handed coordinate system, DirectX \$\endgroup\$ – vector Apr 15 '18 at 23:43
  • \$\begingroup\$ Having loaded in wavefront myself, I checked my loader code and I have -- vp.SetTextureUV(result.Textures[TextureIndex].X, -result.Textures[TextureIndex].Y); -- which looks exactly what you are doing. I assume you get the right result if you also negate the X texture coord? \$\endgroup\$ – ErnieDingo Apr 16 '18 at 1:11

I'm pretty sure Direct 3D flips the textures Y coordinates for you, so you don't need to do it yourself.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.