Exemplo n.º 1
0
 /*
  * (non-Javadoc)
  *
  * @see org.lwjgl.vector.Vector#load(FloatBuffer)
  */
 public Vector load(FloatBuffer buf) {
   x = buf.get();
   y = buf.get();
   z = buf.get();
   w = buf.get();
   return this;
 }
Exemplo n.º 2
0
  public static void updateRenderInfo(EntityPlayer par0EntityPlayer, boolean par1) {
    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);

    // Spout start
    modelMatrix = modelview.duplicate();
    projectionMatrix = projection.duplicate();
    // Spout end

    GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
    float var2 = (float) ((viewport.get(0) + viewport.get(2)) / 2);
    float var3 = (float) ((viewport.get(1) + viewport.get(3)) / 2);
    GLU.gluUnProject(var2, var3, 0.0F, modelview, projection, viewport, objectCoords);
    objectX = objectCoords.get(0);
    objectY = objectCoords.get(1);
    objectZ = objectCoords.get(2);
    int var4 = par1 ? 1 : 0;
    float var5 = par0EntityPlayer.rotationPitch;
    float var6 = par0EntityPlayer.rotationYaw;
    rotationX = MathHelper.cos(var6 * (float) Math.PI / 180.0F) * (float) (1 - var4 * 2);
    rotationZ = MathHelper.sin(var6 * (float) Math.PI / 180.0F) * (float) (1 - var4 * 2);
    rotationYZ =
        -rotationZ * MathHelper.sin(var5 * (float) Math.PI / 180.0F) * (float) (1 - var4 * 2);
    rotationXY =
        rotationX * MathHelper.sin(var5 * (float) Math.PI / 180.0F) * (float) (1 - var4 * 2);
    rotationXZ = MathHelper.cos(var5 * (float) Math.PI / 180.0F);
  }
Exemplo n.º 3
0
  /**
   * Normalizes weights if needed and finds largest amount of weights used for all vertices in the
   * buffer.
   *
   * @param vertCount amount of vertices
   * @param weightsFloatData weights for vertices
   */
  private int endBoneAssigns(int vertCount, FloatBuffer weightsFloatData) {
    int maxWeightsPerVert = 0;
    weightsFloatData.rewind();
    for (int v = 0; v < vertCount; ++v) {
      float w0 = weightsFloatData.get(),
          w1 = weightsFloatData.get(),
          w2 = weightsFloatData.get(),
          w3 = weightsFloatData.get();

      if (w3 != 0) {
        maxWeightsPerVert = Math.max(maxWeightsPerVert, 4);
      } else if (w2 != 0) {
        maxWeightsPerVert = Math.max(maxWeightsPerVert, 3);
      } else if (w1 != 0) {
        maxWeightsPerVert = Math.max(maxWeightsPerVert, 2);
      } else if (w0 != 0) {
        maxWeightsPerVert = Math.max(maxWeightsPerVert, 1);
      }

      float sum = w0 + w1 + w2 + w3;
      if (sum != 1f && sum != 0.0f) {
        weightsFloatData.position(weightsFloatData.position() - 4);
        // compute new vals based on sum
        float sumToB = 1f / sum;
        weightsFloatData.put(w0 * sumToB);
        weightsFloatData.put(w1 * sumToB);
        weightsFloatData.put(w2 * sumToB);
        weightsFloatData.put(w3 * sumToB);
      }
    }
    weightsFloatData.rewind();

    // mesh.setMaxNumWeights(maxWeightsPerVert);
    return maxWeightsPerVert;
  }
