Esempio n. 1
0
  public void load3ds(String filename, int count, float angle_y, float z_adjust, float scale) {
    if (meshCache.get(filename + ("" + angle_y) + count) == null) {
      ArrayByte is = BaseIO.loadArrayByte(filename);
      byte buffer[] = new byte[is.available()];
      is.read(buffer);
      this.mMeshPosition = new float[3];
      currentcount = 0;
      eatChunk(buffer, 0, count, angle_y, z_adjust, scale);
      is.close();
      this.mMeshName = filename;
      this.mMeshPosition = null;
      D3DMesh.storeMesh(filename + ("" + angle_y) + count, this);

    } else {
      this.copyReference(D3DMesh.getMesh(filename + ("" + angle_y) + count));
    }
  }
Esempio n. 2
0
  public static D3DMesh createSphere(int nbSlices, int nbStairs, float radius) {
    D3DMesh mesh = new D3DMesh();

    int triangleCount = nbSlices * (nbStairs) * 2;

    mesh.mVertices = FloatBuffer.allocate(D3DMesh.nbFloatPerVertex * 3 * triangleCount);
    mesh.mIndices = CharBuffer.allocate(triangleCount * 3);

    int indice = 0;
    for (int i = 0; i < nbStairs; i++) {
      for (int j = 0; j < nbSlices; j++) {
        float alpha = MathUtils.PI / (nbStairs) * i;
        float beta = MathUtils.PI * 2 / nbSlices * j;
        float alpha1 = MathUtils.PI / (nbStairs) * ((i + 1));

        float beta1 = MathUtils.PI * 2 / nbSlices * ((j + 1));
        if (j == nbSlices - 1) {
          beta1 = 0.f;
        }

        D3DVector a =
            new D3DVector(
                radius * MathUtils.sin(alpha) * MathUtils.cos(beta),
                radius * MathUtils.cos(alpha),
                radius * MathUtils.sin(alpha) * MathUtils.sin(beta));
        D3DVector b =
            new D3DVector(
                radius * MathUtils.sin(alpha) * MathUtils.cos(beta1),
                radius * MathUtils.cos(alpha),
                radius * MathUtils.sin(alpha) * MathUtils.sin(beta1));
        D3DVector c =
            new D3DVector(
                radius * MathUtils.sin(alpha1) * MathUtils.cos(beta1),
                radius * MathUtils.cos(alpha1),
                radius * MathUtils.sin(alpha1) * MathUtils.sin(beta1));
        D3DVector d =
            new D3DVector(
                radius * MathUtils.sin(alpha1) * MathUtils.cos(beta),
                radius * MathUtils.cos(alpha1),
                radius * MathUtils.sin(alpha1) * MathUtils.sin(beta));

        mesh.mVertices.put(a.get(0));
        mesh.mVertices.put(a.get(1));
        mesh.mVertices.put(a.get(2));

        mesh.mVertices.put(a.get(0) / radius);
        mesh.mVertices.put(a.get(1) / radius);
        mesh.mVertices.put(a.get(2) / radius);

        mesh.mVertices.put((float) (nbSlices - j + 0) / (float) nbSlices);
        mesh.mVertices.put((float) (i + 0) / (float) nbStairs);

        mesh.mVertices.put(b.get(0));
        mesh.mVertices.put(b.get(1));
        mesh.mVertices.put(b.get(2));

        mesh.mVertices.put(b.get(0) / radius);
        mesh.mVertices.put(b.get(1) / radius);
        mesh.mVertices.put(b.get(2) / radius);

        mesh.mVertices.put((float) (nbSlices - (j + 1)) / (float) nbSlices);
        mesh.mVertices.put((float) (i + 0) / (float) nbStairs);

        mesh.mVertices.put(c.get(0));
        mesh.mVertices.put(c.get(1));
        mesh.mVertices.put(c.get(2));

        // normal not computed here
        mesh.mVertices.put(c.get(0) / radius);
        mesh.mVertices.put(c.get(1) / radius);
        mesh.mVertices.put(c.get(2) / radius);

        mesh.mVertices.put((float) (nbSlices - (j + 1)) / (float) nbSlices);
        mesh.mVertices.put((float) (i + 1) / (float) nbStairs);

        mesh.mVertices.put(d.get(0));
        mesh.mVertices.put(d.get(1));
        mesh.mVertices.put(d.get(2));

        mesh.mVertices.put(d.get(0) / radius);
        mesh.mVertices.put(d.get(1) / radius);
        mesh.mVertices.put(d.get(2) / radius);

        mesh.mVertices.put((float) (nbSlices - j + 0) / (float) nbSlices);
        mesh.mVertices.put((float) (i + 1) / (float) nbStairs);

        mesh.mIndices.put((char) (indice + 0));
        mesh.mIndices.put((char) (indice + 2));
        mesh.mIndices.put((char) (indice + 1));

        mesh.mIndices.put((char) (indice + 2));
        mesh.mIndices.put((char) (indice + 0));
        mesh.mIndices.put((char) (indice + 3));

        indice = mesh.mVertices.position() / D3DMesh.nbFloatPerVertex;
      }
    }

    mesh.mIndices.position(0);
    mesh.mVertices.position(0);

    return mesh;
  }
