2 questions geomerative / p3d
in
Contributed Library Questions
•
2 years ago
hi.
i want to add two things two the code below and need some help.
1. because of what the sketch will be used for later, i need to use p3d as a renderer. when i use it, no text is displayed. can somebody tell me why and what i need to change in the code?
2. i want to create only the outlines of the text, so that there is no fill in the text. i have seen the basic geomerative-library examples doing that, but i don't know how to put it in the code.. any ideas? or is there maybe another way than using geomerative?
thanks for any suggestions
--------------
//click and drag to see letters move
import laserschein.*;
Laserschein laser;
import geomerative.*;
import java.awt.geom.Line2D;
static final int fontdepth = 2; // recursion depth for word creation (*5)
static final float G = -0.5; //change gravity (*-1.0) taking away makes fleeing bubbly
static final float cursor_mass = 10000000.; //mass of cursor
float damping = 2.5;
float max_length;
Letter[] letters; //array of the letters
Line2D[] lines; // text lines for intersections
PFont f;
static final char[] charset = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
class Letter
{
PVector p;
PVector o;
PVector dp;
char letter;
float mass;
float k;
Letter(PVector pos,PVector dpos,char letterval,float fontsizeval,float thetaval)
{
p = new PVector(pos.x,pos.y);
o = new PVector(pos.x,pos.y);
dp = new PVector(dpos.x,dpos.y);
letter = letterval;
mass = 1.;
k = 10.;
}
}
void setup()
{
size(400,400); // warum geht es bei p3d nicht mehr?
frameRate(-1); // as fast as possible
laser = new Laserschein(this);
hint(ENABLE_NATIVE_FONTS);
Set();
}
void Set()
{
max_length = min(width,height)/4.0;
letters = new Letter[0];
lines = new Line2D[0];
f = loadFont ("Laserfont-48.vlw");
textFont(f);
text( "loading", (screen.width - textWidth("loading"))/2.0, (screen.height - 24.0)/2.0);
for(int i = 0; i < fontdepth; ++i) {
float fontsize = (48); // *max_length*pow(0.66,i)/wordRatio)
for(int j = 0; j < 1*i + 0; ++j) { //1*i used to be + 10 (number of repeating words)
boolean success = false;
for(int t = 0;t < 100; ++t) {
boolean thisTry = makeWord(fontsize);
if( thisTry ) {
success = true;
break;
}
}
if( !success ) {
println("failed to make letter");
}
}
}
background(0);
}
boolean makeWord(float fontsize)
{
textSize(fontsize);
PVector pos = new PVector(150,200); // word position (random(width),random(height)
String word = words[(int)random(words.length)];
float wlength = textWidth(word);
PVector end = new PVector(pos.x + wlength*cos(0), pos.y + wlength*sin(0));
if( end.x < 0 || width < end.x || end.y < 0 || height < end.y ) {
return false;
}
Letter[] newword = layoutWord(word, pos, fontsize, 0);
Line2D[] newlines = new Line2D[4];
float xoff = fontsize*cos(0-HALF_PI);
float yoff = fontsize*sin(0-HALF_PI);
newlines[0] = (Line2D) new Line2D.Float( pos.x, pos.y, end.x, end.y );
newlines[1] = (Line2D) new Line2D.Float( end.x, end.y, end.x +xoff, end.y +yoff );
newlines[2] = (Line2D) new Line2D.Float( end.x +xoff, end.y +yoff, pos.x +xoff, pos.y +yoff);
newlines[3] = (Line2D) new Line2D.Float( pos.x +xoff, pos.y +yoff, pos.x, pos.y );
for(int i = 0; i < lines.length; ++i) {
for(int j = 0; j < 4; ++j) {
if( newlines[j].intersectsLine(lines[i]) ) {
return false;
}
}
}
letters = (Letter[])concat(letters, newword );
lines = (Line2D[])concat(lines,newlines);
return true;
}
float time = 0.0;
void draw()
{
background(60);
Laser3D renderer = laser.renderer();
beginRaw(renderer);
noFill(); // important!
renderer.noSmooth();
float dtime = millis()/1000.0 - time;
time += dtime;
calcAccel(dtime);
calcMove(dtime);
resetMatrix();
stroke(255);
for(int i = 0; i < letters.length; ++i) {
pushMatrix();
translate(letters[i].p.x,letters[i].p.y);
text(letters[i].letter,0,0);
popMatrix();
}
endRaw();
}
void calcAccel(float time)
{
if( mousePressed ) {
PVector mouse_p = new PVector((float)mouseX,(float)mouseY);
float Gprime = G;
if( mouseButton == RIGHT ) {
Gprime *= -1;
}
for(int i = 0; i < letters.length; ++i) {
PVector r = new PVector(letters[i].p.x, letters[i].p.y);
r.sub(mouse_p);
PVector d = new PVector(letters[i].p.x, letters[i].p.y);
d.sub(letters[i].o);
PVector accel = new PVector(r.x,r.y);
accel.mult( Gprime*cursor_mass/max( pow(r.mag(),3.0), 250.0 ) );
PVector tmp_1 = new PVector(d.x,d.y);
tmp_1.mult( -letters[i].k/letters[i].mass );
accel.add( tmp_1 );
PVector tmp_2 = new PVector(letters[i].dp.x,letters[i].dp.y);
tmp_2.mult( -damping/letters[i].mass );
accel.add( tmp_2 );
accel.mult( time );
letters[i].dp.add(accel);
}
} else {
for(int i = 0; i < letters.length; ++i) {
PVector d = new PVector(letters[i].p.x, letters[i].p.y);
d.sub(letters[i].o);
PVector accel = new PVector(d.x,d.y);
accel.mult( -letters[i].k/letters[i].mass );
PVector tmp_2 = new PVector(letters[i].dp.x,letters[i].dp.y);
tmp_2.mult( -damping/letters[i].mass );
accel.add( tmp_2 );
accel.mult( time );
letters[i].dp.add(accel);
}
}
}
void calcMove(float time)
{
for(int i = 0; i < letters.length; ++i) {
PVector tmp_3 = new PVector(letters[i].dp.x,letters[i].dp.y);
tmp_3.mult(time);
letters[i].p.add(tmp_3);
}
}
Letter[] layoutWord(String word, PVector pos, float fontsize, float theta)
{
Letter[] newletters = new Letter[5];
PVector cur_pos = new PVector(pos.x,pos.y);
PVector zero = new PVector(0.0,0.0);
for(int i = 0; i < 5; ++i) {
Letter l = new Letter( cur_pos, zero, word.charAt(i), fontsize, 0 );
newletters[i] = l;
textSize(fontsize);
float w = textWidth(word.charAt(i));
cur_pos.x += w*cos(0);
cur_pos.y += w*sin(0);
}
return newletters;
}
String[] words = {
"laser"
};
1