• fullscreen
  • MeshAlignToAxis.pde
  • /**
     * This example shows how to dynamically create a simple box mesh and align it
     * with a given direction vector using the pointTowards() method of the
     * TriangleMesh class.
     */
    
    /* 
     * Copyright (c) 2010 Karsten Schmidt
     * 
     * This library is free software; you can redistribute it and/or
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
     * version 2.1 of the License, or (at your option) any later version.
     * 
     * http://creativecommons.org/licenses/LGPL/2.1/
     * 
     * This library is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
     * 
     * You should have received a copy of the GNU Lesser General Public
     * License along with this library; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     */
    
    import toxi.geom.*;
    import toxi.geom.mesh.*;
    import toxi.processing.*;
    
    Vec3D BOX_SIZE = new Vec3D(5,5,20);
    float SCALE=130;
    
    TriangleMesh[] boxes=new TriangleMesh[300];
    ToxiclibsSupport gfx;
    
    void setup() {
      size(680,382,P3D);
      gfx=new ToxiclibsSupport(this);
      for(int i=0; i<boxes.length; i++) {
        // create a new direction vector for each box
        Vec3D dir=new Vec3D(cos(i*TWO_PI/75),sin(i*TWO_PI/50),sin(i*TWO_PI/25)).normalize();
        // create a position on a sphere, using the direction vector
        Vec3D pos=dir.scale(SCALE);
        // create a box mesh at the origin
        TriangleMesh b=new AABB(new Vec3D(), BOX_SIZE).toMesh();
        // align the Z axis of the box with the direction vector
        b.pointTowards(dir);
        // move the box to the correct position
        b.transform(new Matrix4x4().translateSelf(pos.x,pos.y,pos.z));
        boxes[i]=b;
      }
    }
    
    void draw() {
      background(51);
      lights();
      translate(width / 2, height / 2, 0);
      rotateX(mouseY * 0.01f);
      rotateY(mouseX * 0.01f);
      noStroke();
      for(int i=0; i<boxes.length; i++) {
        gfx.mesh(boxes[i]);
      }
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    toxiclibs

    MeshAlignToAxis

    Add to Faves Me Likey@! 4
    You must login/register to add this sketch to your favorites.

    This example shows how to dynamically create a simple box mesh and align it with a given direction vector using the pointTowards() method of the TriangleMesh class.

    Marius Watz plus+
    30 Dec 2010
    Very nice. Any chance you have a general example of how to calculate the rotation angles needed to align a shape with a direction vector?

    I've tried different hacks to sweep a profile along a path to create a mesh. Sadly, my efforts so far all suffer from gimbal lock issues...
    toxiclibs
    1 Jan 2011
    Hey, thanks Marius! Here's a HNY present for that matter, also using quaternions...

    http://www.openprocessing.org/visuals/?visualID=17229

    See if that helps. It might not be 100% what you're after since I'm only using the rotation around an axis vector, but AFAIK it's the only way to avoid gimbal locking. You could extract the individual axis rotations from the alignment matrix manually, though...
    Marius Watz plus+
    1 Jan 2011
    Perfect, that is exactly what I'm looking for. That way I can reposition a profile and rotate according to the direction of the path to create a sweeped mesh. Thanks, Karsten!
    Marius Watz plus+
    12 Jan 2011
    This code no longer works under Toxiclibs 0020, gives "cannot convert from Mesh3D to TriangleMesh" in the following line:

    TriangleMesh b=new AABB(new Vec3D(), BOX_SIZE).toMesh();
    Marius Watz plus+
    12 Jan 2011
    Easy fix: Cast the result to (TriangleMesh), i.e.

    TriangleMesh b=(TriangleMesh)new AABB(new Vec3D(), BOX_SIZE).toMesh();
    toxiclibs
    12 Jan 2011
    Yeah, sorry about that.. this is because I introduced the generic Mesh3D interface for the future and the toMesh() methods all return this now. The benefit though is that you can now also choose for most of them what kind of triangle mesh you want, the "normal" TriangleMesh or the WETriangleMesh. The latter allows for subdivisions and vertex navigation etc.

    Btw. The example source code is only outdated on this site, the example is also bundled with the 0020 release and has been updated... Hth!
    You need to login/register to comment.