protected ShortBuffer readShortBuffer(byte[] content) throws IOException {
    int length = readInt(content);
    if (length == BinaryOutputCapsule.NULL_OBJECT) return null;

    if (BinaryImporter.canUseFastBuffers()) {
      ByteBuffer value = BufferUtils.createByteBuffer(length * 2);
      value.put(content, index, length * 2).rewind();
      index += length * 2;
      return value.asShortBuffer();
    } else {
      ShortBuffer value = BufferUtils.createShortBuffer(length);
      for (int x = 0; x < length; x++) {
        value.put(readShortForBuffer(content));
      }
      value.rewind();
      return value;
    }
  }
  @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();
  }
Пример #3
0
  public Mesh compileArrays() {
    // Assuming the shape is a perfect cube
    Vector3f v0, v1, v2, v3, v4, v5, v6, v7;
    float s = o.getEdgeSize() / 2f;
    v0 = o.getOrigin().add(new Vector3f(s, s, s));
    v1 = o.getOrigin().add(new Vector3f(s, s, -s));
    v2 = o.getOrigin().add(new Vector3f(-s, s, -s));
    v3 = o.getOrigin().add(new Vector3f(-s, s, s));
    v4 = o.getOrigin().add(new Vector3f(s, -s, s));
    v5 = o.getOrigin().add(new Vector3f(s, -s, -s));
    v6 = o.getOrigin().add(new Vector3f(-s, -s, -s));
    v7 = o.getOrigin().add(new Vector3f(-s, -s, s));

    posArray = new float[12 * 6];
    texCoordsArray = new float[8 * 6];
    normArray = new float[12 * 6];
    tanArray = new float[16 * 6];
    indArray = new short[6 * 6];

    for (int i = 0; i < 6; i++) {
      QuadV4 quad;
      switch (i) {
        case 0:
          quad = new QuadV4(v7, v4, v0, v3, o, i);
          break;
        case 1:
          quad = new QuadV4(v4, v5, v1, v0, o, i);
          break;
        case 2:
          quad = new QuadV4(v5, v6, v2, v1, o, i);
          break;
        case 3:
          quad = new QuadV4(v6, v7, v3, v2, o, i);
          break;
        case 4:
          quad = new QuadV4(v3, v0, v1, v2, o, i);
          break;
        case 5:
          quad = new QuadV4(v6, v5, v4, v7, o, i);
          break;
        default:
          throw new IllegalStateException("Wrong side number");
      }

      System.arraycopy(
          quad.positionArray,
          0,
          posArray,
          i * quad.positionArray.length,
          quad.positionArray.length);
      System.arraycopy(
          quad.texCoordsArray,
          0,
          texCoordsArray,
          i * quad.texCoordsArray.length,
          quad.texCoordsArray.length);
      System.arraycopy(
          quad.normalArray, 0, normArray, i * quad.normalArray.length, quad.normalArray.length);
      // System.arraycopy(quad.tangentArray, 0, tanArray, i * quad.tangentArray.length,
      // quad.tangentArray.length);

      for (int j = 0; j < quad.indexArray.length; j++) {
        short val = quad.indexArray[j];
        val += 4 * i;
        indArray[(quad.indexArray.length * i) + j] = val;
      }
    }

    Mesh mesh = new Mesh();
    mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(posArray));
    mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoordsArray));
    mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normArray));
    mesh.setBuffer(Type.Index, 3, BufferUtils.createShortBuffer(indArray));
    mesh.updateBound();
    return mesh;
  }