Example usage for java.nio ByteBuffer asFloatBuffer

List of usage examples for java.nio ByteBuffer asFloatBuffer

Introduction

In this page you can find the example usage for java.nio ByteBuffer asFloatBuffer.

Prototype

public abstract FloatBuffer asFloatBuffer();

Source Link

Document

Returns a float buffer which is based on the remaining content of this byte buffer.

Usage

From source file:org.apache.druid.query.aggregation.HistogramAggregatorFactory.java

@Override
public byte[] getCacheKey() {
    byte[] fieldNameBytes = StringUtils.toUtf8(fieldName);
    ByteBuffer buf = ByteBuffer.allocate(1 + fieldNameBytes.length + Float.BYTES * breaks.length)
            .put(AggregatorUtil.HIST_CACHE_TYPE_ID).put(fieldNameBytes).put((byte) 0xFF);
    buf.asFloatBuffer().put(breaks);

    return buf.array();
}

From source file:com.kentdisplays.synccardboarddemo.Page.java

/**
 * Sets up the drawing object data for use in an OpenGL ES context.
 *
 * @param is InputStream to the page to load the path data from.
 *//*w w  w .j  av a 2s  .c om*/
public Page(InputStream is, int glProgram, int direction) {

    this.mModel = new float[16];
    this.mGlProgram = glProgram;

    // Calculate the coordinates from the given path.
    ArrayList<Path> paths = pathsFromSamplePageInputStream(is);
    float finalCoords[] = {};
    float finalNormals[] = {};
    float finalColors[] = {};
    mNumberOfPaths = paths.size();
    for (int i = 0; i < mNumberOfPaths; i++) {
        Path path = paths.get(i);
        float x1 = (path.x1 / 13942 * 2) - 1;
        float y1 = (path.y1 / 20280 * 2) - 1;
        float x2 = (path.x2 / 13942 * 2) - 1;
        float y2 = (path.y2 / 20280 * 2) - 1;
        float width = path.width / 3000;
        width = width < 0.013f ? 0.013f : width; // Width should be at least 0.013

        float distance = (float) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        float angle = (float) Math.PI / 2 - (float) Math.asin((x2 - x1) / distance);
        float xdiff = (width / 2) * (float) Math.sin(angle);
        float ydiff = (width / 2) * (float) Math.cos(angle);

        float coords[] = { x1 - xdiff, y1 - ydiff, 1.0f, // top left
                x2 - xdiff, y2 - ydiff, 1.0f, // bottom left
                x1 + xdiff, y1 + ydiff, 1.0f, // top right
                x2 - xdiff, y2 - ydiff, 1.0f, // bottom left
                x2 + xdiff, y2 + ydiff, 1.0f, // bottom right
                x1 + xdiff, y1 + ydiff, 1.0f, // top right
        };

        float normals[] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
                1.0f, 0.0f, 0.0f, 1.0f, };

        float colors[] = { 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f,
                0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f,
                0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, };

        finalCoords = Floats.concat(finalCoords, coords);
        finalNormals = Floats.concat(finalNormals, normals);
        finalColors = Floats.concat(finalColors, colors);
    }

    ByteBuffer bbVertices = ByteBuffer.allocateDirect(finalCoords.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    mPageVertices = bbVertices.asFloatBuffer();
    mPageVertices.put(finalCoords);
    mPageVertices.position(0);

    ByteBuffer bbNormals = ByteBuffer.allocateDirect(finalNormals.length * 4);
    bbNormals.order(ByteOrder.nativeOrder());
    mPageNormals = bbNormals.asFloatBuffer();
    mPageNormals.put(finalNormals);
    mPageNormals.position(0);

    ByteBuffer bbColors = ByteBuffer.allocateDirect(finalColors.length * 4);
    bbColors.order(ByteOrder.nativeOrder());
    mPageColors = bbColors.asFloatBuffer();
    mPageColors.put(finalColors);
    mPageColors.position(0);

    // Correctly place the page in the world.
    Matrix.setIdentityM(mModel, 0);
    switch (direction) {
    case 0:
        Matrix.translateM(mModel, 0, 0, 0, -mDistance); //Front.
        break;
    case 1:
        Matrix.translateM(mModel, 0, -mDistance, 0, 0); // Left.
        Matrix.rotateM(mModel, 0, 90, 0, 1f, 0);
        break;
    case 2:
        Matrix.translateM(mModel, 0, 0, 0, mDistance); // Behind.
        Matrix.rotateM(mModel, 0, 180, 0, 1f, 0);
        break;
    case 3:
        Matrix.translateM(mModel, 0, mDistance, 0, 0); // Right.
        Matrix.rotateM(mModel, 0, 270, 0, 1f, 0);
        break;
    }
}

