Beispiel #1
0
  protected void read(DataInputStream s) {
    try {
      ref = new WeakReference<DataBuffer>(this, Nd4j.bufferRefQueue());
      referencing = Collections.synchronizedSet(new HashSet<String>());
      dirty = new AtomicBoolean(false);
      allocationMode = AllocationMode.valueOf(s.readUTF());
      length = s.readInt();
      Type t = Type.valueOf(s.readUTF());
      if (t == Type.DOUBLE) {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // double -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // double -> double
            doubleData = new double[length()];
          } else {
            // double -> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        }
      } else {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // float -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // float -> double
            doubleData = new double[length()];
          } else {
            // float-> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Beispiel #2
0
  /**
   * Create a data buffer from the given length
   *
   * @param buffer
   * @param length
   */
  public BaseDataBuffer(ByteBuffer buffer, int length) {
    allocationMode = Nd4j.alloc;
    this.length = length;
    buffer.order(ByteOrder.nativeOrder());
    if (allocationMode() == AllocationMode.DIRECT) {
      this.wrappedBuffer = buffer;
    } else if (dataType() == Type.INT) {
      intData = new int[length];
      IntBuffer intBuffer = buffer.asIntBuffer();
      for (int i = 0; i < length; i++) {
        intData[i] = intBuffer.get(i);
      }
    } else if (dataType() == Type.DOUBLE) {
      doubleData = new double[length];
      DoubleBuffer doubleBuffer = buffer.asDoubleBuffer();
      for (int i = 0; i < length; i++) {
        doubleData[i] = doubleBuffer.get(i);
      }

    } else if (dataType() == Type.FLOAT) {
      floatData = new float[length];
      FloatBuffer floatBuffer = buffer.asFloatBuffer();
      for (int i = 0; i < length; i++) {
        floatData[i] = floatBuffer.get(i);
      }
    }
  }
Beispiel #3
0
  /**
   * @param data
   * @param copy
   */
  public BaseDataBuffer(int[] data, boolean copy) {
    allocationMode = Nd4j.alloc;
    if (allocationMode == AllocationMode.HEAP) {
      if (copy) intData = ArrayUtil.copy(data);
      else this.intData = data;

    } else {
      wrappedBuffer = ByteBuffer.allocateDirect(4 * data.length);
      wrappedBuffer.order(ByteOrder.nativeOrder());
      IntBuffer buffer = wrappedBuffer.asIntBuffer();
      for (int i = 0; i < data.length; i++) {
        buffer.put(i, data[i]);
      }
    }
    length = data.length;
  }
Beispiel #4
0
  /**
   * Instantiate a buffer with the given length
   *
   * @param length the length of the buffer
   */
  protected BaseDataBuffer(int length) {
    this.length = length;
    allocationMode = Nd4j.alloc;
    if (length < 0) throw new IllegalArgumentException("Unable to create a buffer of length <= 0");

    ref = new WeakReference<DataBuffer>(this, Nd4j.bufferRefQueue());
    if (allocationMode == AllocationMode.HEAP) {
      if (length >= Integer.MAX_VALUE)
        throw new IllegalArgumentException(
            "Length of data buffer can not be > Integer.MAX_VALUE for heap (array based storage) allocation");
      if (dataType() == Type.DOUBLE) doubleData = new double[length];
      else if (dataType() == Type.FLOAT) floatData = new float[length];
    } else {
      if (length * getElementSize() < 0)
        throw new IllegalArgumentException(
            "Unable to create buffer of length " + length + " due to negative length specified");
      wrappedBuffer =
          ByteBuffer.allocateDirect(getElementSize() * length).order(ByteOrder.nativeOrder());
    }
  }
Beispiel #5
0
  /**
   * @param data
   * @param copy
   */
  public BaseDataBuffer(double[] data, boolean copy) {
    allocationMode = Nd4j.alloc;
    if (allocationMode == AllocationMode.HEAP) {
      if (copy) {
        doubleData = ArrayUtil.copy(data);
      } else {
        this.doubleData = data;
      }
    } else {
      wrappedBuffer = ByteBuffer.allocateDirect(8 * data.length);
      wrappedBuffer.order(ByteOrder.nativeOrder());
      DoubleBuffer buffer = wrappedBuffer.asDoubleBuffer();
      for (int i = 0; i < data.length; i++) {
        buffer.put(i, data[i]);
      }
    }

    length = data.length;
    underlyingLength = data.length;
  }
Beispiel #6
0
 private static int biTypePF(int biType) {
   ByteOrder byteOrder = ByteOrder.nativeOrder();
   switch (biType) {
     case BufferedImage.TYPE_3BYTE_BGR:
       return TJ.PF_BGR;
     case BufferedImage.TYPE_4BYTE_ABGR:
     case BufferedImage.TYPE_4BYTE_ABGR_PRE:
       return TJ.PF_XBGR;
     case BufferedImage.TYPE_BYTE_GRAY:
       return TJ.PF_GRAY;
     case BufferedImage.TYPE_INT_BGR:
       if (byteOrder == ByteOrder.BIG_ENDIAN) return TJ.PF_XBGR;
       else return TJ.PF_RGBX;
     case BufferedImage.TYPE_INT_RGB:
       if (byteOrder == ByteOrder.BIG_ENDIAN) return TJ.PF_XRGB;
       else return TJ.PF_BGRX;
     case BufferedImage.TYPE_INT_ARGB:
     case BufferedImage.TYPE_INT_ARGB_PRE:
       if (byteOrder == ByteOrder.BIG_ENDIAN) return TJ.PF_ARGB;
       else return TJ.PF_BGRA;
   }
   return 0;
 }
  /**
   * 描画用マテリアル情報をMQOデータから作成
   *
   * @param mqomat MQOファイルから読み込んだマテリアル情報
   * @param i_mqomat MQOファイルのマテリアル番号
   * @param mqoObjs MQOファイルのオブジェクト情報
   * @param vn 頂点法線配列
   * @return 描画用マテリアル情報
   */
  private GLMaterial makeMats(
      GL10 gl, material mqomat, int i_mqomat, objects mqoObjs, KGLPoint[] vn) {
    GLMaterial ret = new GLMaterial();
    ArrayList<KGLPoint> apv = new ArrayList<KGLPoint>();
    ArrayList<KGLPoint> apn = new ArrayList<KGLPoint>();
    ArrayList<KGLPoint> apuv = new ArrayList<KGLPoint>();
    ArrayList<KGLPoint> apc = new ArrayList<KGLPoint>();
    KGLPoint wpoint = null;
    boolean uvValid = false;
    boolean colValid = false;
    KGLPoint fn;
    float s;
    for (int f = 0; f < mqoObjs.face.length; f++) {
      if (mqoObjs.face[f].M == null) {
        continue;
      }
      if (mqoObjs.face[f].M != i_mqomat) continue;

      fn =
          calcNormal(
              mqoObjs.vertex, mqoObjs.face[f].V[0], mqoObjs.face[f].V[1], mqoObjs.face[f].V[2]);
      for (int v = 0; v < 3; v++) {
        apv.add(mqoObjs.vertex[mqoObjs.face[f].V[v]]);
        // apv.add(new KGLPoint(mqoObjs.vertex[mqoObjs.face[f].V[v]])) ;
        s =
            (float)
                Math.acos(
                    fn.X() * vn[mqoObjs.face[f].V[v]].X()
                        + fn.Y() * vn[mqoObjs.face[f].V[v]].Y()
                        + fn.Z() * vn[mqoObjs.face[f].V[v]].Z());
        if (mqoObjs.data.facet < s) {
          apn.add(fn);
        } else {
          apn.add(vn[mqoObjs.face[f].V[v]]);
        }
        wpoint = new KGLPoint(2);
        if (mqoObjs.face[f].UV == null) {
          wpoint.set_UV(0, 0);
        } else {
          wpoint.set_UV(mqoObjs.face[f].UV[v * 2 + 0], mqoObjs.face[f].UV[v * 2 + 1]);
          uvValid = true;
        }
        apuv.add(wpoint);
        wpoint = new KGLPoint(4);
        if (mqoObjs.face[f].COL == null) {
          if (mqomat.data.col == null) {
            wpoint.set_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
          } else {
            wpoint.set_COLOR(
                mqomat.data.col[0], mqomat.data.col[1], mqomat.data.col[2], mqomat.data.col[3]);
          }
        } else {
          wpoint.set_COLOR(
              mqoObjs.face[f].COL[v * 4 + 0],
              mqoObjs.face[f].COL[v * 4 + 1],
              mqoObjs.face[f].COL[v * 4 + 2],
              mqoObjs.face[f].COL[v * 4 + 3]);
          colValid = true;
        }
        apc.add(wpoint);
      }
    }
    ret.texID = texPool.getGLTexture(gl, mqomat.data.tex, mqomat.data.aplane, false);
    // @@@ reload 用
    if (ret.texID != 0) {
      ret.texName = mqomat.data.tex;
      ret.alphaTexName = mqomat.data.aplane;
    } else {
      ret.texName = null;
      ret.alphaTexName = null;
    }

    if (apv.size() == 0) return null;
    uvValid &= (ret.texID != 0);
    ret.name = mqomat.name;
    // uvValid = false ;
    KGLPoint[] wfv = null;
    KGLPoint[] wfn = null;
    KGLPoint[] wft = null;
    KGLPoint[] wfc = null;
    wfv = apv.toArray(new KGLPoint[0]);
    wfn = apn.toArray(new KGLPoint[0]);
    wft = apuv.toArray(new KGLPoint[0]);
    wfc = apc.toArray(new KGLPoint[0]);
    ret.vertex_num = wfv.length;

    // @@@ interleaveFormat は無いので分ける
    ret.uvValid = uvValid;
    ret.colValid = colValid;

    ret.vertexBuffer = ByteBuffer.allocateDirect(ret.vertex_num * 3 * 4);
    ret.vertexBuffer.order(ByteOrder.nativeOrder());
    ret.vertexBuffer.position(0);

    ret.normalBuffer = ByteBuffer.allocateDirect(ret.vertex_num * 3 * 4);
    ret.normalBuffer.order(ByteOrder.nativeOrder());
    ret.normalBuffer.position(0);

    if (uvValid) {
      ret.uvBuffer = ByteBuffer.allocateDirect(ret.vertex_num * 2 * 4);
      ret.uvBuffer.order(ByteOrder.nativeOrder());
      ret.uvBuffer.position(0);
    }
    if (colValid) {
      ret.colBuffer = ByteBuffer.allocateDirect(ret.vertex_num * 4 * 4);
      ret.colBuffer.order(ByteOrder.nativeOrder());
      ret.colBuffer.position(0);
    }

    // Log.i("KGLMetaseq", "vertex_num: "+ ret.vertex_num);

    for (int v = 0; v < ret.vertex_num; v++) {
      ret.vertexBuffer.putFloat(wfv[v].X());
      ret.vertexBuffer.putFloat(wfv[v].Y());
      ret.vertexBuffer.putFloat(wfv[v].Z());

      ret.normalBuffer.putFloat(wfn[v].X());
      ret.normalBuffer.putFloat(wfn[v].Y());
      ret.normalBuffer.putFloat(wfn[v].Z());

      if (uvValid) {
        ret.uvBuffer.putFloat(wft[v].U());
        ret.uvBuffer.putFloat(wft[v].V());
      }
      if (colValid) {
        ret.colBuffer.putFloat(wfc[v].R());
        ret.colBuffer.putFloat(wfc[v].G());
        ret.colBuffer.putFloat(wfc[v].B());
        ret.colBuffer.putFloat(wfc[v].A());
      }
    }
    if (mqomat.data.col != null) {
      ret.color = new float[mqomat.data.col.length];
      for (int c = 0; c < mqomat.data.col.length; c++) {
        ret.color[c] = mqomat.data.col[c];
      }
      if (mqomat.data.dif != null) {
        ret.dif = new float[mqomat.data.col.length];
        for (int c = 0; c < mqomat.data.col.length; c++) {
          ret.dif[c] = mqomat.data.dif * mqomat.data.col[c];
        }
        // KEICHECK difでアルファ値を1未満にすると透明度が変化する?
        ret.dif[3] = mqomat.data.col[3];
      }
      if (mqomat.data.amb != null) {
        ret.amb = new float[mqomat.data.col.length];
        for (int c = 0; c < mqomat.data.col.length; c++) {
          ret.amb[c] = mqomat.data.amb * mqomat.data.col[c];
        }
      }
      if (mqomat.data.emi != null) {
        ret.emi = new float[mqomat.data.col.length];
        for (int c = 0; c < mqomat.data.col.length; c++) {
          ret.emi[c] = mqomat.data.emi * mqomat.data.col[c];
        }
      }
      if (mqomat.data.spc != null) {
        ret.spc = new float[mqomat.data.col.length];
        for (int c = 0; c < mqomat.data.col.length; c++) {
          ret.spc[c] = mqomat.data.spc * mqomat.data.col[c];
        }
      }
    }
    if (mqomat.data.pow != null) {
      ret.power = new float[1];
      ret.power[0] = mqomat.data.pow;
    }
    ret.shadeMode_IsSmooth = true; // defaultはtrue
    if (mqoObjs.data.shading == 0) ret.shadeMode_IsSmooth = false;

    return ret;
  }
 protected static CharBuffer newCharBuffer(int numElements) {
   ByteBuffer bb = ByteBuffer.allocateDirect((Character.SIZE / 8) * numElements);
   bb.order(ByteOrder.nativeOrder());
   return bb.asCharBuffer();
 }
Beispiel #9
0
 // sets the nio wrapped buffer (allows to be overridden for other use cases like cuda)
 protected void setNioBuffer() {
   wrappedBuffer = ByteBuffer.allocateDirect(elementSize * length);
   wrappedBuffer.order(ByteOrder.nativeOrder());
 }
Beispiel #10
0
  @SuppressWarnings("unchecked")
  public Mesh(LinkedHashMap mesh, User user) {

    this.mesh = mesh;
    this.user = user;

    try {
      title = getStringFromHash(mesh, "title", "Some Object");

      ArrayList<Float> vs = new ArrayList<Float>(256);
      ArrayList<Float> ns = new ArrayList<Float>(256);
      ArrayList<Float> tp = new ArrayList<Float>(256);

      LinkedList verts = getListFromHash(mesh, "vertices");
      for (Object vert : verts) {
        vs.add(getFloatFromList(vert, 0, 0f));
        vs.add(getFloatFromList(vert, 1, 0f));
        vs.add(getFloatFromList(vert, 2, 0f));
      }
      LinkedList norms = getListFromHash(mesh, "normals");
      for (Object norm : norms) {
        ns.add(getFloatFromList(norm, 0, 0f));
        ns.add(getFloatFromList(norm, 1, 0f));
        ns.add(getFloatFromList(norm, 2, 0f));
      }
      LinkedList texts = getListFromHash(mesh, "texturepoints");
      for (Object text : texts) {
        tp.add(getFloatFromList(text, 0, 0f));
        tp.add(getFloatFromList(text, 1, 0f));
      }
      ArrayList<Float> vnt = new ArrayList<Float>(1024);
      ArrayList<Short> ind = new ArrayList<Short>(1024);
      short index = 0;
      LinkedList faces = getListFromHash(mesh, "faces");
      for (Object face : faces) {
        for (int i = 0; i < 3; i++) {
          String f = getStringFromList(face, i, "1/1/2");
          StringTokenizer st = new StringTokenizer(f, "/");
          int fv = Integer.parseInt(st.nextToken()) - 1;
          int ft = Integer.parseInt(st.nextToken()) - 1;
          int fn = Integer.parseInt(st.nextToken()) - 1;

          vnt.add(vs.get(fv * 3));
          vnt.add(vs.get(fv * 3 + 1));
          vnt.add(vs.get(fv * 3 + 2));

          vnt.add(ns.get(fn * 3));
          vnt.add(ns.get(fn * 3 + 1));
          vnt.add(ns.get(fn * 3 + 2));

          vnt.add(tp.get(ft * 2));
          vnt.add(tp.get(ft * 2 + 1));

          ind.add(index++);
        }
      }
      vnt.trimToSize();
      float[] va = new float[vnt.size()];
      for (int i = 0; i < vnt.size(); i++) va[i] = vnt.get(i);

      ind.trimToSize();
      short[] ia = new short[ind.size()];
      for (int i = 0; i < ind.size(); i++) ia[i] = ind.get(i);

      vb = ByteBuffer.allocateDirect(va.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
      vb.put(va).position(0);

      ib = ByteBuffer.allocateDirect(ia.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
      ib.put(ia).position(0);

      il = ia.length;

      textures = getListFromHash(mesh, "textures");
      if (textures.size() == 0) textures = list("placeholder");
      vertexShader = user.shaders.get(getStringFromHash(mesh, "vertex-shader", ""));
      fragmentShader = user.shaders.get(getStringFromHash(mesh, "fragment-shader", ""));
      subObjects = getListFromHash(mesh, "sub-items");
      rotationX = getFloatFromList(getListFromHash(mesh, "rotation"), 0, 0f);
      rotationY = getFloatFromList(getListFromHash(mesh, "rotation"), 1, 0f);
      rotationZ = getFloatFromList(getListFromHash(mesh, "rotation"), 2, 0f);
      scaleX = getFloatFromList(getListFromHash(mesh, "scale"), 0, 1f);
      scaleY = getFloatFromList(getListFromHash(mesh, "scale"), 1, 1f);
      scaleZ = getFloatFromList(getListFromHash(mesh, "scale"), 2, 1f);
      lightR = getFloatFromList(getListFromHash(mesh, "light"), 0, 0f);
      lightG = getFloatFromList(getListFromHash(mesh, "light"), 1, 0f);
      lightB = getFloatFromList(getListFromHash(mesh, "light"), 2, 0f);

    } catch (Exception e) {
      e.printStackTrace();
      Log.e("Mesh Constructor", e.getLocalizedMessage());
      return;
    }
  }