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 › Random Textures.
Page Index Toggle Pages: 1
Random Textures. (Read 631 times)
Random Textures.
Nov 29th, 2006, 7:37am
 
Hi,

i'm trying to randomly map texture on objects, but i can't find the proper command.

I actually would like to do something like this :

Quote:
PImage img0 = loadImage("tex0.png");
PImage img1 = loadImage("tex1.png");
PImage img2 = loadImage("tex2.png");
PImage img3 = loadImage("tex3.png");
texture(random(img0,img1,img2,img3));


I know this code won't work, because random() is only dealing with numbers.

Any idea ?
Re: Random Textures.
Reply #1 - Nov 29th, 2006, 12:14pm
 
Hi,

You can simply make a PImage array as follows:
Code:

PImage img0 = loadImage("tex0.png");
PImage img1 = loadImage("tex1.png");
PImage img2 = loadImage("tex2.png");
PImage img3 = loadImage("tex3.png");

PImage[] images = new PImage{img0, img1, img2, img3};

// retrieve random 0..3 element of the array
PImage texture = images[(round(random(0,3))];


Hope that helps Smiley
Re: Random Textures.
Reply #2 - Nov 29th, 2006, 4:59pm
 
Tried, but it doesn't work as i get an error message ( unexpected token: { )pointing at

Quote:
PImage[] images = new PImage{img0, img1, img2};


Here is the portion of the code where i put your example :

Quote:
PImage img0 = loadImage("skin0.png");
PImage img1 = loadImage("skin1.png");
PImage img2 = loadImage("skin2.png");
PImage[] images = new PImage{img0, img1, img2};
PImage texture = images[ round(random(0,2))];
void getFrogs(){
   float[] pos;
   textureMode(NORMALIZED);
   beginShape();
   texture(images);
   ........
 }


So after reading the PImage reference section, i changed the {} by ().

Now i get :

Quote:
/tmp/build40927.tmp/Temporary_5188_8530.java:36:23:36:50: Semantic Error: No applicable overload was found for a constructor with signature "PImage(processing.core.PImage, processing.core.PImage, processing.core.PImage)" in type "processing.core.PImage". Perhaps you wanted the overloaded version "PImage(int $1, int $2, int $3);" instead?

/tmp/build40927.tmp/Temporary_5188_8530.java:242:13:242:18: Semantic Error: No accessible field named "images" was found in type "Temporary_5188_8530$Frogs".


What's wrong with it ?
Re: Random Textures.
Reply #3 - Nov 29th, 2006, 5:54pm
 
Sorry about the faulty syntax. What I meant to do is declare a PImage array literal, which the curly braces do in java, which is not my usual language, so I forgot the exact syntax of it.

Here's what you want:

Code:

PImage img0 = loadImage("skin0.png");
PImage img1 = loadImage("skin1.png");
PImage img2 = loadImage("skin2.png");
PImage[] images = {img0, img1, img2};

void getFrogs(){
float[] pos;
textureMode(NORMALIZED);
beginShape();
texture(images[ round(random(0,3))] );
........
}


This should work as you intended. You can learn more about arrays at http://www.scit.wlv.ac.uk/~jphb/java/arrays.html or just google it--they're a fundamental and powerful concept in programming, so become well versed in them, as they come up a lot.
Re: Random Textures.
Reply #4 - Nov 29th, 2006, 6:21pm
 
WOW, it's working like a charm !

Thank you so much Smiley
Page Index Toggle Pages: 1