From source file:com.kentdisplays.synccardboarddemo.MainActivity.java

/**
 * Creates the buffers we use to store information about the 3D world. OpenGL doesn't use Java
 * arrays, but rather needs data in a format it can understand. Hence we use ByteBuffers.
 * @param config The EGL configuration used when creating the surface.
 */// ww  w  .  j av  a 2  s .  c o  m
@Override
public void onSurfaceCreated(EGLConfig config) {
    Log.i(TAG, "onSurfaceCreated");

    // make a floor
    ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(DATA.FLOOR_COORDS.length * 4);
    bbFloorVertices.order(ByteOrder.nativeOrder());
    mFloorVertices = bbFloorVertices.asFloatBuffer();
    mFloorVertices.put(DATA.FLOOR_COORDS);
    mFloorVertices.position(0);

    ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(DATA.FLOOR_NORMALS.length * 4);
    bbFloorNormals.order(ByteOrder.nativeOrder());
    mFloorNormals = bbFloorNormals.asFloatBuffer();
    mFloorNormals.put(DATA.FLOOR_NORMALS);
    mFloorNormals.position(0);

    ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(DATA.FLOOR_COLORS.length * 4);
    bbFloorColors.order(ByteOrder.nativeOrder());
    mFloorColors = bbFloorColors.asFloatBuffer();
    mFloorColors.put(DATA.FLOOR_COLORS);
    mFloorColors.position(0);

    int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, R.raw.light_vertex);
    int gridShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.grid_fragment);

    mGlProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mGlProgram, vertexShader);
    GLES20.glAttachShader(mGlProgram, gridShader);
    GLES20.glLinkProgram(mGlProgram);

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    Matrix.setIdentityM(mModelFloor, 0);
    Matrix.translateM(mModelFloor, 0, 0, -mFloorDepth, 0); // Floor appears below user

    // Create the placeholder pages.
    mPages = new Page[4];
    mPages[0] = new Page(getResources().openRawResource(R.raw.boogie_board), mGlProgram, 0);
    mPages[1] = new Page(getResources().openRawResource(R.raw.house), mGlProgram, 1);
    mPages[2] = new Page(getResources().openRawResource(R.raw.placeholder), mGlProgram, 2);
    mPages[3] = new Page(getResources().openRawResource(R.raw.cylinder), mGlProgram, 3);

    checkGLError("onSurfaceCreated");
}

From source file:org.bimserver.collada.ColladaSerializer.java

private void printMatrix(PrintWriter out, GeometryInfo geometryInfo) {
    ByteBuffer transformation = ByteBuffer.wrap(geometryInfo.getTransformation());
    transformation.order(ByteOrder.LITTLE_ENDIAN);
    FloatBuffer floatBuffer = transformation.asFloatBuffer();
    // Prepare to create the transform matrix.
    float[] matrix = new float[16];
    // Add the first 16 values of the buffer.
    for (int i = 0; i < matrix.length; i++)
        matrix[i] = floatBuffer.get();//from   w w w.  j a v  a 2 s .  co  m
    // Switch from column-major (x.x ... x.y ... x.z ... 0 ...) to row-major orientation (x.x x.y x.z 0 ...)?
    matrix = Matrix.changeOrientation(matrix);
    // List all 16 elements of the matrix as a single space-delimited String object.
    out.println("    <matrix>" + floatArrayToSpaceDelimitedString(matrix) + "</matrix>");
}

From source file:rb.app.GLObject.java