Exemplo n.º 4
0
  public synchronized DataPoint[] readDataPoints(int ID) throws IOException {
    final Long currentOffset = dataPointsOffsets.get(ID);
    final Integer numOfDataPoints = dataPointsLengths.get(ID);

    if ((currentOffset == null) || (numOfDataPoints == null)) {
      throw new IllegalArgumentException("Unknown storage ID " + ID);
    }

    final int numOfBytes = numOfDataPoints * 2 * 4;

    if (buffer.capacity() < numOfBytes) {
      buffer = ByteBuffer.allocate(numOfBytes * 2);
    } else {
      buffer.clear();
    }

    dataPointsFile.seek(currentOffset);
    dataPointsFile.read(buffer.array(), 0, numOfBytes);

    FloatBuffer floatBuffer = buffer.asFloatBuffer();

    DataPoint dataPoints[] = new DataPoint[numOfDataPoints];

    for (int i = 0; i < numOfDataPoints; i++) {
      float mz = floatBuffer.get();
      float intensity = floatBuffer.get();
      dataPoints[i] = new SimpleDataPoint(mz, intensity);
    }

    return dataPoints;
  }
  public void draw() {

    g.clearColor(0);
    g.clear();
    _myShaderTexture.beginDraw();

    g.clearColor(255, 0, 0);
    g.clear();
    g.color(255);
    CCMath.randomSeed(0);
    for (int i = 0; i < 200; i++) {
      g.color(CCMath.random(), CCMath.random(), CCMath.random());
      g.ellipse(CCMath.random(400), CCMath.random(400), 0, 20, 20);
    }
    g.rect(-200, -200, 50, 50);
    _myShaderTexture.endDraw();

    FloatBuffer outputData0 = _myShaderTexture.getData(0);
    System.err.printf("toutput0\toutput1\toutput2\toutput3\n");
    for (int i = 0; i < _myShaderTexture.width() * _myShaderTexture.height() * 3; i++)
      System.err.printf(
          "t%.2f\t%.2f\t%.2f\n", outputData0.get(), outputData0.get(), outputData0.get());

    g.color(255);
    g.image(_myShaderTexture.attachment(0), 0, 0, 200, 200);
    //		g.texture(_myRenderBuffer);
    //		g.beginShape(CCDrawMode.QUADS);
    //		g.vertex(-200, -200, 0, 0f);
    //		g.vertex( 200, -200, 1, 0f);
    //		g.vertex( 200,  200, 1, 1);
    //		g.vertex(-200,  200, 0, 1);
    //		g.endShape();
    //		g.noTexture();

  }
Exemplo n.º 6
0
  /**
   * @param object1
   * @param object2
   * @throws Exception
   */
  private void init(SurfaceTriangleList object1, SurfaceTriangleList object2) throws Exception {

    // bruteforce for now
    FloatBuffer ver1 = object1.getTriangleBuffer();
    FloatBuffer ver2 = object2.getTriangleBuffer();

    int cnt1 = object1.getChunkCount();
    int cnt2 = object2.getChunkCount();

    float[] t1 = new float[9];
    float[] t2 = new float[9];
    ver1.rewind();
    ver2.rewind();

    Boolean intersect = false;
    Boolean coplanar = false;
    float[] segment;
    for (int i = 0; i < cnt1; i++) {
      ver1.get(t1);
      for (int j = 0; j < cnt2; j++) {
        ver2.get(t2);
        segment = Collision.triTriIntersect(t1, t2, coplanar, intersect);

        if (coplanar) { // TODO: handle coplanearity
          throw new Exception("coplanearity not yet supported");
        } else if (intersect) {
          tris.add(segment, true);
        }
      }
    }
  }
Exemplo n.º 7
0
  @Override
  public boolean gluUnProject(
      float winX,
      float winY,
      float winZ,
      float[] model,
      int modelOffset,
      float[] project,
      int projectOffset,
      int[] view,
      int viewOffset,
      float[] obj,
      int objOffset) {
    modelb.clear();
    modelb.put(model, modelOffset, 16);
    projectb.clear();
    projectb.put(project, projectOffset, 16);
    viewb.clear();
    viewb.put(view, viewOffset, 4);
    winb.clear();

    boolean result =
        org.lwjgl.util.glu.GLU.gluUnProject(winX, winY, winZ, modelb, projectb, viewb, winb);
    obj[objOffset] = winb.get(0);
    obj[objOffset + 1] = winb.get(1);
    obj[objOffset + 2] = winb.get(2);
    return result;
  }
