Drawing a collection of 3D models at the vertices of another 3D model

Hello all,

I’m trying to draw some 3D models that I’m loading with ofxAssimpModelLoader. I’m able to draw the first 3D model successfully as I followed the assimpLoader example in oF. However, I need to draw a collection of the second 3D model at each of the vertex of the first 3D model. Here’s my code.

#include "ofApp.h"

CoinObject::CoinObject() {
  model.loadModel("HighCoin.obj", true);
  model.setRotation(0, 0, 0, 0, 0);
  model.setScale(0.9, 0.9, 0.9);
}

//--------------------------------------------------------------
void ofApp::setup(){
  ofBackground(ofColor::black);
  ofSetVerticalSync(true);
  
  // Load the 1st 3D model.
  humanModel.loadModel("Breathing.dae", false);
  humanModel.setPosition(0, 0, 0);
  humanModel.setRotation(0, 180, 0, 0, 1);
  humanModel.setScale(1.5, 1.5, 1.5);
  humanModel.setLoopStateForAllAnimations(OF_LOOP_NORMAL);
  humanModel.setPausedForAllAnimations(false);
  humanModel.playAllAnimations();
  humanMesh = humanModel.getCurrentAnimatedMesh(0);
  
  topLight.setPosition(0, 500, 500);
  bottomLight.setPosition(0, -500, 500);
  
  ofEnableBlendMode(OF_BLENDMODE_ALPHA);
  
  // Create a collection of 2nd 3D model for each vertex of the mesh.
  auto vertexCount = humanMesh.getVertices().size();
  for (int i = 0; i < vertexCount; i++) {
    CoinObject  *coin = new CoinObject();
    coins.push_back(coin);
  }
}

//--------------------------------------------------------------
void ofApp::update(){
  humanModel.update();
  humanMesh = humanModel.getCurrentAnimatedMesh(0);
}

//--------------------------------------------------------------
void ofApp::draw(){
  ofEnableDepthTest();
    ofEnableLighting();
      topLight.enable();
      bottomLight.enable();
      ofPushMatrix();
        cam.begin();
          drawMeshVertices();
        cam.end();
      ofPopMatrix();
      bottomLight.disable();
      topLight.disable();
    ofDisableLighting;
  ofDisableDepthTest();
}

void ofApp::drawMeshVertices() {
  ofPushMatrix();
    ofFill();
    ofSetColor(ofColor::red);
    ofxAssimpMeshHelper & meshHelper = humanModel.getMeshHelper(0);
    ofMultMatrix(humanModel.getModelMatrix());
    ofMultMatrix(meshHelper.matrix);
    for (int i = 0; i < humanMesh.getVertices().size(); i++) {
      // None of the coins can be seen on the screen. 
      coins[i] -> model.drawFaces();
    }
  ofPopMatrix();
}

I need to call ofMultMatrix twice to draw the mesh for the 1st 3D model. I tried calling that for the 2nd model as well before the drawFaces call on the coin model, but nothing shows up on the screen. Any suggestion/advice? Thanks in advance :slight_smile:

I’m suspicious that the coordinate system of the 2nd 3D model is not the same as the 1st one and thus I don’t see it. One thing that I tried which works is using regular ofDraw calls to draw sphere, circle, etc.

void ofApp::drawMeshVertices() {
  ofPushMatrix();
    ofFill();
    ofSetColor(ofColor::red);
    ofxAssimpMeshHelper & meshHelper = humanModel.getMeshHelper(0);
    ofMultMatrix(humanModel.getModelMatrix());
    ofMultMatrix(meshHelper.matrix);
    ofSetColor(ofColor::red); 
    for (int i = 0; i < humanMesh.getVertices().size(); i++) {
      ofPushMatrix();
      ofTranslate(humanMesh.getVertices()[i]);
      ofDrawSphere(0, 0, 0.001);
      ofPopMatrix();
    }
  ofPopMatrix();
}

Translating at that vertex and drawing a shape using ofDraw… works. But if I create something like ofCylinderPrimitive and set the position of the vertex of the mesh, then nothing shows up. It’s somehow related to the coordinate space in which the primitive object exists because the actual position of these vertices is really small as compared to the size of the model that gets drawn.

Any inputs/help will be really appreciated.

I found this helper function to convert the mesh coordinates from an assimp model to the real world coordinates. This solved my question.

   int meshIndex = 0;
    ofMatrix4x4 modelMatrix = model.getModelMatrix();
    ofMatrix4x4 meshMatrix = model.getMeshHelper(meshIndex).matrix;
    ofMatrix4x4 concatMatrix;
    concatMatrix.preMult(modelMatrix);
    concatMatrix.preMult(meshMatrix);

    ofMesh mesh = model.getMesh(meshIndex);

    for(int i=0; i<mesh.getNumVertices(); i++) {
        ofVec3f & vert = mesh.getVertices()[i];
        vert.set(concatMatrix.preMult(vert));
    }