Skip to main content
added 31 characters in body
Source Link

enter image description here First picture is reality and second picture expectation

Reality Expectation

enter image description here

First picture is reality and second picture expectation

Reality Expectation

Source Link

Cannot Get The Texture Showed Up Correctly glDrawElements

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; } 

enter image description here