Beispiel #1
0
 /** Clear the framebuffer here. */
 private void beginFrame() {
   zBuffer = new float[width][height];
   mergedDisplayMatrix = new Matrix4f(viewPortMatrix);
   mergedDisplayMatrix.mul(sceneManager.getFrustum().getProjectionMatrix());
   mergedDisplayMatrix.mul(sceneManager.getCamera().getCameraMatrix());
   colorBuffer.getGraphics().clearRect(0, 0, width, height);
 }
Beispiel #2
0
  /**
   * Creates the matrices that are used for the projection from the 3D-space onto a 2D-screen.
   * (Result=ViewMatrix*ProjectionMatrix*ViewportMatrix)
   *
   * @return the complete matrix
   */
  private Matrix4f createMatrices() {

    Matrix4f staticMatrix = new Matrix4f(mRenderer.getViewportMatrix());
    Matrix4f projMatrix = mRenderer.getSceneManager().getFrustum().getProjectionMatrix();
    staticMatrix.mul(projMatrix);
    Matrix4f cameraMatrix = mRenderer.getSceneManager().getCamera().getCameraMatrix();
    staticMatrix.mul(cameraMatrix);

    return staticMatrix;
  }
Beispiel #3
0
 private void updateLocalTransform() {
   localTransform = parent.transform();
   Matrix4f ms = new Matrix4f();
   ms.setIdentity();
   Vector3f ps = parent.scale();
   ms.m00 = ps.x;
   ms.m11 = ps.y;
   ms.m22 = ps.z;
   localTransform.mul(ms);
   localTransform.invert();
   localTransform.mul(transform());
 }
Beispiel #4
0
  public void updateChildTransforms() {
    Matrix4f pt = transform();
    Matrix4f ct = new Matrix4f();
    Matrix4f ms = new Matrix4f();
    ms.setIdentity();
    Vector3f ps = scale();
    ms.m00 = ps.x;
    ms.m11 = ps.y;
    ms.m22 = ps.z;
    pt.mul(ms);

    for (GameObject c : children) {
      ct.mul(pt, c.localTransform);
      c.transform(ct, false);
    }
  }
  private void setTransformation(Matrix4f transformation) {
    // Compute the modelview matrix by multiplying the camera matrix and
    // the transformation matrix of the object
    Matrix4f modelview = new Matrix4f(sceneManager.getCamera().getCameraMatrix());
    modelview.mul(transformation);

    // Set modelview and projection matrices in shader
    gl.glUniformMatrix4fv(
        gl.glGetUniformLocation(activeShaderID, "modelview"),
        1,
        false,
        transformationToFloat16(modelview),
        0);
    gl.glUniformMatrix4fv(
        gl.glGetUniformLocation(activeShaderID, "projection"),
        1,
        false,
        transformationToFloat16(sceneManager.getFrustum().getProjectionMatrix()),
        0);

    int id = gl.glGetUniformLocation(activeShaderID, "camera");
    if (id != -1) {
      Vector3f cop = sceneManager.getCamera().getCenterOfProjection();
      gl.glUniform4f(id, cop.x, cop.y, cop.z, 0);
    }
    // } else
    // System.out.println("Could not get location of uniform variable camera");
  }
 /**
  * Consolidated the array of transform elements into the argument matrix
  *
  * @param te The array of TransformElements
  */
 static void getMatrix(TransformElement[] te, Matrix4f matrix) {
   Matrix4f m = new Matrix4f();
   matrix.setIdentity();
   for (int i = 0; i < te.length; i++) {
     te[i].getMatrix(m);
     matrix.mul(m);
   }
 }
  @Override
  public void preRender(VertexType vt, Matrix4f worldMatrix, RenderManager rm) {
    auxMatrix.setIdentity();
    rm.getViewProjectionMatrix(auxMatrix);
    auxMatrix.mul(worldMatrix);
    RenderManager.matrixToBuffer(auxMatrix, fb);
    glUniformMatrix4(worldViewProj, false, fb);

    preRenderShortcut.preRender(vt, worldMatrix, rm);
  }
