Addendum: importing data.
There are many ways of importing data into processing. The important thing to figure out first is what kind of data we want and how we want to represent it in our program (as an array, through some more complex data structure or representation…). For example, it is possible to import height data as an image (a height-map) which is a standard form of representing topographic data in computer graphics. Here are for example a few examples of using this type of data in Processing to render a height-map as points (example by Anastasis Chasandras) or for creating contour maps (from the tutorial by Onformative). There are a number of sites where this type of data is available, often downloadable under different licenses and conditions, for example from theNASA Shuttle Radar Topography Mission (SRTM) .
For geometry, there are also few libraries available for importing 3D OBJ files, such as the OBJLoader
by Tatsuya Saito and Matt Ditton and the equivalent for output in OBJ format, OBJExport by Jesse Louis-Rosenberg. These are Contributed libraries, and can be installed and used as we explained in the previous tutorial. Another alternative to using these libraries, is to import files “by hand”: From programs such as Rhino, one can export all data as row triangles (.row). This produces a text file with all the vertices of the trinagles. One can take a file like this and edit it in a text editor (through for example find-replace) until it has the form of java/processing code, as an example, this is a .row file produced by Rhino:
Object1
-4.000000 -2.000000 0.000000 -1.000000 -2.000000 0.000000 -1.000000 -2.000000 7.000000
-1.000000 -2.000000 7.000000 -4.000000 -2.000000 7.000000 -4.000000 -2.000000 0.000000
-1.000000 -2.000000 0.000000 -1.000000 2.000000 0.000000 -1.000000 2.000000 7.000000
-1.000000 2.000000 7.000000 -1.000000 -2.000000 7.000000 -1.000000 -2.000000 0.000000
-1.000000 2.000000 0.000000 -4.000000 2.000000 0.000000 -4.000000 2.000000 7.000000
-4.000000 2.000000 7.000000 -1.000000 2.000000 7.000000 -1.000000 2.000000 0.000000
-4.000000 2.000000 0.000000 -4.000000 -2.000000 0.000000 -4.000000 -2.000000 7.000000
-4.000000 -2.000000 7.000000 -4.000000 2.000000 7.000000 -4.000000 2.000000 0.000000
-1.000000 -2.000000 0.000000 -4.000000 -2.000000 0.000000 -4.000000 2.000000 0.000000
-1.000000 2.000000 0.000000 -1.000000 -2.000000 0.000000 -4.000000 2.000000 0.000000
-4.000000 -2.000000 7.000000 -1.000000 -2.000000 7.000000 -1.000000 2.000000 7.000000
-1.000000 2.000000 7.000000 -4.000000 2.000000 7.000000 -4.000000 -2.000000 7.000000
here is a link to the file: a box exported as raw data.
And this is the way it can look after editing it in a text editor, so it looks like an array of floats in Processing. It is important to notice the syntax used for declaring and initialising an array at the same time. This is different from the cases we have previously seen, which used “new”, and is an easy way of filling an array with values when we know those values beforehand, as in this case. There is more information about initialising arrays in the array reference at the processing website.
float[] Object1={ -4.000000,-2.000000,0.000000,-1.000000,-2.000000,0.000000,-1.000000,-2.000000,7.000000, -1.000000,-2.000000,7.000000,-4.000000,-2.000000,7.000000,-4.000000,-2.000000,0.000000, -1.000000,-2.000000,0.000000,-1.000000,2.000000,0.000000,-1.000000,2.000000,7.000000, -1.000000,2.000000,7.000000,-1.000000,-2.000000,7.000000,-1.000000,-2.000000,0.000000, -1.000000,2.000000,0.000000,-4.000000,2.000000,0.000000,-4.000000,2.000000,7.000000, -4.000000,2.000000,7.000000,-1.000000,2.000000,7.000000,-1.000000,2.000000,0.000000, -4.000000,2.000000,0.000000,-4.000000,-2.000000,0.000000,-4.000000,-2.000000,7.000000, -4.000000,-2.000000,7.000000,-4.000000,2.000000,7.000000,-4.000000,2.000000,0.000000, -1.000000,-2.000000,0.000000,-4.000000,-2.000000,0.000000,-4.000000,2.000000,0.000000, -1.000000,2.000000,0.000000,-1.000000,-2.000000,0.000000,-4.000000,2.000000,0.000000, -4.000000,-2.000000,7.000000,-1.000000,-2.000000,7.000000,-1.000000,2.000000,7.000000, -1.000000,2.000000,7.000000,-4.000000,2.000000,7.000000,-4.000000,-2.000000,7.000000 };
here is a link to the text file with: the box after transforming it (using a text editor).
This particular data is made of vertices which form triangular faces. For displaying it we need to paste it inside our code. Here is an example using PeasyCam, and a function called “drawData()” to do this:
import peasy.*; PeasyCam cam; void setup(){ size(500,500, P3D); cam = new PeasyCam(this, 100); cam.setSuppressRollRotationMode(); //strokeWeight(0.2); noStroke(); } void draw(){ // background(255,255,255); background(0,0,0); // a bit of drama with the lights... lights(); rotateX(radians(45)); rotateY(radians(30)); scale(5);//make it a bit bigger... drawData(); } void drawData() { beginShape(TRIANGLES); //this uses beginShape for drawing...check the reference if you want to know more! //we step 3 at a time, since every point will be made of 3 //float values, x,y and z for (int i=0; i<Object1.length; i+=3){ vertex(Object1[i],Object1[i+1],Object1[i+2]); } endShape(); } //here is our "hand-edited" code, which describes an array. float[] Object1={ -4.000000,-2.000000,0.000000,-1.000000,-2.000000,0.000000,-1.000000,-2.000000,7.000000, -1.000000,-2.000000,7.000000,-4.000000,-2.000000,7.000000,-4.000000,-2.000000,0.000000, -1.000000,-2.000000,0.000000,-1.000000,2.000000,0.000000,-1.000000,2.000000,7.000000, -1.000000,2.000000,7.000000,-1.000000,-2.000000,7.000000,-1.000000,-2.000000,0.000000, -1.000000,2.000000,0.000000,-4.000000,2.000000,0.000000,-4.000000,2.000000,7.000000, -4.000000,2.000000,7.000000,-1.000000,2.000000,7.000000,-1.000000,2.000000,0.000000, -4.000000,2.000000,0.000000,-4.000000,-2.000000,0.000000,-4.000000,-2.000000,7.000000, -4.000000,-2.000000,7.000000,-4.000000,2.000000,7.000000,-4.000000,2.000000,0.000000, -1.000000,-2.000000,0.000000,-4.000000,-2.000000,0.000000,-4.000000,2.000000,0.000000, -1.000000,2.000000,0.000000,-1.000000,-2.000000,0.000000,-4.000000,2.000000,0.000000, -4.000000,-2.000000,7.000000,-1.000000,-2.000000,7.000000,-1.000000,2.000000,7.000000, -1.000000,2.000000,7.000000,-4.000000,2.000000,7.000000,-4.000000,-2.000000,7.000000 };