import saito.objloader.*; OBJModel model; Camera cam; float theta,phi,rayon,angle; Quat lequat,lequat0; Vecteur axe; boolean brelache; //++++++++++++++++++++++++++++++++++++++++++++++++++ // //++++++++++++++++++++++++++++++++++++++++++++++++++ void setup(){ model = new OBJModel(this); model.debugMode(); model.load("Teddy.obj"); size(700,500,P3D); brelache=true; rayon=400; theta=1.0f; phi=1.0f; lequat=new Quat(1,0,0,0); lequat0=new Quat(1,0,0,0); cam=new Camera(); angle=0; axe=new Vecteur(1,0,0); } //++++++++++++++++++++++++++++++++++++++++++++++++++ // //++++++++++++++++++++++++++++++++++++++++++++++++++ void draw(){ background(140,120,200); pushMatrix(); pointLight(70, 52, 100, 0, -500, -1000); directionalLight(255, 255, 255, 0,10,5); //spotLight(v1, v2, v3, x, y, z, nx, ny, nz, angle, concentration) spotLight(155, 102, 255, 1000, -5000, -2000, 2, -2, 23, 0.8, 0.2); //ambientLight(100,100, 150); cam.placercamera(); dessinerDecor(); dessinerSphere(); pointeur(); popMatrix(); } //++++++++++++++++++++++++++++++++++++++++++++++++++ // //++++++++++++++++++++++++++++++++++++++++++++++++++ void mouseReleased() { brelache=true; } void mouseDragged(){ if(brelache==true){ brelache=false; cam.intersectionRayonSphere(); } else{ if(mouseButton==RIGHT){//Déplacer la caméra en calculant son nouveau repere theta=(mouseY-pmouseY)/120.0f; phi=(mouseX-pmouseX)/120.0f; lequat=Quat.mul(new Quat(cos(theta/2),sin(theta/2),0,0),lequat); lequat=Quat.mul(new Quat(cos(phi/2),0,0,sin(phi/2)),lequat); cam.tournerCam(lequat);//lequat est le quaternion de rotation de la caméra! } if(mouseButton==LEFT){//déplacer le sélecteuren calculant le vecteur vectImpact Vecteur v=cam.vectImpact.normerCopie(1); cam.intersectionRayonSphere(); Vecteur w=cam.vectImpact.normerCopie(1); angle = acos(w.dot(v)); axe=(v.cross(w)).normerCopie(1); lequat0=Quat.mul(new Quat(axe,angle,1),lequat0);//lequat0 est le quaternion de rotation du trackball } } } //++++++++++++++++++++++++++++++++++++++++++++++++++ // //++++++++++++++++++++++++++++++++++++++++++++++++++ public void pointeur(){ //dessiner le pointeur au point d'impact Vecteur v=cam.vectImpact.normerCopie(1); float a=acos(v.x); Vecteur w=new Vecteur(0,-v.z,v.y); w.normalize(); pushMatrix(); rotate(a,w.x,w.y,w.z); fill(255,100,100); noStroke(); beginShape(QUAD_STRIP); for(int i=0;i<=12;i++){ float a1=PI/6*i; vertex(0,10*cos(a1),10*sin(a1)); vertex(rayon-15,10*cos(a1),10*sin(a1)); } endShape(); beginShape(TRIANGLE_FAN); vertex(rayon,0,0); for(int i=0;i<=12;i++){ float a1=PI/6*i; vertex(rayon-20,20*cos(a1),20*sin(a1)); } endShape(); popMatrix(); } //++++++++++++++++++++++++++++++++++++++++++++++++++ // //++++++++++++++++++++++++++++++++++++++++++++++++++ void dessinerSphere(){ float[] a=lequat0.getValue(); noStroke(); pushMatrix(); rotate(a[0],a[1],a[2],a[3]); pushMatrix(); // translate(0,-200,-1900); scale(17); rotateX(PI); model.draw(); popMatrix(); for(int li=0;li<4;li++){ rotateX(PI/4); for(int j=0;j<40;j++){ pushMatrix(); rotateZ(j*PI/20); translate(rayon,0,0); rotateZ(PI/2); dessinerCylindre(4,rayon*sin(PI/20)/2+3); popMatrix(); } } popMatrix(); } //++++++++++++++++++++++++++++++++++++++++++++++++++ // //++++++++++++++++++++++++++++++++++++++++++++++++++ void dessinerCylindre(float d2,float h2){ float ds=d2*sin(PI/3); float dc=d2*cos(PI/3); noStroke(); fill(255); beginShape(QUADS); vertex(h2,d2,0);//,0,0 vertex(h2,dc,ds);//,0,10 vertex(-h2,dc,ds);//,400,10 vertex(-h2,d2,0);//,400,0 endShape(); beginShape(QUADS); vertex(h2,dc,ds);//,0,0 vertex(h2,-dc,ds);//,0,10 vertex(-h2,-dc,ds);// ,400,10 vertex(-h2,dc,ds);//,400,0 endShape(); beginShape(QUADS); vertex(h2,-dc,ds);//,0,0 vertex(h2,-d2,0); vertex(-h2,-d2,0); vertex(-h2,-dc,ds); endShape(); beginShape(QUADS); vertex(h2,-d2,0); vertex(-h2,-d2,0); vertex(-h2,-dc,-ds); vertex(h2,-dc,-ds); endShape(); beginShape(QUADS); vertex(h2,-dc,-ds); vertex(h2,dc,-ds); vertex(-h2,dc,-ds); vertex(-h2,-dc,-ds); endShape(); beginShape(QUADS); vertex(h2,d2,0); vertex(h2,dc,-ds); vertex(-h2,dc,-ds); vertex(-h2,d2,0); endShape(); } void dessinerDecor(){ noStroke(); beginShape(QUADS); fill(105,55,155); vertex(-5555,-2222,7000); vertex(5555,-2222,7000); fill(105,155,155); vertex(5555,2222,7000); vertex(-5555,2222,7000); fill(105,55,155); vertex(5555,-2222,7000); vertex(5555,2222,7000); fill(105,155,155); vertex(9666,2222,1000); vertex(9666,-2222,1000); fill(105,55,155); vertex(-5555,-2222,7000); vertex(-5555,2222,7000); fill(105,155,155); vertex(-9666,2222,1000); vertex(-9666,-2222,1000); endShape(); }