Пример #1
0
  /**
   * This is a <b>VERY </b> brute force method of detecting if two TriMesh objects intersect.
   *
   * @param mesh1 The first TriMesh.
   * @param mesh2 The second TriMesh.
   * @return True if they intersect, false otherwise.
   */
  public static boolean meshIntersection(TriMesh mesh1, TriMesh mesh2) {

    IntBuffer indexA = mesh1.getIndexBuffer();
    IntBuffer indexB = mesh2.getIndexBuffer();
    TransformMatrix aTransform = new TransformMatrix();
    aTransform.setRotationQuaternion(mesh1.getWorldRotation());
    aTransform.setTranslation(mesh1.getWorldTranslation());
    aTransform.setScale(mesh1.getWorldScale());

    TransformMatrix bTransform = new TransformMatrix();
    bTransform.setRotationQuaternion(mesh2.getWorldRotation());
    bTransform.setTranslation(mesh2.getWorldTranslation());
    bTransform.setScale(mesh2.getWorldScale());

    Vector3f[] vertA = BufferUtils.getVector3Array(mesh1.getVertexBuffer());
    for (int i = 0; i < vertA.length; i++) aTransform.multPoint(vertA[i]);

    Vector3f[] vertB = BufferUtils.getVector3Array(mesh2.getVertexBuffer());
    for (int i = 0; i < vertB.length; i++) bTransform.multPoint(vertB[i]);

    for (int i = 0; i < mesh1.getTriangleCount(); i++) {
      for (int j = 0; j < mesh2.getTriangleCount(); j++) {
        if (intersection(
            vertA[indexA.get(i * 3 + 0)],
            vertA[indexA.get(i * 3 + 1)],
            vertA[indexA.get(i * 3 + 2)],
            vertB[indexB.get(j * 3 + 0)],
            vertB[indexB.get(j * 3 + 1)],
            vertB[indexB.get(j * 3 + 2)])) return true;
      }
    }
    return false;
  }
  public static void generate(TriMesh mesh) {

    FloatBuffer tangents = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 3);
    FloatBuffer binormals = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 3);

    IntBuffer indexBuffer = mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = mesh.getVertexBuffer();
    FloatBuffer textureBuffer = mesh.getTextureCoords(0).coords;
    indexBuffer.rewind();

    Vector3f tangent = new Vector3f();
    Vector3f binormal = new Vector3f();
    Vector3f normal = new Vector3f();
    Vector3f verts[] = new Vector3f[3];
    Vector2f texcoords[] = new Vector2f[3];

    for (int i = 0; i < 3; i++) {
      verts[i] = new Vector3f();
      texcoords[i] = new Vector2f();
    }

    for (int t = 0; t < indexBuffer.capacity() / 3; t++) {

      int index[] = new int[3];

      for (int v = 0; v < 3; v++) {
        index[v] = indexBuffer.get();
        verts[v].x = vertexBuffer.get(index[v] * 3);
        verts[v].y = vertexBuffer.get(index[v] * 3 + 1);
        verts[v].z = vertexBuffer.get(index[v] * 3 + 2);

        texcoords[v].x = textureBuffer.get(index[v] * 2);
        texcoords[v].y = textureBuffer.get(index[v] * 2 + 1);
      }

      computeTriangleTangentSpace(tangent, binormal, normal, verts, texcoords);

      for (int v = 0; v < 3; v++) {
        tangents.position(index[v] * 3);
        tangents.put(tangent.x);
        tangents.put(tangent.y);
        tangents.put(tangent.z);

        binormals.position(index[v] * 3);
        binormals.put(binormal.x);
        binormals.put(binormal.y);
        binormals.put(binormal.z);
      }
    }

    mesh.setTangentBuffer(tangents);
    mesh.setBinormalBuffer(binormals);
  }