Need help with Toxi Tiler code
in
Contributed Library Questions
•
2 years ago
Hi everyone,
I was trying to use the Toxi Tiler code to see if I can save large images from dynamically moving sketches.
To do a quick try, I adopted a sketch from Alessandro Capozzo - GHOSTAGENCY.
I added a keypress code to pause the sketch first before saving the image.
Here is the code:
- // built with processing 0.67/0.68
- // by Alessandro Capozzo - GHOSTAGENCY -
- // winter 03-04
- // Adopted to Processing 1.2.1 by Sajid Saiyed (www.ssdesigninteractive.com/g2)
- import processing.opengl.*;
- import toxi.geom.*;
- boolean go = true;
- float noiseScale=0.012f;
- float gg=1;
- int limit;
- /////////
- Tiler tiler;
- // camera field of vision and desired clipping planes
- float FOV=radians(90);
- float CLIP_NEAR=1;
- float CLIP_FAR=5000;
- int NUM_TILES=3;
- /////////
- void setup(){
- size(600,200,OPENGL);
- frameRate(30);
- limit=width+180;
- tiler=new Tiler((PGraphics3D)g,NUM_TILES);
- }
- void draw() {
- graph();
- }
- void graph() {
- // Check whether to play the sketch or Pause.
- if(go == true)
- {
- background(220);
- noFill();
- gg+=1;
- for(int x=0; x<(limit); x++) {
- float noiseVal = noise((gg+x)*noiseScale,.3f*noiseScale);
- int c=color(noiseVal*255,noiseVal*230,noiseVal*210,20);
- stroke(c);
- beginShape();
- vertex(x, height*noiseVal);
- bezierVertex(.8f*x+noiseVal, 200+noiseVal*1.2f, .6f*x-noiseVal, 200+noiseVal*1.4f, x,height*noiseVal);
- //bezierVertex(.6f*x-noiseVal, 200+noiseVal*1.4f);
- //bezierVertex(x,height*noiseVal);
- endShape();
- int d=color(noiseVal*220,noiseVal*230,noiseVal*210,20);
- stroke(d);
- beginShape();
- vertex(x,height*noiseVal);
- bezierVertex(.8f*x-noiseVal, noiseVal*1.2f, .9f*x+noiseVal, noiseVal*1.4f, x,height*noiseVal);
- //bezierVertex(.9f*x+noiseVal, noiseVal*1.4f);
- //bezierVertex(x,height*noiseVal);
- endShape();
- }
- }
- else
- {
- tiler.pre();
- tiler.post();
- }
- }
- int value;
- void keyPressed() {
- println(keyCode);
- value = keyCode;
- // Press "s" to stop the sketch.
- // after that Press "a" to save the tiler image.
- // then pres any other alphabet key to resume sketch
- if (value == 83) {
- go = false;
- }
- else if (value == 65)
- {
- saveMe();
- }
- else {
- go = true;
- }
- }
- void saveMe() {
- tiler.initTiles(FOV,CLIP_NEAR,CLIP_FAR);
- tiler.save(sketchPath("export"), "tiles-" + (System.currentTimeMillis() / 1000), "tga");
- }
- /**
- * Implements a screen tile exporter with support for FOV,
- * clip planes and flexible file formats.
- *
- * Re-engineered from an older version by Marius Watz:
- * http://workshop.evolutionzone.com/unlekkerlib/
- *
- * @author toxi <info at postspectacular dot com>
- */
- class Tiler {
- protected PGraphics3D gfx;
- protected PImage buffer;
- protected Vec2D[] tileOffsets;
- protected double top;
- protected double bottom;
- protected double left;
- protected double right;
- protected Vec2D tileSize;
- protected int numTiles;
- protected int tileID;
- protected float subTileID;
- protected boolean isTiling;
- protected String fileName;
- protected String format;
- protected double cameraFOV;
- protected double cameraFar;
- protected double cameraNear;
- public Tiler(PGraphics3D g, int n) {
- gfx = g;
- numTiles = n;
- }
- public void chooseTile(int id) {
- Vec2D o = tileOffsets[id];
- gfx.frustum(o.x, o.x + tileSize.x, o.y, o.y + tileSize.y,
- (float) cameraNear, (float) cameraFar);
- }
- public void initTiles(float fov, float near, float far) {
- tileOffsets = new Vec2D[numTiles * numTiles];
- double aspect = (double) gfx.width / gfx.height;
- cameraFOV = fov;
- cameraNear = near;
- cameraFar = far;
- top = Math.tan(cameraFOV * 0.5) * cameraNear;
- bottom = -top;
- left = aspect * bottom;
- right = aspect * top;
- int idx = 0;
- tileSize = new Vec2D((float) (2 * right / numTiles), (float) (2 * top / numTiles));
- double y = top - tileSize.y;
- while (idx < tileOffsets.length) {
- double x = left;
- for (int xi = 0; xi < numTiles; xi++) {
- tileOffsets[idx++] = new Vec2D((float) x, (float) y);
- x += tileSize.x;
- }
- y -= tileSize.y;
- }
- }
- public void post() {
- if (isTiling) {
- subTileID += 0.5;
- if (subTileID > 1) {
- int x = tileID % numTiles;
- int y = tileID / numTiles;
- gfx.loadPixels();
- buffer.set(x * gfx.width, y * gfx.height, gfx);
- if (tileID == tileOffsets.length - 1) {
- buffer.save(fileName + "_" + buffer.width + "x"
- + buffer.height + "." + format);
- buffer = null;
- }
- subTileID = 0;
- isTiling = (++tileID < tileOffsets.length);
- }
- }
- }
- public void pre() {
- if (isTiling) {
- chooseTile(tileID);
- }
- }
- public void save(String path, String baseName, String format) {
- (new File(path)).mkdirs();
- this.fileName = path + "/" + baseName;
- this.format = format;
- buffer = new PImage(gfx.width * numTiles, gfx.height * numTiles);
- tileID = 0;
- subTileID = 0;
- isTiling = true;
- }
- public boolean isTiling() {
- return isTiling;
- }
- }
Now when I run this code, the image that is getting saved is a large tile of 9 identical images instead of one big image.
I am sure I am doing something wrong here :(
Would really appreciate some help as I am planning to write a dynamic sketch like this one and would want to print it large size.
Thanks