// Digital Material Cube 011 // FabLab Japan // Hiroya Tanaka import java.awt.event.*; // Import Mesh Library import toxi.geom.*; import toxi.geom.mesh.*; import toxi.geom.mesh.subdiv.*; import toxi.processing.*; import toxi.util.*; import toxi.volume.*; import toxi.physics.*; import toxi.physics.behaviors.*; import toxi.physics.constraints.*; VerletPhysics physics; AttractionBehavior inflate; ToxiclibsSupport gfx; WETriangleMesh mesh; boolean mesh_display = true; boolean voxel_display = true; boolean wire_display = true; // Import import processing.opengl.*; import javax.media.opengl.*; import controlP5.*; import javax.swing.*; import SpringGUI.*; SpringGUI gui; // File IO String getFile; String setFile; String ssetFile; String [] txtarray = null; String txt = null; // Field int FIELD_SIZE = 50; int FIELD_STEP = 10; // Pointer int mx = 1; int my = 1; int mz = 1; // Camera float expo = 1; float rotx, roty; float rate = 0.01; // Resolution of Box int inflation = 1; // ControlP5 ControlP5 controlP5; Textfield tfUserName; // テキストフィールド String txts = ""; // Text PFont font; long time; // Unlekker import unlekker.data.*; import unlekker.geom.*; STL stl; FaceList poly; int faces; // メッシュの数 // Array int[][] xyarrays = new int[48][48]; int[][] yzarrays = new int[48][48]; int[][] zxarrays = new int[48][48]; int[][][] xyzarrays = new int[48][48][48]; //Filling Out Queue int pixelQueuex[] = new int[48*48*48]; int pixelQueuey[] = new int[48*48*48]; int pixelQueuez[] = new int[48*48*48]; int pixelQueueSize = 0; void setup() { addMouseWheelListener(new MouseWheelListener() { public void mouseWheelMoved(MouseWheelEvent mwe) { mouseWheel(mwe.getWheelRotation()); }}); setFile = ""; getFile = ""; // Array初期化 for(int k = 0; k<48; k = k + 1){ for(int j = 0; j<48; j = j + 1){ for(int i = 0; i<48; i = i + 1){ xyzarrays[k][j][i] = 0; } } } size(640, 480, OPENGL); gfx=new ToxiclibsSupport(this); controlP5 = new ControlP5(this); controlP5.addButton("loadfile",1,10,45,80,19); controlP5.addButton("savefile",1,100,45,80,19); controlP5.addSlider("inflation",1,500,10,450,200,20); gfx = new ToxiclibsSupport(this); initPhysics(); } void draw() { background(0); PGraphicsOpenGL pgl = (PGraphicsOpenGL)g; // Lighting ambientLight(150, 150, 150); directionalLight(255,255,255,-1,0,0); pointLight(160, 160, 160, 0, 0, 200); spotLight(100, 100, 100, 0, 0, 200, 0, 0, -1, PI, 2); // FileIO if(getFile != ""){ //ファイルを取り込む fileLoader(); } //Text font = createFont("Meiryo", 12); textFont(font); fill(255,255,255); text("11 Inflation (WETriangleMesh) ", 10, 25); // 中心を合わせる pushMatrix(); translate(320,240,200); rotateX(rotx); rotateY(roty); scale(expo); // STL書き出し if(setFile != ""){ beginRaw("unlekker.data.STL",setFile+".stl"); println("start :"+setFile); } // 風船メッシュの描画 if(mesh!=null && mesh_display == true) { fill(255,255,255); noStroke(); //poly.draw(this); physics.update(); for (Vertex v : mesh.vertices.values()) { v.set(physics.particles.get(v.id)); } mesh.center(null); for (Vertex v : mesh.vertices.values()) { physics.particles.get(v.id).set(v); } //mesh.computeFaceNormals(); //mesh.faceOutwards(); //mesh.computeVertexNormals(); //noFill(); gfx.origin(new Vec3D(), 50); fill(192); noStroke(); gfx.mesh(mesh, true,0); } popMatrix(); // STL書き出し終了 if(setFile != "") { endRaw(); setFile = ""; println("end"); } // α合成を有効にする GL gl = pgl.beginGL(); gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); gl.glDisable(GL.GL_DEPTH_TEST); pgl.endGL(); // GUI コンポーネント描画のための設定 camera(); gl = pgl.beginGL(); gl.glDisable(GL.GL_DEPTH_TEST); // 深度テストを無効化 gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); pgl.endGL(); } // テキストが入力された際にコールバックされるメソッド public synchronized void textfield(String txt) { txts = txt; } public void loadfile(int theValue) { getFile = getFileName(); } public void savefile(int theValue) { setFile = setFileName(); } void mouseDragged() { // Camera if (getFile == "" && setFile == "") { rotx = rotx + (mouseY - pmouseY) * rate; roty = roty + (mouseX - pmouseX) * rate; } } void mouseWheel(float delta) { expo = expo + delta*0.1; } void keyPressed() { // Mouse Pointer Move if (key==' ') { inflate=new AttractionBehavior(new Vec3D(), inflation, -0.3f, 0.001f); physics.addBehavior(inflate); } if (key == 'r') { initPhysics(); } } void keyReleased() { if (key== ' ') { physics.removeBehavior(inflate); } } void initPhysics() { // box = new WETriangleMesh(); // create a simple start mesh //box.addMesh(new Cone(new Vec3D(0, 0, 0), new Vec3D(0, 1, 0), 10, 50, 100).toMesh(4)); //box.addMesh(new AABB(new Vec3D(), 50).toMesh()); // then subdivide a few times... //bpx.subdivide(); //box.subdivide(); //box.subdivide(); //box.subdivide(); if (mesh != null) { physics = new VerletPhysics(); physics.setWorldBounds(new AABB(new Vec3D(), 180)); // turn mesh vertices into physics particles for (Vertex v : mesh.vertices.values()) { physics.addParticle(new VerletParticle(v)); } // turn mesh edges into springs for (WingedEdge e : mesh.edges.values()) { VerletParticle a = physics.particles.get(((WEVertex) e.a).id); VerletParticle b = physics.particles.get(((WEVertex) e.b).id); physics.addSpring(new VerletSpring(a, b, a.distanceTo(b), 0.005f)); } } }