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 & HelpOpenGL and 3D Libraries › changing a textures alpha
Page Index Toggle Pages: 1
changing a textures alpha (Read 1069 times)
changing a textures alpha
Apr 8th, 2006, 2:48am
 
i tried changing the alpha values of the image texture to test if i could change the transparency of the image itself but and display the image using the image function and i the transparency of the image was the same (although when i changed the rgb values the differences were visible). im assuming when i use the image function to display the altered (as opposed to writing to the pixels array) it ignores alpha/transparency values? but my ultimate question is how do you change a textures alpha/opacity?

Re: changing a textures alpha
Reply #1 - Apr 8th, 2006, 12:40pm
 
You would set an alpha fill colour before drawing your polygon with the texture on it.
Code:

PImage picture;
void setup(){
size(400,400,P3D);
picture = loadImage("pic.gif");//a 100x100 .gif
}
void draw(){
background(150);
translate(200,200);
rotateX((PI/400)*mouseY);
rotateY((PI/400)*mouseX);
fill(255, 255, 255, 150);
beginShape(QUADS);
texture(picture);
vertex(-100,-100,0,0,0);
vertex(100,-100,0,100,0);
vertex(100,100,0,100,100);
vertex(-100,100,0,0,100);
endShape();
beginShape(QUADS);
texture(picture);
vertex(-100,-100,100,0,0);
vertex(100,-100,100,100,0);
vertex(100,100,100,100,100);
vertex(-100,100,100,0,100);
endShape();
}
Re: changing a textures alpha
Reply #2 - Apr 8th, 2006, 6:34pm
 
hm. that solution works. but the transparency doesnt blend as i thought because of the transparent white fill. it saturates the colors of whatever image is behind the texture... is there a way to create a more transparent effect such as in photoshop or flash? i dont think blend() work because im overlaying two textured shapes.
Re: changing a textures alpha
Reply #3 - Apr 9th, 2006, 4:41pm
 
i tried using PGraphics3 as a texture instead of PImage but the texture rendered the same...
Re: changing a textures alpha
Reply #4 - Apr 9th, 2006, 5:09pm
 
I haven't used this command before so I've no idea how to represent how to implement it.

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

It isn't even referenced properly, you only find an allusion to it in the PImage page. You might try experimenting with it though.
Re: changing a textures alpha
Reply #5 - Apr 9th, 2006, 7:46pm
 
i wrote a smaller test program as i should have from the beginning... and the transparency works as i wanted.

http://newmedia.rit.edu/allclasses/ve_2005/students/bryanijeoma/CRUNK.gif
http://newmedia.rit.edu/allclasses/ve_2005/students/bryanijeoma/CRUNKtest.gif
http://newmedia.rit.edu/allclasses/ve_2005/students/bryanijeoma/transparencytest.jpg

PImage a,b;

void setup(){  
 a = loadImage("a.gif");//black
 b = loadImage("b.gif");//pink
 
 size(a.width+200,a.height+200,P3D);

 noStroke();
}
void draw(){
 background(255);
   
 pushMatrix();  
 translate(width/2-a.width/2,height/2-a.height/2);
 beginShape(QUADS);
 fill(255,255,255,255);
 texture(a);
 vertex(0,0,0,0);
 vertex(a.width,0,a.width,0);
 vertex(a.width,a.height,a.width,a.height);
 vertex(0,a.height,0,a.height);
 endShape();
 popMatrix();
 
 pushMatrix();
 translate((width/2-a.width/2)+10,height/2-a.height/2);
 beginShape(QUADS);
 fill(255,255,255,200);
 texture(b);
 vertex(0,0,0,0);
 vertex(a.width,0,a.width,0);
 vertex(a.width,a.height,a.width,a.height);
 vertex(0,a.height,0,a.height);
 endShape();  
 popMatrix();
}
Re: changing a textures alpha
Reply #6 - Apr 9th, 2006, 8:33pm
 
in my program though i use copy... to copy certain parts of the image and save into an new PImage object the tranparency stops working. i tried changing the format parameter of both images but i didnt see any noticeable changes. also because of  more complex manipulations in the large program i cannot use the mask function to mask the area i do not want to be visible.

import processing.opengl.*;

PImage a,b;
PImage[] as,bs;
int[][] xs, ys;

int polygon_group_width, polygon_group_height, polygon_size, polygon_count;

