2016-10-25 22 views
0

イムをロードされていない、しかし、これは、それが今のようになります。3d modelOBJモデルの読み込みテクスチャcoordsの頂点位置が私のゲームに3Dの.objモデルをロードし、正しく

間違いなく、それが見えるように仮定するものではありませんています。多分思考イムは、インデックスが間違っている接続の私の方法ですが、私は問題に

を見つけることができない、これは私のコードです:

char lineHeader[128]; 
FILE * file = fopen(fileName, "r"); 

if (file == NULL) { 
    printf("Impossible to open the file !\n"); 
} 

vector <vec3> positionVec; 
vector <vec2> texCoordVec; 
vector <vec3> normalVec; 
vector <GLuint> vertexIndices; 
vector <GLuint> uvIndices; 
vector <GLuint> normalIndices; 

while (true) { 
    char lineHeader[128]; 
    int res = fscanf(file, "%s", lineHeader); 

    if (res == EOF) { 
     break; 
    } 

     if (strcmp(lineHeader, "v") == 0) { 
      glm::vec3 vertex; 
      fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z); 
      positionVec.push_back(vertex); 
     } 
     else if (strcmp(lineHeader, "vt") == 0) { 
      glm::vec2 uv; 
      fscanf(file, "%f %f\n", &uv.x, &uv.y); 
      texCoordVec.push_back(uv); 
     } 
     else if (strcmp(lineHeader, "vn") == 0) { 
      glm::vec3 normal; 
      fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z); 
      normalVec.push_back(normal); 
     } 
     else if (strcmp(lineHeader, "f") == 0) { 
      int vertexIndex1, vertexIndex2, vertexIndex3, uvIndex1, uvIndex2, uvIndex3, normalIndex1, normalIndex2, normalIndex3; 
      fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex1, &uvIndex1, &normalIndex1, &vertexIndex2, &uvIndex2, &normalIndex2, &vertexIndex3, &uvIndex3, &normalIndex3); 
      vertexIndices.push_back(vertexIndex1); 
      vertexIndices.push_back(vertexIndex2); 
      vertexIndices.push_back(vertexIndex3); 
      uvIndices.push_back(uvIndex1); 
      uvIndices.push_back(uvIndex2); 
      uvIndices.push_back(uvIndex3); 
      normalIndices.push_back(normalIndex1); 
      normalIndices.push_back(normalIndex2); 
      normalIndices.push_back(normalIndex3); 
     } 
    } 

vector <vec3> vertexPositions; 
vector <vec2> textureCoords; 
vector <vec3> normals; 

for (unsigned int i = 0; i < vertexIndices.size(); i++) { 
    vertexIndices[i] = vertexIndices[i] - 1; 
} 

for (unsigned int i = 0; i < uvIndices.size(); i++) { 
    unsigned int index = uvIndices[i]; 
    vec2 uv = texCoordVec[index - 1]; 
    textureCoords.push_back(uv); 
} 

for (unsigned int i = 0; i < normalIndices.size(); i++) { 
    unsigned int index = normalIndices[i]; 
    vec3 normal = normalVec[index - 1]; 
    normals.push_back(normal); 
} 

    GLuint modelVertecesBuffer; 
    glGenBuffers(1, &modelVertecesBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, modelVertecesBuffer); 
    glBufferData(GL_ARRAY_BUFFER, positionVec.size() * sizeof(glm::vec3), &positionVec[0], GL_STATIC_DRAW); 
    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

    GLuint modelTextureCoordBuffer; 
    glGenBuffers(1, &modelTextureCoordBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, modelTextureCoordBuffer); 
    glBufferData(GL_ARRAY_BUFFER, texCoordVec.size() * sizeof(vec2), &texCoordVec[0], GL_STATIC_DRAW); 
    glEnableVertexAttribArray(2); 
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0); 

    GLuint modelNormalBuffer; 
    glGenBuffers(1, &modelNormalBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, modelNormalBuffer); 
    glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec3), &normals[0], GL_STATIC_DRAW); 
    glEnableVertexAttribArray(1); 
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0); 

    GLuint positionIndicesBuffer; 
    glGenBuffers(1, &positionIndicesBuffer); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, positionIndicesBuffer); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertexIndices.size() * sizeof(int), &vertexIndices[0], GL_STATIC_DRAW); 
+0

[mcve]で編集します。 [this](http://stackoverflow.com/a/14887071/44729)をベースとして自由に使用してください。 – genpfault

答えて

0

いくつかあります。

指標では、 OBJモデルは1ベースです。あなたはベクトルvertexPositionsなどを記入するときにこれを考慮します。しかし、その後はこれらのベクトルを使用しないでください。 OBJからのインデックスをインデックスバッファとして直接使用することはできません。あなたは1つを引く必要があります。コメントに記載されているgenpfaultのように、インデックスは相対参照では負の値になることさえあります。

OBJの面の頂点は、異なる位置と通常のID(およびテクスチャ座標ID)を参照することがあります。これはOpenGLで表現できないもので、データを複製する必要があります。したがって、このアプローチは、参照されるIDがすべて同じである場合にのみ機能します。

+1

OBJインデックスも負である可能性があります。 – genpfault

+0

@genpfaultありがとう、答えに追加されました。 –

+0

@NicoSchertlerあなたのお手伝いをしていただきありがとうございます。モデルが正しく見えるように位置インデックスを修正しましたが、そのテクスチャはまだ乱れています。何が問題になっているかについてのアイデアはありますか? (私はコードを更新しました) – lucasyyy

関連する問題