Load obj and mtl files via ofxAssimpModelLoader

I followed assimpExample to load obj and mtl files but 3D model is shown only in white color.

  
  
void testApp::draw(){  
//    ofSetColor(255);  
      
    ofPushMatrix();  
    ofTranslate(model.getPosition().x+100, model.getPosition().y, 0);  
    ofRotate(-mouseX, 0, 1, 0);  
    ofTranslate(-model.getPosition().x, -model.getPosition().y, 0);  
    material = model.getMaterialForMesh(0);  
    material.begin();  
    model.enableMaterials();  
    model.enableTextures();  
    model.enableColors();  
    model.draw(OF_MESH_FILL);  
    material.end();  
//    material = model.getMaterialForMesh(0);  
//    ofLogVerbose(ofToString(material.getDiffuseColor()));  
//    ofLogVerbose(ofToString(material.getEmissiveColor()));  
//    ofLogVerbose(ofToString(material.getAmbientColor()));  
//    ofLogVerbose(ofToString(material.getSpecularColor()));  
    ofPopMatrix();  
      
    if(ofGetGLProgrammableRenderer()){  
		glPushAttrib(GL_ALL_ATTRIB_BITS);  
		glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);  
    }  
  
    glEnable(GL_NORMALIZE);  
  

I thought maybe I should load mtl file manually? ofMaterial seems relevant but I cannot find any useful examples. It would be really basic question but I would appreciate any guidance!! Thanks :slight_smile:

ofMaterial is really just the basic OpenGL material so it’s not going to help a ton. I think what people usually do is get the material as a texture in their model and then apply it, rather than trying to use an MTL file (I could be wrong about that though). What are you using to generate your model?

Thanks for the reply! I am using obj file I downloaded from the web. I just replaced astro example with my file name as follows:

  
  
void testApp::setup(){  
    ofSetLogLevel(OF_LOG_VERBOSE);  
    ofBackground(255);  
      
    ofDisableArbTex();  
    //some model / light stuff  
    ofEnableDepthTest();  
    model.loadModel("battlecruiser/Endor Battlecruiser.obj",true);  
    model.setPosition(ofGetWidth()/2, (float)ofGetHeight() * 0.75 , 0);  
      
    //model.setLoopStateForAllAnimations(OF_LOOP_NORMAL);  
    //model.playAllAnimations();  
      
    ofEnableBlendMode(OF_BLENDMODE_ALPHA);  
    ofEnableDepthTest();      
    glShadeModel(GL_SMOOTH);  
      
    light.enable();  
    ofEnableSeparateSpecularLight();  
}  
  

Could you elaborate how to get the material for a texture in my model? Thanks!

Well, a material and a texture are different things. I think your model might have textures associated with it. MTL files are (I’m pretty sure) specific to Maya/3DS and aren’t something that Assimp reads properly. If you have a 3D modeling program you should be able to generate and correctly associate a texture with a model but that’s a little outside of my area of expertise.

Thanks very much for your answer! I guess I just need to download 3DS files instead of obj files to use ofx3DModelLoader.

Hey,

If your using assimp to load an .obj, assimp loads the .mtl file automatically. If your not seeing the texture, it’s probably because the image path in the .mtl is absolute or the image file is in a sub folder. Try opening up the .mtl in a text editor and making sure the path/s to your images are relative and the image files are in the same folder as the .mtl. Also, you don’t need to create an ofMaterial or ofTexture with assimp.

Good luck.

Hi,

I am having some problems related to this as well. I’ve searched for answers but did not encounter anything helpful. I am trying to load a .obj file exported with a material .mtl file from blender. It is a santa hat, this model from here. Note that the model does not have a texture, just two diffuse materials with different colors.
The model is loaded and displayed correctly, however with no corresponding colour.
The assimp example from OF only has examples that include textures, does anyone have experience in loading .obj files with material and no texture? I also double checked that the paths in the .obj file are relative and point to the corresponding .mtl file.

Here is the code:

void ofApp::setup() { 

santaHat.loadModel("haide.obj", true);
santaHat.setPosition(300, 300, 0);
santaHat.setScale(0.3, 0.3,0.3);

ofEnableBlendMode(OF_BLENDMODE_ALPHA);
ofEnableDepthTest();
glShadeModel(GL_SMOOTH);

light.enable();
ofEnableSeparateSpecularLight();  
}

void ofApp::draw() {
santaHat.drawFaces(); 
}

Regards,
Andrei

are you sure? cuz i tried it and didn’t work.

void ofApp::setup(){
ofBackground(255);
ofEnableDepthTest();
ofDisableArbTex();

}

void ofApp::draw(){
ofxAssimpModelLoader model;
model.loadModel(“teste.obj”);
model.setScale(0.1, 0.1, 0.1);
model.setPosition(20, 20, 20);
model.drawFaces();

}

mtl file:

newmtl Material__25
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.5880 0.5880 0.5880
Kd 0.5880 0.5880 0.5880
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000
map_Ka of.png
map_Kd of.png

Sorry for late reply - which OF version are you using? This fix was added to the master branch back in July 2014 (https://github.com/openframeworks/openFrameworks/pull/3099) but I don’t think it’s in the 0.8.4 download.