public void allocateBuffer() {
    int SHORT_MAX = 250000;
    int FLOAT_MAX = 1000000;

    Log.d("GLRenderer", "Allocate (short):" + SHORT_MAX * 2 + " bytes");
    ByteBuffer vbb = ByteBuffer.allocateDirect(SHORT_MAX * 2);
    vbb.order(ByteOrder.nativeOrder());
    _shortBuffer = vbb.asShortBuffer();/*  w w w  . j a  va  2  s . co  m*/
    _shortBuffer.position(0);

    Log.d("GLRenderer", "Allocate (float):" + FLOAT_MAX * 4 + " bytes");
    ByteBuffer fbb = ByteBuffer.allocateDirect(FLOAT_MAX * 4);
    fbb.order(ByteOrder.nativeOrder());
    _floatBuffer = fbb.asFloatBuffer();
    _floatBuffer.position(0);
}

From source file:org.bimserver.GeometryGenerator.java

private void setTransformationMatrix(GeometryInfo geometryInfo, float[] transformationMatrix) {
    ByteBuffer byteBuffer = ByteBuffer.allocate(16 * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer asFloatBuffer = byteBuffer.asFloatBuffer();
    for (float f : transformationMatrix) {
        asFloatBuffer.put(f);//from   w  ww. ja  v a2s  .  co m
    }
    geometryInfo.setTransformation(byteBuffer.array());
}

From source file:org.bimserver.GeometryGenerator.java

private byte[] floatArrayToByteArray(float[] vertices) {
    if (vertices == null) {
        return null;
    }/*w  ww  .  j  a va2  s .  c om*/
    ByteBuffer buffer = ByteBuffer.wrap(new byte[vertices.length * 4]);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    FloatBuffer asFloatBuffer = buffer.asFloatBuffer();
    for (float f : vertices) {
        asFloatBuffer.put(f);
    }
    return buffer.array();
}

From source file:com.sveder.cardboardpassthrough.MainActivity.java

/**
 * Creates the buffers we use to store information about the 3D world. OpenGL doesn't use Java
 * arrays, but rather needs data in a format it can understand. Hence we use ByteBuffers.
 * @param config The EGL configuration used when creating the surface.
 *//*from   ww w  .  j a va2  s .  c o m*/
@Override
public void onSurfaceCreated(EGLConfig config) {
    Log.i(TAG, "onSurfaceCreated");
    GLES20.glClearColor(0.1f, 0.1f, 0.1f, 0.5f); // Dark background so text shows up well

    ByteBuffer bb = ByteBuffer.allocateDirect(squareVertices.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareVertices);
    vertexBuffer.position(0);

    ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    ByteBuffer bb2 = ByteBuffer.allocateDirect(textureVertices.length * 4);
    bb2.order(ByteOrder.nativeOrder());
    textureVerticesBuffer = bb2.asFloatBuffer();
    textureVerticesBuffer.put(textureVertices);
    textureVerticesBuffer.position(0);

    int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
    GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);

    texture = createTexture();
    startCamera(texture);

    //        ByteBuffer bbVertices = ByteBuffer.allocateDirect(DATA.CUBE_COORDS.length * 4);
    //        bbVertices.order(ByteOrder.nativeOrder());
    //        mCubeVertices = bbVertices.asFloatBuffer();
    //        mCubeVertices.put(DATA.CUBE_COORDS);
    //        mCubeVertices.position(0);
    //
    //        ByteBuffer bbColors = ByteBuffer.allocateDirect(DATA.CUBE_COLORS.length * 4);
    //        bbColors.order(ByteOrder.nativeOrder());
    //        mCubeColors = bbColors.asFloatBuffer();
    //        mCubeColors.put(DATA.CUBE_COLORS);
    //        mCubeColors.position(0);
    //
    //        ByteBuffer bbFoundColors = ByteBuffer.allocateDirect(DATA.CUBE_FOUND_COLORS.length * 4);
    //        bbFoundColors.order(ByteOrder.nativeOrder());
    //        mCubeFoundColors = bbFoundColors.asFloatBuffer();
    //        mCubeFoundColors.put(DATA.CUBE_FOUND_COLORS);
    //        mCubeFoundColors.position(0);
    //
    //        ByteBuffer bbNormals = ByteBuffer.allocateDirect(DATA.CUBE_NORMALS.length * 4);
    //        bbNormals.order(ByteOrder.nativeOrder());
    //        mCubeNormals = bbNormals.asFloatBuffer();
    //        mCubeNormals.put(DATA.CUBE_NORMALS);
    //        mCubeNormals.position(0);
    //
    //        // make a floor
    //        ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(DATA.FLOOR_COORDS.length * 4);
    //        bbFloorVertices.order(ByteOrder.nativeOrder());
    //        mFloorVertices = bbFloorVertices.asFloatBuffer();
    //        mFloorVertices.put(DATA.FLOOR_COORDS);
    //        mFloorVertices.position(0);
    //
    //        ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(DATA.FLOOR_NORMALS.length * 4);
    //        bbFloorNormals.order(ByteOrder.nativeOrder());
    //        mFloorNormals = bbFloorNormals.asFloatBuffer();
    //        mFloorNormals.put(DATA.FLOOR_NORMALS);
    //        mFloorNormals.position(0);
    //
    //        ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(DATA.FLOOR_COLORS.length * 4);
    //        bbFloorColors.order(ByteOrder.nativeOrder());
    //        mFloorColors = bbFloorColors.asFloatBuffer();
    //        mFloorColors.put(DATA.FLOOR_COLORS);
    //        mFloorColors.position(0);
    //
    //        int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, R.raw.light_vertex);
    //        int gridShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.grid_fragment);
    //
    //        mGlProgram = GLES20.glCreateProgram();
    //        GLES20.glAttachShader(mGlProgram, vertexShader);
    //        GLES20.glAttachShader(mGlProgram, gridShader);
    //        GLES20.glLinkProgram(mGlProgram);
    //
    //        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    //
    //        // Object first appears directly in front of user
    //        Matrix.setIdentityM(mModelCube, 0);
    //        Matrix.translateM(mModelCube, 0, 0, 0, -mObjectDistance);
    //
    //        Matrix.setIdentityM(mModelFloor, 0);
    //        Matrix.translateM(mModelFloor, 0, 0, -mFloorDepth, 0); // Floor appears below user
    //
    //        checkGLError("onSurfaceCreated");
}

