Example usage for java.nio FloatBuffer position

List of usage examples for java.nio FloatBuffer position

Introduction

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

Prototype

public final Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

From source file:Main.java

public static FloatBuffer float2Buffer(float[] a) {
    FloatBuffer floatBuffer;
    ByteBuffer bb = ByteBuffer.allocateDirect(a.length * 4);
    bb.order(ByteOrder.nativeOrder());
    floatBuffer = bb.asFloatBuffer();//  w  w  w.java 2  s . c o  m
    floatBuffer.put(a);
    floatBuffer.position(0);
    return floatBuffer;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(float[] triangleCoords) {
    FloatBuffer vertexbuffer;
    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);// 9*4
    bb.order(ByteOrder.nativeOrder());
    vertexbuffer = bb.asFloatBuffer();/*from   ww w .  j  a va  2 s . c o m*/
    vertexbuffer.put(triangleCoords);
    vertexbuffer.position(0);
    return vertexbuffer;
}

From source file:Main.java

public static FloatBuffer setupFloatBuffer(FloatBuffer preBuffer, float[] array) {

    if (preBuffer == null || preBuffer.capacity() < array.length) {
        preBuffer = createFloatBuffer(array.length * 2);
    } else {//from   w w w  .  ja v  a2s. co  m
        preBuffer.clear();
    }
    preBuffer.put(array);
    preBuffer.position(0);
    return preBuffer;
}

From source file:ivorius.ivtoolkit.rendering.grid.GridQuadCache.java

protected static <T> GridQuadCache<T> createQuadCacheGreedy(int[] size, float[] scale,
        Function<Pair<BlockCoord, ForgeDirection>, T> mapper) {
    Map<QuadContext<T>, CoordGrid> partialCache = new HashMap<>();

    for (int x = 0; x < size[0]; x++)
        for (int y = 0; y < size[1]; y++)
            for (int z = 0; z < size[2]; z++) {
                BlockCoord coord = new BlockCoord(x, y, z);
                addToCache(partialCache, mapper, UP, coord);
                addToCache(partialCache, mapper, DOWN, coord);
                addToCache(partialCache, mapper, NORTH, coord);
                addToCache(partialCache, mapper, EAST, coord);
                addToCache(partialCache, mapper, SOUTH, coord);
                addToCache(partialCache, mapper, WEST, coord);
            }/* w  ww.j  a  v  a  2 s  .c  o m*/

    Set<Map.Entry<QuadContext<T>, CoordGrid>> quads = partialCache.entrySet();
    GridQuadCache<T> cache = new GridQuadCache<>();
    cache.size = new float[3];
    for (int i = 0; i < 3; i++)
        cache.size[i] = size[i] * scale[i];

    for (Map.Entry<QuadContext<T>, CoordGrid> entry : quads) {
        QuadContext<T> context = entry.getKey();

        int[] sAxes = getCacheAxes(context.direction, size);
        float[] scAxes = getCacheAxes(context.direction, scale);

        QuadCollection mesh = entry.getValue().computeMesh(0, 0, sAxes[1], sAxes[2]);
        FloatBuffer cachedQuadCoords = BufferUtils.createFloatBuffer(mesh.quadCount() * 4);

        float pxAxis = scAxes[1];
        float pzAxis = scAxes[2];

        for (int i = 0; i < mesh.quadCount(); i++) {
            cachedQuadCoords.put(mesh.x1(i) * pxAxis).put(mesh.y1(i) * pzAxis).put((mesh.x2(i) + 1) * pxAxis)
                    .put((mesh.y2(i) + 1) * pzAxis);
        }
        cachedQuadCoords.position(0);

        float zLevel;
        zLevel = (context.direction.offsetX + context.direction.offsetY + context.direction.offsetZ > 0
                ? context.layer + 1
                : context.layer) * scAxes[0];

        cache.cachedQuadLevels
                .add(new CachedQuadLevel<>(zLevel, context.direction, context.t, cachedQuadCoords));
    }

    return cache;
}

From source file:com.alvermont.terraj.fracplanet.render.TriangleMeshViewerDisplay.java

/**
 * Called to display a frame. This carries out the rendering of the scene
 * using the drawable object./*from   w ww. j  a  v  a 2  s.c o m*/
 *
 * @param gLAutoDrawable The GL drawable object that is to be used for
 * rendering
 */
