Browse Source

moved drawSpheres into functions.pde, try 2

master
Nova deViator 6 years ago
parent
commit
b1324a1e3e
2 changed files with 732 additions and 0 deletions
  1. +209
    -0
      pde/IF3Si/IF3Si.pde
  2. +523
    -0
      pde/IF3Si/functions.pde

+ 209
- 0
pde/IF3Si/IF3Si.pde View File

@ -0,0 +1,209 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Interface Fractures III - SILICON
(c) nova@deviator.si
IF3Si.pde
*/
// undecorate window (remove window borders etc) - - - - - - - - - - - - - - -
public void init() { frame.removeNotify(); frame.setUndecorated(true);
frame.addNotify(); super.init(); }
// load libs
import oscP5.*; // Open Sound Control
import netP5.*;
// declarations - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// declare OSC object
OscP5 oscP5;
// IMAGE POOL, a 2D array
PImage[][] imgPool;
// texture (tiles)
float texX = 0;
float texY = 0;
// generate an array of random numbers
int[] rands = new int[500];
IntList randz; // arrayList
// for draw cube
int cubesnum = 20;
PGraphics[] cubes = new PGraphics[cubesnum];
PShader blur;
PGraphics bpass1, bpass2;
// spheres
int ptsW, ptsH;
int numPointsW;
int numPointsH_2pi;
int numPointsH;
float[] coorX;
float[] coorY;
float[] coorZ;
float[] multXZ;
PGraphics sphere;
int tilesOverlap;
void setup() { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
float sizefactor = 1; // define the size of the screen, 1 = 1080p
size( int( 1920 * sizefactor ),
int( 1080 * sizefactor ),
P3D ); // renderer
// framerate
frameRate(60);
smooth(32); // 32??
noCursor();
background(10);
println("\n\n~~~ Hello. Starting Interface Fractures III - SILICON." +
" - - - - - - - - - - - - - - - - - - - - - -\n");
// start oscP5, listening for incoming messages at port 12000
println("~~~ starting oscP5 ...");
oscP5 = new OscP5(this,12000);
oscP5.plug(this,"ctlin","/ctlin"); // to be converted for PD OSC input
// get all textures into an image pool
println("\n\n~~~ loading textures into image pool ...\n");
imgPool = getImages("/images/");
// create an array of random value between -50 and 50
for (int i=0; i < 500; i++) { rands[i] = i-250; }
shuffle(rands);
// drawCube ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
// an array of PGraphics
for (int i = 0; i < cubesnum; i++) {
cubes[i] = createGraphics(width, height, P3D);
}
blur = loadShader("blur.glsl");
bpass1 = createGraphics(width, height, P3D);
bpass1.smooth();
bpass2 = createGraphics(width, height, P3D);
bpass2.smooth();
randz = new IntList(width);
for (int i=0; i < width; i++) {
randz.set(i, i);
}
randz.shuffle();
//println(randz);
// spheres
ptsW=30;
ptsH=30;
initializeSphere(ptsW, ptsH); // number of vertices around the width and height
sphere = createGraphics(width, height, P3D);
}
// process OSC messages
public void ctlin(int cc, int val) { // - - - - - - - - - - - - - - - - - - - - - - -
// debug
println("## OSC: /ctlin cc:" + cc + " value:" + val);
if (cc == 2) { tilesOverlap = val; }
/* // triggers are on controller number 0
if (cc == 0) {
}
if (cc == 2) { flySpeedXfactor = (val - 64); } // speed (&direction) on X axis [-1 - 1] FIXit!
if (cc == 3) { flySpeedYfactor = (val - 64); } // speed (&direction) on Y axis [-1 - 1] FIXit!
*/
}
void draw() { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// clean screen ````````````````````````````````````````````````````|
blendMode(BLEND);
screenClean(color(50));
// SCENENGINES / / / / / / / / / / / / / / / / / / / / / / / / / / |
// draw spheress```````````````````````````````````````````````````|
drawSpheres(boolean(0)
);
// draw tiles `````````````````````````````````````````````````````|
tiles(boolean(0), // render on/off
color(0, 0, 0, 80), // background color (HSBA)
color(80, 70, 20, 100), // tile color
20, // tile hue distance
0, // blendMode
6, // number of tiles on X axis
1, // number of tiles on Y axis
2, // texture bank number
0, // texture number/id
10, // texture speed X
1, // texture speed Y
tilesOverlap // overlap. 127 = 300%
);
// draw cubes `````````````````````````````````````````````````````|
drawCube(boolean(0), cubes,
100, height/2, -100,
400, 300, 300,
radians(frameCount), radians(frameCount*0.7), PI/2,
0);
// test pattern```````````````````````````````````````````````````|
testPattern(boolean(0), // on/off
2, 0, // img bank & ID
255, // image alpha
10, // number of horizontal 'lanes'
10, // density
4, // stroke width
255, // stroke alpha
2 // speed
);
// debug `````````````````````````````````````````````````````````|
// draw test picture
testPicture(boolean(0));
// frames per second
displayFps(true);
// document
autoSnap(false);
} // --------------------------------------------------------------------------

