Package org.mt4j.util.math

Examples of org.mt4j.util.math.Vector3D


   * Gets the window center.
   *
   * @return the window center
   */
  public Vector3D getWindowCenter(){
    return new Vector3D (getWindowWidth()/2, getWindowHeight()/2 , 0);
  }
View Full Code Here


             float camEyePosX, float camEyePosY, float camEyePosZ,
             float xAxisUp, float yAxisUp, float zAxisUp,
             PApplet processingApplet){
   
    this.pa       = processingApplet;
    this.camPos     = new Vector3D(cameraPosX, cameraPosY, cameraPosZ);
    this.viewCenterPos  = new Vector3D(camEyePosX, camEyePosY, camEyePosZ);
    this.xAxisUp     = xAxisUp;
    this.yAxisUp     = yAxisUp;
    this.zAxisUp     = zAxisUp;
   
    this.zoomMinDistance = 0;
View Full Code Here

   *
   * @param factor the factor
   */
  public void zoomFactor(float factor){
    factor = 1/factor;
    Vector3D dirToCamVect = camPos.getSubtracted(viewCenterPos);
    dirToCamVect.scaleLocal(factor);
    if (dirToCamVect.length() > zoomMinDistance){
      Vector3D toCam = viewCenterPos.getAdded(dirToCamVect);
      camPos.setXYZ(toCam.getX(), toCam.getY(), toCam.getZ());
     
      this.dirty = true;
    }
  }
View Full Code Here

   * @param amount the amount
   */
  public void zoomAmount(float amount){
    amount*=-1;
    //Get direction vector from eye to camera
    Vector3D dirToCamVect = camPos.getSubtracted(viewCenterPos);
    //get the length of that vector
    float mag = dirToCamVect.length();
    //normalize the vector
    dirToCamVect.normalizeLocal();
    //scale the normalized vector with the original amount + the zoom amount
    dirToCamVect.scaleLocal(mag + amount);
   
    if (dirToCamVect.length() > zoomMinDistance){
      //Get the Vector to the camera from origin
      Vector3D toCam = viewCenterPos.getAdded(dirToCamVect);
      //set the new camPos
      camPos.setXYZ(toCam.getX(), toCam.getY(), toCam.getZ());
     
      this.dirty = true;
    }
  }
View Full Code Here

 
  /**
   * Reset to default.
   */
  public void resetToDefault(){
    this.camPos = new Vector3D((float)(pa.width/2.0), (float)(pa.height/2.0), (float)(pa.height/2.0) / PApplet.tan((float)(PApplet.PI*60.0 / 360.0)));
    this.viewCenterPos  = new Vector3D((float)(pa.width/2.0), (float)(pa.height/2.0), 0) ;
    this.xAxisUp = 0;
    this.yAxisUp = 1;
    this.zAxisUp = 0;
   
    this.dirty = true;
View Full Code Here

 
  /* (non-Javadoc)
   * @see util.camera.Icamera#getPosition()
   */
  public Vector3D getPosition() {
    return new Vector3D(camPos);
  }
View Full Code Here

    }
   
    //Init light settings
    MTLight.enableLightningAndAmbient(mtApplication, 150, 150, 150, 255);
    //Create a light source //I think GL_LIGHT0 is used by processing!
    MTLight light = new MTLight(mtApplication, GL.GL_LIGHT3, new Vector3D(0,-300,0));
   
    //Set up a material to react to the light
    GLMaterial material = new GLMaterial(Tools3D.getGL(mtApplication));
    material.setAmbient(new float[]{ .5f, .5f, .5f, 1f });
    material.setDiffuse(new float[]{ .8f, .8f, .8f, 1f } );
    material.setEmission(new float[]{ .0f, .0f, .0f, 1f });
    material.setSpecular(new float[]{ 0.9f, 0.9f, 0.9f, 1f })// almost white: very reflective
    material.setShininess(110);// 0=no shine,  127=max shine
   
    //Group used to move to the screen center and to put the mesh group in
    MTComponent group1 = new MTComponent(mtApplication);
   
    //Create a group and set the light for the whole mesh group ->better for performance than setting light to more comps
    final MTComponent meshGroup = new MTComponent(mtApplication, "Mesh group");
    meshGroup.setLight(light);
   
    //Desired position for the meshes to appear at
    Vector3D destinationPosition = new Vector3D(mtApplication.width/2, mtApplication.height/2, 50);
    //Desired scale for the meshes
    float destinationScale = mtApplication.width*0.85f;

    //Load the meshes with the ModelImporterFactory (A file can contain more than 1 mesh)
    //Loads 3ds model
    MTTriangleMesh[] meshes = ModelImporterFactory.loadModel(mtApplication, modelsPath + "jazz_Obj" + MTApplication.separator + "honda_jazz.obj", 180, true, false );
   
    //Get the biggest mesh in the group to use as a reference for setting the position/scale
    final MTTriangleMesh biggestMesh = this.getBiggestMesh(meshes);
   
    Vector3D translationToScreenCenter = new Vector3D(destinationPosition);
    translationToScreenCenter.subtractLocal(biggestMesh.getCenterPointGlobal());
   
    Vector3D scalingPoint = new Vector3D(biggestMesh.getCenterPointGlobal());
    float biggestWidth = biggestMesh.getWidthXY(TransformSpace.GLOBAL)
    float scale = destinationScale/biggestWidth;
   
    //Move the group the the desired position
    group1.scaleGlobal(scale, scale, scale, scalingPoint);
    group1.translateGlobal(translationToScreenCenter);
    this.getCanvas().addChild(group1);
    group1.addChild(meshGroup);
   
    //Inverts the normals, if they are calculated pointing inside of the mesh instead of outside
    boolean invertNormals = true;
   
    for (int i = 0; i < meshes.length; i++) {
      MTTriangleMesh mesh = meshes[i];
      meshGroup.addChild(mesh);
      mesh.unregisterAllInputProcessors(); //Clear previously registered input processors
      mesh.setPickable(true);

      if (invertNormals){
        Vector3D[] normals = mesh.getGeometryInfo().getNormals();
        for (int j = 0; j < normals.length; j++) {
          Vector3D vector3d = normals[j];
          vector3d.scaleLocal(-1);
        }
        mesh.getGeometryInfo().setNormals(mesh.getGeometryInfo().getNormals(), mesh.isUseDirectGL(), mesh.isUseVBOs());
      }

      //If the mesh has more than 20 vertices, use a display list for faster rendering
View Full Code Here

    if (MT4jSettings.getInstance().isOpenGlMode()){
      this.gl = ((PGraphicsOpenGL)pa.g).gl;
    }
   
   
    camPos = new Vector3D();
    Z = new Vector3D();
    X = new Vector3D();
    Y = new Vector3D();
   
    planes = new Plane[]{
         new Plane(Vector3D.ZERO_VECTOR,Vector3D.ZERO_VECTOR)
        ,new Plane(Vector3D.ZERO_VECTOR,Vector3D.ZERO_VECTOR)
        ,new Plane(Vector3D.ZERO_VECTOR,Vector3D.ZERO_VECTOR)
View Full Code Here

    _tmpVec2.setValues(Z);
    Y.setValues(_tmpVec2.crossLocal(X));
   
//    /*
    // compute the center of the near and far planes - THE PLANES ARENT NEEDED FOR THE CALCULATIONS TO WORK!
    Vector3D nc,fc;
    nc = this.camPos.getSubtracted(Z.getScaled(nearD));
    fc = this.camPos.getSubtracted(Z.getScaled(farD));

    // compute the 8 corners of the frustum
    Vector3D yScaledNh = Y.getScaled(nh);
    Vector3D xScaledNw = X.getScaled(nw);
    Vector3D yScaledfh = Y.getScaled(fh);
    Vector3D xScaledfw = X.getScaled(fw);
    ntl = nc.getAdded(yScaledNh).subtractLocal(xScaledNw);
    ntr = nc.getAdded(yScaledNh).addLocal(xScaledNw);
    nbl = nc.getSubtracted(yScaledNh).subtractLocal(xScaledNw);
    nbr = nc.getSubtracted(yScaledNh).addLocal(xScaledNw);
    ftl = fc.getAdded(yScaledfh).subtractLocal(xScaledfw);
    fbr = fc.getSubtracted(yScaledfh).addLocal(xScaledfw);
    ftr = fc.getAdded(yScaledfh).addLocal(xScaledfw);
    fbl = fc.getSubtracted(yScaledfh).subtractLocal(xScaledfw);
//    */
   
    /*
    System.out.println("NEAR TOP LEFT: " + ntl + " RIGHT: " + ntr);
//    System.out.println("NEAR TOP RIGHT: " + ntr);
    System.out.println("NEAR BOTTOM LEFT: " + nbl + " RIGHT: " + nbr);
//    System.out.println("NEAR BOTTOM RIGHT: " + nbr);
    System.out.println();
    System.out.println("FAR TOP LEFT: " + ftl + " RIGHT: " + ftr);
//    System.out.println("FAR BOTTOM RIGHT: " + fbr);
//    System.out.println("FAR TOP RIGHT: " + ftr);
    System.out.println("FAR BOTTOM LEFT: " + fbl + " BOTTOM RIGHT: " + fbr);
    System.out.println();
    */
   
    // compute the six planes
    // the function set3Points asssumes that the points
    // are given in counter clockwise order
//    /*
    //This computes the 6 frustum planes
    planes[NEARP].reconstruct(nc ,Z.getScaled(-1));
    planes[FARP].reconstruct(fc, Z);
   
    Vector3D aux,normal;

    aux = (nc.getAdded(yScaledNh)).subtractLocal(camPos);
    normal = aux.crossLocal(X);
    planes[TOP].reconstruct(nc.getAdded(yScaledNh), normal);

    aux = (nc.getSubtracted(yScaledNh)).subtractLocal(camPos);
    normal = X.getCross(aux);
    planes[BOTTOM].reconstruct(nc.getSubtracted(yScaledNh), normal);
   
    aux = (nc.getSubtracted(xScaledNw)).subtractLocal(camPos);
    normal = aux.crossLocal(Y);
    planes[LEFT].reconstruct(nc.getSubtracted(xScaledNw), normal);

    aux = (nc.getAdded(xScaledNw)).subtractLocal(camPos);
    normal = Y.getCross(aux);
    planes[RIGHT].reconstruct(nc.getAdded(xScaledNw), normal);
View Full Code Here

   
    //Init light settings
    MTLight.enableLightningAndAmbient(pa, 150, 150, 150, 255);
    //Create a light source //I think GL_LIGHT0 is used by processing!
//    MTLight light = new MTLight(pa, GL.GL_LIGHT3, new Vector3D(0,0,0));
    MTLight light = new MTLight(pa, GL.GL_LIGHT3, new Vector3D(pa.width/5f,-pa.height/10f,0));
   
    //Set up a material to react to the light
    GLMaterial material = new GLMaterial(Tools3D.getGL(pa));
    material.setAmbient(new float[]{ .1f, .1f, .1f, 1f });
    material.setDiffuse(new float[]{ 1.0f, 1.0f, 1.0f, 1f } );
    material.setEmission(new float[]{ .0f, .0f, .0f, 1f });
    material.setSpecular(new float[]{ 1.0f, 1.0f, 1.0f, 1f })// almost white: very reflective
    material.setShininess(127);// 0=no shine,  127=max shine
   
    //Create the earth
    earth = new MTSphere(pa, "earth", 40, 40, 80, TextureMode.Projected); //TextureMode.Polar);
    earth.setLight(light);
    earth.setMaterial(material);
    earth.rotateX(earth.getCenterPointRelativeToParent(), -90);
    earth.setTexture(new GLTexture(pa,imagesPath + "worldMap.jpg", new GLTextureSettings(TEXTURE_TARGET.TEXTURE_2D, SHRINKAGE_FILTER.Trilinear, EXPANSION_FILTER.Bilinear, WRAP_MODE.CLAMP_TO_EDGE, WRAP_MODE.CLAMP_TO_EDGE)));
        earth.generateAndUseDisplayLists();
        earth.setPositionGlobal(new Vector3D(pa.width/2f, pa.height/2f, 250)); //earth.setPositionGlobal(new Vector3D(200, 200, 250));
        //Animate earth rotation
        new Animation("rotation animation", new MultiPurposeInterpolator(0,360, 17000, 0, 1, -1) , earth).addAnimationListener(new IAnimationListener(){
          public void processAnimationEvent(AnimationEvent ae) {
            earth.rotateY(earth.getCenterPointLocal(), ae.getCurrentStepDelta(), TransformSpace.LOCAL);
          }}).start();
       
        //Put planets in a group that can be manipulated by gestures
        //so the rotation of the planets doesent get changed by the gestures
    MTComponent group = new MTComponent(mtApplication);
    group.setComposite(true); //This makes the group "consume" all picking and gestures of the children
    group.registerInputProcessor(new DragProcessor(mtApplication));
    group.addGestureListener(DragProcessor.class, new DefaultDragAction());
    group.addGestureListener(DragProcessor.class, new InertiaDragAction(80, 0.8f, 10));
    group.registerInputProcessor(new RotateProcessor(mtApplication));
    group.addGestureListener(RotateProcessor.class, new DefaultRotateAction());
    //Scale the earth from the center. Else it might get distorted
    group.registerInputProcessor(new ScaleProcessor(mtApplication));
    group.addGestureListener(ScaleProcessor.class, new IGestureEventListener() {
      public boolean processGestureEvent(MTGestureEvent ge) {
        ScaleEvent se = (ScaleEvent)ge;
        earth.scaleGlobal(se.getScaleFactorX(), se.getScaleFactorY(), se.getScaleFactorX(), earth.getCenterPointGlobal());
        return false;
      }
    });
    this.getCanvas().addChild(group);
        group.addChild(earth);
       
        //Create the moon
        final MTSphere moonSphere = new MTSphere(pa, "moon", 35, 35, 25, TextureMode.Polar);
         moonSphere.setMaterial(material);
        moonSphere.translate(new Vector3D(earth.getRadius() + moonSphere.getRadius() + 50, 0,0));
        moonSphere.setTexture(new GLTexture(pa, imagesPath + "moonmap1k.jpg", new GLTextureSettings(TEXTURE_TARGET.RECTANGULAR, SHRINKAGE_FILTER.Trilinear, EXPANSION_FILTER.Bilinear, WRAP_MODE.CLAMP_TO_EDGE, WRAP_MODE.CLAMP_TO_EDGE)));
        moonSphere.generateAndUseDisplayLists();
        moonSphere.unregisterAllInputProcessors();
        //Rotate the moon around the earth
        new Animation("moon animation", new MultiPurposeInterpolator(0,360, 12000, 0, 1, -1) , moonSphere).addAnimationListener(new IAnimationListener(){
View Full Code Here

TOP

Related Classes of org.mt4j.util.math.Vector3D

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.