Android Open Source - android-plotter Texample Renderer






From Project

Back to project page android-plotter.

License

The source code is released under:

Apache License

If you think the Android project android-plotter 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

// This is based on the OpenGL ES 1.0 sample application from the Android Developer website:
// http://developer.android.com/resources/tutorials/opengl/opengl-es10.html
/*  w ww  . jav  a 2s  .  c  o m*/
package com.android.texample;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class TexampleRenderer implements GLSurfaceView.Renderer {

  public static float fov_degrees = 45f;
  public static float fov_radians = fov_degrees / 180 * (float) Math.PI;
  public static float aspect;
  public static float camZ;
  private GLText glText;                             // A GLText Instance
  private Context context;                           // Context (from Activity)
  private int width = 100;                           // Updated to the Current Width + Height in onSurfaceChanged()
  private int height = 100;
  public TexampleRenderer(Context context) {
    super();
    this.context = context;                         // Save Specified Context
  }

  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // Set the background frame color
    gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

    // Create the GLText
    glText = new GLText(gl, context.getAssets());

    // Load the font from file (set size + padding), creates the texture
    // NOTE: after a successful call to this the font is ready for rendering!
    glText.load("Roboto-Regular.ttf", 14, 2, 2);  // Create Font (Height: 14 Pixels / X+Y Padding 2 Pixels)
  }

  public void onDrawFrame(GL10 gl) {
    // Redraw background color
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    // Set to ModelView mode
    gl.glMatrixMode(GL10.GL_MODELVIEW);           // Activate Model View Matrix
    gl.glLoadIdentity();                            // Load Identity Matrix

    // enable texture + alpha blending
    // NOTE: this is required for text rendering! we could incorporate it into
    // the GLText class, but then it would be called multiple times (which impacts performance).
    gl.glEnable(GL10.GL_TEXTURE_2D);              // Enable Texture Mapping
    gl.glEnable(GL10.GL_BLEND);                   // Enable Alpha Blend
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);  // Set Alpha Blend Function

    // TEST: render the entire font texture
    //gl.glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );         // Set Color to Use
    //glText.drawTexture( width, height, 0 );            // Draw the Entire Texture

    // TEST: render some strings with the font
    glText.begin(1.0f, 1.0f, 1.0f, 1.0f);         // Begin Text Rendering (Set Color WHITE)
    glText.draw("Test String :)", 0, 0, 0);          // Draw Test String
    glText.draw("Line 1", 50, 50, 0);                // Draw Test String
    glText.draw("Line 2", 100, 100, 0);              // Draw Test String
    glText.end();                                   // End Text Rendering

   /*   glText.begin( 0.0f, 0.0f, 1.0f, 1.0f );         // Begin Text Rendering (Set Color BLUE)
    glText.draw( "More Lines...", 50, 150, 0 );        // Draw Test String
      glText.draw( "The End.", 50, 150 + glText.getCharHeight(), 0 );  // Draw Test String
      glText.end();                                   // End Text Rendering
     */

    glText.begin(1.0f, 0.0f, 0.0f, 1.0f);         // Begin Text Rendering (Set Color RED)
    glText.draw("zoom out !", -200, 0, -800);        // Draw Test String
    glText.draw("zoom in !", -50, -50, 600);        // Draw Test String

    glText.end();

    // disable texture + alpha
    gl.glDisable(GL10.GL_BLEND);                  // Disable Alpha Blend
    gl.glDisable(GL10.GL_TEXTURE_2D);             // Disable Texture Mapping
  }

  public void onSurfaceChanged(GL10 gl, int x, int y) {


    aspect = (float) x / (float) y;

    camZ = y / 2 / (float) Math.tan(fov_radians / 2);


    if (x == 0) { // Prevent A Divide By Zero By
      x = 1; // Making Height Equal One
    }


    gl.glViewport(0, 0, x, y); // Reset The Current Viewport
    gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
    gl.glLoadIdentity(); // Reset The Projection Matrix

    // Calculate The Aspect Ratio Of The Window
    GLU.gluPerspective(gl, fov_degrees, aspect, camZ / 10, camZ * 10);
    //GLU.gluOrtho2D(gl, 0, x, 0, y);

    GLU.gluLookAt(gl, 0, 0, camZ, 0, 0, 0, 0, 1, 0); // move camera back


    gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix

    gl.glLoadIdentity(); // Reset The Modelview Matrix


  }
}