Exemplo n.º 8
0
  private double interpolate_value(
      final UnstructuredVolumeObject volume, final int index0, final int index1)
      throws KVSException {
    Buffer buf = volume.values();
    float[] coords = volume.coords();

    final float value0 = this.substitute_plane_equation(new Vector3f(coords, 3 * index0));
    final float value1 = this.substitute_plane_equation(new Vector3f(coords, 3 * index1));
    final float ratio = kvs.core.util.Math.abs(value0 / (value1 - value0));

    if (buf instanceof ByteBuffer) {
      ByteBuffer values = (ByteBuffer) buf;
      return (values.get(index0) + ratio * (values.get(index1) - values.get(index0)));
    } else if (buf instanceof ShortBuffer) {
      ShortBuffer values = (ShortBuffer) buf;
      return (values.get(index0) + ratio * (values.get(index1) - values.get(index0)));
    } else if (buf instanceof IntBuffer) {
      IntBuffer values = (IntBuffer) buf;
      return (values.get(index0) + ratio * (values.get(index1) - values.get(index0)));
    } else if (buf instanceof LongBuffer) {
      LongBuffer values = (LongBuffer) buf;
      return (values.get(index0) + ratio * (values.get(index1) - values.get(index0)));
    } else if (buf instanceof FloatBuffer) {
      FloatBuffer values = (FloatBuffer) buf;
      return (values.get(index0) + ratio * (values.get(index1) - values.get(index0)));
    } else if (buf instanceof DoubleBuffer) {
      DoubleBuffer values = (DoubleBuffer) buf;
      return (values.get(index0) + ratio * (values.get(index1) - values.get(index0)));
    } else if (buf instanceof CharBuffer) {
      CharBuffer values = (CharBuffer) buf;
      return (values.get(index0) + ratio * (values.get(index1) - values.get(index0)));
    } else {
      throw new KVSException("Unsupported data type");
    }
  }
Exemplo n.º 9
0
  /**
   * Load from a float buffer. The buffer stores the matrix in column major (OpenGL) order.
   *
   * @param buf A float buffer to read from
   * @return this
   */
  public Matrix load(FloatBuffer buf) {

    m00 = buf.get();
    m01 = buf.get();
    m10 = buf.get();
    m11 = buf.get();

    return this;
  }
Exemplo n.º 10
0
  public static Vec3 unproject(float winX, float winY, float winZ) {
    GLU.gluUnProject(winX, winY, winZ, modelview, projection, viewport, objectCoords);

    float objectX = objectCoords.get(0);
    float objectY = objectCoords.get(1);
    float objectZ = objectCoords.get(2);

    return Vec3.createVectorHelper(objectX, objectY, objectZ);
  }
Exemplo n.º 11
0
  /**
   * Computes a <code>Box</code> that bounds a specified buffer of points. Principal axes are
   * computed for the points and used to form a <code>Box</code>. This returns <code>null</code> if
   * the buffer is empty or contains only a partial point.
   *
   * <p>The buffer must contain XYZ coordinate tuples which are either tightly packed or offset by
   * the specified stride. The stride specifies the number of buffer elements between the first
   * coordinate of consecutive tuples. For example, a stride of 3 specifies that each tuple is
   * tightly packed as XYZXYZXYZ, whereas a stride of 5 specifies that there are two elements
   * between each tuple as XYZabXYZab (the elements "a" and "b" are ignored). The stride must be at
   * least 3. If the buffer's length is not evenly divisible into stride-sized tuples, this ignores
   * the remaining elements that follow the last complete tuple.
   *
   * @param buffer the buffer containing the point coordinates for which to compute a bounding
   *     volume.
   * @param stride the number of elements between the first coordinate of consecutive points. If
   *     stride is 3, this interprets the buffer has having tightly packed XYZ coordinate tuples.
   * @return the bounding volume, with axes lengths consistent with the conventions described in the
   *     <code>Box</code> class overview.
   * @throws IllegalArgumentException if the buffer is null or empty, or if the stride is less than
   *     three.
   */
  public static Box computeBoundingBox(FloatBuffer buffer, int stride) {
    if (buffer == null) {
      String msg = Logging.getMessage("nullValue.BufferIsNull");
      Logging.error(msg);
      throw new IllegalArgumentException(msg);
    }

    if (stride < 3) {
      String msg = Logging.getMessage("generic.StrideIsInvalid", stride);
      Logging.error(msg);
      throw new IllegalArgumentException(msg);
    }

    Vec4[] axes = WWMath.computePrincipalAxes(buffer, stride);
    if (axes == null) {
      String msg = Logging.getMessage("generic.BufferIsEmpty");
      Logging.error(msg);
      throw new IllegalArgumentException(msg);
    }

    Vec4 r = axes[0];
    Vec4 s = axes[1];
    Vec4 t = axes[2];

    // Find the extremes along each axis.
    double minDotR = Double.MAX_VALUE;
    double maxDotR = -minDotR;
    double minDotS = Double.MAX_VALUE;
    double maxDotS = -minDotS;
    double minDotT = Double.MAX_VALUE;
    double maxDotT = -minDotT;

    for (int i = buffer.position(); i <= buffer.limit() - stride; i += stride) {
      double x = buffer.get(i);
      double y = buffer.get(i + 1);
      double z = buffer.get(i + 2);

      double pdr = x * r.x + y * r.y + z * r.z;
      if (pdr < minDotR) minDotR = pdr;
      if (pdr > maxDotR) maxDotR = pdr;

      double pds = x * s.x + y * s.y + z * s.z;
      if (pds < minDotS) minDotS = pds;
      if (pds > maxDotS) maxDotS = pds;

      double pdt = x * t.x + y * t.y + z * t.z;
      if (pdt < minDotT) minDotT = pdt;
      if (pdt > maxDotT) maxDotT = pdt;
    }

    if (maxDotR == minDotR) maxDotR = minDotR + 1;
    if (maxDotS == minDotS) maxDotS = minDotS + 1;
    if (maxDotT == minDotT) maxDotT = minDotT + 1;

    return new Box(axes, minDotR, maxDotR, minDotS, maxDotS, minDotT, maxDotT);
  }
