2017年12月14日 星期四

7Hao-計算機圖學-Week14

主題: Rasterization

用P語言
1.
size(600,600,P3D);
background(255);
beginShape(TRIANGLE);
stroke(255,0,0); vertex(300,100);
stroke(255,255,0); vertex(500,500);
stroke(0,0,255); vertex(100,500);
endShape();


2.
size(600,600,P3D);
background(255);
beginShape(TRIANGLE);
fill(255,0,0); vertex(300,100);
fill(255,255,0); vertex(500,500);
fill(0,0,255); vertex(100,500);
endShape();


3.
void setup(){
  size(600,600,P3D);
}
void draw(){
  background(255);
  beginShape(TRIANGLE);
  fill(255,0,0); vertex(100,100,0);
  fill(255,0,255); vertex(100,500,100);
  fill(255,255,0); vertex(500,500,0);
  endShape();
  beginShape(TRIANGLE);
  fill(0,255,0); vertex(500,100,0);
  fill(0,255,255); vertex(500,500,100);
  fill(255,255,0); vertex(100,500,0);
  endShape();
}

4.
void setup(){
  size(600,600,P3D);
}
void draw(){
  background(255);
  beginShape(TRIANGLE);
  fill(255,0,0); vertex(100,100,0);
  fill(255,0,255); vertex(100,500,100);
  fill(255,255,0); vertex(500,500,0);
  endShape();
  beginShape(TRIANGLE);
  fill(0,255,0); vertex(500,100,0);
  fill(0,255,255); vertex(500,500,20);
  fill(255,255,0); vertex(100,500,0);
  endShape();
}

-----------------------------------------------------------
期末Q&A

1. 3D Mario
void setup(){
  size(600,400,P3D);
}
float marioX=300,marioY=300,marioZ=0;
float marioVX=0,marioVY=0,marioVZ=0;
int marioDir=0;
boolean jumping=false;
void draw(){
  background(#6DD5FA);
  beginShape(QUAD);
    vertex(0,300,-300);
    vertex(0,300,300);
    vertex(600,300,300);
    vertex(600,300,-300);
  endShape();
  lights();
  pushMatrix();
    translate(marioX,marioY,marioZ);
    noStroke(); sphere(20);
  popMatrix();
    marioX+=marioVX;
    marioZ+=marioVZ;
    if(jumping){
      marioY+=marioVY;
      marioVY+=0.98;
      if(marioY>300){
        jumping=false; 
        marioVY=0;
        marioY=300;
      }
    }
}
void keyPressed(){
  if(keyCode==RIGHT) marioVX=3;
  if(keyCode==LEFT) marioVX=-3;
  if(keyCode==UP) marioVZ=-3;
  if(keyCode==DOWN) marioVZ=3;
  if(key==' ' && jumping==false){marioVY=-20; jumping=true;}
}
void keyReleased(){
  if(keyCode==RIGHT || keyCode==LEFT) marioVX=0;
  if(keyCode==UP || keyCode==DOWN ) marioVZ=0;
}

沒有留言:

張貼留言