2016-07-14 12 views
0

をレンダリング(コアプロファイル)は、スクリーンショットを見てください、私のプロジェクトの問題です方法。頂点が正しく接続されていません。 私はそれがモデルの指標と関係があると考えます。とにかく、ここで私のためにメッシュを構築するコードは次のとおりです。OpenGLのモデルの三角形が間違ってここ

この機能で
mesh model::processMesh(aiMesh * mesh_, const aiScene * scene) 
{ 
    std::vector<vertex> vertices; 
    std::vector<GLuint> indices; 
    std::vector<texture> textures; 

    //vertices 
    for (GLuint i = 0; i < mesh_->mNumVertices; i++) 
    { 
     vertex vert; 
     glm::vec3 vector; 

     //positions 
     vector.x = mesh_->mVertices[i].x; 
     vector.y = mesh_->mVertices[i].y; 
     vector.z = mesh_->mVertices[i].z; 
     vert.position = vector; 

     //normals 
     vector.x = mesh_->mNormals[i].x; 
     vector.y = mesh_->mNormals[i].y; 
     vector.z = mesh_->mNormals[i].z; 
     vert.normal = vector; 

     //texture coords 
     if (mesh_->mTextureCoords[0]) 
     { 
      glm::vec2 vector_; 
      vector_.x = mesh_->mTextureCoords[0][i].x; 
      vector_.y = mesh_->mTextureCoords[0][i].y; 
      vert.texCoords = vector_; 
     } 
     else vert.texCoords = glm::vec2(0.0f, 0.0f); 

     vertices.push_back(vert); 
    } 

    //indices 
    for (GLuint i = 0; i < mesh_->mNumFaces; i++) 
    { 
     aiFace face = mesh_->mFaces[i]; 
     for (GLuint j = 0; j < mesh_->mNumFaces; j++) indices.push_back(face.mIndices[j]); 
    } 

    //textures 
    if (mesh_->mMaterialIndex >= 0) 
    { 
     aiMaterial* material = scene->mMaterials[mesh_->mMaterialIndex]; 

     //diffuse 
     std::vector<texture> diffuseMaps = this->loadMaterialTextures(material, aiTextureType_DIFFUSE, TEX_DIFF_NAME); 
     textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end()); 
     //specular 
     std::vector<texture> specularMaps = this->loadMaterialTextures(material, aiTextureType_SPECULAR, TEX_SPEC_NAME); 
     textures.insert(textures.end(), specularMaps.begin(), specularMaps.end()); 
    } 

    return mesh(vertices, indices, textures); 
} 

私はすべてのオブジェクトの設定:

void mesh::setupMesh() 
{ 
    //buffers 
    glGenVertexArrays(1, &this->vao); 
    glGenBuffers(1, &this->vbo); 
    glGenBuffers(1, &this->ebo); 

    glBindVertexArray(this->vao); 

    glBindBuffer(GL_ARRAY_BUFFER, this->vbo); 
    glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(vertex), &this->vertices[0], GL_STATIC_DRAW); 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ebo); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_STATIC_DRAW); 

    //attributes 
    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)0); 
    glEnableVertexAttribArray(1); 
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, normal)); 
    glEnableVertexAttribArray(2); 
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, texCoords)); 

    //unbind 
    glBindVertexArray(0); 
} 

をそしてここでレンダリング

void mesh::draw(shader* shader) 
{ 
    GLuint tex_diffNumber = 1; 
    GLuint tex_specNumber = 1; 

    for (GLuint i = 0; i < this->textures.size() ; i++) 
    { 
     //load target texture 
     glActiveTexture(GL_TEXTURE0 + i); 

     std::stringstream sstream; 
     std::string number; 
     std::string name = this->textures[i].type; 

     if (name == TEX_DIFF_NAME) 
      sstream << tex_diffNumber++; 
     else if (name == TEX_SPEC_NAME) 
      sstream << tex_specNumber++; 

     number = sstream.str(); 

     glBindTexture(GL_TEXTURE_2D, this->textures[i].id); 
     glUniform1i(glGetUniformLocation(shader->shaderProgID, (name + number).c_str()), i); 

    } 

    //set shininess 
    //glUniform1f(glGetUniformLocation(shader->shaderProgID, "material.shininess"), 16.0f); 

    //draw 
    glBindVertexArray(this->vao); 
    glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0); 
    glBindVertexArray(0); 

    //release 
    for (GLuint i = 0; i < this->textures.size(); i++) 
    { 
     glActiveTexture(GL_TEXTURE0 + i); 
     glBindTexture(GL_TEXTURE_2D, 0); 
    } 
} 

質問はありますなぜ私のモデルは間違って三角形を作りましたか? 追加情報をお尋ねください。 事前にお手伝いいただきありがとうございます。

答えて

2
for (GLuint i = 0; i < mesh_->mNumFaces; i++) 
{ 
    aiFace face = mesh_->mFaces[i]; 
    for (GLuint j = 0; j < mesh_->mNumFaces; j++) 
     indices.push_back(face.mIndices[j]); 
} 

これは私には間違っています。私は内側のループがすべての顔に再び走っているとは思わない。代わりに、それが顔にすべてのインデックスを反復処理する必要があります、元のバージョンは、ループはおそらくJ = 5(6面)まで行っていたことから、使用可能なメモリの外に読んでいたことを、

for (GLuint i = 0; i < mesh_->mNumFaces; i++) 
{ 
    aiFace face = mesh_->mFaces[i]; 
    for (GLuint j = 0; j < face->mNumIndices; j++) 
     indices.push_back(face.mIndices[j]); 
} 

注意が、そこ最大4つの指標を持つ顔だけです。デバッガを接続してそれを踏んだ場合、これは簡単に見えるはずです。

+0

ありがとう、教祖!それは問題を解決しました。完璧な意味合いを持つ。私はしばしばループのためにコピー貼り付けをします。その結果、いくつかのことは忘れてしまいます。 –

関連する問題