Exemplo n.º 12
0
  /** GL_DrawParticles */
  void GL_DrawParticles(int num_particles) {
    float origin_x, origin_y, origin_z;

    Math3D.VectorScale(vup, 1.5f, up);
    Math3D.VectorScale(vright, 1.5f, right);

    GL_Bind(r_particletexture.texnum);
    GL11.glDepthMask(false); // no z buffering
    GL11.glEnable(GL11.GL_BLEND);
    GL_TexEnv(GL11.GL_MODULATE);

    GL11.glBegin(GL11.GL_TRIANGLES);

    FloatBuffer sourceVertices = particle_t.vertexArray;
    IntBuffer sourceColors = particle_t.colorArray;
    float scale;
    int color;
    for (int j = 0, i = 0; i < num_particles; i++) {
      origin_x = sourceVertices.get(j++);
      origin_y = sourceVertices.get(j++);
      origin_z = sourceVertices.get(j++);

      // hack a scale up to keep particles from disapearing
      scale =
          (origin_x - r_origin[0]) * vpn[0]
              + (origin_y - r_origin[1]) * vpn[1]
              + (origin_z - r_origin[2]) * vpn[2];

      scale = (scale < 20) ? 1 : 1 + scale * 0.004f;

      color = sourceColors.get(i);

      GL11.glColor4ub(
          (byte) ((color) & 0xFF),
          (byte) ((color >> 8) & 0xFF),
          (byte) ((color >> 16) & 0xFF),
          (byte) ((color >>> 24)));
      // first vertex
      GL11.glTexCoord2f(0.0625f, 0.0625f);
      GL11.glVertex3f(origin_x, origin_y, origin_z);
      // second vertex
      GL11.glTexCoord2f(1.0625f, 0.0625f);
      GL11.glVertex3f(origin_x + up[0] * scale, origin_y + up[1] * scale, origin_z + up[2] * scale);
      // third vertex
      GL11.glTexCoord2f(0.0625f, 1.0625f);
      GL11.glVertex3f(
          origin_x + right[0] * scale, origin_y + right[1] * scale, origin_z + right[2] * scale);
    }
    GL11.glEnd();

    GL11.glDisable(GL11.GL_BLEND);
    GL11.glColor4f(1, 1, 1, 1);
    GL11.glDepthMask(true); // back to normal Z buffering
    GL_TexEnv(GL11.GL_REPLACE);
  }
  public static void generate(TriMesh mesh) {

    FloatBuffer tangents = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 3);
    FloatBuffer binormals = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 3);

    IntBuffer indexBuffer = mesh.getIndexBuffer();
    FloatBuffer vertexBuffer = mesh.getVertexBuffer();
    FloatBuffer textureBuffer = mesh.getTextureCoords(0).coords;
    indexBuffer.rewind();

    Vector3f tangent = new Vector3f();
    Vector3f binormal = new Vector3f();
    Vector3f normal = new Vector3f();
    Vector3f verts[] = new Vector3f[3];
    Vector2f texcoords[] = new Vector2f[3];

    for (int i = 0; i < 3; i++) {
      verts[i] = new Vector3f();
      texcoords[i] = new Vector2f();
    }

    for (int t = 0; t < indexBuffer.capacity() / 3; t++) {

      int index[] = new int[3];

      for (int v = 0; v < 3; v++) {
        index[v] = indexBuffer.get();
        verts[v].x = vertexBuffer.get(index[v] * 3);
        verts[v].y = vertexBuffer.get(index[v] * 3 + 1);
        verts[v].z = vertexBuffer.get(index[v] * 3 + 2);

        texcoords[v].x = textureBuffer.get(index[v] * 2);
        texcoords[v].y = textureBuffer.get(index[v] * 2 + 1);
      }

      computeTriangleTangentSpace(tangent, binormal, normal, verts, texcoords);

      for (int v = 0; v < 3; v++) {
        tangents.position(index[v] * 3);
        tangents.put(tangent.x);
        tangents.put(tangent.y);
        tangents.put(tangent.z);

        binormals.position(index[v] * 3);
        binormals.put(binormal.x);
        binormals.put(binormal.y);
        binormals.put(binormal.z);
      }
    }

    mesh.setTangentBuffer(tangents);
    mesh.setBinormalBuffer(binormals);
  }