public void display(GLAutoDrawable gLAutoDrawable) {
    final GL2 gl = (GL2) gLAutoDrawable.getGL();
    final GLU glu = new GLU();

    if (parameters.isEnableFog()) {
        // clear to fog background colour
        final FloatRGBA fogCol = parameters.getFogColour();

        final float[] colBuffer = new float[3];

        colBuffer[0] = fogCol.getR();
        colBuffer[1] = fogCol.getG();
        colBuffer[2] = fogCol.getB();

        gl.glClearColor(colBuffer[0], colBuffer[1], colBuffer[2], 1.0f);

        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        gl.glEnable(GL2.GL_FOG);

        gl.glFogi(GL2.GL_FOG_MODE, GL.GL_LINEAR);

        gl.glFogfv(GL2.GL_FOG_COLOR, colBuffer, 0);
        gl.glFogf(GL2.GL_FOG_DENSITY, 0.35f);
        gl.glFogf(GL2.GL_FOG_START, 0.0f);
        gl.glFogf(GL2.GL_FOG_END, parameters.getFogDistance());
    } else {
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        gl.glDisable(GL2.GL_FOG);

        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    }

    // set up ambient light
    final float a = parameters.getAmbient();

    final float[] globalAmbient = { a, a, a, 1.0f };
    gl.glLightModelfv(GL2ES1.GL_LIGHT_MODEL_AMBIENT, globalAmbient, 0);

    final float[] lightDiffuse = { 1.0f - a, 1.0f - a, 1.0f - a, 1.0f };
    gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_DIFFUSE, lightDiffuse, 0);

    // position and orient the camera
    getCamera().moveLeft(cameraMotion.getX());
    getCamera().moveUp(cameraMotion.getY());
    getCamera().moveBackward(cameraMotion.getZ());

    camRotZ += cameraRotation.getZ();

    getCamera().rotateEye(cameraRotation.getX(), cameraRotation.getY());
    getCamera().rotateAxis(camRotZ, 0.0f, 0.0f, 1.0f);

    //log.debug(camRotZ);
    getCamera().lookAtScene();

    //        float lightPosition[] = {-2.0f, -3.0f, 1.0f, 0.0f };
    //        gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition, 0);

    // set up sunlight
    final XYZ sunpos = parameters.getSunPosition();

    final float[] lightPosition = new float[4];

    lightPosition[0] = sunpos.getX();
    lightPosition[1] = sunpos.getY();
    lightPosition[2] = sunpos.getZ();

    gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, lightPosition, 0);

    final FloatRGBA lightColour = parameters.getSunColour();

    lightPosition[0] = lightColour.getR();
    lightPosition[1] = lightColour.getG();
    lightPosition[2] = lightColour.getB();
    lightPosition[0] = 1.0f;

    gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_DIFFUSE, lightPosition, 0);
    gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_SPECULAR, lightPosition, 0);

    objectRotation += objectRotationSpeed;

    objectRotation %= 360.0f;

    gl.glRotatef(objectTilt, 0.0f, 1.0f, 0.0f);
    gl.glRotatef(objectRotation, 0.0f, 0.0f, 1.0f);

    int drawMode = GL2GL3.GL_FILL;

    if (parameters.isWireframe()) {
        drawMode = GL2GL3.GL_LINE;
    }

    // end of mods
    gl.glPolygonMode(gl.GL_FRONT_AND_BACK, drawMode);

    gl.glEnable(gl.GL_CULL_FACE);
    gl.glFrontFace(gl.GL_CCW);

    if (parameters.isDisplayList() && (displayListIndex != 0)) {
        gl.glCallList(displayListIndex);
    } else {
        final boolean buildingList = parameters.isDisplayList() && (displayListIndex == 0);

        if (buildingList) {
            displayListIndex = gl.glGenLists(1);
            gl.glNewList(displayListIndex, GL2.GL_COMPILE);

            if (log.isDebugEnabled()) {
                log.debug("Building display list");
            }
        }

        final float[] defaultMaterialWhite = { 1.0f, 1.0f, 1.0f };
        final float[] defaultMaterialBlack = { 0.0f, 0.0f, 0.0f };

        gl.glMaterialfv(GL.GL_FRONT, GLLightingFunc.GL_AMBIENT_AND_DIFFUSE, defaultMaterialWhite, 0);
        gl.glMaterialfv(GL.GL_FRONT, GLLightingFunc.GL_EMISSION, defaultMaterialBlack, 0);

        boolean first = true;

        for (TriangleMesh mesh : meshes) {
            // Meshes after the first are rendered twice: first the backfacing polys then the front facing.
            // This solves the problem of either clouds disappearing when we're under them (with backface culling)
            // or weird stuff around the periphery when culling is on.
            // It's quite an expensive solution!
            final int passes = (first ? 1 : 2);

            for (int pass = 0; pass < passes; pass++) {
                if (passes == 2 && pass == 0) {
                    gl.glCullFace(gl.GL_FRONT);
                } else {
                    gl.glCullFace(gl.GL_BACK);
                }

                if (mesh.getEmissive() == 0.0) {
                    // Switch blending on for non-emissive meshes after the first
                    if (!first) {
                        gl.glEnable(gl.GL_BLEND);
                        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
                    } else {
                        gl.glDisable(gl.GL_BLEND);
                    }

                    // Use "Color Material" mode 'cos everything is the same
                    // material.... just change the colour
                    gl.glEnable(GLLightingFunc.GL_COLOR_MATERIAL);
                    gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GLLightingFunc.GL_AMBIENT_AND_DIFFUSE);

                    // Point GL at arrays of data
                    final FloatBuffer vertBuffer = mesh.getVertices().getPositionBuffer();
                    vertBuffer.position(0);

                    gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertBuffer);

                    final FloatBuffer normBuffer = mesh.getVertices().getNormalBuffer();
                    normBuffer.position(0);

                    gl.glNormalPointer(GL.GL_FLOAT, 0, normBuffer);

                    final Buffer colBuffer = mesh.getVertices().getColourBuffer();
                    colBuffer.position(0);

                    if (first) {
                        gl.glColorPointer(3, GL.GL_BYTE, 8, colBuffer);
                    } else {
                        gl.glColorPointer(4, GL.GL_BYTE, 8, colBuffer);
                    }

                    // Draw the colour-zero triangles
                    final Buffer triBuffer = mesh.getTriangles().getBuffer();

                    triBuffer.position(0);

                    gl.glDrawElements(GL.GL_TRIANGLES, 3 * mesh.getTriangleColour0Count(), GL.GL_UNSIGNED_INT,
                            triBuffer);

                    // Switch to alternate colour and draw the colour-one triangles
                    colBuffer.position(4);

                    if (first) {
                        gl.glColorPointer(3, GL.GL_BYTE, 8, colBuffer);
                    } else {
                        gl.glColorPointer(4, GL.GL_BYTE, 8, colBuffer);
                    }

                    triBuffer.position(mesh.getTriangleColour0Count() * 3);

                    gl.glDrawElements(GL.GL_TRIANGLES, 3 * mesh.getTriangleColour1Count(), GL.GL_UNSIGNED_INT,
                            triBuffer);

                    gl.glDisable(GLLightingFunc.GL_COLOR_MATERIAL);
                } else {
                    // We abuse alpha for emission, so no blending
                    gl.glDisable(gl.GL_BLEND);

                    // If there could be emissive vertices, we need to do things the hard way.
                    final float k = 1.0f / 255.0f;
                    final float em = k * (mesh.getEmissive());
                    final float ad = k * (1.0f - mesh.getEmissive());

                    gl.glBegin(GL.GL_TRIANGLES);

                    final float[] cAmbDiff = new float[3];
                    final float[] cEmissive = new float[3];

                    for (int t = 0; t < mesh.getTriangles().size(); ++t) {
                        int c = 0;

                        if (t >= mesh.getTriangleColour0Count()) {
                            c = 1;
                        }

                        for (int i = 0; i < 3; ++i) {
                            final int vn = mesh.getTriangles().get(t).getVertex(i);
                            final Vertex v = mesh.getVertices().get(vn);

                            final ByteRGBA col = v.getColour(c);

                            if (v.getEmissive(c)) {
                                cAmbDiff[0] = col.getR() * ad;
                                cAmbDiff[1] = col.getG() * ad;
                                cAmbDiff[2] = col.getB() * ad;
                                cEmissive[0] = col.getR() * em;
                                cEmissive[1] = col.getG() * em;
                                cEmissive[2] = col.getB() * em;
                            } else {
                                cAmbDiff[0] = col.getR() * k;
                                cAmbDiff[1] = col.getG() * k;
                                cAmbDiff[2] = col.getB() * k;
                                cEmissive[0] = 0.0f;
                                cEmissive[1] = 0.0f;
                                cEmissive[2] = 0.0f;
                            }

                            gl.glNormal3f(v.getNormal().getX(), v.getNormal().getY(), v.getNormal().getZ());
                            gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GLLightingFunc.GL_AMBIENT_AND_DIFFUSE,
                                    cAmbDiff, 0);
                            gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GLLightingFunc.GL_EMISSION, cEmissive, 0);
                            gl.glVertex3f(v.getPosition().getX(), v.getPosition().getY(),
                                    v.getPosition().getZ());
                        }
                    }

                    gl.glEnd();
                }
            }

            first = false;
        }

        if (buildingList) {
            gl.glEndList();

            if (log.isDebugEnabled()) {
                log.debug("... built display list");
            }
        }
    }

    gl.glFlush();
}