Skip to content

Commit

Permalink
many changes - deleting shapes, now rendering to dual offscreen buffers
Browse files Browse the repository at this point in the history
of different sizes for performance reasons, added some text display and
source mode additive blending for shapes, also 3D option for destination verts
  • Loading branch information
pixelpusher committed Jan 10, 2012
1 parent 98b7693 commit ca5746a
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 161 deletions.
3 changes: 2 additions & 1 deletion TextureMappingMultiple/DynamicDrawing.pde
@@ -1,3 +1,4 @@

/*
* This shows how to use a dynamic animation to draw into
* a projection-mapped shape.
Expand All @@ -10,7 +11,7 @@ void setupDynamicImages()
// Right now there is only the Whitney one
// All we need to do is create it, and it does the rest!

DynamicGraphic whitneyDynamicImage = new DynamicWhitney(this, 512, 512);
DynamicGraphic whitneyDynamicImage = new DynamicWhitney(this, 1024, 1024);
}


Expand Down
40 changes: 32 additions & 8 deletions TextureMappingMultiple/DynamicWhitney.pde
Expand Up @@ -16,6 +16,7 @@ public class DynamicWhitney extends DynamicGraphic
float cycleLength;
float startTime;
int counter;
int numPetals;
float speed;
boolean usePoints;

Expand All @@ -34,13 +35,14 @@ public class DynamicWhitney extends DynamicGraphic

void initialize()
{
usePoints = true;
nbrPoints = 140;
numPetals = 2;
usePoints = false;
nbrPoints = 160;
counter = 0;
cx = this.width/2;
cy = this.height/2;
crad = (min(this.width, this.height)/2) * 0.95;
cycleLength = 320000;
cycleLength = 320000*4;
speed = (TWO_PI*nbrPoints) / cycleLength;
startTime = millis();

Expand All @@ -61,14 +63,22 @@ public class DynamicWhitney extends DynamicGraphic
float my = 20;

this.beginDraw();
this.smooth();

GL gl = this.beginGL();
gl.glClearColor(0f,0f,0f,0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
this.endGL();


//this.smooth();
this.colorMode(HSB, 1);
this.strokeWeight(2);
this.strokeWeight(4);

//startTime = -(cycleLength*20) / (float) this.height;
float timer = (millis() - startTime) % cycleLength;

this.background(0);
//this.background(0);

//counter = int(timer / cycleLength);

counter = int(timer);
Expand All @@ -95,8 +105,22 @@ public class DynamicWhitney extends DynamicGraphic
float a = timer * speed * r; // pow(i * .001,2);
float rad = max(2, len*.05);

// len *= sin(a*timer); // big fun!
if (false)
{
float tmps = sin(numPetals*a);
float tmpc = cos(numPetals*a);

len *= 2*tmps*tmpc;
}

if (true)
{
float tmps = sin(numPetals*(a+TWO_PI*timer/cycleLength));
//float tmpc = cos(numPetals*a+timer);

len *= tmps*tmps;
}

float x = (cx + cos(a)*len);
float y = (cy + sin(a)*len);
float h = map(sin(len*TWO_PI) * sin(PI*timer/cycleLength), -1, 1, 0, 1);
Expand All @@ -109,7 +133,7 @@ public class DynamicWhitney extends DynamicGraphic
}
else
{
this.stroke(h, .8, 1-r/2);
this.stroke(h, .8, 1-r/2, 0.7);
this.curveVertex(x, y);
}
}
Expand Down
3 changes: 2 additions & 1 deletion TextureMappingMultiple/GeometryFunctions.pde
Expand Up @@ -44,7 +44,8 @@ final float distancePointToLine(PVector l0, PVector l1, PVector p)


// Adapted from http://paulbourke.net/geometry/insidepoly/
// Originally by Alexander Motrichuk (in C++)
// Originally by Alexander Motrichuk (in C++) and Paul Bourke
// Needs to be updated to 3D!

boolean isInsideShape(ProjectedShape projShape, float x, float y, boolean testSource)
{
Expand Down
28 changes: 28 additions & 0 deletions TextureMappingMultiple/ProjectedShape.pde
Expand Up @@ -11,11 +11,16 @@ class ProjectedShape
{
PImage srcImage = null;

// for tinting:
int r;
int g;
int b;
int a;
int blendMode = LIGHTEST;

// for outlining:
color srcColor = color(0, 255, 0, 180);
color dstColor = color(255, 0, 255, 180);

String name = null;

Expand All @@ -32,6 +37,29 @@ class ProjectedShape
// give it a random name
name = "shape" + random(0,MAX_INT);
}


// deep copy ProjectedShape

ProjectedShape(ProjectedShape srcShape)
{
if (srcShape.srcImage != null)
srcImage = srcShape.srcImage;
else
println("ERROR::::IMAGE FOR PROJECTED SHAPE CANNOT BE NULL!!");

// deep copy verts
verts = new LinkedList<ProjectedShapeVertex>();

for (ProjectedShapeVertex psvert : srcShape.verts )
{
verts.add(new ProjectedShapeVertex(psvert) );
}

// give it a random name
name = "shape" + random(0,MAX_INT);
}



// Add a new source and destination vertex
Expand Down
119 changes: 97 additions & 22 deletions TextureMappingMultiple/ProjectedShapeRenderer.pde
Expand Up @@ -18,24 +18,27 @@ import javax.media.opengl.GL2;
class ProjectedShapeRenderer
{

float cr=0f, cg=0f, cb=0f, ca=1f; // clear colour

//PGraphics renderer = null; // rendering target object
PGraphicsOpenGL renderer;

ProjectedShapeRenderer(PGraphicsOpenGL pgl)
{
//renderer = g; // get default graphics object for this sketch
renderer = pgl;
renderer = pgl;
}

// This is taken directly from the PGraphics2.java renderer
// Copyright (c) 2004-08 Ben Fry and Casey Reas
public void screenBlend(int mode) {

// This is taken directly from the PGraphics2.java renderer
// Copyright (c) 2004-08 Ben Fry and Casey Reas

final public void screenBlend(int mode, PGraphicsOpenGL renderTarget) {

boolean blendEqSupported = true; // necessary?
GL gl = renderer.beginGL();
GL gl = renderTarget.beginGL();
gl.glEnable(GL.GL_BLEND);
gl.glDisable(GL.GL_DEPTH_TEST);

if (mode == REPLACE) {
// This is equivalent to disable blending.
Expand Down Expand Up @@ -96,41 +99,107 @@ class ProjectedShapeRenderer
// HARD_LIGHT, SOFT_LIGHT, OVERLAY, DODGE, BURN modes cannot be implemented
// in fixed-function pipeline because they require conditional blending and
// non-linear blending equations.

renderTarget.endGL();
}




//
// various flavours of starting off rendering
//

void beginRender()
{
beginRender(this.renderer, true);
}

void beginRender(boolean clearScreen)
{
beginRender(this.renderer, clearScreen);
}

void beginRender(PGraphics renderTarget)
{
beginRender(renderTarget, true);
}

void beginRender(PGraphics renderTarget, boolean clearScreen)
{
renderer = (PGraphicsOpenGL)renderTarget;

renderer.endGL();
renderer.beginDraw();

// black background
// mappedView.background(0);
if (clearScreen)
{
/*
GL gl = renderer.beginGL();
gl.glClearColor(cr,cg,cb,ca);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
renderer.endGL();
*/
renderer.background(0);
}
}