Beispiel #8
0
  public void scale(float x, float y, float z, boolean updateLocal) {
    activate();
    // Set unit scale
    Matrix4 t = modelInstance.transform;
    Matrix4 mat_scale = new Matrix4();
    Vector3 s = new Vector3();
    t.getScale(s);
    mat_scale.scl(1 / s.x, 1 / s.y, 1 / s.z);
    t.mul(mat_scale);

    // Set target scale
    mat_scale.idt();
    mat_scale.scl(x, y, z);
    t.mul(mat_scale);

    // Relevant bullet body update
    CollisionShape cs = body.getCollisionShape();
    cs.setLocalScaling(new Vector3f(x, y, z));
    if (body.isInWorld() && body.isStaticOrKinematicObject()) scene.world.updateSingleAabb(body);

    // Child propagation
    Vector3f ps = scale();
    Matrix4f pt = transform();
    Matrix4f ct = new Matrix4f();
    Matrix4f ms = new Matrix4f();
    ms.setIdentity();
    ms.m00 = ps.x;
    ms.m11 = ps.y;
    ms.m22 = ps.z;
    pt.mul(ms);

    for (GameObject c : children) {
      c.scale(scale().mul(c.localScale), false);
      ct.mul(pt, c.localTransform);
      c.transform(ct, false);
    }

    if (parent != null && updateLocal) {
      updateLocalScale();
    }
  }
Beispiel #9
0
    public Vertex bake(Mesh mesh, Function<Node<?>, Matrix4f> animator) {
      // geometry
      Float totalWeight = 0f;
      Matrix4f t = new Matrix4f();
      if (mesh.getWeightMap().get(this).isEmpty()) {
        t.setIdentity();
      } else {
        for (Pair<Float, Node<Bone>> bone : mesh.getWeightMap().get(this)) {
          totalWeight += bone.getLeft();
          Matrix4f bm = animator.apply(bone.getRight());
          bm.mul(bone.getLeft());
          t.add(bm);
        }
        if (Math.abs(totalWeight) > 1e-4) t.mul(1f / totalWeight);
        else t.setIdentity();
      }

      // pos
      Vector4f pos = new Vector4f(this.pos), newPos = new Vector4f();
      pos.w = 1;
      t.transform(pos, newPos);
      Vector3f rPos = new Vector3f(newPos.x / newPos.w, newPos.y / newPos.w, newPos.z / newPos.w);

      // normal
      Vector3f rNormal = null;

      if (this.normal != null) {
        Matrix3f tm = new Matrix3f();
        t.getRotationScale(tm);
        tm.invert();
        tm.transpose();
        Vector3f normal = new Vector3f(this.normal);
        rNormal = new Vector3f();
        tm.transform(normal, rNormal);
        rNormal.normalize();
      }

      // texCoords TODO
      return new Vertex(rPos, rNormal, color, texCoords);
    }
Beispiel #10
0
    public void run() {
      adjustToScreenSize =
          (float)
              Math.min(
                  jframe.getWidth(),
                  jframe.getHeight()); // used here, since you can change the screen-Size

      Matrix4f newTranslation = new Matrix4f();
      newTranslation.setIdentity();

      Matrix4f oldcTranslation = new Matrix4f();
      oldcTranslation = camera.getCameraMatrix();

      // world z-Axis-turn
      if (mouseWorldTurn != null) {
        newTranslation.mul(mouseWorldTurn);
        mouseWorldTurn.setIdentity();
      }

      // camera x-Axis-turn
      if (mouseTurn != null) {
        newTranslation.mul(mouseTurn);
        mouseTurn.setIdentity();
      }

      // camera movement
      if (keyMove != null) {
        newTranslation.mul(keyMove);
        keyMove.setIdentity();
      }

      newTranslation.mul(oldcTranslation);

      camera.setCameraMatrix(newTranslation);
      // something still appears to be wrong while turning

      // Trigger redrawing of the render window
      renderPanel.getCanvas().repaint();
    }
  private void setTransformation(Matrix4f transformation) {
    // Compute the modelview matrix by multiplying the camera matrix and
    // the transformation matrix of the object
    Matrix4f modelview = new Matrix4f(sceneManager.getCamera().getCameraMatrix());
    modelview.mul(transformation);

    // Set modelview and projection matrices in shader
    gl.glUniformMatrix4fv(
        gl.glGetUniformLocation(activeShaderID, "modelview"),
        1,
        false,
        transformationToFloat16(modelview),
        0);
    gl.glUniformMatrix4fv(
        gl.glGetUniformLocation(activeShaderID, "projection"),
        1,
        false,
        transformationToFloat16(sceneManager.getFrustum().getProjectionMatrix()),
        0);
  }
