I have spent some time with this and not come up with any answers. I have posted a MCVE below using ofxAssimpModelLoader and ofxBullet. I am unable to upload the .obj and .mtl files. The one I am using has eight meshes consisting of differently coloured spheres and cylinders. assimpModel.getMesh(i)
does not have setScale or setPosition methods so I am not sure how to change them. I tried saving it to an ofMesh but again, no setScale or setPosition methods were available there either.
In the example below, assimpModel.drawFaces()
works, as does the method you suggested using getMesh() (although I do not know how to scale or reposition it). The code from the ofxBullet CustomShapesExample still does not work. I get write access violations when the compiler hits the line: customModel[i]->init((btCompoundShape*)customModel[0]->getCollisionShape(), customModel[0]->getCentroid())
;. I am not sure why, because customModel[0]->getNumChildShapes
returns an ‘8’.
I am prepared to settle for spheres for now - obviously I’m on the steep part of the learning curve here. Thank you for any help.
ofApp.h:
#pragma once
#include "ofMain.h"
#include "ofxAssimpModelLoader.h"
#include "ofxBullet.h"
class ofApp : public ofBaseApp{
public:
(...)
ofxBulletWorldRigid world;
ofEasyCam camera;
ofLight light;
ofxAssimpModelLoader assimpModel;
vector<ofxBulletCustomShape*> customModel;
};
ofApp.cpp:
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(0);
camera.setPosition(ofVec3f(0.f, -10.f, -100.f));
camera.lookAt(ofVec3f(0, 0, 0), ofVec3f(0, -1, 0));
world.setup();
world.enableGrabbing();
world.setCamera(&camera);
world.setGravity(glm::uvec3(0., 160., 0.));
ofVec3f scale(0.1, 0.1, 0.1);
assimpModel.loadModel("model-01-02.obj", true);
assimpModel.setScale(scale.x, scale.y, scale.z);
assimpModel.setPosition(0,0,0);
// create and add custom model to the scene
ofQuaternion startRot = ofQuaternion(0., 1., 0., PI);
ofVec3f startLoc = ofVec3f(0., -30., 0.);
customModel.resize(2);
/*
for (int j = 0; j < customModel.size(); j++) {
customModel[0]->addMesh(assimpModel.getMesh(j), scale, true);
}
customModel[0]->create(world.world, startLoc, startRot, 3.);
customModel[0]->add();
*/
// code from CustomShapesExample
for (int i = 0; i < 1; i++) {
customModel[0] = new ofxBulletCustomShape();
if (i == 0) {
for (int j = 0; j < assimpModel.getNumMeshes(); j++) {
customModel[i]->addMesh(assimpModel.getMesh(j), scale, true);
}
}
else {
customModel[i]->init((btCompoundShape*)customModel[0]->getCollisionShape(),
+ customModel[0]->getCentroid()); //program halts here
}
customModel[i]->create(world.world, startLoc, startRot, 3.);
customModel[i]->add();
}
cout << customModel[0]->getNumChildShapes() << endl; // returns '8'
}
//--------------------------------------------------------------
void ofApp::update(){
world.update();
}
//--------------------------------------------------------------
void ofApp::draw(){
ofEnableDepthTest();
camera.begin();
// this works
ofSetColor(255, 0, 0);
assimpModel.drawFaces();
ofEnableLighting();
light.enable();
light.setPosition(0,-100,0);
ofSetColor(0, 0, 255);
customModel[0]->draw(); // does not work
//customModel[1]->draw();
light.disable();
ofDisableLighting();
// this works
// drawing a custom model
for (size_t i{ 0 }; i <= assimpModel.getMeshCount(); ++i) {
ofMaterial material = assimpModel.getMaterialForMesh(i);
ofColor col = material.getDiffuseColor();
ofSetColor(col);
assimpModel.getMesh(i).drawFaces();
}
camera.end();
ofDisableDepthTest();
}