void endRender()
{
this.renderer.endDraw();
}



// Draw the source shape (no texture)
//

final void drawSourceShape(final ProjectedShape projShape)
{
drawSourceShape( projShape, renderer);
drawSourceShape( projShape, renderer, false);
}

final void drawSourceShape(final ProjectedShape projShape, boolean showSrcImage)
{
drawSourceShape( projShape, renderer, showSrcImage);
}


final void drawSourceShape(final ProjectedShape projShape, PGraphics renderTarget)
final void drawSourceShape(final ProjectedShape projShape, PGraphics renderTarget, boolean showSrcImage)
{
this.screenBlend(REPLACE, (PGraphicsOpenGL)renderTarget);
if (showSrcImage)
{
renderTarget.noSmooth(); // otherwise we see white lines!
renderTarget.image( projShape.srcImage, 0, 0, projShape.srcImage.width/2, projShape.srcImage.height/2);
}

renderTarget.smooth();
renderTarget.strokeWeight(2);
renderTarget.stroke(0, 255, 0, 80);
renderTarget.noFill();
renderTarget.stroke(projShape.srcColor);
// draw the shape using source and destination vertices
if (projShape.verts != null && projShape.verts.size() > 0)
{
renderTarget.beginShape();
for (ProjectedShapeVertex vert : projShape.verts)
{
// add it to our shape
renderTarget.vertex(vert.src.x, vert.src.y);
renderTarget.vertex(vert.src.x/2, vert.src.y/2);
}
renderTarget.endShape(CLOSE);
}

for (ProjectedShapeVertex vert : projShape.verts)
{
// add it to our shape
renderTarget.ellipse(vert.src.x, vert.src.y, 8, 8);
renderTarget.pushMatrix();
renderTarget.translate(vert.src.x/2, vert.src.y/2);
renderTarget.ellipse(0, 0, 8, 8);
renderTarget.popMatrix();
}
}