From source file:com.tumblr.cardboard.Tumblr3DActivity.java

/**
 * Creates the buffers we use to store information about the 3D world. OpenGL doesn't use Java
 * arrays, but rather needs data in a format it can understand. Hence we use ByteBuffers.
 *
 * @param config The EGL configuration used when creating the surface.
 */// w ww .  j  av  a  2  s  .  c  o  m
@Override
public void onSurfaceCreated(EGLConfig config) {
    Log.i(TAG, "onSurfaceCreated");
    GLES20.glClearColor(0.1f, 0.1f, 0.1f, 0.5f); // Dark background so text shows up well

    ByteBuffer bbVertices = ByteBuffer.allocateDirect(WorldLayoutData.RECT_COORDS.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    mRectVertices = bbVertices.asFloatBuffer();
    mRectVertices.put(WorldLayoutData.RECT_COORDS);
    mRectVertices.position(0);

    ByteBuffer bbColors = ByteBuffer.allocateDirect(WorldLayoutData.RECT_COLORS.length * 4);
    bbColors.order(ByteOrder.nativeOrder());
    mRectColors = bbColors.asFloatBuffer();
    mRectColors.put(WorldLayoutData.RECT_COLORS);
    mRectColors.position(0);

    ByteBuffer bbFoundColors = ByteBuffer.allocateDirect(WorldLayoutData.RECT_FOUND_COLORS.length * 4);
    bbFoundColors.order(ByteOrder.nativeOrder());
    mRectFoundColors = bbFoundColors.asFloatBuffer();
    mRectFoundColors.put(WorldLayoutData.RECT_FOUND_COLORS);
    mRectFoundColors.position(0);

    ByteBuffer bbNormals = ByteBuffer.allocateDirect(WorldLayoutData.RECT_NORMALS.length * 4);
    bbNormals.order(ByteOrder.nativeOrder());
    mRectNormals = bbNormals.asFloatBuffer();
    mRectNormals.put(WorldLayoutData.RECT_NORMALS);
    mRectNormals.position(0);

    ByteBuffer bbTextureCoordinates = ByteBuffer.allocateDirect(WorldLayoutData.RECT_TEX_COORDS.length * 4);
    bbTextureCoordinates.order(ByteOrder.nativeOrder());
    mRectTexCoords = bbTextureCoordinates.asFloatBuffer();
    mRectTexCoords.put(WorldLayoutData.RECT_TEX_COORDS);
    mRectTexCoords.position(0);

    // make a floor
    ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_COORDS.length * 4);
    bbFloorVertices.order(ByteOrder.nativeOrder());
    mFloorVertices = bbFloorVertices.asFloatBuffer();
    mFloorVertices.put(WorldLayoutData.FLOOR_COORDS);
    mFloorVertices.position(0);

    ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_NORMALS.length * 4);
    bbFloorNormals.order(ByteOrder.nativeOrder());
    mFloorNormals = bbFloorNormals.asFloatBuffer();
    mFloorNormals.put(WorldLayoutData.FLOOR_NORMALS);
    mFloorNormals.position(0);

    ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_COLORS.length * 4);
    bbFloorColors.order(ByteOrder.nativeOrder());
    mFloorColors = bbFloorColors.asFloatBuffer();
    mFloorColors.put(WorldLayoutData.FLOOR_COLORS);
    mFloorColors.position(0);

    int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, R.raw.light_vertex);
    int gridShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.flat_fragment);

    mGlProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mGlProgram, vertexShader);
    GLES20.glAttachShader(mGlProgram, gridShader);
    GLES20.glLinkProgram(mGlProgram);

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    Matrix.setIdentityM(mModelFloor, 0);
    Matrix.translateM(mModelFloor, 0, 0, -FLOOR_DEPTH, 0); // Floor appears below user

    checkGLError("onSurfaceCreated");
}