Java Source Code List

com.android.texample.GLText.java
com.android.texample.SpriteBatch.java
com.android.texample.TexampleRenderer.java
com.android.texample.TextureRegion.java
com.android.texample.Vertices.java
org.solovyev.android.plotter.Angle.java
org.solovyev.android.plotter.AxisStyle.java
org.solovyev.android.plotter.Check.java
org.solovyev.android.plotter.Color.java
org.solovyev.android.plotter.DefaultPlotter.java
org.solovyev.android.plotter.Dimensions.java
org.solovyev.android.plotter.Frustum.java
org.solovyev.android.plotter.Function0.java
org.solovyev.android.plotter.Function1.java
org.solovyev.android.plotter.Function2.java
org.solovyev.android.plotter.Function.java
org.solovyev.android.plotter.LineStyle.java
org.solovyev.android.plotter.MeshConfig.java
org.solovyev.android.plotter.MultisampleConfigChooser.java
org.solovyev.android.plotter.PinchZoomTracker.java
org.solovyev.android.plotter.PlotData.java
org.solovyev.android.plotter.PlotFunction.java
org.solovyev.android.plotter.PlotRenderer.java
org.solovyev.android.plotter.PlotView.java
org.solovyev.android.plotter.Plot.java
org.solovyev.android.plotter.Plotter.java
org.solovyev.android.plotter.PlottingView.java
org.solovyev.android.plotter.Spf.java
org.solovyev.android.plotter.SuperFunction.java
org.solovyev.android.plotter.TouchHandler.java
org.solovyev.android.plotter.ZoomLevels.java
org.solovyev.android.plotter.Zoomer.java
org.solovyev.android.plotter.app.MainActivity.java
org.solovyev.android.plotter.app.PlotterApplication.java
org.solovyev.android.plotter.meshes.Arrays.java
org.solovyev.android.plotter.meshes.AxisGrid.java
org.solovyev.android.plotter.meshes.Axis.java
org.solovyev.android.plotter.meshes.BaseCube.java
org.solovyev.android.plotter.meshes.BaseCurve.java
org.solovyev.android.plotter.meshes.BaseMesh.java
org.solovyev.android.plotter.meshes.BaseSurface.java
org.solovyev.android.plotter.meshes.DimensionsAwareSwapper.java
org.solovyev.android.plotter.meshes.DimensionsAware.java
org.solovyev.android.plotter.meshes.DoubleBufferGroup.java
org.solovyev.android.plotter.meshes.DoubleBufferMesh.java
org.solovyev.android.plotter.meshes.FunctionGraph2d.java
org.solovyev.android.plotter.meshes.FunctionGraph3d.java
org.solovyev.android.plotter.meshes.FunctionGraphSwapper.java
org.solovyev.android.plotter.meshes.FunctionGraph.java
org.solovyev.android.plotter.meshes.Graph.java
org.solovyev.android.plotter.meshes.Group.java
org.solovyev.android.plotter.meshes.IndicesOrder.java
org.solovyev.android.plotter.meshes.ListGroup.java
org.solovyev.android.plotter.meshes.ListPool.java
org.solovyev.android.plotter.meshes.Mesh.java
org.solovyev.android.plotter.meshes.Meshes.java
org.solovyev.android.plotter.meshes.Pool.java
org.solovyev.android.plotter.meshes.Scene.java
org.solovyev.android.plotter.meshes.SolidCube.java
org.solovyev.android.plotter.meshes.SurfaceInitializer.java
org.solovyev.android.plotter.meshes.WireFrameCube.java