Exemplo n.º 14
0
  /**
   * Sets the x, y, and z components of this vector to the values supplied by the <code>source
   * </code> buffer.
   *
   * @param source The buffer which supplies the values of this vector. The values are expected to
   *     be stored in XYZ order.
   * @throws IllegalArgumentException If the <code>source</code> buffer does not have enough
   *     elements remaining for all components of this vector.
   */
  public final void setComponents(final FloatBuffer source) {
    // Make sure the source contains enough components:
    if (source.remaining() < Vector3d.COMPONENT_COUNT) {
      throw new IllegalArgumentException(
          "The specified source buffer does not contain enough components.");
    }

    // The components are stored in XYZ order.
    this.x = source.get();
    this.y = source.get();
    this.z = source.get();
  }
Exemplo n.º 15
0
  public static float[] getAmountTranslated() {
    float translated[] = new float[3];

    FloatBuffer buffer = BufferUtils.createFloatBuffer(4 * 4);

    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, buffer);

    translated[0] = buffer.get(0 + 3 * 4);
    translated[1] = buffer.get(1 + 3 * 4);
    translated[2] = buffer.get(2 + 3 * 4);

    return translated;
  }
  public void byte2float(byte[] src, int index_src, float[] dst, int index_dst, int len) {

    if (len > (BUFFER_SIZE / 4)) {
      FloatBuffer buffer = ByteBuffer.wrap(src, index_src, len * 4).order(order).asFloatBuffer();
      buffer.get(dst, index_dst, len);
    } else {
      byteBuffer.clear();
      byteBuffer.put(src, index_src, len * 4);

      floatBuffer.position(0).limit(len);
      floatBuffer.get(dst, index_dst, len);
    }
  }
Exemplo n.º 17
0
 /**
  * Initializes this quaternion from a 4x4 column rotation matrix
  *
  * <p>See <a href="ftp://ftp.cis.upenn.edu/pub/graphics/shoemake/quatut.ps.Z">Graphics Gems
  * Code</a>,<br>
  * <a href="http://mathworld.wolfram.com/MatrixTrace.html">MatrixTrace</a>.
  *
  * <p>Buggy <a
  * href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q55">Matrix-FAQ
  * Q55</a>
  *
  * @param m 4x4 column matrix
  * @return this quaternion for chaining.
  * @see #toMatrix(FloatBuffer)
  */
 public final Quaternion setFromMatrix(final FloatBuffer m) {
   final int m_off = m.position();
   return setFromMatrix(
       m.get(0 + 0 * 4 + m_off),
       m.get(0 + 1 * 4 + m_off),
       m.get(0 + 2 * 4 + m_off),
       m.get(1 + 0 * 4 + m_off),
       m.get(1 + 1 * 4 + m_off),
       m.get(1 + 2 * 4 + m_off),
       m.get(2 + 0 * 4 + m_off),
       m.get(2 + 1 * 4 + m_off),
       m.get(2 + 2 * 4 + m_off));
 }
Exemplo n.º 18
0
  public static float[] getAmountScaled() {
    float scaled[] = new float[3];

    FloatBuffer buffer = BufferUtils.createFloatBuffer(4 * 4);

    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, buffer);

    scaled[0] = buffer.get(0);
    scaled[1] = buffer.get(1 + 1 * 4);
    scaled[2] = buffer.get(2 + 2 * 4);

    return scaled;
  }
