예제 #1
0
  public synchronized int compress(byte[] b, int off, int len) throws IOException {
    if (b == null) {
      throw new NullPointerException();
    }
    if (off < 0 || len < 0 || off > b.length - len) {
      throw new ArrayIndexOutOfBoundsException();
    }

    int n = 0;

    // Check if there is compressed data
    n = compressedDirectBuf.remaining();
    if (n > 0) {
      n = Math.min(n, len);
      ((ByteBuffer) compressedDirectBuf).get(b, off, n);
      return n;
    }

    // Re-initialize the zlib's output direct buffer
    compressedDirectBuf.rewind();
    compressedDirectBuf.limit(directBufferSize);

    // Compress data
    n = deflateBytesDirect();
    compressedDirectBuf.limit(n);

    // Get atmost 'len' bytes
    n = Math.min(n, len);
    ((ByteBuffer) compressedDirectBuf).get(b, off, n);

    return n;
  }
예제 #2
0
 public synchronized void reset() {
   checkStream();
   reset(stream);
   finish = false;
   finished = false;
   uncompressedDirectBuf.rewind();
   uncompressedDirectBufOff = uncompressedDirectBufLen = 0;
   compressedDirectBuf.limit(directBufferSize);
   compressedDirectBuf.position(directBufferSize);
   userBufOff = userBufLen = 0;
 }
예제 #3
0
  synchronized void setInputFromSavedData() {
    uncompressedDirectBufOff = 0;
    uncompressedDirectBufLen = userBufLen;
    if (uncompressedDirectBufLen > directBufferSize) {
      uncompressedDirectBufLen = directBufferSize;
    }

    // Reinitialize zlib's input direct buffer
    uncompressedDirectBuf.rewind();
    ((ByteBuffer) uncompressedDirectBuf).put(userBuf, userBufOff, uncompressedDirectBufLen);

    // Note how much data is being fed to zlib
    userBufOff += uncompressedDirectBufLen;
    userBufLen -= uncompressedDirectBufLen;
  }
예제 #4
0
  public void drawTriangleList(VertexBuffer indexBuf, Mesh mesh, int count) {
    Mesh.Mode mode = mesh.getMode();

    Buffer indexData = indexBuf.getData();
    indexData.rewind();

    if (mesh.getMode() == Mode.Hybrid) {
      throw new UnsupportedOperationException();
      /*
      int[] modeStart = mesh.getModeStart();
      int[] elementLengths = mesh.getElementLengths();

      int elMode = convertElementMode(Mode.Triangles);
      int fmt = convertVertexFormat(indexBuf.getFormat());
      //            int elSize = indexBuf.getFormat().getComponentSize();
      //            int listStart = modeStart[0];
      int stripStart = modeStart[1];
      int fanStart = modeStart[2];
      int curOffset = 0;
      for (int i = 0; i < elementLengths.length; i++) {
      if (i == stripStart) {
      elMode = convertElementMode(Mode.TriangleStrip);
      } else if (i == fanStart) {
      elMode = convertElementMode(Mode.TriangleStrip);
      }
      int elementLength = elementLengths[i];
      indexData.position(curOffset);

      drawElements(elMode,
      fmt,
      indexData);

      curOffset += elementLength;
      }*/
    } else {
      drawElements(convertElementMode(mode), convertVertexFormat(indexBuf.getFormat()), indexData);
    }
  }
