0

Assimpを使用してテクスチャで 3D モデルをレンダリングしようとしています。変換は完璧に進み、すべてのテクスチャの位置とロードされないものがあります。テクスチャ イメージを 2D で画面に描画してテストしました。何らかの理由で、テクスチャがモデルにレンダリングされません。私は OpenGL の初心者なので、正しく説明していない場合はご容赦ください。コードの基にしたチュートリアルはhereからのものですが、独自のカメラ/移動システムがあるため、大きな部分を取り除きました。

モデルは次のようにレンダリングされます: http://i.stack.imgur.com/5sK9K.png

使用中のテクスチャは次のようになります: http://i.stack.imgur.com/sWGp7.jpg

関連するレンダリング コードは次のとおりです。

データ ファイルからテクスチャを生成する:

int Mesh::LoadGLTextures(const aiScene* scene){
  if (scene->HasTextures()) return -1; //yes this is correct
  /* getTexture Filenames and Numb of Textures */
  for (unsigned int m = 0; m<scene->mNumMaterials; m++){
    int texIndex = 0;

    aiReturn texFound;
    aiString path;  // filename

    while ((texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, &path)) == AI_SUCCESS){
        textureIdMap[path.data] = NULL; //fill map with textures, pointers still NULL yet
        texIndex++;
    }
  }

  int numTextures = textureIdMap.size();
  /* create and fill array with GL texture ids */
  GLuint* textureIds = new GLuint[numTextures];

  /* get iterator */
  std::map<std::string, GLuint>::iterator itr = textureIdMap.begin();

  std::string basepath = getBasePath(path);

  ALLEGRO_BITMAP *image;
  for (int i = 0; i<numTextures; i++){
    std::string filename = (*itr).first;  // get filename
    (*itr).second = textureIds[i];    // save texture id for filename in map
    itr++;                                // next texture

    std::string fileloc = basepath + filename;  /* Loading of image */
    image = al_load_bitmap(fileloc.c_str());

    if (image) /* If no error occured: */{
        GLuint texId = al_get_opengl_texture(image);

        //glGenTextures(numTextures, &textureIds[i]); /* Texture name generation */
        glBindTexture(GL_TEXTURE_2D, texId); /* Binding of texture name */
        //redefine standard texture values
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear
                                                                          interpolation for magnification filter */
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear
                                                                          interpolation for minifying filter */
        textureIdMap[filename] = texId;
    } else {
        /* Error occured */
        std::cout << "Couldn't load Image: " << fileloc.c_str() << "\n";
    }
  }

  //Cleanup
  delete[] textureIds;

  //return success
  return true;
}

VBO/VAO の生成:

