zoom and translate 3d models

edited May 2015 in How To...

greeting , i wanna make zoom upon the 3d model when i click upon the flash button(UP, DOWN) and translate when i click upon the flash button(RIGHT, LEFT) thank you :)

Tagged:

Answers

  • int zoom = 20;
    int offset = 0;
    
    void setup(){
      size(440,440,P3D);
      fill(0,255,0);
      noStroke();
    }
    
    void draw(){
      background(0);
      translate(width/2 + offset,height/2,0);
      scale(zoom);
      lights();
      rotateX(map(millis()%7000,0,7000,0,TWO_PI));
      rotateY(map(millis()%19000,0,19000,0,TWO_PI));
      box(1);
    }
    
    void keyPressed(){
      if(keyCode==UP) zoom++;
      if(keyCode==DOWN) zoom--;
      if(keyCode==LEFT) offset--;
      if(keyCode==RIGHT) offset++;
    }
    
  • edited May 2015

    can i her make a limite upon the zoom because i have problem there ? :/

  • edited May 2015

    Go for it. The amount to scale is controlled by the zoom variable. If you limit the range of values that variable can take, you also limit the amount of scaling that happens. You can limit it by checking to see if it more or less than certain values, and then setting it to a valid value if it is.

    This is often not taught in programming classes, but if you want your program to do something different, you have to change the code so that the code is different.

    Post your attempt at it for more help.

  • the code

    // import codeanticode.glgraphics.*;
    // import codeanticode.gltexture.*;
    import saito.objloader.*;
    import codeanticode.gsvideo.*;
    import java.util.Calendar;     
    
    import ddf.minim.*;
    Minim minim;
    AudioPlayer song;
    
    OBJModel model;
    GSCapture cam;
    float thetaX = 0.0;
    float thetaY = 0.0;   
    String timestamp() {
      Calendar now = Calendar.getInstance();
      return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
    }
    
    int offset = 5;
    float scaler = 1;
    void setup() { 
      size(640, 480, P3D);
      smooth();
      noStroke();
      model = new OBJModel(this, "");
      cam = new GSCapture(this, 640, 480); // initialize the webcam capture at a specific resolution (correct and/or possible settings depend on YOUR webcam)
      cam.start(); // start capturing
    }
    void caricasuono() {
      minim = new Minim(this);
    
      song = minim.loadFile("THE SOUND OF SILENCE.mp3");
    }
    
    void keyPressed() {
      if (keyPressed) {
    
        if (key == 'w' ) { 
          model = new OBJModel(this, "tablebois.obj"); 
          model.scale(65);
          model.translateToCenter();
          noStroke();
        }
        if (key == 'd' ) { 
          model = new OBJModel(this, "1.obj"); 
          model.scale(40);
          model.translateToCenter();
          noStroke();
        }
      }
    
      if (key == 'q' ) { 
        caricasuono();
        song.play();
        model = new OBJModel(this, "bedbois.obj"); 
        model.scale(60);
        model.translateToCenter(); 
        noStroke();
      } 
    
      if (keyCode==LEFT) offset--;
      if (keyCode==RIGHT) offset++;
      if ( keyCode==UP) {
        scaler -= 0.1; } // smaller
      if (keyCode==DOWN) {
        scaler += 0.1; } // bigger
      if (keyCode == ENTER) {    
        saveFrame("dot_images/"+timestamp()+".png");
      }
    }   
    
    void draw() {
      if (cam.available()) {
        cam.read(); // read the cam image
        lights();
        background(cam); 
        translate(width/2 + offset, height/2);
        translate(0, 0, height/2); // use translate around scale
        scale(scaler);
        translate(0, 0, -height/2); // to scale from the center   
    
        //Perpective 
        if (mousePressed) {
          float fov = PI/3.0; 
          float cameraZ = (height/1.0) / tan(fov/1.0); 
          perspective(fov, float(width)/float(height), cameraZ/5.0, cameraZ*5.0);
        } else {
          ortho(0, width, 0, height);
        }
        rotateX(thetaX);
        rotateY(thetaY);    
        // directionalLight(51, 102, 126, 0, -1, 0);
        // ambientLight(102, 102, 102);
        pointLight(200, 200, 200, width/2, height/2, -200);
        model.draw();
        if (mousePressed) {
          thetaY -= radians(pmouseX-mouseX);
          thetaX += radians(pmouseY-mouseY);
        }
      }
    }
    
  • Uh. Huh. I'm confused why you reload each model every time a key is pressed. Couldn't you just have an array of models, and pick which one you want to draw by using an index into that array?

    Also, the code you've posted doesn't help illustrate the problems you're having. Can you cut it down to an small section of code that shows the trouble you're having without using things like object models and webcam libraries? Hopefully something I can copy, paste, and run.

  • i like when you say "array of models" and "using an index into that array" can you help me there please it's very important for me :)

  • Model[] models = new Model[3];
    int which_model;
    
    void setup(){
      size(440,440);
      models[0] = new Model(255,0,0);
      models[1] = new Model(0,255,0);
      models[2] = new Model(0,0,255);
      which_model = 2;
    }
    
    void draw(){
      background(0);
      models[which_model].draw();
    }
    
    void mousePressed(){
      which_model++;
      which_model%=models.length;
    }
    
    class Model{
      color c;
      Model(int R, int G, int B){
        c = color(R,G,B);
      }
      void draw(){
        fill(c);
        rect(20,20,20,20);
      }
    }
    
  • **thank you ==>TfGuy44 i did it ** and this is the code code where i need to make a limite for the zoom , i meen when i zoom back.. the zoom stoped when the value of scaler=20, for exemle i don't need to do the zoom back until i can't see the model and the same think for the forward zoom.. and thank you another time :)

    the code

    int offset = 5;
    float scaler = 20;
    void setup(){
      size(440,440,P3D);
      
      fill(0,255,0);
      noStroke();
    }
     
    void draw(){
      background(0);
      
      translate(width/2 + offset,height/2,0);
      scale(scaler);
      lights();
      
      box(1);
    }
     
  • Example for limiting variables' values:

    int x, y;
    
    void setup() {
      size(440, 440);
      x = 20;
      y = 20;
      fill(0, 255, 0);
    }
    
    void draw() {
      background(0);
      rect(x, y, 20, 20);
    }
    
    void keyPressed() {
      if (keyCode==UP) y--;
      if (keyCode==DOWN) y++;
      if (keyCode==LEFT) x--;
      if (keyCode==RIGHT) x++;
      if(x<20) x=20;
      if(y<20) y=20;
      if(x>40) x=40;
      if(y>40) y=40;
    }
    
  • @TFGuy44 ==> thank you brother i did it :) but another think very important if you can ,can you give me some help i wanna translate my code to android please ?

  • Nope. I don't help feature creepers. Plus I have no experience making things work on Android. Do/try it yourself!

Sign In or Register to comment.