+ 523
- 0
pde/IF3Si/functions.pde View File

@ -0,0 +1,523 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Interface Fractures III - SILICON
(c) nova@deviator.si
functions.pde
*/
// loads all images into an array - - - - - - - - - - - - - - - - - - - - - - - - -
PImage[][] getImages(String folder) {
PImage[][] imgPool; // declare 2D array imgPool
File dir = new File(dataPath(sketchPath + folder)); // first folder
String[] dirlist = dir.list(); // an array of folders (strings)
dirlist = sort(dirlist); //
imgPool = new PImage[dirlist.length][10]; // create 2d array imgPool
for (int i = 0; i < dirlist.length; i++) {
String fulldir = dataPath(sketchPath + folder + dirlist[i]) + "/";
File dir2 = new File(fulldir);
String[] filelist = dir2.list(); // an array of image names
filelist = sort(filelist);
if (filelist.length != 0) {
for (int j = 0; j < filelist.length; j++) {
println("imgPool[" + i + "][" + j + "]: " + fulldir + filelist[j]);
imgPool[i][j] = loadImage(fulldir + filelist[j]);
}
} else {
println("No files in this folder: " + fulldir);
}
}
return imgPool;
}
// function to shuffle an array of integers - - - - - - - - - - - - - - - - - - - - -
void shuffle(int[] a)
{
int temp, pick;
for(int i=0; i<a.length; i++)
{
temp = a[i];
pick = (int)random(a.length);
a[i] = a[pick];
a[pick] = temp;
}
}
void switchBlendMode(int blendMode) // - - - - - - - - - - - - - - - - - - - - - - -
{
switch(blendMode) {
case 0:
blendMode(ADD); // additive blending with white clip: C = min(A*factor + B, 255)
break;
case 1:
blendMode(SUBTRACT); // subtractive blend w/ black clip: C = max(B - A*factor, 0)
break;
case 2:
blendMode(LIGHTEST); // only the lightest colour succeeds: C = max(A*factor, B)
break;
case 3:
blendMode(DARKEST); // only the darkest colour succeeds: C = min(A*factor, B)
break;
case 4:
blendMode(SCREEN); // opposite multiply, uses inverse values of the colors.
break;
case 5:
blendMode(MULTIPLY); // multiply the colors, result will always be darker.
break;
case 6:
blendMode(EXCLUSION); // similar to DIFFERENCE, but less extreme.
break;
case 7:
blendMode(REPLACE); // pixels entirely replace others + don't utilize alpha values
break;
default:
blendMode(BLEND); // linear interp. of colours: C = A*factor + B. Default.
break;
} // DIFFERENCE: subtract colors from underlying image. NOT SUPPORTED BY P3D!! */
}
// TILES - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/* TODO:
- different zooms (random?)
- better rotation
- multiple instances of the same thing - with
different texture & some parameters + parameterize
- zoom variations
- ?amount of speed variations accross tiles
*/
void tiles( boolean render, // should we render or not?
color bgfill, // backgorund color
color tilecolor, // tile color
int huedist, // tile color distance for interpolation
int blendMode, // blending mode of tiles
int numx, // number of tiles on X
int numy, // number of tiles on Y
int texBank,
int texNum,
float texSpeedX,
float texSpeedY,
float overlap )
{
if (render) {
colorMode(HSB, 360, 100, 100, 100);
blendMode(BLEND);
fill(bgfill);
noStroke();
rect(0, 0, width, height);
switchBlendMode(blendMode); // blendMode function using integers
texSpeedX *= 0.01;
texSpeedY *= 0.01;
texX += sq(texSpeedX);
texY += sq(texSpeedY);
float numxfact = 1 / float(numx);
float numyfact = 1 / float(numy);
float aspectRatio = (height / float(numy)) / (width / float(numx));
float offsetPerc = overlap * (300/127);
float uniZoom = numxfact * (float(width)/1024);
float offsetX = width * numxfact * offsetPerc * 0.01;
float offsetY = height * numyfact * offsetPerc * 0.01;
float texoffsetX = 1 + 0.01 * offsetPerc ;
float texoffsetY = texoffsetX;
float huefactor = float(huedist) / (numx * numy);
for (int nx=0; nx < numx; nx++) {
for (int ny=0; ny < numy; ny++) {
int tileID = nx * numy + ny;
float randX = rands[ (tileID % rands.length) ] * 0.01;
float randY = rands[ ((tileID + 50) % rands.length) ] * 0.01;
float newhue = hue(tilecolor) + huefactor * tileID;
tint(newhue, saturation(tilecolor), brightness(tilecolor), alpha(tilecolor));
textureWrap(REPEAT);
textureMode(NORMAL);
beginShape();
texture(imgPool[texBank][texNum]);
vertex(width * numxfact * nx - offsetX, height * numyfact * ny - offsetY, 0 + texX * randX, 0 + texY * randY);
vertex(width * numxfact * (nx + 1) + offsetX, height * numyfact * ny - offsetY, 1 * uniZoom * texoffsetX + texX * randX, 0 + texY * randY);
vertex(width * numxfact * (nx + 1) + offsetX, height * numyfact * (ny + 1) + offsetY, 1 * uniZoom * texoffsetX + texX * randX, aspectRatio * uniZoom * texoffsetY + texY * randY);
vertex(width * numxfact * nx - offsetX, height * numyfact * (ny + 1) + offsetY, 0 + texX * randX, aspectRatio * uniZoom * texoffsetY + texY * randY);
endShape();
}
}
}
}
// TEST PICTURE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void testPicture(boolean render) {
if (render) {
fill(127);
stroke(255);
strokeWeight(1);
rect(1, 1, width-2, height-2);
ellipse(width/2, height/2, height-4, height-4);
ellipse(height/4, height/4, height/2-4, height/2-4);
ellipse(height/4, 3*height/4, height/2-4, height/2-4);
ellipse(width-height/4, height/4, height/2-4, height/2-4);
ellipse(width-height/4, 3*height/4, height/2-4, height/2-4);
line(width/2, 0, width/2, height);
line(0, height/2, width, height/2);
fill(0);
textSize(50);
textAlign(CENTER,CENTER);
text("Interface Fractures III", width/2, height/2,4);
/* more tests:
- horizontal and vertical block, moving fast!
- flicker test, black&white, 60Hz
- color and gray stripes of interpolation
*/
}
}
// DRAW CUBE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void drawCube( boolean render, PGraphics cubes[],
int posX, int posY, int posZ,
int dimW, int dimH, int dimL,
float rotX, float rotY, float rotZ,
int blurAmount
//float xd, float yd
)
{
if (render) {
blendMode(BLEND);
fill(0);
rect(0, 0, width, height);
//blendMode(LIGHTEST);
for (int i=0; i < cubes.length; i++) {
posX += 100;
cubes[i].beginDraw();
cubes[i].clear();
cubes[i].smooth();
cubes[i].lights();
cubes[i].stroke(255);
cubes[i].strokeWeight(10);
cubes[i].fill(127);
//cubes[i].noFill();
cubes[i].translate(posX, posY, posZ);
cubes[i].rotateX(rotX);
cubes[i].rotateY(rotY);
cubes[i].rotateZ(rotZ);
cubes[i].box(dimW, dimH, dimL);
cubes[i].endDraw();
if (blurAmount != 0) { // this blur chokes graphic processor
blur.set("blurSize", blurAmount);
blur.set("sigma", 9.0f);
blur.set("horizontalPass", 0);
bpass1.beginDraw();
bpass1.clear();
//bpass1.noLights();
bpass1.shader(blur);
bpass1.image(cubes[i], 0, 0);
bpass1.endDraw();
blur.set("horizontalPass", 1);
cubes[i].beginDraw();
cubes[i].clear();
//cubes[i].noLights();
cubes[i].shader(blur);
cubes[i].image(bpass1, 0, 0);
cubes[i].endDraw();
}
image(cubes[i], 0, 0);
}
}
}
// SPHERES -----------------------------------------------------------
//offscreen render spheres
void drawSpheres( boolean render
)
{
if (render) {
/*
sphere.beginDraw();
sphere.blendMode(BLEND);
sphere.rect(0, 0, width, height);
sphere.blendMode(DARKEST);
sphere.noStroke();
sphere.pushMatrix();
sphere.translate(width/2+(width/8), height/2, 580);
sphere.rotateX(radians(float(frameCount) * 0.1));
sphere.rotateY(radians(float(frameCount) * 0.23));
sphere.rotateZ(radians(float(frameCount) * 0.2));
textureSphere(sphere, 400, 400, 400, imgPool[3][1]);
sphere.popMatrix();
sphere.pushMatrix();
sphere.translate(width/2+(width/8), height/2, 580);
sphere.rotateX(radians(float(frameCount) * 0.2));
sphere.rotateY(radians(float(frameCount) * 0.13));
sphere.rotateZ(radians(float(frameCount) * 0.01));
textureSphere(sphere, 390, 390, 390, imgPool[3][1]);
sphere.popMatrix();
sphere.endDraw();
// tint(0);
// image(sphere, 0, 0);
textureWrap(REPEAT);
//textureMode(NORMAL);
pushMatrix();
//tint(255);
translate(width/2, height/2); // center coordinate system
translate(((frameCount*1) % width) - width/2,0);
//stroke(0,50);
//strokeWeight(1);
noStroke();
float quadW = 500;
float quadH = height;
float v1x = 0; // sin(radians(frameCount))*250+400;
pushMatrix() ;
//rotate(PI/PI);
beginShape(QUADS);
texture(sphere);
vertex(-quadW/2, -quadH/2, 0, 0);
vertex( quadW/2, -quadH/2, quadW, 0);
vertex( quadW/2, quadH/2, quadW, quadH);
vertex(-quadW/2, quadH/2, 0, quadH);
endShape();
popMatrix();
popMatrix();
*/
}
// TEST PATTERN - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/* TO BE ADDED:
- use quads
- add textures
>- more TO BE EXPANDED / REFINED!!! */
void testPattern( boolean render,
int imgBank, int imgID,
int imgAlpha,
int ylines,
int density,
int lwidth,
int lalpha,
int speed
)
{
if (render) {
tint(imgAlpha);
image(imgPool[imgBank][imgID], 0, 0);
blendMode(BLEND);
int yheight = height/ylines;
for (int y=0; y < ylines; y++) {
int yloc = int((y+1)*height/(ylines+1));
for (int x=0; x < width; x++) {
int index = x + (y * (width/ylines));
index = index % width;
if (randz.get(index) < density * 0.01 * randz.get((index + x + y) % randz.size())) {
int fx = (x + frameCount); // move!
fx = fx * speed * 1 * randz.get((abs(index - x + y) % randz.size())) / randz.size(); // speed!
fx %= width; // wrap at the right edge
color imgC = imgPool[imgBank][imgID].get(fx, yloc);
stroke(imgC, lalpha);
strokeWeight(1 + map(brightness(imgC), 0, 255, 0, lwidth));
line(fx, yheight * y + (yheight/30), fx, yheight * (y+1) - (yheight/30));
}
}
}
}
}
// SPHERES - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// A 3D textured sphere with simple rotation control.
// thanks to https://processing.org/examples/texturesphere.html
// Texture Sphere written by Gillian Ramsay to better display the poles.
// Previous version by Mike 'Flux' Chang (and cleaned up by Aaron Koblin).
// Original based on code by Toxi.
// modified to accomodate offscreen PGraphics
void initializeSphere(int numPtsW, int numPtsH_2pi) {
// The number of points around the width and height
numPointsW=numPtsW+1;
numPointsH_2pi=numPtsH_2pi; // How many actual pts around the sphere (not just from top to bottom)
numPointsH=ceil((float)numPointsH_2pi/2)+1; // How many pts from top to bottom (abs(....) b/c of the possibility of an odd numPointsH_2pi)
coorX=new float[numPointsW]; // All the x-coor in a horizontal circle radius 1
coorY=new float[numPointsH]; // All the y-coor in a vertical circle radius 1
coorZ=new float[numPointsW]; // All the z-coor in a horizontal circle radius 1
multXZ=new float[numPointsH]; // The radius of each horizontal circle (that you will multiply with coorX and coorZ)
for (int i=0; i<numPointsW ;i++) { // For all the points around the width
float thetaW=i*2*PI/(numPointsW-1);
coorX[i]=sin(thetaW);
coorZ[i]=cos(thetaW);
}
for (int i=0; i<numPointsH; i++) { // For all points from top to bottom
if (int(numPointsH_2pi/2) != (float)numPointsH_2pi/2 && i==numPointsH-1) { // If the numPointsH_2pi is odd and it is at the last pt
float thetaH=(i-1)*2*PI/(numPointsH_2pi);
coorY[i]=cos(PI+thetaH);
multXZ[i]=0;
}
else {
//The numPointsH_2pi and 2 below allows there to be a flat bottom if the numPointsH is odd
float thetaH=i*2*PI/(numPointsH_2pi);
//PI+ below makes the top always the point instead of the bottom.
coorY[i]=cos(PI+thetaH);
multXZ[i]=sin(thetaH);
}
}
}
void textureSphere(PGraphics pg, float rx, float ry, float rz, PImage t) {
// These are so we can map certain parts of the image on to the shape
float changeU=t.width/(float)(numPointsW-1);
float changeV=t.height/(float)(numPointsH-1);
float u=0; // Width variable for the texture
float v=0; // Height variable for the texture
pg.beginShape(TRIANGLE_STRIP);
pg.texture(t);
for (int i=0; i<(numPointsH-1); i++) { // For all the rings but top and bottom
// Goes into the array here instead of loop to save time
float coory=coorY[i];
float cooryPlus=coorY[i+1];
float multxz=multXZ[i];
float multxzPlus=multXZ[i+1];
for (int j=0; j<numPointsW; j++) { // For all the pts in the ring
pg.normal(coorX[j]*multxz, coory, coorZ[j]*multxz);
pg.vertex(coorX[j]*multxz*rx, coory*ry, coorZ[j]*multxz*rz, u, v);
pg.normal(coorX[j]*multxzPlus, cooryPlus, coorZ[j]*multxzPlus);
pg.vertex(coorX[j]*multxzPlus*rx, cooryPlus*ry, coorZ[j]*multxzPlus*rz, u, v+changeV);
u+=changeU;
}
v+=changeV;
u=0;
}
pg.endShape();
}
// TOOLS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void screenClean(color c) { // -----------------------------------------
fill(c);
noStroke();
rect(0, 0, width, height);
}
void displayFps(boolean render) { // -----------------------------------
if (render){
// Display Fps
fill(0); noStroke(); rect(width-80, height-30, 80, 30, 4);
fill(200); text(int(frameRate)+"fps", width-40, height-10, 5);}
}
void autoSnap(boolean render) { // -------------------------------------
// auto-save snapshots
if (frameCount == 1000) {
saveFrame("../snapshots/"
+ year() + nf(month(),2)
+ nf(day(),2)
+ nf(hour(),2)
+ nf(minute(),2)
+ nf(second(),2)
+ "_.png");
}
}

Loading…
Cancel
Save