Exemplo n.º 1
0
 /**
  * Configure the light source used for shadow mapping
  *
  * @param lightNodeInstance The node containing the light instance
  * @param lightIndex The index of the light in light instances of the node
  */
 public void setLight(final Node lightNodeInstance, final int lightIndex) {
   // android.util.Log.d(TAG,"setLight("+lightNodeInstance.id+","+lightIndex+")");
   this.lightType = lightNodeInstance.lightInstances[lightIndex].type;
   switch (this.lightType) {
     case Light.DIRECTIONAL:
       this.lightModel[8] = this.lightModel[8] = this.lightModel[9] = this.lightModel[11] = 0f;
       this.lightModel[10] = -1f;
       MatrixUtils.multiplyMV(this.lightModel, 8, lightNodeInstance.model, 0, this.lightModel, 8);
       this.lightModel[0] = this.lightModel[1] = this.lightModel[2] = 0f;
       this.lightModel[3] = 1f;
       MatrixUtils.multiplyMV(this.lightModel, 0, lightNodeInstance.model, 0, this.lightModel, 0);
       final float length =
           Matrix.length(this.lightModel[0], this.lightModel[1], this.lightModel[2]);
       this.lightModel[8] *= length;
       this.lightModel[9] *= length;
       this.lightModel[10] *= length;
       break;
     case Light.POINT:
       this.lightModel[0] = this.lightModel[1] = this.lightModel[2] = 0f;
       this.lightModel[3] = 1f;
       MatrixUtils.multiplyMV(this.lightModel, 0, lightNodeInstance.model, 0, this.lightModel, 0);
       break;
     case Light.SPOT:
       this.lightModel[0] = this.lightModel[1] = this.lightModel[2] = 0f;
       this.lightModel[3] = 1f;
       MatrixUtils.multiplyMV(this.lightModel, 0, lightNodeInstance.model, 0, this.lightModel, 0);
       this.lightModel[8] = this.lightModel[8] = this.lightModel[9] = this.lightModel[11] = 0f;
       this.lightModel[10] = -1f;
       MatrixUtils.multiplyMV(this.lightModel, 8, lightNodeInstance.model, 0, this.lightModel, 8);
       this.lightModel[4] = this.lightModel[0] + this.lightModel[8];
       this.lightModel[5] = this.lightModel[1] + this.lightModel[9];
       this.lightModel[6] = this.lightModel[2] + this.lightModel[10];
       this.lightModel[7] = 1f;
       break;
   }
 }
Exemplo n.º 2
0
  /**
   * Define a viewing transformation in terms of an eye point, a center of view, and an
   * mLocalTranslation vector.
   *
   * @param eyex eye x coordinate
   * @param eyey eye y coordinate
   * @param eyez eye z coordinate
   * @param centerx view center x coordinate
   * @param centery view center y coordinate
   * @param centerz view center z coordinate
   * @param mLocalTranslationx mLocalTranslation vector x coordinate
   * @param mLocalTranslationy mLocalTranslation vector y coordinate
   * @param mLocalTranslationz mLocalTranslation vector z coordinate
   */
  public void lookAt(
      float eyex,
      float eyey,
      float eyez,
      float centerx,
      float centery,
      float centerz,
      float mLocalTranslationx,
      float mLocalTranslationy,
      float mLocalTranslationz) {

    float[] x = new float[3];
    float[] y = new float[3];
    float[] z = new float[3];
    float mag;

    // Make rotation matrix

    // Z vector
    z[0] = eyex - centerx;
    z[1] = eyey - centery;
    z[2] = eyez - centerz;

    mag = Matrix.length(z[0], z[1], z[2]);
    if (mag > 0) { // mpichler, 19950515
      mag = 1 / mag;
      z[0] *= mag;
      z[1] *= mag;
      z[2] *= mag;
    }

    // Y vector
    y[0] = mLocalTranslationx;
    y[1] = mLocalTranslationy;
    y[2] = mLocalTranslationz;

    // X vector = Y cross Z
    x[0] = y[1] * z[2] - y[2] * z[1];
    x[1] = -y[0] * z[2] + y[2] * z[0];
    x[2] = y[0] * z[1] - y[1] * z[0];

    // Recompute Y = Z cross X
    y[0] = z[1] * x[2] - z[2] * x[1];
    y[1] = -z[0] * x[2] + z[2] * x[0];
    y[2] = z[0] * x[1] - z[1] * x[0];

    // mpichler, 19950515

    // cross product gives area of parallelogram, which is < 1.0 for
    // non-perpendicular unit-length vectors; so normalize x, y here

    mag = Matrix.length(x[0], x[1], x[2]);
    if (mag > 0) {
      mag = 1 / mag;
      x[0] *= mag;
      x[1] *= mag;
      x[2] *= mag;
    }

    mag = Matrix.length(y[0], y[1], y[2]);
    if (mag > 0) {
      mag = 1 / mag;
      y[0] *= mag;
      y[1] *= mag;
      y[2] *= mag;
    }

    synchronized (mViewMatrix) {
      mViewMatrix[0] = x[0];
      mViewMatrix[4] = x[1];
      mViewMatrix[8] = x[2];
      mViewMatrix[12] = 0.0f;
      mViewMatrix[1] = y[0];
      mViewMatrix[5] = y[1];
      mViewMatrix[9] = y[2];
      mViewMatrix[13] = 0.0f;
      mViewMatrix[2] = z[0];
      mViewMatrix[6] = z[1];
      mViewMatrix[10] = z[2];
      mViewMatrix[14] = 0.0f;
      mViewMatrix[3] = 0.0f;
      mViewMatrix[7] = 0.0f;
      mViewMatrix[11] = 0.0f;
      mViewMatrix[15] = 1.0f;

      // Matrix.multiplyMM(model, 0, m, 0, model, 0);
      // Translate Eye to Origin
      mPosition[0] = eyex;
      mPosition[1] = eyey;
      mPosition[2] = eyez;
      Matrix.translateM(mViewMatrix, 0, -mPosition[0], -mPosition[1], -mPosition[2]);
    }
  }