void setup(){  
 a = loadImage("CRUNK.gif");
 b = loadImage("CRUNKtest.gif");
 
 //a.format = ALPHA;
 //b.format = ALPHA;

 size(a.width+200,a.height+200,OPENGL);

 polygon_size = 45;

 polygon_group_width = ceil(a.width/polygon_size)+2;
 polygon_group_height = ceil(a.height/polygon_size)+2;

 polygon_count = (polygon_group_width) * ( polygon_group_height);

 xs = new int[polygon_group_width][polygon_group_height];
 ys = new int[polygon_group_width][polygon_group_height];

 for(int y = 0; y < polygon_group_height; y++) {
   for(int x = 0; x < polygon_group_width; x++) {  
     xs[x][y] = x*polygon_size;
     ys[x][y] = y*polygon_size;
   }
 }  

 as = new PImage[polygon_count];  
 bs = new PImage[polygon_count];  

 splitImage();
 noStroke();
}

void draw(){
 background(255);
 renderSplitImage();
}

void renderSplitImage() {
 pushMatrix();
 translate((width/2-a.width/2),(height/2-a.height/2));
 for(int y = 0; y < (polygon_group_height-1); y++) {
   for(int x = 0; x < (polygon_group_width-1); x++) {          
     beginShape(QUADS);      
     fill(255, 255, 255, 255);
     texture(as[x + y*polygon_group_width]);
     vertex(xs[x][y], ys[x][y],0,0);
     vertex(xs[x+1][y], ys[x+1][y],polygon_size,0);
     vertex(xs[x+1][y+1], ys[x+1][y+1],polygon_size,polygon_size);
     vertex(xs[x][y+1], ys[x][y+1],0,polygon_size);
     endShape();
   }
 }  
 popMatrix();

 pushMatrix();
 translate((width/2-b.width/2)+10,height/2-b.height/2);
 for(int y = 0; y < (polygon_group_height-1); y++) {
   for(int x = 0; x < (polygon_group_width-1); x++) {          
     beginShape(QUADS);      
     fill(255, 255, 255, 100);
     texture(bs[x + y*polygon_group_width]);
     vertex(xs[x][y], ys[x][y],0,0);
     vertex(xs[x+1][y], ys[x+1][y],polygon_size,0);
     vertex(xs[x+1][y+1], ys[x+1][y+1],polygon_size,polygon_size);
     vertex(xs[x][y+1], ys[x][y+1],0,polygon_size);
     endShape();
   }
 }  
 popMatrix();  
}

void splitImage() {
 int count = 0;  

 for(int y = 0; y < polygon_group_height; y++) {
   for(int x = 0; x < polygon_group_width; x++) {      
     as[count] = new PImage(polygon_size,polygon_size);  
     //as[count] = copyImage(a, x*polygon_size,y*polygon_size, (x*polygon_size)+polygon_size, (y*polygon_size)+polygon_size, polygon_size, polygon_size);        
     as[count].copy(a, x*polygon_size,y*polygon_size, polygon_size, polygon_size, 0, 0, polygon_size, polygon_size);
     
     bs[count] = new PImage(polygon_size,polygon_size);  
     //bs[count] = copyImage(b, x*polygon_size,y*polygon_size, (x*polygon_size)+polygon_size, (y*polygon_size)+polygon_size, polygon_size, polygon_size);  
     bs[count].copy(b, x*polygon_size,y*polygon_size, polygon_size, polygon_size, 0, 0, polygon_size, polygon_size);
     count++;
   }
 }
}

PImage copyImage(PImage c,int x1, int y1, int x2, int y2, int w, int h) {
 PImage d = new PImage(w,h);
 int count = 0;
 loadPixels();  
 for (int y = y1; y <y2; y++)
 {  
   for (int x = x1; x < x2; x++)
   {
     d.pixels[count] = c.pixels[y*width+x];
     count++;
   }
 }  
 updatePixels();
 return d;
}

http://newmedia.rit.edu/allclasses/ve_2005/students/bryanijeoma/CRUNK.gif
http://newmedia.rit.edu/allclasses/ve_2005/students/bryanijeoma/CRUNKtest.gif
http://newmedia.rit.edu/allclasses/ve_2005/students/bryanijeoma/transparencytest2.jpg

Re: changing a textures alpha
Reply #7 - Apr 10th, 2006, 1:50am
 
