

import processing.opengl.*;
void setup(){
size(100,100,OPENGL);
}

import unlekker.util.*;
import unlekker.modelbuilder.*;
import ec.util.*;
// unlekkerLib - Marius Watz, workshop.evolutionzone.com
// Example showing how to write geometry to a STL file.
import unlekker.data.*;
void setup() {
size(400,400, P3D);
noLoop();
}
void draw() {
background(100);
// Initialize STL output
beginRaw("unlekker.data.STL","myFile.stl");
translate(width/2,height/2,-width/2);
sphere(width/2);
// End STL output
endRaw();
}
import toxi.geom.*;
import toxi.geom.mesh.*;
import toxi.processing.*;
TriangleMesh mesh;
ToxiclibsSupport gfx;
void setup() {
size(400, 400, P3D);
gfx=new ToxiclibsSupport(this);
}
void draw() {
// define a rounded cube using the SuperEllipsoid surface function
SurfaceFunction f=new SuperEllipsoid(width/(mouseX+10), height/(mouseY+10));
SurfaceMeshBuilder b = new SurfaceMeshBuilder(f);
// execute the mesh (resolution=80, radius=100)
mesh = (TriangleMesh)b.createMesh(null, 30, 60);
background(128);
fill(255);
stroke(0);
lights();
translate(width / 2, height / 2, 0);
rotateX(mouseY * 0.01f);
rotateY(mouseX * 0.01f);
gfx.mesh(mesh);
}
void keyPressed() {
if (key=='e') {
try {
String fileID="shape-"+(System.currentTimeMillis()/1000);
FileOutputStream fs;
fs=new FileOutputStream(sketchPath(fileID+".stl"));
mesh.saveAsSTL(fs);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
import processing.opengl.*;
import toxi.geom.*;
import toxi.geom.mesh.*;
import toxi.processing.*;
TriangleMesh mesh = new TriangleMesh();
ToxiclibsSupport gfx;
int i=0;
void setup() {
size(400,400,OPENGL);
gfx=new ToxiclibsSupport(this);
}
void draw() {
background(51);
lights();
translate(width / 2, height / 2, 0);
rotateX(mouseY * 0.01f);
noStroke();
Vec3D BOX_SIZE = new Vec3D(mouseX/2,mouseY/2,mouseX/2);
TriangleMesh b=(TriangleMesh)new AABB(new Vec3D(), BOX_SIZE).toMesh();
gfx.mesh(b);
}
void keyPressed() {
i++;
if (key == 's') {
mesh.saveAsSTL(sketchPath("cube"+i+".stl"));
}
}
/** *SphericalHarmonicsMeshBuilder demonstrates how to use the SurfaceMeshBuilder class in conjunction with a spherical harmonics function to dynamically create a variety of organic looking forms. The function is described in detail on Paul Bourke's website. Included is also a re-usable function for displaying a generic TriangleMesh instance using normal mapping. the display of surface normals useful for debug purposes. r: randomize spherical harmonics w: toggle wireframe on/off n: toggle normal vector display on/off s: save current mesh as STL file * Copyright (c) 2010 Karsten Schmidt */ import processing.opengl.*; import toxi.math.waves.*; import toxi.geom.*; import toxi.geom.mesh.*; TriangleMesh mesh = new TriangleMesh(); boolean isWireFrame; boolean showNormals; boolean doSave; int i=0; Matrix4x4 normalMap = new Matrix4x4().translateSelf(128,128,128).scaleSelf(127); void setup() { size(400,400, OPENGL); randomizeMesh(); } void draw() { background(0); translate(width / 2, height / 2, 0); rotateX(mouseY * 0.01f); rotateY(mouseX * 0.01f); lights(); shininess(16); directionalLight(255,255,255,0,-1,1); specular(255); drawAxes(400); if (isWireFrame) { noFill(); stroke(255); } else { fill(255); noStroke(); } drawMesh(g, mesh, !isWireFrame, showNormals); } void drawAxes(float l) { stroke(255, 0, 0); line(0, 0, 0, l, 0, 0); stroke(0, 255, 0); line(0, 0, 0, 0, l, 0); stroke(0, 0, 255); line(0, 0, 0, 0, 0, l); } void drawMesh(PGraphics gfx, TriangleMesh mesh, boolean vertexNormals, boolean showNormals) { gfx.beginShape(PConstants.TRIANGLES); AABB bounds=mesh.getBoundingBox(); Vec3D min=bounds.getMin(); Vec3D max=bounds.getMax(); if (vertexNormals) { for (Iterator i=mesh.faces.iterator(); i.hasNext();) { Face f=(Face)i.next(); Vec3D n = normalMap.applyTo(f.a.normal); gfx.fill(n.x, n.y, n.z); gfx.normal(f.a.normal.x, f.a.normal.y, f.a.normal.z); gfx.vertex(f.a.x, f.a.y, f.a.z); n = normalMap.applyTo(f.b.normal); gfx.fill(n.x, n.y, n.z); gfx.normal(f.b.normal.x, f.b.normal.y, f.b.normal.z); gfx.vertex(f.b.x, f.b.y, f.b.z); n = normalMap.applyTo(f.c.normal); gfx.fill(n.x, n.y, n.z); gfx.normal(f.c.normal.x, f.c.normal.y, f.c.normal.z); gfx.vertex(f.c.x, f.c.y, f.c.z); } } else { for (Iterator i=mesh.faces.iterator(); i.hasNext();) { Face f=(Face)i.next(); gfx.normal(f.normal.x, f.normal.y, f.normal.z); gfx.vertex(f.a.x, f.a.y, f.a.z); gfx.vertex(f.b.x, f.b.y, f.b.z); gfx.vertex(f.c.x, f.c.y, f.c.z); } } gfx.endShape(); if (showNormals) { if (vertexNormals) { for (Iterator i=mesh.vertices.values().iterator(); i.hasNext();) { Vertex v=(Vertex)i.next(); Vec3D w = v.add(v.normal.scale(10)); Vec3D n = v.normal.scale(127); gfx.stroke(n.x + 128, n.y + 128, n.z + 128); gfx.line(v.x, v.y, v.z, w.x, w.y, w.z); } } else { for (Iterator i=mesh.faces.iterator(); i.hasNext();) { Face f=(Face)i.next(); Vec3D c = f.a.add(f.b).addSelf(f.c).scaleSelf(1f / 3); Vec3D d = c.add(f.normal.scale(20)); Vec3D n = f.normal.scale(127); gfx.stroke(n.x + 128, n.y + 128, n.z + 128); gfx.line(c.x, c.y, c.z, d.x, d.y, d.z); } } } } void keyPressed() { if (key == 'r') { randomizeMesh(); } if (key == 'w') { isWireFrame = !isWireFrame; } if (key == 'n') { showNormals = !showNormals; } if (key == 's') { mesh.saveAsSTL(sketchPath("shape-"+i+".stl")); } } void randomizeMesh() { float[] m=new float[8]; for(int i=0; i<8; i++) { m[i]=(int)random(9); } SurfaceMeshBuilder b = new SurfaceMeshBuilder(new SphericalHarmonics(m)); mesh = (TriangleMesh)b.createMesh(null,80, 40); }
/** * ControlP5basics
* The following example demonstrates the basic use of controlP5.
* After initializing controlP5 you can add controllers to controlP5. * Here we use three numberboxes, one slider and one textfield. * The numberbox with name numberboxC will trigger function numberboxC() * in the example below. Whenever controlP5 detects a function in your * sketch that corresponds to the name of a controller, it will forward * an event to that function. Any event triggered by a controller * will be forwarded to function controlEvent in your sketch.
* related examples ControlP5numberbox, ControlP5slider, ControlP5textfield
* by Andreas Schlegel 2010
* */ import controlP5.*; ControlP5 controlP5; public int myColorRect = 200; public int myColorBackground = 100; void setup() { size(400,400); frameRate(25); controlP5 = new ControlP5(this); controlP5.addNumberbox("numberboxA",myColorRect,100,140,100,14).setId(1); controlP5.addNumberbox("numberboxB",myColorBackground,100,180,100,14).setId(2); controlP5.addNumberbox("numberboxC",0,100,220,100,14).setId(3); controlP5.addSlider("sliderA",100,200,100,100,260,100,14).setId(4); controlP5.addTextfield("textA",100,290,100,20).setId(5); controlP5.controller("numberboxA").setMax(255); controlP5.controller("numberboxA").setMin(0); } void draw() { background(myColorBackground); fill(myColorRect); rect(0,0,width,100); } public void numberboxC(int theValue) { println("### got an event from numberboxC : "+theValue); } // a slider event will change the value of textfield textA public void sliderA(int theValue) { ((Textfield)controlP5.controller("textA")).setValue(""+theValue); } // for every change in textfield textA, this function will be called public void textA(String theValue) { println("### got an event from textA : "+theValue); } public void controlEvent(ControlEvent theEvent) { println("got a control event from controller with id "+theEvent.controller().id()); switch(theEvent.controller().id()) { case(1): // numberboxA myColorRect = (int)(theEvent.controller().value()); break; case(2): // numberboxB myColorBackground = (int)(theEvent.controller().value()); break; }
import shapes3d.utils.*;
import shapes3d.*;
import processing.opengl.*;
import unlekker.util.*;
import unlekker.modelbuilder.*;
import ec.util.*;
import unlekker.data.*;
int i=0;
Box box;
Ellipsoid sphere;
float a;
void setup() {
size(200, 200, OPENGL);
cursor(CROSS);
}
void draw() {
float size=map(mouseX, 0, width, 10, width/8);
//controls slices (north to south)
int slices=10;
//controls segments (east to west)
int segments=(int)map(mouseY, 0, height, 10, height/2);
box=new Box(this, size, size, size);
box.moveTo(0, size, 0);
box.fill(255);
box.stroke(color(64, 0, 64));
box.strokeWeight(1.2);
box.drawMode(Shape3D.SOLID | Shape3D.WIRE);
sphere=new Ellipsoid(this, slices, segments);
sphere.setRadius(size);
sphere.moveTo(0, -size/4, 0);
sphere.fill(255);
sphere.stroke(color(64, 0, 64));
sphere.strokeWeight(1.2);
sphere.drawMode(Shape3D.SOLID | Shape3D.WIRE);
background(128);
pushMatrix();
camera(70 * sin(a), 10, 70 * cos(a), 0, 0, 0, 0, 1, 0);
ambientLight(200, 200, 200);
directionalLight(128, 128, 128, -1, 0, -1);
box.draw();
sphere.draw();
popMatrix();
a += 0.002;
}
void keyPressed() {
if (key == 'e') {
i++;
beginRaw("unlekker.data.STL", "shape"+i+".stl");
pushMatrix();
camera(70 * sin(a), 10, 70 * cos(a), 0, 0, 0, 0, 1, 0);
ambientLight(200, 200, 200);
directionalLight(128, 128, 128, -1, 0, -1);
box.draw();
sphere.draw();
popMatrix();
endRaw();
}
}