优雅的方式找到立方体的顶点

dan*_*jar 9 c++ opengl algorithm cube vertices

几乎每个OpenGL教程都允许您实现绘制多维数据集.因此需要立方体的顶点.在示例代码中,我看到了一个定义每个顶点的长列表.但我想计算一个立方体的顶点,而不是使用预先计算的坐标的超长列表.

立方体由八个顶点和十二个三角形组成.顶点由x,y和z定义.三角形由三个顶点的索引定义.

有一种优雅的方法来计算立方体的顶点和元素索引吗?

Sor*_*ror 5

当我将csg.js项目“移植”到 Java 时,我发现了一些可爱的代码,它们生成具有选定中心点和半径的立方体。(我知道这是JS,但无论如何)

// Construct an axis-aligned solid cuboid. Optional parameters are `center` and
// `radius`, which default to `[0, 0, 0]` and `[1, 1, 1]`. The radius can be
// specified using a single number or a list of three numbers, one for each axis.
// 
// Example code:
// 
//     var cube = CSG.cube({
//       center: [0, 0, 0],
//       radius: 1
//     });
CSG.cube = function(options) {
  options = options || {};
  var c = new CSG.Vector(options.center || [0, 0, 0]);
  var r = !options.radius ? [1, 1, 1] : options.radius.length ?
           options.radius : [options.radius, options.radius, options.radius];
  return CSG.fromPolygons([
    [[0, 4, 6, 2], [-1, 0, 0]],
    [[1, 3, 7, 5], [+1, 0, 0]],
    [[0, 1, 5, 4], [0, -1, 0]],
    [[2, 6, 7, 3], [0, +1, 0]],
    [[0, 2, 3, 1], [0, 0, -1]],
    [[4, 5, 7, 6], [0, 0, +1]]
  ].map(function(info) {
    return new CSG.Polygon(info[0].map(function(i) {
      var pos = new CSG.Vector(
        c.x + r[0] * (2 * !!(i & 1) - 1),
        c.y + r[1] * (2 * !!(i & 2) - 1),
        c.z + r[2] * (2 * !!(i & 4) - 1)
      );
      return new CSG.Vertex(pos, new CSG.Vector(info[1]));
    }));
  }));
};
Run Code Online (Sandbox Code Playgroud)