I don't get how you got the first example to work. That image of yours gets downloaded as a .gif.png and the code does not work on my PC version of Processing. Processing cannot load pngs, if it does, it doesn't do it prettily.

I sat down and figured out mask. I just inverted your black CRUNK and saved it as CRUNKmask.gif, then coloured the original CRUNK pink so I had a usable CRUNKtest.gif file instead of your png which goes all funky.

I've come across repeated issues with OPENGL and rendering images. If you come across the same trouble, try calling pimage.updatePixels() on an image the moment before you use it. It doesn't use up any overhead and it will fix any image garbling that tends to crop up after its been running for a while.

Code:

import processing.opengl.*;

PImage a,b,m;
PImage[] as,bs;
int[][] xs, ys;
int polygon_group_width, polygon_group_height, polygon_size, polygon_count;

void setup(){
size(597,311,OPENGL);
a = loadImage("CRUNK.gif");
b = loadImage("CRUNKtest.gif");
m = loadImage("CRUNKmask.gif");
polygon_size = 45;
polygon_group_width = ceil(a.width/polygon_size)+2;
polygon_group_height = ceil(a.height/polygon_size)+2;
polygon_count = (polygon_group_width) * ( polygon_group_height);
xs = new int[polygon_group_width][polygon_group_height];
ys = new int[polygon_group_width][polygon_group_height];
for(int y = 0; y < polygon_group_height; y++) {
for(int x = 0; x < polygon_group_width; x++) {
xs[x][y] = x*polygon_size;
ys[x][y] = y*polygon_size;
}
}
as = new PImage[polygon_count];
bs = new PImage[polygon_count];
splitImage();
noStroke();
}

void draw(){
background(255);
renderSplitImage();
}

void renderSplitImage() {
pushMatrix();
translate((width/2-a.width/2),(height/2-a.height/2));
for(int x = 0; x < (polygon_group_width-1); x++) {
for(int y = 0; y < (polygon_group_height-1); y++) {
beginShape(QUADS);
fill(255, 255, 255, 255);
texture(as[x + y*polygon_group_width]);
vertex(xs[x][y], ys[x][y],0,0);
vertex(xs[x+1][y], ys[x+1][y],polygon_size,0);
vertex(xs[x+1][y+1], ys[x+1][y+1],polygon_size,polygon_size);
vertex(xs[x][y+1], ys[x][y+1],0,polygon_size);
endShape();
}
}
popMatrix();
pushMatrix();
translate((width/2-b.width/2)+10,height/2-b.height/2);
for(int x = 0; x < (polygon_group_width-1); x++) {
for(int y = 0; y < (polygon_group_height-1); y++) {
beginShape(QUADS);
fill(255, 255, 255, 155);
texture(bs[x + y*polygon_group_width]);
vertex(xs[x][y], ys[x][y],0,0);
vertex(xs[x+1][y], ys[x+1][y],polygon_size,0);
vertex(xs[x+1][y+1], ys[x+1][y+1],polygon_size,polygon_size);
vertex(xs[x][y+1], ys[x][y+1],0,polygon_size);
endShape();
}
}
popMatrix();
}

void splitImage() {
PImage [] ms = new PImage[polygon_count];
for(int x = 0; x < polygon_group_width; x++) {
for(int y = 0; y < polygon_group_height; y++) {
as[x + y * polygon_group_width] = new PImage(polygon_size,polygon_size);
as[x + y * polygon_group_width].copy(a, x*polygon_size,y*polygon_size, polygon_size, polygon_size, 0, 0, polygon_size, polygon_size);
bs[x + y * polygon_group_width] = new PImage(polygon_size,polygon_size);
bs[x + y * polygon_group_width].copy(b, x*polygon_size,y*polygon_size, polygon_size, polygon_size, 0, 0, polygon_size, polygon_size);
ms[x + y * polygon_group_width] = new PImage(polygon_size,polygon_size);
ms[x + y * polygon_group_width].copy(m, x*polygon_size,y*polygon_size, polygon_size, polygon_size, 0, 0, polygon_size, polygon_size);
bs[x + y * polygon_group_width].mask(ms[x + y * polygon_group_width]);
}
}
}
Re: changing a textures alpha
Reply #8 - Apr 10th, 2006, 3:44am
 
thanks for the responses st33d i actually figured a simplier way than what your solution which ill post later.
Page Index Toggle Pages: 1