Beispiel #12
0
 /** The main rendering method. You will need to implement this to draw 3D objects. */
 private void draw(RenderItem renderItem) {
   Matrix4f t = new Matrix4f(mergedDisplayMatrix);
   t.mul(renderItem.getT());
   drawTrianglesSeparately(renderItem.getShape(), t);
   // drawDotty(renderItem.getShape(), t);
 }
  /** Calculate transforms needed to handle VRML semantics formula: T x C x R x SR x S x -SR x -C */
  private void updateTransform() {

    // System.out.println(this);
    tempVec.x = -vfCenter[0];
    tempVec.y = -vfCenter[1];
    tempVec.z = -vfCenter[2];

    matrix.setIdentity();
    matrix.setTranslation(tempVec);

    float scaleVal = 1.0f;

    if (floatEq(vfScale[0], vfScale[1]) && floatEq(vfScale[0], vfScale[2])) {

      scaleVal = vfScale[0];
      tempMtx1.set(scaleVal);
      // System.out.println("S" + tempMtx1);

    } else {
      // non-uniform scale
      // System.out.println("Non Uniform Scale");
      tempAxis.x = vfScaleOrientation[0];
      tempAxis.y = vfScaleOrientation[1];
      tempAxis.z = vfScaleOrientation[2];
      tempAxis.angle = -vfScaleOrientation[3];

      double tempAxisNormalizer =
          1
              / Math.sqrt(
                  tempAxis.x * tempAxis.x + tempAxis.y * tempAxis.y + tempAxis.z * tempAxis.z);

      tempAxis.x *= tempAxisNormalizer;
      tempAxis.y *= tempAxisNormalizer;
      tempAxis.z *= tempAxisNormalizer;

      tempMtx1.set(tempAxis);
      tempMtx2.mul(tempMtx1, matrix);

      // Set the scale by individually setting each element
      tempMtx1.setIdentity();
      tempMtx1.m00 = vfScale[0];
      tempMtx1.m11 = vfScale[1];
      tempMtx1.m22 = vfScale[2];

      matrix.mul(tempMtx1, tempMtx2);

      tempAxis.x = vfScaleOrientation[0];
      tempAxis.y = vfScaleOrientation[1];
      tempAxis.z = vfScaleOrientation[2];
      tempAxis.angle = vfScaleOrientation[3];
      tempMtx1.set(tempAxis);
    }

    tempMtx2.mul(tempMtx1, matrix);

    // System.out.println("Sx-C" + tempMtx2);
    float magSq =
        vfRotation[0] * vfRotation[0]
            + vfRotation[1] * vfRotation[1]
            + vfRotation[2] * vfRotation[2];

    if (magSq < ZEROEPS) {
      tempAxis.x = 0;
      tempAxis.y = 0;
      tempAxis.z = 1;
      tempAxis.angle = 0;
    } else {
      if ((magSq > 1.01) || (magSq < 0.99)) {

        float mag = (float) (1 / Math.sqrt(magSq));
        tempAxis.x = vfRotation[0] * mag;
        tempAxis.y = vfRotation[1] * mag;
        tempAxis.z = vfRotation[2] * mag;
      } else {
        tempAxis.x = vfRotation[0];
        tempAxis.y = vfRotation[1];
        tempAxis.z = vfRotation[2];
      }

      tempAxis.angle = vfRotation[3];
    }

    tempMtx1.set(tempAxis);
    // System.out.println("R" + tempMtx1);

    matrix.mul(tempMtx1, tempMtx2);
    // System.out.println("RxSx-C" + matrix);

    tempVec.x = vfCenter[0];
    tempVec.y = vfCenter[1];
    tempVec.z = vfCenter[2];

    tempMtx1.setIdentity();
    tempMtx1.setTranslation(tempVec);
    // System.out.println("C" + tempMtx1);

    tempMtx2.mul(tempMtx1, matrix);
    // System.out.println("CxRxSx-C" + tempMtx2);

    tempVec.x = vfTranslation[0];
    tempVec.y = vfTranslation[1];
    tempVec.z = vfTranslation[2];

    tempMtx1.setIdentity();
    tempMtx1.setTranslation(tempVec);

    matrix.mul(tempMtx1, tempMtx2);

    transform.set(matrix);
    implTG.setTransform(transform);
  }
Beispiel #14
0
 public void act() {
   rotate.mul(rotatePerStep);
   getTransformation().mul(rotatePerStep);
 }