void Mesh::genVAOsAndUniformBuffer(const aiScene *sc) {
  struct MyMesh aMesh;
  struct MyMaterial aMat;
  GLuint buffer;

  // For each mesh
  for (unsigned int n = 0; n < sc->mNumMeshes; ++n){
    const aiMesh* mesh = sc->mMeshes[n];

    // create array with faces
    // have to convert from Assimp format to array
    unsigned int *faceArray;
    faceArray = (unsigned int *)malloc(sizeof(unsigned int) * mesh->mNumFaces * 3);
    unsigned int faceIndex = 0;

    for (unsigned int t = 0; t < mesh->mNumFaces; ++t) {
        const aiFace* face = &mesh->mFaces[t];

        memcpy(&faceArray[faceIndex], face->mIndices, 3 * sizeof(unsigned int));
        faceIndex += 3;
    }
    aMesh.numFaces = sc->mMeshes[n]->mNumFaces;

    // generate Vertex Array for mesh
    glGenVertexArrays(1, &(aMesh.vao));
    glBindVertexArray(aMesh.vao);

    // buffer for faces
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * mesh->mNumFaces * 3, faceArray, GL_STATIC_DRAW);

    // buffer for vertex positions
    if (mesh->HasPositions()) {
        glGenBuffers(1, &buffer);
        glBindBuffer(GL_ARRAY_BUFFER, buffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->mNumVertices, mesh->mVertices, GL_STATIC_DRAW);
        glEnableVertexAttribArray(vertexLoc);
        glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0);
    }

    // buffer for vertex normals
    if (mesh->HasNormals()) {
        glGenBuffers(1, &buffer);
        glBindBuffer(GL_ARRAY_BUFFER, buffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->mNumVertices, mesh->mNormals, GL_STATIC_DRAW);
        glEnableVertexAttribArray(normalLoc);
        glVertexAttribPointer(normalLoc, 3, GL_FLOAT, 0, 0, 0);
    }

    // buffer for vertex texture coordinates
    if (mesh->HasTextureCoords(0)) {
        float *texCoords = (float *)malloc(sizeof(float) * 2 * mesh->mNumVertices);
        for (unsigned int k = 0; k < mesh->mNumVertices; ++k) {
            texCoords[k * 2] = mesh->mTextureCoords[0][k].x;
            texCoords[k * 2 + 1] = mesh->mTextureCoords[0][k].y;
        }

        glGenBuffers(1, &buffer);
        glEnableVertexAttribArray(texCoordLoc);
        glBindBuffer(GL_ARRAY_BUFFER, buffer);

        glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * mesh->mNumVertices, texCoords, GL_STATIC_DRAW);
        glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
    }

    // unbind buffers
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    // create material uniform buffer
    aiMaterial *mtl = sc->mMaterials[mesh->mMaterialIndex];

    aiString texPath;   //contains filename of texture
    if (AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, 0, &texPath)){
        //bind texture
        unsigned int texId = textureIdMap[texPath.data];
        aMesh.texIndex = texId;
        aMat.texCount = 1;
    } else {
        aMat.texCount = 0;
    }

    float c[4];
    set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f);
    aiColor4D diffuse;
    if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
        color4_to_float4(&diffuse, c);
    memcpy(aMat.diffuse, c, sizeof(c));

    set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f);
    aiColor4D ambient;
    if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient))
        color4_to_float4(&ambient, c);
    memcpy(aMat.ambient, c, sizeof(c));

    set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
    aiColor4D specular;
    if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular))
        color4_to_float4(&specular, c);
    memcpy(aMat.specular, c, sizeof(c));

    set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
    aiColor4D emission;
    if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission))
        color4_to_float4(&emission, c);
    memcpy(aMat.emissive, c, sizeof(c));

    float shininess = 0.0;
    unsigned int max;
    aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max);
    aMat.shininess = shininess;

    glGenBuffers(1, &(aMesh.uniformBlockIndex));
    glBindBuffer(GL_UNIFORM_BUFFER, aMesh.uniformBlockIndex);
    glBufferData(GL_UNIFORM_BUFFER, sizeof(aMat), (void *)(&aMat), GL_STATIC_DRAW);

    myMeshes.push_back(aMesh);
  }
}

レンダリング モデル:

void Mesh::recursive_render(const aiScene *sc, const aiNode* nd){
  // draw all meshes assigned to this node
  for (unsigned int n = 0; n < nd->mNumMeshes; ++n){
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, myMeshes[nd->mMeshes[n]].texIndex);

    // bind VAO
    glBindVertexArray(myMeshes[nd->mMeshes[n]].vao);

    // draw
    glDrawElements(GL_TRIANGLES, myMeshes[nd->mMeshes[n]].numFaces * 3, GL_UNSIGNED_INT, 0);
  }

  // draw all children
  for (unsigned int n = 0; n < nd->mNumChildren; ++n){
      recursive_render(sc, nd->mChildren[n]);
  }
}

他の関連するコード部分は、私のオープン github プロジェクトhttps://github.com/kwek20/StrategyGame/tree/master/Strategyにあります。

Mesh.cpp は、main.cpp および Camera.cpp と同様に関連しています。

私が理解している限り、私はガイドラインによく従い、VAO を作成し、VBO を作成し、データを追加し、適切な頂点配列属性を有効にしてシーンをレンダリングしました。

すべてのデータ変数をチェックしましたが、すべてが計画どおりに入力されています

ここで誰かが私が犯した間違いを見つけたり、説明したりできますか? 私が持っている制限のために、いくつかのリンクは奇妙に入力されています:(

4

1 に答える 1

2

シェーダーも投稿していただけると助かります。それが役に立ったら、テクスチャを含むレンダリング コードを投稿できます: OpenGL のテクスチャを生成し、幅と高さを指定してグレースケール (UC8) 画像を GPU にロードする

void GLRenderer::getTexture(unsigned char * image, int width, int height)
{
    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &mTextureID);
    glBindTexture(GL_TEXTURE_2D, mTextureID);
    glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, width, height);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, image);

    if (aux::checkGlErrors(__LINE__, __FILE__))assert(false);
    glBindTexture(GL_TEXTURE_2D, 0);
}

assimp から GPU への頂点のロード

    //** buffer a obj file-style model, initialize the VAO