Expand All @@ -148,9 +217,12 @@ class ProjectedShapeRenderer

final void drawDestShape(final ProjectedShape projShape, PGraphics renderTarget)
{
this.screenBlend(REPLACE, (PGraphicsOpenGL)renderTarget);
renderTarget.smooth();
renderTarget.strokeWeight(2);
renderTarget.stroke(255, 0, 255, 80);
renderTarget.stroke(projShape.dstColor);
//renderTarget.stroke(255);
renderTarget.noFill();

// draw the shape using source and destination vertices
if (projShape.verts != null && projShape.verts.size() > 0)
Expand All @@ -159,16 +231,20 @@ class ProjectedShapeRenderer
for (ProjectedShapeVertex vert : projShape.verts)
{
// add it to our shape
//renderTarget.vertex(vert.dest.x, vert.dest.y, vert.dest.z);
renderTarget.vertex(vert.dest.x, vert.dest.y);
}
renderTarget.endShape(CLOSE);
}

renderTarget.stroke(255, 0, 255, 80);

for (ProjectedShapeVertex vert : projShape.verts)
{
// add it to our shape
renderTarget.ellipse(vert.dest.x, vert.dest.y, 8, 8);
renderTarget.pushMatrix();
//renderTarget.translate(vert.dest.x, vert.dest.y, vert.dest.z);
renderTarget.translate(vert.dest.x, vert.dest.y);
renderTarget.ellipse(0, 0, 8, 8);
renderTarget.popMatrix();
}
}

Expand All @@ -179,29 +255,28 @@ class ProjectedShapeRenderer

final void draw(final ProjectedShape projShape)
{
draw( projShape, renderer );
this.draw( projShape, renderer );
}

final void draw(final ProjectedShape projShape, PGraphics renderTarget)
{
// the shape using source and destination vertices
if (projShape.verts != null && projShape.verts.size() > 0)
{
this.screenBlend(projShape.blendMode, (PGraphicsOpenGL)renderTarget);
renderTarget.noTint();
renderTarget.noStroke();
renderTarget.noSmooth();
screenBlend(projShape.blendMode);
renderTarget.beginShape();
renderTarget.texture( projShape.srcImage );
for (ProjectedShapeVertex vert : projShape.verts)
{
// add it to our shape
renderTarget.vertex(vert.dest.x, vert.dest.y, vert.src.x, vert.src.y);
renderTarget.vertex(vert.dest.x, vert.dest.y, vert.dest.z, vert.src.x, vert.src.y);
}
renderTarget.endShape(CLOSE);
}
}


// end class ProjectedShapeRenderer
}

11 changes: 8 additions & 3 deletions TextureMappingMultiple/ProjectedShapeVertex.pde
Expand Up @@ -9,11 +9,16 @@ class ProjectedShapeVertex

ProjectedShapeVertex(PVector _src, PVector _dest)
{
src = _src;
dest = _dest;
src = new PVector(_src.x, _src.y, _src.z);
dest = new PVector(_dest.x, _dest.y, _dest.z);
}


ProjectedShapeVertex(ProjectedShapeVertex srcVert)
{
src = new PVector(srcVert.src.x, srcVert.src.y, srcVert.src.z);
dest = new PVector(srcVert.dest.x, srcVert.dest.y, srcVert.dest.z);
}

void clear()
{
src = null;
Expand Down

0 comments on commit ca5746a

Please sign in to comment.