Example usage for java.nio FloatBuffer get

List of usage examples for java.nio FloatBuffer get

Introduction

In this page you can find the example usage for java.nio FloatBuffer get.

Prototype

public abstract float get();

Source Link

Document

Returns the float at the current position and increases the position by 1.

Usage

From source file:org.deegree.tools.rendering.manager.buildings.PrototypeManager.java

/**
 * @param rqm/* w w  w  . j a v  a 2 s.  c o m*/
 */
private RenderableQualityModel createScaledQualityModel(RenderableQualityModel rqm) {
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float minZ = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    float maxZ = Float.MIN_VALUE;
    ArrayList<RenderableQualityModelPart> qualityModelParts = rqm.getQualityModelParts();
    for (RenderableQualityModelPart rqmp : qualityModelParts) {
        if (rqmp != null) {
            RenderableGeometry geom = (RenderableGeometry) rqmp;
            FloatBuffer fb = geom.getCoordBuffer();
            fb.rewind();
            int position = fb.position();
            while (position < fb.capacity()) {
                float x = fb.get();
                float y = fb.get();
                float z = fb.get();

                minX = Math.min(minX, x);
                minY = Math.min(minY, y);
                minZ = Math.min(minZ, z);

                maxX = Math.max(maxX, x);
                maxY = Math.max(maxY, y);
                maxZ = Math.max(maxZ, z);
                position = fb.position();
            }
        }
    }

    double scaleX = 1d / (maxX - minX);
    double scaleY = 1d / (maxY - minY);
    double scaleZ = 1d / (maxZ - minZ);

    for (RenderableQualityModelPart rqmp : qualityModelParts) {
        if (rqmp != null) {
            RenderableGeometry geom = (RenderableGeometry) rqmp;
            FloatBuffer fb = geom.getCoordBuffer();
            fb.rewind();
            int i = 0;
            while ((i + 3) <= fb.capacity()) {
                float x = fb.get(i);
                x -= minX;
                x *= scaleX;
                fb.put(i, x);

                float y = fb.get(i + 1);
                y -= minY;
                y *= scaleY;
                fb.put(i + 1, y);

                float z = fb.get(i + 2);
                z -= minZ;
                z *= scaleZ;
                fb.put(i + 2, z);
                i += 3;
            }
        }
    }
    return rqm;

}

From source file:org.shaman.terrain.vegetation.ImpositorCreator.java