Exemplo n.º 19
0
  private void doTransforms(
      FloatBuffer bindBufPos,
      FloatBuffer bindBufNorm,
      FloatBuffer bufPos,
      FloatBuffer bufNorm,
      int start,
      int end,
      Matrix4f transform) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;
    Vector3f norm = vars.vect2;

    int length = (end - start) * 3;

    // offset is given in element units
    // convert to be in component units
    int offset = start * 3;
    bindBufPos.rewind();
    bindBufNorm.rewind();
    // bufPos.position(offset);
    // bufNorm.position(offset);
    bindBufPos.get(tmpFloat, 0, length);
    bindBufNorm.get(tmpFloatN, 0, length);
    int index = 0;
    while (index < length) {
      pos.x = tmpFloat[index];
      norm.x = tmpFloatN[index++];
      pos.y = tmpFloat[index];
      norm.y = tmpFloatN[index++];
      pos.z = tmpFloat[index];
      norm.z = tmpFloatN[index];

      transform.mult(pos, pos);
      transform.multNormal(norm, norm);

      index -= 2;
      tmpFloat[index] = pos.x;
      tmpFloatN[index++] = norm.x;
      tmpFloat[index] = pos.y;
      tmpFloatN[index++] = norm.y;
      tmpFloat[index] = pos.z;
      tmpFloatN[index++] = norm.z;
    }
    vars.release();
    bufPos.position(offset);
    // using bulk put as it's faster
    bufPos.put(tmpFloat, 0, length);
    bufNorm.position(offset);
    // using bulk put as it's faster
    bufNorm.put(tmpFloatN, 0, length);
  }
Exemplo n.º 20
0
  @SubscribeEvent
  @SideOnly(Side.CLIENT)
  public void renderLast(RenderWorldLastEvent evt) {
    // TODO: while the urbanist is deactivated, this code can be dormant.
    // it happens to be very expensive at run time, so we need some way
    // to operate it only when relevant (e.g. in the cycle following a
    // click request).
    if (NONRELEASED_BLOCKS) {
      return;
    }

    /**
     * Note (SpaceToad): Why on earth this thing eventually worked out is a mystery to me. In
     * particular, all the examples I got computed y in a different way. Anyone with further OpenGL
     * understanding would be welcome to explain.
     *
     * <p>Anyway, the purpose of this code is to store the block position pointed by the mouse at
     * each frame, relative to the entity that has the camera.
     *
     * <p>It got heavily inspire from the two following sources:
     * http://nehe.gamedev.net/article/using_gluunproject/16013/ #ActiveRenderInfo.updateRenderInfo.
     *
     * <p>See EntityUrbanist#rayTraceMouse for a usage example.
     */
    if (modelviewF == null) {
      modelviewF = GLAllocation.createDirectFloatBuffer(16);
      projectionF = GLAllocation.createDirectFloatBuffer(16);
      viewport = GLAllocation.createDirectIntBuffer(16);
    }

    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelviewF);
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionF);
    GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
    float f = (viewport.get(0) + viewport.get(2)) / 2;
    float f1 = (viewport.get(1) + viewport.get(3)) / 2;

    float x = Mouse.getX();
    float y = Mouse.getY();

    // TODO: Minecraft seems to instist to have this winZ re-created at
    // each frame - looks like a memory leak to me but I couldn't use a
    // static variable instead, as for the rest.
    FloatBuffer winZ = GLAllocation.createDirectFloatBuffer(1);
    GL11.glReadPixels((int) x, (int) y, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);

    GLU.gluUnProject(x, y, winZ.get(), modelviewF, projectionF, viewport, pos);

    diffX = pos.get(0);
    diffY = pos.get(1);
    diffZ = pos.get(2);
  }
