Exemplo n.º 1
0
  public static void compressIndexBuffer(Mesh mesh) {
    int vertCount = mesh.getVertexCount();
    VertexBuffer vb = mesh.getBuffer(Type.Index);
    Format targetFmt;
    if (vb.getFormat() == Format.UnsignedInt && vertCount <= 0xffff) {
      if (vertCount <= 256) targetFmt = Format.UnsignedByte;
      else targetFmt = Format.UnsignedShort;
    } else if (vb.getFormat() == Format.UnsignedShort && vertCount <= 0xff) {
      targetFmt = Format.UnsignedByte;
    } else {
      return;
    }

    IndexBuffer src = mesh.getIndexBuffer();
    Buffer newBuf = VertexBuffer.createBuffer(targetFmt, vb.getNumComponents(), src.size());

    VertexBuffer newVb = new VertexBuffer(Type.Index);
    newVb.setupData(vb.getUsage(), vb.getNumComponents(), targetFmt, newBuf);
    mesh.clearBuffer(Type.Index);
    mesh.setBuffer(newVb);

    IndexBuffer dst = mesh.getIndexBuffer();
    for (int i = 0; i < src.size(); i++) {
      dst.put(i, src.get(i));
    }
  }
  private static List<VertexData> processTriangles(
      Mesh mesh, int[] index, Vector3f[] v, Vector2f[] t, boolean splitMirrored) {
    IndexBuffer indexBuffer = mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    if (mesh.getBuffer(Type.TexCoord) == null) {
      throw new IllegalArgumentException(
          "Can only generate tangents for " + "meshes with texture coordinates");
    }

    FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

    List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);

    for (int i = 0; i < indexBuffer.size() / 3; i++) {
      for (int j = 0; j < 3; j++) {
        index[j] = indexBuffer.get(i * 3 + j);
        populateFromBuffer(v[j], vertexBuffer, index[j]);
        populateFromBuffer(t[j], textureBuffer, index[j]);
      }

      TriangleData triData = processTriangle(index, v, t);
      if (splitMirrored) {
        triData.setIndex(index);
        triData.triangleOffset = i * 3;
      }
      if (triData != null) {
        vertices.get(index[0]).triangles.add(triData);
        vertices.get(index[1]).triangles.add(triData);
        vertices.get(index[2]).triangles.add(triData);
      }
    }

    return vertices;
  }
  private static List<VertexData> processTriangleStrip(
      Mesh mesh, int[] index, Vector3f[] v, Vector2f[] t) {
    IndexBuffer indexBuffer = mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    FloatBuffer textureBuffer = (FloatBuffer) mesh.getBuffer(Type.TexCoord).getData();

    List<VertexData> vertices = initVertexData(vertexBuffer.limit() / 3);

    index[0] = indexBuffer.get(0);
    index[1] = indexBuffer.get(1);

    populateFromBuffer(v[0], vertexBuffer, index[0]);
    populateFromBuffer(v[1], vertexBuffer, index[1]);

    populateFromBuffer(t[0], textureBuffer, index[0]);
    populateFromBuffer(t[1], textureBuffer, index[1]);

    for (int i = 2; i < indexBuffer.size(); i++) {
      index[2] = indexBuffer.get(i);
      BufferUtils.populateFromBuffer(v[2], vertexBuffer, index[2]);
      BufferUtils.populateFromBuffer(t[2], textureBuffer, index[2]);

      boolean isDegenerate = isDegenerateTriangle(v[0], v[1], v[2]);
      TriangleData triData = processTriangle(index, v, t);

      if (triData != null && !isDegenerate) {
        vertices.get(index[0]).triangles.add(triData);
        vertices.get(index[1]).triangles.add(triData);
        vertices.get(index[2]).triangles.add(triData);
      }

      Vector3f vTemp = v[0];
      v[0] = v[1];
      v[1] = v[2];
      v[2] = vTemp;

      Vector2f tTemp = t[0];
      t[0] = t[1];
      t[1] = t[2];
      t[2] = tTemp;

      index[0] = index[1];
      index[1] = index[2];
    }

    return vertices;
  }
  @Override
  public void initParticleData(Emitter emitter, int numParticles) {
    setMode(Mode.Triangles);

    this.emitter = emitter;

    this.finVerts = BufferUtils.createFloatBuffer(templateVerts.capacity() * numParticles);
    try {
      this.finCoords = BufferUtils.createFloatBuffer(templateCoords.capacity() * numParticles);
    } catch (Exception e) {
    }
    this.finIndexes = BufferUtils.createShortBuffer(templateIndexes.size() * numParticles);
    this.finNormals = BufferUtils.createFloatBuffer(templateNormals.capacity() * numParticles);
    this.finColors = BufferUtils.createFloatBuffer(templateVerts.capacity() / 3 * 4 * numParticles);

    int index = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0;
    int indexOffset = 0;

    for (int i = 0; i < numParticles; i++) {
      templateVerts.rewind();
      for (int v = 0; v < templateVerts.capacity(); v += 3) {
        tempV3.set(templateVerts.get(v), templateVerts.get(v + 1), templateVerts.get(v + 2));
        finVerts.put(index, tempV3.getX());
        index++;
        finVerts.put(index, tempV3.getY());
        index++;
        finVerts.put(index, tempV3.getZ());
        index++;
      }
      try {
        templateCoords.rewind();
        for (int v = 0; v < templateCoords.capacity(); v++) {
          finCoords.put(index2, templateCoords.get(v));
          index2++;
        }
      } catch (Exception e) {
      }
      for (int v = 0; v < templateIndexes.size(); v++) {
        finIndexes.put(index3, (short) (templateIndexes.get(v) + indexOffset));
        index3++;
      }
      indexOffset += templateVerts.capacity() / 3;

      templateNormals.rewind();
      for (int v = 0; v < templateNormals.capacity(); v++) {
        finNormals.put(index4, templateNormals.get(v));
        index4++;
      }

      for (int v = 0; v < finColors.capacity(); v++) {
        finColors.put(v, 1.0f);
      }
    }

    // Help GC
    //	tempV3 = null;
    //	templateVerts = null;
    //	templateCoords = null;
    //	templateIndexes = null;
    //	templateNormals = null;

    // Clear & ssign buffers
    this.clearBuffer(VertexBuffer.Type.Position);
    this.setBuffer(VertexBuffer.Type.Position, 3, finVerts);
    this.clearBuffer(VertexBuffer.Type.TexCoord);
    try {
      this.setBuffer(VertexBuffer.Type.TexCoord, 2, finCoords);
    } catch (Exception e) {
    }
    this.clearBuffer(VertexBuffer.Type.Index);
    this.setBuffer(VertexBuffer.Type.Index, 3, finIndexes);
    this.clearBuffer(VertexBuffer.Type.Normal);
    this.setBuffer(VertexBuffer.Type.Normal, 3, finNormals);
    this.clearBuffer(VertexBuffer.Type.Color);
    this.setBuffer(VertexBuffer.Type.Color, 4, finColors);
    this.updateBound();
  }