0
\$\begingroup\$

I still have this problem almost 1 month. Tried to search on Google but did not find any solution to this. I have loaded all the data correctly but don't know why the texture came up like this.

Here is my loader:

Header

struct SVertex
{
public:
    float X, Y, Z;
};

struct STexture
{
public:
    float U, V;
};

struct SFace
{
public:
    int X, Y, Z;
};

class CBMap
{
public:
    struct SMesh
    {
        SVertex *Vertex;
        SVertex *Normal;
        STexture *TexCoord;
        SFace *Face;
        SFace *FaceNormal;

        char Texture[32];
        int TextureID;
        int TotalVertice;
        int TotalNormal;
        int TotalFace;
    };

    SMesh* Mesh;

    CBMap();
    ~CBMap();

    int TotalMesh;

    int Load(char* szFile);
    int Get_FileHeader(FILE* pFile);
    int Get_TotalMesh(FILE* pFile);
    int GetData(FILE* pFile);
};

CPP

int CBMap::Load(char* szFile)
{
    FILE* pFile = fopen(szFile, "r");

    if (!pFile) return 0;

    int iBMap = Get_FileHeader(pFile);

    if (!iBMap)
    {
        printf("BMAP file is not valid\n");
        return 0;
    }

    TotalMesh   = Get_TotalMesh(pFile);
    Mesh        = new SMesh[TotalMesh];
    GetData(pFile);

    int iMesh = 0;

    char* pJunk;
    char szHeader[32];

    while (!feof(pFile))
    {
        fscanf(pFile, "%s", &szHeader);

        if (!strncmp(szHeader, "BMAP", 4)) continue;
        if (!strncmp(szHeader, "mesh", 4)) continue;
        if (!strncmp(szHeader, "mtl", 3))
        {
            fscanf(pFile, "%s", Mesh[iMesh].Texture);
            Mesh[iMesh].TextureID = LoadTexture(Mesh[iMesh].Texture);
        }

        if (!strncmp(szHeader, "ve", 2))
        {
            fscanf(pFile, "%d", &pJunk);
            Mesh[iMesh].Vertex = (SVertex*)malloc(sizeof (SVertex) * Mesh[iMesh].TotalVertice);

            for (int i = 0; i < Mesh[iMesh].TotalVertice; ++i)
            {
                fscanf(pFile, "%f %f %f", &Mesh[iMesh].Vertex[i].X, &Mesh[iMesh].Vertex[i].Y, &Mesh[iMesh].Vertex[i].Z);
            }
        }

        if (!strncmp(szHeader, "uv", 2))
        {
            fscanf(pFile, "%d", &pJunk);
            Mesh[iMesh].TexCoord = (STexture*)malloc(sizeof (STexture) * Mesh[iMesh].TotalVertice);

            for (int i = 0; i < Mesh[iMesh].TotalVertice; ++i)
            {
                fscanf(pFile, "%f %f", &Mesh[iMesh].TexCoord[i].U, &Mesh[iMesh].TexCoord[i].V);
            }
        }

        if (!strncmp(szHeader, "vn", 2))
        {
            fscanf(pFile, "%d", &pJunk);
            Mesh[iMesh].Normal = (SVertex*)malloc(sizeof (SVertex) * Mesh[iMesh].TotalNormal);

            for (int i = 0; i < Mesh[iMesh].TotalNormal; ++i)
            {
                fscanf(pFile, "%f %f %f", &Mesh[iMesh].Normal[i].X, &Mesh[iMesh].Normal[i].Y, &Mesh[iMesh].Normal[i].Z);
            }
        }

        if (!strncmp(szHeader, "f", 1))
        {
            fscanf(pFile, "%d", &pJunk);
            Mesh[iMesh].Face = (SFace*)malloc(sizeof (SFace) * Mesh[iMesh].TotalFace * 3);
            Mesh[iMesh].FaceNormal = (SFace*)malloc(sizeof (SFace) * Mesh[iMesh].TotalFace * 3);

            for (int i = 0; i < Mesh[iMesh].TotalFace; ++i)
            {
                fscanf(pFile, "%d %d %d %d %d %d",  &Mesh[iMesh].Face[i].X, &Mesh[iMesh].Face[i].Y, &Mesh[iMesh].Face[i].Z,
                                                    &Mesh[iMesh].FaceNormal[i].X, &Mesh[iMesh].FaceNormal[i].Y, &Mesh[iMesh].FaceNormal[i].Z);
            }
        }

        if (!strncmp(szHeader, "end", 3))
        {
            if (iMesh < TotalMesh) iMesh ++;
        }
    }

    printf("CBMap::Model Loaded\n");

    fclose(pFile);
    return 1;
}

First picture is reality and second picture expectation

Reality Expectation

\$\endgroup\$
  • \$\begingroup\$ We can barely see the picture. It's too dark. Is this the problem? If not, what do you mean by "the texture came up like this"? Please provide more context and also describe or better show a desired result. \$\endgroup\$ – Ivan Aksamentov - Drop Jun 13 '15 at 14:48
  • \$\begingroup\$ I needed to set the v components to v = 1 - loadedV. this is because opengl uses a different origin for the texture coordinate system than directx \$\endgroup\$ – Tobias B Jun 14 '15 at 7:34
  • \$\begingroup\$ Thanks Tobias but I don't really understand what you explain. Any example? \$\endgroup\$ – DavidJr Jun 14 '15 at 8:43
  • \$\begingroup\$ Tobias, thank you very much, that makes my code works. How to flag your comment as an answer? \$\endgroup\$ – DavidJr Jun 15 '15 at 6:14
  • \$\begingroup\$ I postet it as answer. When I wrote the comment I wasn't sure about it and didn't have the time to research. \$\endgroup\$ – Tobias B Jun 15 '15 at 8:12
0
\$\begingroup\$

DirectX and OpenGl have different coordinate systems for their texture coordinates. OpenGl uses a origin in the lower left but your model assumes that the origin is in the upper left. Because of that you need to mirror the UV coordinates or your images along the y axis. You can do so by setting the v component to 1 - loadedV.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much Tobias, now the texture showed up correctly. \$\endgroup\$ – DavidJr Jun 15 '15 at 11:56

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.