Exemplo n.º 21
0
  /**
   * Gets all x,y coordinates, that have the lowest z-value, therefore making an appropriate measure
   * for a 3D->2D conversion
   *
   * @param verticesBuffer the vertices buffer
   * @return the coordinates
   */
  private List<Vector2f> getCoordinates(FloatBuffer verticesBuffer) {

    // get Coordinates into an array
    float[] vertices = new float[verticesBuffer.capacity()];

    for (int i = 0; i < verticesBuffer.capacity(); i++) {
      vertices[i] = verticesBuffer.get(i);
    }
    // read closest points based on their z-value

    // find smallest z-value
    float z = Float.MAX_VALUE;
    for (int i = 2; i < vertices.length; i += 3) {
      if (vertices[i] < z) {
        z = vertices[i];
      }
    }

    // add all points with a z-value that are equals
    Vector2f vertex;
    List<Vector2f> finalList = new ArrayList<Vector2f>();
    for (int i = 2; i < vertices.length; i += 9) {
      if (vertices[i] == z) {
        vertex = new Vector2f();
        vertex.x = vertices[i - 2];
        vertex.y = vertices[i - 1];
        finalList.add(vertex);
      }
    }

    return finalList;
  }
Exemplo n.º 22
0
  void ParametricEval(
      final float u,
      final float v,
      final int offsetP,
      final FloatBuffer p,
      final int offsetN,
      final FloatBuffer N) {
    final float theta = (float) Math.PI * u, phi = (float) (2.0 * Math.PI * v);
    P.put(offsetP + 0, (float) (Math.sin(theta) * Math.sin(phi)));
    P.put(offsetP + 1, (float) (Math.sin(theta) * Math.cos(phi)));
    P.put(offsetP + 2, (float) Math.cos(theta));

    N.put(offsetN + 0, P.get(offsetP + 0));
    N.put(offsetN + 1, P.get(offsetP + 1));
    N.put(offsetN + 2, P.get(offsetP + 2));
  }
Exemplo n.º 23
0
  @Override
  public void filter(FloatBuffer[] _in, long[] inPos, FloatBuffer[] out) {
    if (_in.length != 1) {
      throw new IllegalArgumentException("Channel split invoked on more then one input");
    }
    if (out.length != format.getChannels()) {
      throw new IllegalArgumentException(
          "Channel split must be supplied with "
              + format.getChannels()
              + " output buffers to hold the channels.");
    }

    FloatBuffer in0 = _in[0];

    int outSampleCount = in0.remaining() / out.length;
    for (int i = 0; i < out.length; i++) {
      if (out[i].remaining() < outSampleCount)
        throw new IllegalArgumentException(
            "Supplied buffer for "
                + i
                + "th channel doesn't have sufficient space to put the samples ( required: "
                + outSampleCount
                + ", actual: "
                + out[i].remaining()
                + ")");
    }

    while (in0.remaining() >= format.getChannels()) {
      for (int i = 0; i < out.length; i++) {
        out[i].put(in0.get());
      }
    }
  }
Exemplo n.º 24
0
  public static void main(String args[]) throws Exception {
    FloatBuffer buffer = FloatBuffer.allocate(10);

    for (int i = 0; i < buffer.capacity(); ++i) {
      float f = (float) Math.sin((((float) i) / 10) * (2 * Math.PI));
      buffer.put(f);
    }

    /** flip()源码: */
    //		public final Buffer flip() {
    //        	limit = position;
    //        	position = 0;
    //        	mark = -1;
    //        	return this;
    //    	}
    /**
     * 反转缓冲区。首先将限制设置为当前位置,然后将位置设置为 0。 如果已定义了标记,则丢弃该标记。 常与compact方法一起使用。 通常情况下,在准备从缓冲区中读取数据时调用flip方法。
     */
    buffer.flip();

    while (buffer.hasRemaining()) {
      float f = buffer.get();
      System.out.println(f);
    }
  }
Exemplo n.º 25
0
 /**
  * Receive as many items as possible from the given byte buffer to this buffer.
  *
  * <p>The <TT>receiveItems()</TT> method must not block the calling thread; if it does, all
  * message I/O in MP will be blocked.
  *
  * @param i Index of first item to receive, in the range 0 .. <TT>length</TT>-1.
  * @param num Maximum number of items to receive.
  * @param buffer Byte buffer.
  * @return Number of items received.
  */
 protected int receiveItems(int i, int num, ByteBuffer buffer) {
   FloatBuffer floatbuffer = buffer.asFloatBuffer();
   int n = 0;
   int r = i / myColCount;
   int row = r + myLowerRow;
   int c = i % myColCount;
   int col = c + myLowerCol;
   int ncols = Math.min(myColCount - c, floatbuffer.remaining());
   while (r < myRowCount && ncols > 0) {
     float[] myMatrix_row = myMatrix[row];
     while (c < ncols) {
       myMatrix_row[col] = myOp.op(myMatrix_row[col], floatbuffer.get());
       ++c;
       ++col;
     }
     n += ncols;
     ++r;
     ++row;
     c = 0;
     col = myLowerCol;
     ncols = Math.min(myColCount, floatbuffer.remaining());
   }
   buffer.position(buffer.position() + 4 * n);
   return n;
 }
