コード例 #1
0
ファイル: Shape.java プロジェクト: zmjdx/Bachelor-Thesis
  /**
   * Intersect the given Ray with this shape. Tests for every triangle of the shape. Speed depends
   * hence on the complexity of the shape.
   *
   * @param Ray ray The Ray to be intersected
   * @param Matrix4f transformation The transformation of the Shape into its actual position
   * @return a RayShapeIntersection with the coordinates of the HitPoint if any.
   */
  public RayShapeIntersection intersect(Ray ray, Matrix4f transformation) {
    RayShapeIntersection intersection = new RayShapeIntersection();
    List<RayShapeIntersection> hitTriangles = new ArrayList<RayShapeIntersection>();

    // get all triangles
    Iterator<Triangle> it = getTriangles().iterator();
    Triangle triangle;
    // calculate intersection of ray and triangle
    while (it.hasNext()) {
      triangle = it.next();
      triangle.transform(transformation);
      intersection = calculateIntersection(ray, triangle);
      if (intersection.hit) {
        hitTriangles.add(intersection);
      }
    }

    Vector3f distance = new Vector3f();
    float shortestDist = Float.MAX_VALUE;
    for (RayShapeIntersection in : hitTriangles) {
      distance.sub(ray.getOrigin(), in.hitPoint);
      if (distance.length() < shortestDist) {
        intersection = in;
        shortestDist = distance.length();
      }
    }

    // return the intersected point if any
    return intersection;
  }
コード例 #2
0
ファイル: Shape.java プロジェクト: zmjdx/Bachelor-Thesis
  private List<Triangle> getTriangles() {
    List<Triangle> triangles = new ArrayList<Triangle>();

    IntBuffer verticesBuffer = mVertexBuffers.getVertexBuffer();
    int[] verticesInt = new int[verticesBuffer.capacity()];

    for (int i = 0; i < verticesBuffer.capacity(); i++) {
      verticesInt[i] = verticesBuffer.get(i);
    }

    // Fixed Point Conversion
    float[] vertices = new float[verticesInt.length];
    for (int i = 0; i < verticesInt.length; i++) {
      vertices[i] = (float) verticesInt[i] / 65536;
    }

    ShortBuffer indicesBuffer = mVertexBuffers.getIndexBuffer();
    short[] indices = new short[indicesBuffer.capacity()];
    for (int i = 0; i < indicesBuffer.capacity(); i++) {
      indices[i] = indicesBuffer.get(i);
    }

    Triangle triangle;
    Point3f vec;
    for (int i = 0; i < indices.length; i++) {
      triangle = new Triangle();
      vec = new Point3f();
      vec.x = vertices[indices[i] * 3];
      vec.y = vertices[indices[i] * 3 + 1];
      vec.z = vertices[indices[i] * 3 + 2];
      triangle.mX = vec;
      i++;
      vec = new Point3f();
      vec.x = vertices[indices[i] * 3];
      vec.y = vertices[indices[i] * 3 + 1];
      vec.z = vertices[indices[i] * 3 + 2];
      triangle.mY = vec;
      i++;
      vec = new Point3f();
      vec.x = vertices[indices[i] * 3];
      vec.y = vertices[indices[i] * 3 + 1];
      vec.z = vertices[indices[i] * 3 + 2];
      triangle.mZ = vec;

      triangles.add(triangle);
    }

    return triangles;
  }