From source file:com.aimfire.gallery.cardboard.PhotoActivity.java

/**
 * Draws a frame for an eye.// w  ww.ja  va  2 s . c  o m
 *
 * @param eye The eye to render. Includes all required transformations.
 */
@Override
public void onDrawEye(Eye eye) {
    if (mAssetInd == -1) {
        // we are still showing instruction, return without doing anything
        return;
    }

    if (!mAssetChangedLeft && !mAssetChangedRight) {
        // nothing changed, do nothing and return
        return;
    }

    if (eye.getType() == Eye.Type.LEFT)
        mAssetChangedLeft = false;
    else if (eye.getType() == Eye.Type.RIGHT)
        mAssetChangedRight = false;

    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    checkGLError("mColorParam");

    GLES20.glUseProgram(mPicProgram);

    GLES20.glUniform1f(mDimRatioParam, mDimRatio);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    if (eye.getType() == Eye.Type.LEFT) {
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureCurr[0]);
    } else {
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureCurr[1]);
    }

    // set the zoom level
    GLES20.glUniform1f(mZoomParam, sZoom[mImgZoomInd]);

    /*
     * if user prefers negative parallax, shift window on left frame leftward and right frame
     * rightward. if user prefers positive parallax, do the opposite
     */
    if (eye.getType() == Eye.Type.LEFT) {
        GLES20.glUniform1f(mParallaxParam, mImgParallaxAdj / 2.0f);
    } else {
        GLES20.glUniform1f(mParallaxParam, -mImgParallaxAdj / 2.0f);
    }

    // Set the position of the picture
    //float zoomCoords[] = new float[picCoords.length];
    //for(int i=0; i<picCoords.length; i++)
    //zoomCoords[i] = picCoords[i] * zoom[zoomInd];

    //ByteBuffer bblVertices = ByteBuffer.allocateDirect(zoomCoords.length * 4);
    ByteBuffer bblVertices = ByteBuffer.allocateDirect(picCoords.length * 4);
    bblVertices.order(ByteOrder.nativeOrder());
    mPicVertices = bblVertices.asFloatBuffer();
    //mPicVertices.put(zoomCoords);
    mPicVertices.put(picCoords);
    mPicVertices.position(0);

    GLES20.glVertexAttribPointer(mPicPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride,
            mPicVertices);

    GLES20.glDrawElements(GLES20.GL_TRIANGLES, /* mode */
            6, /* count */
            GLES20.GL_UNSIGNED_SHORT, /* type */
            mPicElements /* element array buffer offset */
    );
}