FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   quad in three dimensions
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: quad in three dimensions  (Read 1228 times)
st33d

WWW Email
quad in three dimensions
« on: Feb 22nd, 2005, 12:50am »

Can't get my head around using quads to map out a 3D terrain. The transformation function seems all screwy with it. How do I get the quads view to mimic the other views?
 
keyPressed: change view mode
 
mousePressed: colour or greyscale
 
Code:

boolean newFrame = false;
int xm = 176;
int ym = 144;
int zm = 50;
boolean col = true;
int con = 3;
void setup()  {
  size (800,600);
  ///*
  try{
    quicktime.QTSession.open();
    quicktime.std.sg.SequenceGrabber sg = new quicktime.std.sg.SequenceGrabber();
    quicktime.std.sg.SGVideoChannel sc  = new quicktime.std.sg.SGVideoChannel(sg);
    quicktime.std.sg.VideoDigitizer vd  = sc.getDigitizerComponent();
    println( "dv.getNumberOfInputs :" ); println( vd.getNumberOfInputs() ); println();
    vd.setInput(2);
  } catch (Exception e) {
    e.printStackTrace();
  }
  //*/
  beginVideo(xm, ym, 12);
}
 
void videoEvent() {
  newFrame = true;
 
}
 
void loop()  {
  if(newFrame) {
    //background(video);
    newFrame = false;
  }
  background(255);
  push();
  if(con<3){
  translate(width/2,height/2,300);
  }else{
  translate(70,30);
  }
  rotateY((TWO_PI/width)*mouseX);
  rotateX((TWO_PI/width)*mouseY);
  //rotateX(PI);
  drawCube();
  cubeBrown();
  pop();
}
void mousePressed(){
  col = alt(col);
}
void keyPressed(){
  con = (con+1)%4;
}
void cubeBrown(){
  strokeWeight(1);
  stroke(15,15,15,100);
  line(-(xm/2),-(ym/2),-(zm/2),(xm/2),-(ym/2),-(zm/2));
  line(-(xm/2),-(ym/2),-(zm/2),-(xm/2),(ym/2),-(zm/2));
  line(-(xm/2),-(ym/2),-(zm/2),-(xm/2),-(ym/2),(zm/2));
  line((xm/2),-(ym/2),-(zm/2),(xm/2),(ym/2),-(zm/2));
  line((xm/2),-(ym/2),-(zm/2),(xm/2),-(ym/2),(zm/2));
  line(-(xm/2),(ym/2),-(zm/2),(xm/2),(ym/2),-(zm/2));
  line(-(xm/2),-(ym/2),(zm/2),(xm/2),-(ym/2),(zm/2));
  line(-(xm/2),-(ym/2),(zm/2),-(xm/2),(ym/2),(zm/2));
  line(-(xm/2),(ym/2),(zm/2),(xm/2),(ym/2),(zm/2));
  line(-(xm/2),(ym/2),(zm/2),-(xm/2),(ym/2),-(zm/2));
  line((xm/2),(ym/2),(zm/2),(xm/2),(ym/2),-(zm/2));
  line((xm/2),(ym/2),(zm/2),(xm/2),-(ym/2),(zm/2));
}
boolean alt(boolean xt){
  if (xt){
    xt = false;
  }else{
    xt = true;
  }
  return xt;
}
void drawCube(){
  for (int ix = 1; ix < xm-1; ix++){
    for (int iy = 1; iy < ym-1; iy++){
 float zee = zm;
 int c1 = int((zee/255.0)*grey(video.pixels[ix + iy*xm]));
 int c2 = int((zee/255.0)*grey(video.pixels[ix + 1 + iy*xm]));
 int c3 = int((zee/255.0)*grey(video.pixels[ix + (iy + 1)*xm]));
 int c4 = int((zee/255.0)*grey(video.pixels[ix + 1 + (iy + 1)*xm]));
 if (col){
   stroke(video.pixels[ix + iy*xm]);
 }else{
   stroke(grey(video.pixels[ix + iy*xm]));
 }
 if(con == 0){
 strokeWeight(3);
 point(ix-(xm/2),iy-(ym/2),c1-(zm/2));
 }
 if(con == 1){
 line(ix-(xm/2),iy-(ym/2),c1-(zm/2), ix-(xm/2)+1,iy-(ym/2),c2-(zm/2));
 }
 if(con == 2){
 line(ix-(xm/2),iy-(ym/2),c1-(zm/2), ix-(xm/2)+1,iy-(ym/2),c2-(zm/2));
 line(ix-(xm/2),iy-(ym/2),c1-(zm/2), ix-(xm/2),iy-(ym/2)+1,c3-(zm/2));
 }
 if(con == 3){
 noStroke();
 if (col){
   fill(video.pixels[ix + iy*xm]);
 }else{
   fill(grey(video.pixels[ix + iy*xm]));
 }  
 quad(
   screenX(ix*3,iy*3,c1*3),screenY(ix*3,iy*3,c1*3),
   screenX((ix+1)*3,iy*3,c2*3),screenY((ix+1)*3,iy*3,c2*3),
   screenX((ix+1)*3,(iy+1)*3,c4*3),screenY((ix+1)*3,(iy+1)*3,c4*3),
   screenX(ix*3,(iy+1)*3,c3*3),screenY(ix*3,(iy+1)*3,c3*3)  
   );
 
 }
    }
  }
}
 
