void setup(){
size(300,200);//第一個小視窗
background(255,0,0);
WindowB child =new WindowB();
}
void draw(){//空白的
}
class WindowB extends PApplet{
public WindowB(){//建構子
super();//呼叫上一層建構子
PApplet.runSketch(new String[]{this.getClass().getName()},this);
}
public void settings(){
size(300,200);
}
public void setup(){
//size(200,200);
background(0,255,0);
}
public void draw(){//空白的
}
}
PGraphics pg;
void setup(){
size(400,400,P3D);
pg=createGraphics(200,200,P3D);
}
void draw(){
background(255,0,0);
pg.beginDraw();
pg.background(0,255,0);
pg.translate(100,100);
pg.rotateY(radians(frameCount));
pg.box(100);
pg.endDraw();
image(pg,0,0);
}
PGraphics pg,pg2,pg3,pg4;
void setup(){
size(400,400,P3D);
pg=createGraphics(200,200,P3D);
pg2=createGraphics(200,200,P3D);
pg3=createGraphics(200,200,P3D);
pg4=createGraphics(200,200,P3D);
}
void draw(){
background(255,0,0);
pg.beginDraw();
pg.background(0,255,0);
pg.translate(100,100);
pg.rotateY(radians(frameCount));
pg.box(100);
pg.endDraw();
pg2.beginDraw();
pg2.background(255,255,0);
pg2.translate(100,100);
pg2.rotateY(radians(frameCount));
pg2.box(100);
pg2.endDraw();
pg3.beginDraw();
pg3.background(255,0,0);
pg3.translate(100,100);
pg3.rotateY(radians(frameCount));
pg3.box(100);
pg3.endDraw();
pg4.beginDraw();
pg4.background(0,255,255);
pg4.translate(100,100);
pg4.rotateY(radians(frameCount));
pg4.box(100);
pg4.endDraw();
image(pg,0,0);
image(pg2,200,0);
image(pg3,0,200);
image(pg4,200,200);
}
//修改自week15_03_Multiple_window_pg1_pg2_pg3_pg4
//File>Example>Demos>Tests>MultipleWindows
PGraphics pg,pg2,pg3,pg4;
Arcball arcball;
void setup(){
size(400,400,P3D);
arcball=new Arcball(this,200);
pg=createGraphics(200,200,P3D);
pg2=createGraphics(200,200,P3D);
pg3=createGraphics(200,200,P3D);
pg4=createGraphics(200,200,P3D);
}
void mousePressed(){
arcball.mousePressed();
}
void mouseDragged(){
arcball.mouseDragged();
}
void draw(){
background(255,0,0);
pg.beginDraw();
pg.background(0,255,0);
arcball.run();
//pg.translate(100,100);
//pg.rotateY(radians(frameCount));
pg.box(100);
pg.endDraw();
pg2.beginDraw();
pg2.background(255,255,0);
pg2.translate(100,100);
pg2.rotateY(radians(frameCount));
pg2.box(100);
pg2.endDraw();
pg3.beginDraw();
pg3.background(255,0,0);
pg3.translate(100,100);
pg3.rotateY(radians(frameCount));
pg3.box(100);
pg3.endDraw();
pg4.beginDraw();
pg4.background(0,255,255);
pg4.translate(100,100);
pg4.rotateY(radians(frameCount));
pg4.box(100);
pg4.endDraw();
image(pg,0,0);
image(pg2,200,0);
image(pg3,0,200);
image(pg4,200,200);
}
// Ariel and V3ga's arcball class with a couple tiny mods by Robert Hodgin
class Arcball {
PApplet parent;
float center_x, center_y, radius;
Vec3 v_down, v_drag;
Quat q_now, q_down, q_drag;
Vec3[] axisSet;
int axis;
float mxv, myv;
float x, y;
Arcball(PApplet parent, float radius){
this.parent = parent;
this.radius = radius;
v_down = new Vec3();
v_drag = new Vec3();
q_now = new Quat();
q_down = new Quat();
q_drag = new Quat();
axisSet = new Vec3[] {new Vec3(1.0f, 0.0f, 0.0f), new Vec3(0.0f, 1.0f, 0.0f), new Vec3(0.0f, 0.0f, 1.0f)};
axis = -1; // no constraints...
}
void mousePressed(){
v_down = mouse_to_sphere(parent.mouseX, parent.mouseY);
q_down.set(q_now);
q_drag.reset();
}
void mouseDragged(){
v_drag = mouse_to_sphere(parent.mouseX, parent.mouseY);
q_drag.set(Vec3.dot(v_down, v_drag), Vec3.cross(v_down, v_drag));
}
void run(){
center_x = 100;//pg 200X200 的旋轉中心要放在(100,100)
center_y = 100;
q_now = Quat.mul(q_drag, q_down);
pg.translate(center_x, center_y);//讓旋轉變成PG裡的旋轉
applyQuat2Matrix(q_now);
x += mxv;
y += myv;
mxv -= mxv * .01;
myv -= myv * .01;
}
Vec3 mouse_to_sphere(float x, float y){
Vec3 v = new Vec3();
v.x = (x - center_x) / radius;
v.y = (y - center_y) / radius;
float mag = v.x * v.x + v.y * v.y;
if (mag > 1.0f){
v.normalize();
} else {
v.z = sqrt(1.0f - mag);
}
return (axis == -1) ? v : constrain_vector(v, axisSet[axis]);
}
Vec3 constrain_vector(Vec3 vector, Vec3 axis){
Vec3 res = new Vec3();
res.sub(vector, Vec3.mul(axis, Vec3.dot(axis, vector)));
res.normalize();
return res;
}
void applyQuat2Matrix(Quat q){
// instead of transforming q into a matrix and applying it...
float[] aa = q.getValue();
pg.rotate(aa[0], aa[1], aa[2], aa[3]);
}
}
static class Vec3{
float x, y, z;
Vec3(){
}
Vec3(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
void normalize(){
float length = length();
x /= length;
y /= length;
z /= length;
}
float length(){
return (float) Math.sqrt(x * x + y * y + z * z);
}
static Vec3 cross(Vec3 v1, Vec3 v2){
Vec3 res = new Vec3();
res.x = v1.y * v2.z - v1.z * v2.y;
res.y = v1.z * v2.x - v1.x * v2.z;
res.z = v1.x * v2.y - v1.y * v2.x;
return res;
}
static float dot(Vec3 v1, Vec3 v2){
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
static Vec3 mul(Vec3 v, float d){
Vec3 res = new Vec3();
res.x = v.x * d;
res.y = v.y * d;
res.z = v.z * d;
return res;
}
void sub(Vec3 v1, Vec3 v2){
x = v1.x - v2.x;
y = v1.y - v2.y;
z = v1.z - v2.z;
}
}
static class Quat{
float w, x, y, z;
Quat(){
reset();
}
Quat(float w, float x, float y, float z){
this.w = w;
this.x = x;
this.y = y;
this.z = z;
}
void reset(){
w = 1.0f;
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
void set(float w, Vec3 v){
this.w = w;
x = v.x;
y = v.y;
z = v.z;
}
void set(Quat q){
w = q.w;
x = q.x;
y = q.y;
z = q.z;
}
static Quat mul(Quat q1, Quat q2){
Quat res = new Quat();
res.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
res.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
res.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
res.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
return res;
}
float[] getValue(){
// transforming this quat into an angle and an axis vector...
float[] res = new float[4];
float sa = (float) Math.sqrt(1.0f - w * w);
if (sa < EPSILON){
sa = 1.0f;
}
res[0] = (float) Math.acos(w) * 2.0f;
res[1] = x / sa;
res[2] = y / sa;
res[3] = z / sa;
return res;
}
}
PImage sangi,head,body,uparmL,lefthand,uparmR,righthand,legL,legR;
void setup(){
size(600,600);
sangi = loadImage("1.1.png");
head = loadImage("1.1_head.png");
body = loadImage("1.1_body.png");
uparmL = loadImage("1.1_uparm_L.png");
lefthand = loadImage("1.1_left_hand.png");
uparmR = loadImage("1.1_uparm_R.png");
righthand = loadImage("1.1_right_hands.png");
legL = loadImage("1.1_leg_L.png");
legR = loadImage("1.1_leg_R.png");
}
float[]angleX=new float[10];
float[]angleY=new float[10];
int ID = 0;
ArrayList<String> lines= new ArrayList <String>();
void keyPressed(){
if(key=='s'){
String now="";//要放現在全部關節的值
for(int i=0;i<10;i++){
now+=angleX[i]+" ";
now+=angleY[i]+" ";
}
lines.add(now);
String[] arr= new String[lines.size()];
lines.toArray(arr);
saveStrings("angles.txt",arr);
println("現在存檔:"+now);
}
if(key=='r'){
String [] file = loadStrings("angles.txt");
if(file != null){//如果有讀到檔案
for(int i=0;i<file.length;i++){//就檔案內容逐行
lines.add(file[i]);//加到lines資料結構裡
println("現在存檔:"+file[i]);
}
}
}
if(key=='p') playing = !playing;
if(key=='1') ID=1;//左臂
if(key=='2') ID=2;//左手
if(key=='3') ID=3;//右臂
if(key=='4') ID=4;//右手
if(key=='5') ID=5;//待用
if(key=='6') ID=6;//待用
if(key=='0') ID=0;//頭
}
boolean playing = false;
void mouseDragged(){
//找到頭的位置 299,130
float dx=mouseX-299,dy=mouseY-130;
angleX[0]=degrees(atan2(dy,dx))+90;
//要改成像IK的轉動
//angleX[ID]+= mouseX-pmouseX;
//angleY[ID]+= mouseY-pmouseY;
}
int R=0;
void myInterpolate(){
if(lines.size()>=2){
float alpha=(frameCount%30)/30.0;
if(alpha==0.0) R=(R+1)%lines.size();
int R2=(R+1)%lines.size();
float[]oldAngle =float(split(lines.get(R),' '));
float[]newAngle =float(split(lines.get(R2),' '));
for(int i=0;i<10;i++){
angleX[i] = oldAngle[i*2+0]*(1-alpha)+newAngle[i]*alpha;
angleY[i] = oldAngle[i*2+0]*(1-alpha)+newAngle[i]*alpha;
}
}
}
void draw(){
background(#FFFFF2);
if(playing) myInterpolate();
image(body,0,0);
pushMatrix();
translate(299,130);
rotate(radians(angleX[0]));
translate(-299,-129);
image(head,0,0);
popMatrix();
pushMatrix();
translate(257,275);
rotate(radians(angleX[5]));
translate(-257,-275);
image(legL,0,0);
popMatrix();
pushMatrix();
translate(338,275);
rotate(radians(angleX[6]));
translate(-338,-275);
image(legR,0,0);
popMatrix();
pushMatrix();
translate(237,155);
rotate(radians(angleX[1]));
translate(-237,-155);
image(uparmL,0,0);
pushMatrix();
translate(170,152);
rotate(radians(angleX[2]));
translate(-170,-152);
image(lefthand,0,0);
popMatrix();
popMatrix();
pushMatrix();
translate(357,164);
rotate(radians(angleX[3]));
translate(-357,-164);
image(uparmR,0,0);
pushMatrix();
translate(424,154);
rotate(radians(angleX[4]));
translate(-424,-154);
image(righthand,0,0);
popMatrix();
popMatrix();}
PImage sangi,head,body,uparmL,lefthand,uparmR,righthand,legL,legR;
void setup(){
size(600,600);
sangi = loadImage("1.1.png");
head = loadImage("1.1_head.png");
body = loadImage("1.1_body.png");
uparmL = loadImage("1.1_uparm_L.png");
lefthand = loadImage("1.1_left_hand.png");
uparmR = loadImage("1.1_uparm_R.png");
righthand = loadImage("1.1_right_hands.png");
legL = loadImage("1.1_leg_L.png");
legR = loadImage("1.1_leg_R.png");
}
float[]angleX=new float[10];
float[]angleY=new float[10];
int ID = 0;
ArrayList<String> lines= new ArrayList <String>();
void keyPressed(){
if(key=='s'){
String now="";//要放現在全部關節的值
for(int i=0;i<10;i++){
now+=angleX[i]+" ";
now+=angleY[i]+" ";
}
lines.add(now);
String[] arr= new String[lines.size()];
lines.toArray(arr);
saveStrings("angles.txt",arr);
println("現在存檔:"+now);
}
if(key=='r'){
String [] file = loadStrings("angles.txt");
if(file != null){//如果有讀到檔案
for(int i=0;i<file.length;i++){//就檔案內容逐行
lines.add(file[i]);//加到lines資料結構裡
println("現在存檔:"+file[i]);
}
}
}
if(key=='p') playing = !playing;
if(key=='1') ID=1;//左臂
if(key=='2') ID=2;//左手
if(key=='3') ID=3;//右臂
if(key=='4') ID=4;//右手
if(key=='5') ID=5;//待用
if(key=='6') ID=6;//待用
if(key=='0') ID=0;//頭
}
boolean playing = false;
float[]posX={299,237,170,357,424,257,338};
float[]posY={130,155,152,164,154,275,275};
float[]posAngle={90,180,180,0,0,-90,-90};
void mouseDragged(){
//找到頭的位置 299,130
//float dx=mouseX-299,dy=mouseY-130;
//angleX[0]=degrees(atan2(dy,dx))+90;
float dx=mouseX-posX[ID],dy=mouseY-posY[ID];
angleX[ID]=degrees(atan2(dy,dx))+posAngle[ID];
//要改成像IK的轉動
//angleX[ID]+= mouseX-pmouseX;
//angleY[ID]+= mouseY-pmouseY;
}
int R=0;
void myInterpolate(){
if(lines.size()>=2){
float alpha=(frameCount%30)/30.0;
if(alpha==0.0) R=(R+1)%lines.size();
int R2=(R+1)%lines.size();
float[]oldAngle =float(split(lines.get(R),' '));
float[]newAngle =float(split(lines.get(R2),' '));
for(int i=0;i<10;i++){
angleX[i] = oldAngle[i*2+0]*(1-alpha)+newAngle[i]*alpha;
angleY[i] = oldAngle[i*2+0]*(1-alpha)+newAngle[i]*alpha;
}
}
}
void draw(){
background(#FFFFF2);
if(playing) myInterpolate();
image(body,0,0);
pushMatrix();
translate(299,130);
rotate(radians(angleX[0]));
translate(-299,-129);
image(head,0,0);
popMatrix();
pushMatrix();
translate(257,275);
rotate(radians(angleX[5]));
translate(-257,-275);
image(legL,0,0);
popMatrix();
pushMatrix();
translate(338,275);
rotate(radians(angleX[6]));
translate(-338,-275);
image(legR,0,0);
popMatrix();
pushMatrix();
translate(237,155);
rotate(radians(angleX[1]));
translate(-237,-155);
image(uparmL,0,0);
pushMatrix();
translate(170,152);
rotate(radians(angleX[2]));
translate(-170,-152);
image(lefthand,0,0);
popMatrix();
popMatrix();
pushMatrix();
translate(357,164);
rotate(radians(angleX[3]));
translate(-357,-164);
image(uparmR,0,0);
pushMatrix();
translate(424,154);
rotate(radians(angleX[4]));
translate(-424,-154);
image(righthand,0,0);
popMatrix();
popMatrix();
}
沒有留言:
張貼留言