简体   繁体   中英

Translating a 3d model to 2d using assimp

I'm using c++ to translate a 3d model entered using command line arguments into a 2d picture in assimp. However I'm not sure of the best way to go about it. I have the basic hard coding for to create a set object but I need to redo it using vectors and loops. What's the best way to go about it?

void createSimpleQuad(Mesh &m) {
  // Clear out vertices and elements
  m.vertices.clear();
  m.indices.clear();

  // Create four corners
  Vertex upperLeft, upperRight;
  Vertex lowerLeft, lowerRight;
  Vertex upperMiddle;

  // Set positions of vertices
  // Note: glm::vec3(x, y, z)
  upperLeft.position = glm::vec3(-0.5, 0.5, 0.0);
  upperRight.position = glm::vec3(0.5, 0.5, 0.0);
  lowerLeft.position = glm::vec3(-0.5, -0.5, 0.0);
  lowerRight.position = glm::vec3(0.5, -0.5, 0.0);
  upperMiddle.position = glm::vec3(-0.9, 0.5, 0.0);

  // Set vertex colors (red, green, blue, white)
  // Note: glm::vec4(red, green, blue, alpha)
  upperLeft.color = glm::vec4(1.0, 0.0, 0.0, 1.0);
  upperRight.color = glm::vec4(0.0, 1.0, 0.0, 1.0);
  lowerLeft.color = glm::vec4(0.0, 0.0, 1.0, 1.0);
  lowerRight.color = glm::vec4(1.0, 1.0, 1.0, 1.0);
  upperMiddle.color = glm::vec4(0.5, 0.15, 0.979797979, 1.0);

  // Add to mesh's list of vertices
  m.vertices.push_back(upperLeft);
  m.vertices.push_back(upperRight); 
  m.vertices.push_back(lowerLeft);
  m.vertices.push_back(lowerRight);
  m.vertices.push_back(upperMiddle);

  // Add indices for two triangles
  m.indices.push_back(0);
  m.indices.push_back(3);
  m.indices.push_back(1);

  m.indices.push_back(0);
  m.indices.push_back(2);
  m.indices.push_back(3);

  m.indices.push_back(0);
  m.indices.push_back(2);
  m.indices.push_back(4);
}

If you want to generate a 2D-picture out of a 3D-Model you need to:

  1. Import the model
  2. Render it via a common render-lib into a texture or manually by using our viewer and take a snapshot

At this moment there is no post-process to generate a 2D-View automatically in Assimp.

But when you want to do this with your own render-code this is not so hard to do. After importing your model you have to:

  1. Get the bounding box for your imported asset, just check the opengl-samples in the assimp-repo for some tips
  2. Calculate the diameter for this bounding box.
  3. Create a camera, for OpenGL you can use glm for calculating the View-Matrix
  4. Place the asset at (0|0|0) world coordinate system
  5. Move your camera by the diameter at let it view onto (0|0|0)
  6. Render the view into a 2D-Texture or just take a screenshot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM