Example usage for java.nio Buffer position

List of usage examples for java.nio Buffer position

Introduction

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

Prototype

int position

To view the source code for java.nio Buffer position.

Click Source Link

Document

The current position of this buffer.

Usage

From source file:Main.java

private static void println(Buffer buffer) {
    System.out.println("pos=" + buffer.position() + ", limit=" + buffer.limit() + ", capacity="
            + buffer.capacity() + ": '" + buffer.toString() + "'");
}

From source file:Main.java

public static int getOffset(Buffer buf) {
    return buf.position() << getElementSizeExponent(buf);
}

From source file:Main.java

public static int getOffset(Buffer buffer) {
    return buffer.position() << getElementSizeExponent(buffer);
}

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.//  w  ww .j a  v a 2  s.  co  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();
}