void GLRenderer::bufferModel(float* aVertexArray, int aNumberOfVertices, float* aNormalArray, int aNumberOfNormals, float* aUVList, int aNumberOfUVs, unsigned int* aIndexList, int aNumberOfIndices)
{
    //** just to be sure we are current
    glfwMakeContextCurrent(mWin);

    //** Buffer all data in VBOs
    glGenBuffers(1, &mVertex_buffer_object);
    glBindBuffer(GL_ARRAY_BUFFER, mVertex_buffer_object);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * aNumberOfVertices * 3, aVertexArray, GL_STATIC_DRAW);
    glGenBuffers(1, &mNormal_buffer_object);
    glBindBuffer(GL_ARRAY_BUFFER, mNormal_buffer_object);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * aNumberOfNormals * 3, aNormalArray, GL_STATIC_DRAW);
    glGenBuffers(1, &mUV_buffer_object);
    glBindBuffer(GL_ARRAY_BUFFER, mUV_buffer_object);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * aNumberOfUVs * 2, aUVList, GL_STATIC_DRAW);
    glGenBuffers(1, &mIndex_buffer_object);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndex_buffer_object);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * aNumberOfIndices, aIndexList, GL_STATIC_DRAW);

    if (aux::checkGlErrors(__LINE__, __FILE__))assert(false);

    //** VAO tells our shaders how to match up data from buffer to shader input variables
    glGenVertexArrays(1, &mVertex_array_object);
    glBindVertexArray(mVertex_array_object);

    //** vertices first
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, mVertex_buffer_object);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

    //** normals next
    if (aNumberOfNormals > 0){
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, mNormal_buffer_object);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    }
    //** UVs last
    if (aNumberOfUVs > 0){
    glEnableVertexAttribArray(2);
    glBindBuffer(GL_ARRAY_BUFFER, mUV_buffer_object);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    }
    //** indexing for reusing vertices in triangle-meshes
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndex_buffer_object);


    //** check errors and store the number of vertices
    if (aux::checkGlErrors(__LINE__, __FILE__))assert(false);
    mNumVert = aNumberOfVertices;
    mNumNormals = aNumberOfNormals;
    mNumUVs = aNumberOfUVs;
    mNumIndices = aNumberOfIndices;

}

上記のコードは次のように呼び出されます。

//read vertices from file
std::vector<float> vertex, normal, uv;
std::vector<unsigned int> index;
//assimp-wrapping function to load obj to vectors
aux::loadObjToVectors("Resources\\vertices\\model.obj", vertex, normal, index, uv);
mPtr->bufferModel(&vertex[0], static_cast<int>(vertex.size()) / 3, &normal[0], static_cast<int>(normal.size()) / 3, &uv[0], static_cast<int>(uv.size()) / 2, &index[0], static_cast<int>(index.size()));

次に、シェーダー部分が来ます。頂点シェーダーでは、UV 座標レイヤーを渡すだけです。

#version 400 core
layout (location = 0) in vec3 vertexPosition_modelspace;
layout (location = 1) in vec3 vertexNormal_modelspace;
layout (location = 2) in vec2 vertexUV;

out vec2 UV;

[... in main then ...]
UV = vertexUV;

フラグメント シェーダーでピクセルに値を割り当てます。 #version 400 core in vec2 UV; 均一なサンプラー2D textureSampler; レイアウト (場所 = 0) vec4 outColor; [... in main then ...] // おそらくここでもライティングを計算したいので、内部のテクスチャを取得する最も簡単な方法です。 outColor = vec4(texture2D(textureSampler, UV).rgb, cosAngle); //次のコマンドを使用して、UV 座標が正しくバインドされているかどうかを確認することもできます: outColor = vec4(UV.x, UV.y,1,1); //そして、結果の画像のピクセル値をチェックします (たとえば、PBO にレンダリングしてから、CPU にダウンロードします)。

レンダリング ループでは、すべてのユニフォームが正しくバインドされていること (特にテクスチャ関連のもの) と、テクスチャがアクティブでバインドされていることも確認してください。

if (mTextureID != -1) {
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, mTextureID); 
}

GLint textureLocation = glGetUniformLocation(mShaderProgram, "textureSampler");
glUniform1i(textureLocation, 0);
//**set the poligon mode
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//**drawElements because of indexing
glDrawElements(GL_TRIANGLES, mNumIndices, GL_UNSIGNED_INT, 0);

お役に立てれば幸いです。よろしく、VdoP

于 2018-03-09T13:49:08.167 に答える