Esempio n. 3
0
  public D3DMesh buildShadowVolume(D3DVector lightVector) {

    int nbface = this.mIndices.capacity() / 3;

    int trianglecount = 0;
    D3DMesh shadowMesh = new D3DMesh();

    boolean faceToLight[] = new boolean[nbface];

    D3DVector v0 = new D3DVector();
    D3DVector v1 = new D3DVector();
    D3DVector v2 = new D3DVector();
    D3DVector vm1 = new D3DVector();
    D3DVector vm2 = new D3DVector();
    D3DVector res = new D3DVector();
    D3DVector vf = new D3DVector();
    for (int i = 0; i < this.mIndices.capacity(); i = i + 3) {
      int a = (int) (mIndices.get(i));
      int b = (int) (mIndices.get(i + 1));
      int c = (int) (mIndices.get(i + 2));

      v0.setFromVertice(mVertices, a);
      v1.setFromVertice(mVertices, b);
      v2.setFromVertice(mVertices, c);

      D3DVector.sub(vm1, v1, v0);
      D3DVector.sub(vm2, v2, v0);
      D3DVector.cross(res, vm1, vm2);
      res.normalize();

      D3DVector.add(vf, v0, v1);
      D3DVector.add(vf, vf, v2);

      float scalar = D3DVector.dot(res, lightVector);

      if (scalar > 0.0) {
        faceToLight[i / 3] = true;
      } else {

        trianglecount++;
      }
    }

    for (int i = 0; i < this.mIndices.capacity(); i = i + 3) {
      if (faceToLight[i / 3]) {
        for (int k = 0; k < 3; k++) {
          int a = mIndices.get(i + k);
          int b = mIndices.get(i + (k + 1) % 3);

          for (int l = 0; l < this.mIndices.capacity(); l = l + 3) {
            if (!faceToLight[l / 3]) {

              for (int z = 0; z < 3; z++) {
                int a2 = mIndices.get(l + z);
                int b2 = mIndices.get(l + (z + 1) % 3);

                if (((a == a2) && (b == b2)) || ((a == b2) && (b == a2))) {

                  trianglecount++;
                  trianglecount++;
                }
              }
            }
          }
        }
      }
    }

    shadowMesh.init(trianglecount);

    for (int i = 0; i < this.mIndices.capacity(); i = i + 3) {
      int a = (int) (mIndices.get(i));
      int b = (int) (mIndices.get(i + 1));
      int c = (int) (mIndices.get(i + 2));

      v0.setFromVertice(mVertices, a);
      v1.setFromVertice(mVertices, b);
      v2.setFromVertice(mVertices, c);

      D3DVector.sub(vm1, v1, v0);
      D3DVector.sub(vm2, v2, v0);
      D3DVector.cross(res, vm1, vm2);
      res.normalize();

      D3DVector.add(vf, v0, v1);
      D3DVector.add(vf, vf, v2);

      float scalar = D3DVector.dot(res, lightVector);

      if (scalar > 0.0) {
        faceToLight[i / 3] = true;
      } else {

        shadowMesh.putTriangle(
            mVertices.get(a * nbFloatPerVertex),
            mVertices.get(a * nbFloatPerVertex + 1),
            mVertices.get(a * nbFloatPerVertex + 2),
            mVertices.get(b * nbFloatPerVertex),
            mVertices.get(b * nbFloatPerVertex + 1),
            mVertices.get(b * nbFloatPerVertex + 2),
            mVertices.get(c * nbFloatPerVertex),
            mVertices.get(c * nbFloatPerVertex + 1),
            mVertices.get(c * nbFloatPerVertex + 2));
      }
    }

    for (int i = 0; i < this.mIndices.capacity(); i = i + 3) {
      if (faceToLight[i / 3]) {
        for (int k = 0; k < 3; k++) {
          int a = mIndices.get(i + k);
          int b = mIndices.get(i + (k + 1) % 3);

          for (int l = 0; l < this.mIndices.capacity(); l = l + 3) {
            if (!faceToLight[l / 3]) {

              for (int z = 0; z < 3; z++) {
                int a2 = mIndices.get(l + z);
                int b2 = mIndices.get(l + (z + 1) % 3);

                if (((a == a2) && (b == b2)) || ((a == b2) && (b == a2))) {
                  D3DVector lightVectP = lightVector.cpy();
                  lightVectP.mul(10000.f);

                  D3DVector vt1 = new D3DVector();
                  vt1.setFromVertice(mVertices, a2);
                  D3DVector vt1p = new D3DVector();
                  D3DVector.sub(vt1p, vt1, lightVectP);

                  D3DVector vt2 = new D3DVector();
                  vt2.setFromVertice(mVertices, b2);
                  D3DVector vt2p = new D3DVector();
                  D3DVector.sub(vt2p, vt2, lightVectP);

                  shadowMesh.putTriangle(
                      mVertices.get(a * nbFloatPerVertex),
                      mVertices.get(a * nbFloatPerVertex + 1),
                      mVertices.get(a * nbFloatPerVertex + 2),
                      mVertices.get(b * nbFloatPerVertex),
                      mVertices.get(b * nbFloatPerVertex + 1),
                      mVertices.get(b * nbFloatPerVertex + 2),
                      vt2p.get(0),
                      vt2p.get(1),
                      vt2p.get(2));

                  shadowMesh.putTriangle(
                      vt2p.get(0),
                      vt2p.get(1),
                      vt2p.get(2),
                      vt1p.get(0),
                      vt1p.get(1),
                      vt1p.get(2),
                      mVertices.get(a * nbFloatPerVertex),
                      mVertices.get(a * nbFloatPerVertex + 1),
                      mVertices.get(a * nbFloatPerVertex + 2));
                }
              }
            }
          }
        }
      }
    }

    shadowMesh.mVertices.position(0);
    shadowMesh.mIndices.position(0);
    return shadowMesh;
  }