Android Open Source - planets G L E S20 Renderer






From Project

Back to project page planets.

License

The source code is released under:

GNU General Public License

If you think the Android project planets listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package se.liu.lysator.dahlberg.planets;
//from ww w  .  java  2 s . c  o m
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.opengl.GLES20;
import android.opengl.Matrix;
import android.os.SystemClock;

public class GLES20Renderer extends GLRenderer {

  /** Store our model data in a float buffer. */
  private final FloatBuffer mTriangle1Vertices;
  private final FloatBuffer mTriangle2Vertices;
  private final FloatBuffer mTriangle3Vertices;

  /** How many bytes per float. */
  private final int mBytesPerFloat = 4;

  /** How many elements per vertex. */
  private final int strideBytes = 7 * mBytesPerFloat;

  /** Offset of the position data. */
  private final int positionOffset = 0;

  /** Size of the position data in elements. */
  private final int positionDataSize = 3;

  /** Offset of the color data. */
  private final int colorOffset = 3;

  /** Size of the color data in elements. */
  private final int colorDataSize = 4;

  /**
   * Allocate storage for the final combined matrix. This will be passed into
   * the shader program.
   */
  private float[] mMVPMatrix = new float[16];

  /** This will be used to pass in the transformation matrix. */
  private int mvpMatrixHandle;

  /** This will be used to pass in model position information. */
  private int positionHandle;

  /** This will be used to pass in model color information. */
  private int colorHandle;

  /**
   * Initialize the model data.
   */
  public GLES20Renderer() {
    // Define points for equilateral triangles.

    // This triangle is red, green, and blue.
    final float[] triangle1VerticesData = {
        // X, Y, Z,
        // R, G, B, A
        -0.5f, -0.25f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,

        0.5f, -0.25f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,

        0.0f, 0.559016994f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f };

    // This triangle is yellow, cyan, and magenta.
    final float[] triangle2VerticesData = {
        // X, Y, Z,
        // R, G, B, A
        -0.5f, -0.25f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,

        0.5f, -0.25f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,

        0.0f, 0.559016994f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f };

    // This triangle is white, gray, and black.
    final float[] triangle3VerticesData = {
        // X, Y, Z,
        // R, G, B, A
        -0.5f, -0.25f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,

        0.5f, -0.25f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f,

        0.0f, 0.559016994f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };

    // Initialize the buffers.
    mTriangle1Vertices = ByteBuffer
        .allocateDirect(triangle1VerticesData.length * mBytesPerFloat)
        .order(ByteOrder.nativeOrder()).asFloatBuffer();
    mTriangle2Vertices = ByteBuffer
        .allocateDirect(triangle2VerticesData.length * mBytesPerFloat)
        .order(ByteOrder.nativeOrder()).asFloatBuffer();
    mTriangle3Vertices = ByteBuffer
        .allocateDirect(triangle3VerticesData.length * mBytesPerFloat)
        .order(ByteOrder.nativeOrder()).asFloatBuffer();

    mTriangle1Vertices.put(triangle1VerticesData).position(0);
    mTriangle2Vertices.put(triangle2VerticesData).position(0);
    mTriangle3Vertices.put(triangle3VerticesData).position(0);
  }

  @Override
  public void onCreate(int width, int height, boolean surfaceLost) {
    // Set the OpenGL viewport to the same size as the surface.
    GLES20.glViewport(0, 0, width, height);

    // Create a new perspective projection matrix. The height will stay the
    // same while the width will vary as per aspect ratio.
    final float ratio = (float) width / height;
    final float left = -ratio;
    final float right = ratio;
    final float bottom = -1.0f;
    final float top = 1.0f;
    final float near = 1.0f;
    final float far = 10.0f;

    float[] matrix = getProjectionMatrix();
    Matrix.frustumM(matrix, 0, left, right, bottom, top, near, far);
  }

  @Override
  public void onDrawFrame(boolean firstDraw) {
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

    // Do a complete rotation every 10 seconds.
    long time = SystemClock.uptimeMillis() % 10000L;
    float angleInDegrees = (360.0f / 10000.0f) * ((int) time);

    // Draw the triangle facing straight on.
    float[] matrix = getModelMatrix();
    Matrix.setIdentityM(matrix, 0);
    Matrix.rotateM(matrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
    drawTriangle(mTriangle1Vertices);

    // Draw one translated a bit down and rotated to be flat on the ground.
    Matrix.setIdentityM(matrix, 0);
    Matrix.translateM(matrix, 0, 0.0f, -1.0f, 0.0f);
    Matrix.rotateM(matrix, 0, 90.0f, 1.0f, 0.0f, 0.0f);
    Matrix.rotateM(matrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
    drawTriangle(mTriangle2Vertices);

    // Draw one translated a bit to the right and rotated to be facing to
    // the left.
    Matrix.setIdentityM(matrix, 0);
    Matrix.translateM(matrix, 0, 1.0f, 0.0f, 0.0f);
    Matrix.rotateM(matrix, 0, 90.0f, 0.0f, 1.0f, 0.0f);
    Matrix.rotateM(matrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
    drawTriangle(mTriangle3Vertices);
  }

  /**
   * Draws a triangle from the given vertex data.
   *
   * @param aTriangleBuffer
   *            The buffer containing the vertex data.
   */
  private void drawTriangle(final FloatBuffer aTriangleBuffer) {
    // Pass in the position information
    aTriangleBuffer.position(positionOffset);
    GLES20.glVertexAttribPointer(positionHandle, positionDataSize,
        GLES20.GL_FLOAT, false, strideBytes, aTriangleBuffer);

    GLES20.glEnableVertexAttribArray(positionHandle);

    // Pass in the color information
    aTriangleBuffer.position(colorOffset);
    GLES20.glVertexAttribPointer(colorHandle, colorDataSize,
        GLES20.GL_FLOAT, false, strideBytes, aTriangleBuffer);

    GLES20.glEnableVertexAttribArray(colorHandle);

    // This multiplies the view matrix by the model matrix, and stores the
    // result in the MVP matrix
    // (which currently contains model * view).
    Matrix.multiplyMM(mMVPMatrix, 0, getViewMatrix(), 0, getModelMatrix(),
        0);

    // This multiplies the modelview matrix by the projection matrix, and
    // stores the result in the MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, getProjectionMatrix(), 0, mMVPMatrix,
        0);

    GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mMVPMatrix, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
  }

  @Override
  public void onSurfaceCreated(int programHandle) {
    // Set program handles. These will later be used to pass in values to
    // the program.
    mvpMatrixHandle = GLES20.glGetUniformLocation(programHandle,
        "u_MVPMatrix");
    positionHandle = GLES20
        .glGetAttribLocation(programHandle, "a_Position");
    colorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");

    // Tell OpenGL to use this program when rendering.
    GLES20.glUseProgram(programHandle);

  }
}




Java Source Code List

se.liu.lysator.dahlberg.planets.GLES20Activity.java
se.liu.lysator.dahlberg.planets.GLES20Renderer.java
se.liu.lysator.dahlberg.planets.GLRenderer.java
se.liu.lysator.dahlberg.planets.PlanetDetailsActivity.java
se.liu.lysator.dahlberg.planets.PlanetsLauncherActivity.java
se.liu.lysator.dahlberg.planets.util.SystemUiHiderBase.java
se.liu.lysator.dahlberg.planets.util.SystemUiHiderHoneycomb.java
se.liu.lysator.dahlberg.planets.util.SystemUiHider.java