12

Nearly every OpenGL tutorial lets you implement drawing a cube. Therefore the vertices of the cube are needed. In the example code I saw a long list defining every vertex. But I would like to compute the vertices of a cube rather that using a overlong list of precomputed coordinates.

A cube is made of eight vertices and twelve triangles. Vertices are defined by x, y, and z. Triangles are defined each by the indexes of three vertices.

Is there an elegant way to compute the vertices and the element indexes of a cube?

danijar
  • 32,406
  • 45
  • 166
  • 297
  • 7
    It takes more code to generate a cube than it does to just write out 8 vertices. – Pubby Dec 19 '12 at 13:36
  • 2
    Just specifying the vertices and elements is by far the easiest solution. – Andreas Brinck Dec 19 '12 at 13:47
  • Sad to head that, but you might be right. – danijar Dec 19 '12 at 13:54
  • @Pubby I think that he is trying to dynamically generate the vertices for things such as moving vertices. – ihsoy ih Dec 19 '12 at 13:54
  • 2
    I think this is a very valid question, and I am sure you can generate the vertices and vertex indices. – Jakob Dec 19 '12 at 13:57
  • 4
    @YoshiHi. Movement and Rotation is done later by matrix calculation. It is just about generating vertices and indices of a cube. – danijar Dec 19 '12 at 14:01
  • 1
    That looks like a nice entry for [code golf](http://codegolf.stackexchange.com/). – Kos Dec 19 '12 at 14:09
  • Also: The solution will vary whether you want to only generate 8 vertices (position only), or 24 vertices (duplicated positions to also handle normal vectors or texture coords) – Kos Dec 19 '12 at 14:13
  • I want to generate 8 vertices and define the triangles by indices. No normals or texture coordinates. – danijar Dec 19 '12 at 14:18
  • Seems to me, that if scaling/rotation/translation are all handled later, generating the vertices of a cube (centered at origin and aligned to axes) should just be a trivial `{x,y,z}={+k,-k}`... – twalberg Dec 19 '12 at 15:47
  • It is not only about the vertices but also about the indices of the triangles. – danijar Dec 19 '12 at 15:54
  • @Kos. Here it is http://codegolf.stackexchange.com/questions/9331/find-vertices-of-a-cube – danijar Dec 29 '12 at 10:03
  • @Kos. Sorry I deleted it later because it was no well suited definition of task. – danijar Dec 29 '12 at 23:30

2 Answers2

4

When i was "porting" the csg.js project to Java I've found some cute code which generated cube with selected center point and radius. (I know it's JS, but anyway)

// 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]));
    }));
  }));
};
Sorceror
  • 4,835
  • 2
  • 21
  • 35
0

I solved this problem with this piece code (C#):

public CubeShape(Coord3 startPos, int size) {
    int l = size / 2;
    verts = new Coord3[8];
    for (int i = 0; i < 8; i++) {
        verts[i] = new Coord3(
            (i & 4) != 0 ? l : -l,
            (i & 2) != 0 ? l : -l,
            (i & 1) != 0 ? l : -l) + startPos;
    }

    tris = new Tris[12];
    int vertCount = 0;
    void AddVert(int one, int two, int three) =>
        tris[vertCount++] = new Tris(verts[one], verts[two], verts[three]);
        
    for (int i = 0; i < 3; i++) {
        int v1 = 1 << i;
        int v2 = v1 == 4 ? 1 : v1 << 1;
        AddVert(0, v1, v2);
        AddVert(v1 + v2, v2, v1);
        AddVert(7, 7 - v2, 7 - v1);
        AddVert(7 - (v1 + v2), 7 - v1, 7 - v2);
    }
}

If you want to understand more of what is going on, you can check out the github page I wrote that explains it.

Catonif
  • 33
  • 1
  • 6