We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Texture 2d not working
Page Index Toggle Pages: 1
Texture 2d not working? (Read 404 times)
Texture 2d not working?
Jan 4th, 2009, 12:48am
 
I'm generating a texture with PGraphics and create an image using PGraphics.get();
Then I try to texture() a shape using the created image.  It doesn't work!

Can someone glance at the source code to see what's going on here?

live demo:
http://www.ivanlazarte.com/processing/ref/polygon_tex_2d/applet/

code:
PImage tex1;
color cred = color(255,0,0,255);

void setup() {
 size(500,300,P2D);
 smooth();
 textures();
}

void draw() {
 background(0);
 translate(mouseX,mouseY);
 image(tex1,0,0);
   
 stroke(cred);
 strokeWeight(2);
 
 textureMode(IMAGE);
 beginShape();
 texture(tex1);
 vertex(0, 00);
 vertex(75, 0);
 vertex(75, 75);
 vertex(0, 75);
 endShape(CLOSE);
}

void textures() {
 PGraphics tex1g = createGraphics(150,150,JAVA2D);
 tex1g.beginDraw();
 tex2(tex1g);
 tex1 = tex1g.get();
 tex1g.endDraw();
}

void tex2(PGraphics g) {
 for (int y = 0; y < g.height; y+=10) {
   for (int x = 0; x < g.width; x+=10) {
     g.ellipse(x,y,10,10);
   }
 }
}

Re: Texture 2d not working?
Reply #1 - Jan 4th, 2009, 8:16am
 
You have to specify the mapping of the texture onto the polygon in the calls to vertex(). For example,

 textureMode(NORMALIZED);
 beginShape();
 texture(tex1);
 vertex(0, 0, 0, 0);
 vertex(75, 0, 1, 0);
 vertex(75, 75, 1, 1);
 vertex(0, 75, 0, 1);
 endShape(CLOSE);

I set the texture mode to NORMALIZED to simplify the mapping.

Check out the last paragraph of the description for vertex() for more info.

http://processing.org/reference/vertex_.html

Change the third vertex() call to something like

 vertex(75, 75, 0.3, 0.3);

to get a better sense of how the mapping works.
Re: Texture 2d not working?
Reply #2 - Jan 4th, 2009, 6:18pm
 
How did I miss that?? Thanks Wombat.  I'm reading up on this uv coordinate stuff now.  Thanks again!
Re: Texture 2d not working?
Reply #3 - Jan 5th, 2009, 7:02am
 
If I understand correctly, bezier and curve-based shapes don't allow for texturing?  I'm trying to make it look like a curve is "cut" out of a pattern.  I guess the alternative are quads or strips to emulate curvy shapes?
Re: Texture 2d not working?
Reply #4 - Jan 5th, 2009, 8:58am
 
you can use bezierPoint() to access the points on the bezier curve and set vertices.

F
Re: Texture 2d not working?
Reply #5 - Jan 6th, 2009, 5:19am
 
Thanks fjen!

For fellow beginners, I've put together a working sample:
http://www.ivanlazarte.com/processing/test/bezier_point_tex/applet/

Quote:
/**
Try commenting/uncommenting textureMode.
By default the texture will warp to the curve.
*/

PImage tex;
int steps = 5;
int slen = 10;

void setup() {
  size(500,300,P2D);
  tex = loadImage("tex.jpg");
}

void draw() {
  background(127);
  translate(mouseX,mouseY);
  //textureMode(IMAGE);
  beginShape();
  texture(tex);
  vertex(0,0,0,0);
  int steps = 50;
  for (int i = 0; i <= steps; i++) {
    float t = i / float(steps);
    float x = bezierPoint(0,100,100,0,t);
    float y = bezierPoint(0,0,100,100,t);
    vertex(x,y,x,y);
  }
  endShape();
}


Page Index Toggle Pages: 1