Exemplo n.º 26
0
  /** Ensures that the specified FloatBuffer is null-terminated. */
  public static FloatBuffer checkNT(FloatBuffer buf) {
    checkBuffer(buf, 1);
    if (buf.get(buf.limit() - 1) != 0.0f)
      throw new IllegalArgumentException("Missing null termination");

    return buf;
  }
Exemplo n.º 27
0
 public static void main(String[] args) {
   ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'});
   bb.rewind();
   printnb("Byte Buffer ");
   while (bb.hasRemaining()) printnb(bb.position() + " -> " + bb.get() + ", ");
   print();
   CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
   printnb("Char Buffer ");
   while (cb.hasRemaining()) printnb(cb.position() + " -> " + cb.get() + ", ");
   print();
   FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
   printnb("Float Buffer ");
   while (fb.hasRemaining()) printnb(fb.position() + " -> " + fb.get() + ", ");
   print();
   IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
   printnb("Int Buffer ");
   while (ib.hasRemaining()) printnb(ib.position() + " -> " + ib.get() + ", ");
   print();
   LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
   printnb("Long Buffer ");
   while (lb.hasRemaining()) printnb(lb.position() + " -> " + lb.get() + ", ");
   print();
   ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
   printnb("Short Buffer ");
   while (sb.hasRemaining()) printnb(sb.position() + " -> " + sb.get() + ", ");
   print();
   DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
   printnb("Double Buffer ");
   while (db.hasRemaining()) printnb(db.position() + " -> " + db.get() + ", ");
 }
Exemplo n.º 28
0
  public static void main(String[] args) {
    Mat pFrame = imread("image0.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat cFrame = imread("image1.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat pGray = new Mat();
    Mat cGray = new Mat();

    pFrame.convertTo(pGray, CV_32FC1);
    cFrame.convertTo(cGray, CV_32FC1);
    Mat Optical_Flow = new Mat();

    DenseOpticalFlow tvl1 = createOptFlow_DualTVL1();
    tvl1.calc(pGray, cGray, Optical_Flow);

    Mat OF = new Mat(pGray.rows(), pGray.cols(), CV_32FC1);
    FloatBuffer in = Optical_Flow.getFloatBuffer();
    FloatBuffer out = OF.getFloatBuffer();

    int height = pGray.rows();
    int width = pGray.cols();

    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        float xVelocity = in.get();
        float yVelocity = in.get();
        float pixelVelocity = (float) Math.sqrt(xVelocity * xVelocity + yVelocity * yVelocity);
        out.put(pixelVelocity);
      }
    }
    imwrite("OF.png", OF);
  }
Exemplo n.º 29
0
  /*
   * Reads FLOAT image data organized as separate image planes.
   *
   */
  public float[][] readPlanarFloat32(
      int width,
      int height,
      int samplesPerPixel,
      long[] stripOffsets,
      long[] stripCounts,
      long rowsPerStrip)
      throws IOException {
    float[][] data = new float[samplesPerPixel][width * height];
    int band = 0;
    int numRows = 0;

    ByteBuffer buff = ByteBuffer.allocateDirect(width * height * FLOAT_SIZEOF);
    buff.order(this.getByteOrder());

    for (int i = 0; i < stripOffsets.length; i++) {
      this.theChannel.position(stripOffsets[i]);
      int len = (int) stripCounts[i];
      if ((buff.position() + len) >= data[band].length * FLOAT_SIZEOF)
        len = data[band].length * FLOAT_SIZEOF - buff.position();
      buff.limit(buff.position() + len);
      this.theChannel.read(buff);
      numRows += rowsPerStrip;
      if (numRows >= height) {
        buff.flip();
        FloatBuffer fbuff = buff.asFloatBuffer();
        fbuff.get(data[band]);
        buff.clear();
        ++band;
        numRows = 0;
      }
    }

    return data;
  }
Exemplo n.º 30
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);
      }
    }
  }