public TreeInfo createTree(Biome biome, String treeName, int variation, float prob) throws IOException {
    LOG.info("create tree from " + treeName);

    File folder = new File(OUTPUT_FOLDER + treeName + "_v" + variation);
    if (!folder.exists() && !folder.mkdir()) {
        LOG.log(Level.SEVERE, "unable to make directory {0}", folder);
        return null;
    }//from   w w  w  . java  2 s .  co m

    //create tree
    Params params = new Params();
    params.prepare(13);
    params.clearParams();
    params.readFromXML(new FileInputStream(INPUT_FOLDER + treeName + ".xml"));
    params.prepare(rand.nextInt(Short.MAX_VALUE));
    TreeGenerator treeGenerator = TreeGeneratorFactory.createTreeGenerator(params);
    treeGenerator.setSeed(rand.nextInt(Short.MAX_VALUE));
    treeGenerator.setParam("Smooth", params.getParam("Smooth").toString());
    ExporterFactory.setRenderW(1024);
    ExporterFactory.setRenderH(1024);
    ExporterFactory.setExportFormat(-1);
    ExporterFactory.setOutputStemUVs(true);
    ExporterFactory.setOutputLeafUVs(true);
    Progress progress = new Progress();
    Tree treeData = treeGenerator.makeTree(progress);
    MeshGenerator meshGenerator = MeshGeneratorFactory.createMeshGenerator(/*params,*/ true);
    ArbaroToJmeExporter exporter = new ArbaroToJmeExporter(assetManager, treeData, meshGenerator);
    exporter.setBarkTexture("org/shaman/terrain/textures2/" + params.getParam("BarkTexture").getValue());
    exporter.setLeafTexture("org/shaman/terrain/textures2/" + params.getParam("LeafTexture").getValue());
    exporter.setLeafRotation(Float.parseFloat(params.getParam("LeafTextureRotation").getValue()));
    exporter.doWrite();
    Spatial tree = exporter.getSpatial();
    LOG.log(Level.INFO, "tree generated, vertices:{0}, triangles:{1}",
            new Object[] { tree.getVertexCount(), tree.getTriangleCount() });

    //save tree
    BinaryExporter binaryExporter = new BinaryExporter();
    binaryExporter.save(tree, new File(folder, "Tree.j3o"));

    //compute bounding cylinder
    List<Geometry> geometries = new ArrayList<>();
    listGeometries(tree, geometries);
    System.out.println("count of geometries: " + geometries.size());
    //compute bounding cylinder, assuming that the tree starts at the origin and grows in z-direction
    BoundingVolume oldBoundingVolume = tree.getWorldBound();
    System.out.println("original bounding volume: " + oldBoundingVolume);
    float radius = 0;
    float height = 0;
    Vector3f pos = new Vector3f();
    Vector3f pos2 = new Vector3f();
    for (Geometry geom : geometries) {
        Mesh mesh = geom.getMesh();
        Transform trafo = geom.getWorldTransform();
        VertexBuffer buffer = mesh.getBuffer(VertexBuffer.Type.Position);
        FloatBuffer fbuf = (FloatBuffer) buffer.getData();
        fbuf.rewind();
        for (int i = 0; i < buffer.getNumElements(); ++i) {
            pos.x = fbuf.get();
            pos.y = fbuf.get();
            pos.z = fbuf.get();
            pos2 = trafo.transformVector(pos, pos2);
            radius = Math.max(radius, pos2.x * pos2.x + pos2.y * pos2.y);
            height = Math.max(height, pos2.z);
        }
        fbuf.rewind();
    }
    radius = FastMath.sqrt(radius);
    System.out.println("cylinder radius: " + radius + ", height: " + height);

    //setup scene
    Node sceneNode = new Node("scene");
    sceneNode.attachChild(tree);
    DirectionalLight light = new DirectionalLight();
    Vector3f lightDir = new Vector3f(-0.1f, -0.1f, -0.1f);
    light.setDirection(lightDir.normalize());
    AmbientLight ambientLight = new AmbientLight(new ColorRGBA(0.6f, 0.6f, 0.6f, 1));
    sceneNode.addLight(ambientLight);
    sceneNode.setQueueBucket(RenderQueue.Bucket.Gui);
    for (Geometry geom : geometries) {
        geom.setQueueBucket(RenderQueue.Bucket.Gui);
        geom.getMaterial().setFloat("FadeNear", 2000);
        geom.getMaterial().setFloat("FadeFar", 3000);
    }
    //transform to match texture size
    Node sceneNode2 = new Node("scene2");
    sceneNode2.attachChild(sceneNode);
    sceneNode2.rotate(-FastMath.HALF_PI, 0, 0);
    float scale = TEXTURE_SIZE / Math.max(height, 2 * radius);
    sceneNode2.scale(scale);
    Node sceneNode3 = new Node("scene3");
    sceneNode3.attachChild(sceneNode2);
    sceneNode3.setLocalTranslation(TEXTURE_SIZE / 2, 0, 0);
    //create offscreen surface
    ByteBuffer data = BufferUtils.createByteBuffer(TEXTURE_SIZE * TEXTURE_SIZE * 4);
    BufferedImage image = new BufferedImage(TEXTURE_SIZE, TEXTURE_SIZE, BufferedImage.TYPE_4BYTE_ABGR);
    Camera camera = new Camera(TEXTURE_SIZE, TEXTURE_SIZE);
    camera.setParallelProjection(true);
    final ViewPort view = new ViewPort("Off", camera);
    view.setBackgroundColor(ColorRGBA.BlackNoAlpha);
    view.setClearFlags(true, true, true);
    final FrameBuffer buffer = new FrameBuffer(TEXTURE_SIZE, TEXTURE_SIZE, 1);
    buffer.setDepthBuffer(Image.Format.Depth);
    buffer.setColorBuffer(Image.Format.RGBA32F);
    view.setOutputFrameBuffer(buffer);
    view.attachScene(sceneNode3);
    sceneNode3.setCullHint(Spatial.CullHint.Never);
    sceneNode2.setCullHint(Spatial.CullHint.Never);
    sceneNode.setCullHint(Spatial.CullHint.Never);
    tree.setCullHint(Spatial.CullHint.Never);
    view.setEnabled(true);
    //render
    sceneNode.addLight(light);
    for (int i = 0; i < IMPOSITOR_COUNT; ++i) {
        sceneNode3.updateGeometricState();

        renderManager.renderViewPort(view, 0);
        renderer.readFrameBuffer(buffer, data);
        sceneNode.rotate(0, 0, FastMath.TWO_PI / IMPOSITOR_COUNT);
        Quaternion rot = new Quaternion();
        rot.fromAngles(0, 0, i * FastMath.TWO_PI / IMPOSITOR_COUNT);
        light.setDirection(rot.mult(lightDir).normalizeLocal());

        try {
            convertScreenShot(data, image);
            BufferedImage img = new BufferedImage(OUTPUT_TEXTURE_SIZE, OUTPUT_TEXTURE_SIZE,
                    BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D G = img.createGraphics();
            G.drawImage(image, 0, 0, OUTPUT_TEXTURE_SIZE, OUTPUT_TEXTURE_SIZE, null);
            G.dispose();
            ImageIO.write(img, "png", new File(folder, i + ".png"));
        } catch (IOException ex) {
            Logger.getLogger(ImpositorCreator.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    view.clearScenes();

    //create tree info
    TreeInfo info = new TreeInfo();
    info.biome = biome;
    info.name = treeName + "_v" + variation;
    info.treeSize = height;
    info.probability = prob;
    info.impostorCount = IMPOSITOR_COUNT;
    info.impostorFadeNear = 30;
    info.impostorFadeFar = 50;
    info.highResStemFadeNear = 30;
    info.highResStemFadeFar = 50;
    info.highResLeavesFadeNear = 35;
    info.highResLeavesFadeFar = 55;

    System.out.println("impostors created");
    Runtime.getRuntime().gc();
    assetManager.clearCache();

    return info;
}

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();
    // 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:gephi.spade.panel.fcsFile.java

/**
 * readFloatData ---/*  www . j  a  v a2s . co  m*/
 * <p>
 * Reads floating point values in list mode in the DATA segment and updates
 * eventList with the integer values of the values.
 * </p>
 *
 * @param data
 *            <code>ByteBuffer</code> containing the DATA segment of the
 *            underlying file.
 */
private void readFloatData(ByteBuffer data) {
    // Allocate the eventList
    eventList = new double[parameters][totalEvents];

    if (littleEndianP) {
        data.order(ByteOrder.LITTLE_ENDIAN);
    }

    // Convert the byte buffer into a float buffer - doesn't get any easier
    FloatBuffer fb = data.asFloatBuffer();

    final int totalEvents = this.totalEvents;
    final int parameters = this.parameters;

    for (int i = 0; i < totalEvents; i++) {
        for (int j = 0; j < parameters; j++) {
            // Store the value into the array
            eventList[j][i] = fb.get();
        }
    }
}