使用 assimp 将 3d 模型转换为 2d

时间:2021-04-06 18:17:33

标签: c++ loops assimp

我正在使用 C++ 将使用命令行参数输入的 3d 模型转换为 assimp 中的 2d 图片。但是,我不确定最好的方法。我有用于创建集合对象的基本硬编码,但我需要使用向量和循环重做它。最好的方法是什么?

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);
}

1 个答案:

答案 0 :(得分:0)

如果您想从 3D 模型中生成 2D 图片,您需要:

  1. 导入模型
  2. 通过通用渲染库将其渲染为纹理或使用我们的查看器手动渲染并拍摄快照

目前在 Assimp 中没有自动生成 2D 视图的后期处理。

但是当你想用你自己的渲染代码来做这件事时,这并不难。导入模型后,您必须:

  1. 获取导入资产的边界框,只需查看 assimp-repo 中的 opengl-samples 以获取一些提示
  2. 计算此边界框的直径。
  3. 创建一个摄像头,对于 OpenGL,您可以使用 glm 来计算视图矩阵
  4. 将资产放置在 (0|0|0) 世界坐标系
  5. 将您的相机按直径移动,让它看到 (0|0|0)
  6. 将视图渲染为 2D 纹理或仅截取屏幕截图