예제 #5
0
  public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
    if (vb.getBufferType() == VertexBuffer.Type.Color && !context.useVertexColor) {
      // Ignore vertex color buffer if vertex color is disabled.
      return;
    }

    int arrayType = convertArrayType(vb.getBufferType());
    if (arrayType == -1) {
      return; // unsupported
    }
    glEnableClientState(arrayType);
    context.boundAttribs[vb.getBufferType().ordinal()] = vb;

    if (vb.getBufferType() == Type.Normal) {
      // normalize if requested
      if (vb.isNormalized() && !context.normalizeEnabled) {
        glEnable(GL_NORMALIZE);
        context.normalizeEnabled = true;
      } else if (!vb.isNormalized() && context.normalizeEnabled) {
        glDisable(GL_NORMALIZE);
        context.normalizeEnabled = false;
      }
    }

    // NOTE: Use data from interleaved buffer if specified
    Buffer data = idb != null ? idb.getData() : vb.getData();
    int comps = vb.getNumComponents();
    int type = convertVertexFormat(vb.getFormat());

    data.rewind();

    switch (vb.getBufferType()) {
      case Position:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        glVertexPointer(comps, vb.getStride(), (FloatBuffer) data);
        break;
      case Normal:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        glNormalPointer(vb.getStride(), (FloatBuffer) data);
        break;
      case Color:
        if (data instanceof FloatBuffer) {
          glColorPointer(comps, vb.getStride(), (FloatBuffer) data);
        } else if (data instanceof ByteBuffer) {
          glColorPointer(comps, true, vb.getStride(), (ByteBuffer) data);
        } else {
          throw new UnsupportedOperationException();
        }
        break;
      case TexCoord:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        glTexCoordPointer(comps, vb.getStride(), (FloatBuffer) data);
        break;
      default:
        // Ignore, this is an unsupported attribute for OpenGL1.
        break;
    }
  }
  // Don't remove splitmirorred boolean,It's not used right now, but i intend to
  // make this method also split vertice with rotated tangent space and I'll
  // add another splitRotated boolean
  private static List<VertexData> splitVertices(
      Mesh mesh, List<VertexData> vertexData, boolean splitMirorred) {
    int nbVertices = mesh.getBuffer(Type.Position).getNumElements();
    List<VertexData> newVertices = new ArrayList<VertexData>();
    Map<Integer, Integer> indiceMap = new HashMap<Integer, Integer>();
    FloatBuffer normalBuffer = mesh.getFloatBuffer(Type.Normal);

    for (int i = 0; i < vertexData.size(); i++) {
      ArrayList<TriangleData> triangles = vertexData.get(i).triangles;
      Vector3f givenNormal = new Vector3f();
      populateFromBuffer(givenNormal, normalBuffer, i);

      ArrayList<TriangleData> trianglesUp = new ArrayList<TriangleData>();
      ArrayList<TriangleData> trianglesDown = new ArrayList<TriangleData>();
      for (int j = 0; j < triangles.size(); j++) {
        TriangleData triangleData = triangles.get(j);
        if (parity(givenNormal, triangleData.normal) > 0) {
          trianglesUp.add(triangleData);
        } else {
          trianglesDown.add(triangleData);
        }
      }

      // if the vertex has triangles with opposite parity it has to be split
      if (!trianglesUp.isEmpty() && !trianglesDown.isEmpty()) {
        log.log(Level.FINE, "Splitting vertex {0}", i);
        // assigning triangle with the same parity to the original vertex
        vertexData.get(i).triangles.clear();
        vertexData.get(i).triangles.addAll(trianglesUp);

        // creating a new vertex
        VertexData newVert = new VertexData();
        // assigning triangles with opposite parity to it
        newVert.triangles.addAll(trianglesDown);

        newVertices.add(newVert);
        // keep vertex index to fix the index buffers later
        indiceMap.put(nbVertices, i);
        for (TriangleData tri : newVert.triangles) {
          for (int j = 0; j < tri.index.length; j++) {
            if (tri.index[j] == i) {
              tri.index[j] = nbVertices;
            }
          }
        }
        nbVertices++;
      }
    }

    if (!newVertices.isEmpty()) {

      // we have new vertices, we need to update the mesh's buffers.
      for (Type type : VertexBuffer.Type.values()) {
        // skip tangent buffer as we're gonna overwrite it later
        if (type == Type.Tangent || type == Type.BindPoseTangent) {
          continue;
        }
        VertexBuffer vb = mesh.getBuffer(type);
        // Some buffer (hardware skinning ones) can be there but not
        // initialized, they must be skipped.
        // They'll be initialized when Hardware Skinning is engaged
        if (vb == null || vb.getNumComponents() == 0) {
          continue;
        }

        Buffer buffer = vb.getData();
        // IndexBuffer has special treatement, only swapping the vertex indices is needed
        if (type == Type.Index) {
          boolean isShortBuffer = vb.getFormat() == VertexBuffer.Format.UnsignedShort;
          for (VertexData vertex : newVertices) {
            for (TriangleData tri : vertex.triangles) {
              for (int i = 0; i < tri.index.length; i++) {
                if (isShortBuffer) {
                  ((ShortBuffer) buffer).put(tri.triangleOffset + i, (short) tri.index[i]);
                } else {
                  ((IntBuffer) buffer).put(tri.triangleOffset + i, tri.index[i]);
                }
              }
            }
          }
          vb.setUpdateNeeded();
        } else {
          // copy the buffer in a bigger one and append nex vertices to the end
          Buffer newVerts =
              VertexBuffer.createBuffer(vb.getFormat(), vb.getNumComponents(), nbVertices);
          if (buffer != null) {
            buffer.rewind();
            bulkPut(vb.getFormat(), newVerts, buffer);

            int index = vertexData.size();
            newVerts.position(vertexData.size() * vb.getNumComponents());
            for (int j = 0; j < newVertices.size(); j++) {
              int oldInd = indiceMap.get(index);
              for (int i = 0; i < vb.getNumComponents(); i++) {
                putValue(vb.getFormat(), newVerts, buffer, oldInd * vb.getNumComponents() + i);
              }
              index++;
            }
            vb.updateData(newVerts);
            // destroy previous buffer as it's no longer needed
            destroyDirectBuffer(buffer);
          }
        }
      }
      vertexData.addAll(newVertices);

      mesh.updateCounts();
    }

    return vertexData;
  }