0

In my program (C++/OpenGL/Assimp/Windows10), I load and display a .OBJ file with its corresponding .MTL

It all works fine, but I need to:

  1. load from memory instead of whatever is written in the .MTL file (ex : map_Kd output.jpg).
  2. I need to change the texture in runtime.

This is my LoadOpenGLTexture(), I am using DevIL to load the textures (going to change to OpenCV).

bool Model::LoadGLTextures(const aiScene* scene)
{
    ILboolean success;

    ilInit();

    for (unsigned int m = 0; m < scene->mNumMaterials; ++m)
    {
        int texIndex = 0;
        aiString path;  // filename

        aiReturn texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, 
        &path);
        while (texFound == AI_SUCCESS)
        {
            //fill map with textures, OpenGL image ids set to 0
            textureIdMap[path.data] = 0;
            // more textures?
            texIndex++;
            texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, &path);
        }
    }

    int numTextures = textureIdMap.size();

    /* create and fill array with DevIL texture ids */
    ILuint* imageIds = new ILuint[numTextures];
    ilGenImages(numTextures, imageIds);

    /* create and fill array with GL texture ids */
    GLuint* textureIds = new GLuint[numTextures];
    glGenTextures(numTextures, textureIds); /* Texture name generation */

    /* get iterator */
    std::map<std::string, GLuint>::iterator itr = textureIdMap.begin();
    int i = 0;
    for (; itr != textureIdMap.end(); ++i, ++itr)
    {
        //save IL image ID
        std::string filename = (*itr).first;  // get filename
        (*itr).second = textureIds[i];    // save texture id for filename in map

        ilBindImage(imageIds[i]); /* Binding of DevIL image name */
        ilEnable(IL_ORIGIN_SET);
        ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
        success = ilLoadImage((ILstring)filename.c_str());

        if (success) {
            /* Convert image to RGBA */
            ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

            /* Create and load textures to OpenGL */
            glBindTexture(GL_TEXTURE_2D, textureIds[i]);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), 
            ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE, ilGetData());
        }
        else
            printf("Couldn't load Image: %s\n", filename.c_str());
    }
    /* Because we have already copied image data into texture data
    we can release memory used by image. */
    ilDeleteImages(numTextures, imageIds);

    //Cleanup
    delete[] imageIds;
    delete[] textureIds;

    //return success;
    return true;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Mrax
  • 63
  • 9

1 Answers1

2

At the moment the texture-definition in the Asset-Importer-Lib does not provide such a dynamic update for texture- / material-definitions. Each definition of a texture stays stable during the runtime of your application, you cannont change it. I you are interested in such a feature we need to update the material-system in a proper way which makes this work for the user. So if you are interested in this feature it would be great to get a better understanding of your requirements / use-case.

You can open a feature-request on our project-side for it, see: Assimp on Github

KimKulling
  • 2,654
  • 1
  • 15
  • 26
  • Oh boy ... perhaps ... would you know any open source alternative for this specific issue ? And thanks for your time. – Mrax Dec 01 '19 at 22:00
  • You can just update the textue data in your render-engine? – KimKulling Dec 02 '19 at 06:01
  • I am not sure if I understood, I tried to update my texture, but nothing change into my object. – Mrax Dec 02 '19 at 12:54
  • You have to update the texture on your GPU, not in the Assimp-datastructure. – KimKulling Dec 02 '19 at 13:06
  • you mean using my shaders ? – Mrax Dec 03 '19 at 18:08
  • Sorry for the dumb question, it is actually pretty easy to change using shaders. But I don't understand what is the difference now, to change using shaders or using texture-definition in the Asset-Importer-Lib ? – Mrax Dec 07 '19 at 23:04