float grey(color p){
  float r = red(p);
  float g = green(p);
  float b = blue(p);
  return (r+g+b)/3;
}
 

I could murder a pint.
fjen

WWW
Re: quad in three dimensions
« Reply #1 on: Feb 22nd, 2005, 9:37am »

as you can see from the reference quad() is a 2d function. use beginShape(QUADS) for that in 3d ...
http://processing.org/reference/beginShape_.html
 
then to map the video image onto that use textureVertex():
http://processing.org/discourse/yabb/board_Proce55ing_Software__action_display_num_1079193026.html
 
( note that in .68 vertexTexture(BImage img) was texture(BImage img) )
 
and here's how screenX are ment to be used:
http://processing.org/discourse/yabb/board_Syntax_action_displa_y_num_1103032712_start_2.html
 
 
/F
 
st33d

WWW Email
Re: quad in three dimensions
« Reply #2 on: Feb 22nd, 2005, 8:08pm »

Three dimensional quads work fine. Thanks very much for that, I have the desired landscape effect without any major change to the script.
 
Since I'm building a landscape of quads though, I don't understand how width settings like u and v give me a pixel location to paint a quad.
Code:

beginShape(QUADS);
texture(video);
vertex(ix-(xm/2),iy-(ym/2),c1-(zm/2),(1/xm)*ix,(1/ym)*iy);
vertex((ix+1)-(xm/2),iy-(ym/2),c2-(zm/2),(1/xm)*ix,(1/ym)*iy);
vertex((ix+1)-(xm/2),(iy+1)-(ym/2),c4-(zm/2),(1/xm)*ix,(1/ym)*iy);
vertex(ix-(xm/2),(iy+1)-(ym/2),c3-(zm/2),(1/xm)*ix,(1/ym)*iy);
endShape();

I think I may have the u,v equation wrong but I don't understand how.
 
I have noticed i've written a completley useless function though. My boolean alt is exactly the same as doing boolean = !boolean.
 
Code:

beginShape(QUADS);
texture(video);
vertex(-(xm/2),-(ym/2),0,0,0);
vertex((xm/2),-(ym/2),0,1,0);
vertex((xm/2),(ym/2),0,1,1);
vertex(-(xm/2),(ym/2),0,0,1);
endShape();
}

This doesn't seem to work either. I have no idea how to use this texture() thing.
« Last Edit: Feb 22nd, 2005, 11:35pm by st33d »  

I could murder a pint.
JohnG

WWW
Re: quad in three dimensions
« Reply #3 on: Feb 23rd, 2005, 12:36am »

the u and v values are the co-ordinates in the texture, that that vertex should be attached to.
 
Think of it like you've got an image made of rubber, you say where abouts on the image the verticies of your quad lay, then the computer stretches the image so that the corners line up.
 
an attempted example:
Code:

My texture:
  u--->  
v  +-----+
|  |     |
|  |     |
.  |     |
   +-----+

for vertex 1, I set the u,v values to be, say, 0,0, that means that that vertex will be attached to the top left of this texture.
Then set the u,v co-orde of the other corners and you could have somethign like this:
Code:

1
+--------+
|\ ^-,   |
| \   ^-,|2
|  \    /|
|   *--* |
|  4   3 |
+--------+

I've numbered the verticies in the order they're specificed in a beginShape(QUADS);
Now, when it comes time to draw, this bi of the texture is cut out of the whole image, then stretched so it fits onto the actual QUAD, which may well be square, not the shape shown on the texture.
 
It's not the simplest thing to explain, but basically, the U is the horizontal position in the texture that this vertex should be painted with, and v is the vertical position, then the computer fills in all the parts between the corners correctly.
 
st33d

WWW Email
Re: quad in three dimensions
« Reply #4 on: Feb 23rd, 2005, 12:23pm »

Then why doesn't this work?
Code:

beginShape(QUADS);
texture(video);
vertex(-(xm/2),-(ym/2),0,0,0);
vertex((xm/2),-(ym/2),0,1,0);
vertex((xm/2),(ym/2),0,1,1);
vertex(-(xm/2),(ym/2),0,0,1);
endShape();
}

or this?
Code:

texture(video);
vertex(-(xm/2),-(ym/2),0,0,0);
vertex((xm/2),-(ym/2),0,176,0);
vertex((xm/2),(ym/2),0,176,144);
vertex(-(xm/2),(ym/2),0,0,144);
endShape();
 

I could murder a pint.
fjen

WWW
Re: quad in three dimensions
« Reply #5 on: Feb 23rd, 2005, 3:30pm »

seems like you are looking in the wrong place, the code works fine for me ...
 
Code:

BImage img;
 
void setup() {
  size(200,200);
  img = loadImage("house.jpg");  // from examples
}
 
void loop(){
  int xm = width;
  int ym = height;
 
  beginShape(QUADS);
   texture(img);  
   vertex(-(xm/2),-(ym/2),0,  0,0);  
   vertex((xm/2),-(ym/2),0,  176,0);  
   vertex((xm/2),(ym/2),0,  176,144);  
   vertex(-(xm/2),(ym/2),0,  0,144);
 endShape();
}

 
/F
 
st33d

WWW Email
Re: quad in three dimensions
« Reply #6 on: Feb 23rd, 2005, 5:05pm »

Ah. Didn't understand that I had to white the square first (fill(255). Thanks.
 

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »