Android Open Source - GLSpin G L Renderer Main






From Project

Back to project page GLSpin.

License

The source code is released under:

MIT License

If you think the Android project GLSpin 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 st.kirara.tochikashobby;
//ww w  .  j  a v  a 2s  . c  om
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

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

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

/**
 * OpenGL renderer.
 * @author Tochika
 *
 */
public class GLRendererMain implements Renderer {

  private Context mContext = null;


  private float mAspect = 0.0f;
  
  private int mVertices = 0;
  private int mIndices = 0;
  private int mIndicesLength = 0;
  
  private SpinCube spinCube;
  
//  private float mSpinMovementX = 0.0f;
  private float mSpinMovementY = 0.0f;
  
  
  public GLRendererMain(Context context){
    mContext = context;
    
    spinCube = new SpinCube();
  }
  

  @Override
  public void onSurfaceChanged(GL10 gl10, int width, int height) {

    mAspect = (float) width / (float) height;
    gl10.glViewport(0, 0, width, height);
    
    gl10.glEnable(GL10.GL_DEPTH_TEST);

  }

  
  @Override
  public void onSurfaceCreated(GL10 gl10, EGLConfig config) {
    initVertex(gl10); 
  }


  @Override
  public void onDrawFrame(GL10 gl10) {

    GL11 gl11 = (GL11)gl10;

    // Clear background.    
    gl11.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    gl11.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    
    setCamera(gl10);

    // Prepare for draw.
    gl11.glMatrixMode(GL10.GL_MODELVIEW);
    gl11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
    
    
    final float MOVEMENT_MAX = 15.0f;
    float my = mSpinMovementY;
    if(my > MOVEMENT_MAX){
      my = MOVEMENT_MAX;
    }else if(my < -MOVEMENT_MAX){
      my = -MOVEMENT_MAX;
    }
    spinCube.rotateY += my;
    if(spinCube.rotateY > 360.0f){
      spinCube.rotateY -= 360.0f;
    }else if(spinCube.rotateY < 0.0f){
      spinCube.rotateY += 360.0f;
    }
    // Log.d(MyD.TAG, "movement:" + mSpinMovementY);
    mSpinMovementY *= 0.96f;
    
//    float my = mSpinMovementY;
//    if(my > MOVEMENT_MAX){
//      my = MOVEMENT_MAX;
//    }
//    spinCube.rotateY += my;
//    if(spinCube.rotateY > 360.0f){
//      spinCube.rotateY -= 360.0f;
//    }else if(spinCube.rotateY < 0.0f){
//      spinCube.rotateY += 360.0f;
//    }
//    mSpinMovementY *= 0.96f;
    
    spinCube.spin(gl10);
  }
  
  
  /**
   * Initialize vertex and index buffer.
   */
  private void initVertex(GL10 gl10){

    GL11 gl11 = (GL11)gl10;

    // Generate buffers.
    int[] buffer = new int[2];
    gl11.glGenBuffers(2, buffer, 0);
    mVertices = buffer[0];
    mIndices = buffer[1];

    // Generate vertex.
    final float one = 1.0f;
    final float[] vertices = new float[] {
        one, one, one, 
        one, one, -one, 
        -one, one, one,
        -one, one, -one, 
        one, -one, one, 
        one, -one, -one, 
        -one, -one, one, 
        -one, -one, -one,
    };

    FloatBuffer fb = ByteBuffer.allocateDirect(vertices.length * 4) .order(ByteOrder.nativeOrder()).asFloatBuffer();
    fb.put(vertices);
    fb.position(0);
    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, this.mVertices);
    gl11.glBufferData(GL11.GL_ARRAY_BUFFER, fb.capacity() * 4, fb, GL11.GL_STATIC_DRAW);

    gl11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0); 


    // Generate index buffer.
    final byte[] indices = new byte[] {
        0, 1, 2, 
        2, 1, 3, 
        2, 3, 6, 
        6, 3, 7, 
        6, 7, 4, 
        4, 7, 5, 
        4, 5, 0, 
        0, 5, 1, 
        1, 5, 3,
        3, 5, 7, 
        0, 2, 4, 
        4, 2, 6,
    };
    ByteBuffer bb = ByteBuffer.allocateDirect(indices.length).order(ByteOrder.nativeOrder());
    bb.put(indices);
    mIndicesLength = bb.capacity(); 
    bb.position(0);
    gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, this.mIndices);
    gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, bb.capacity(), bb, GL11.GL_STATIC_DRAW); 
  }
  
  /**
   * setting Camera positions.
   */
  private void setCamera(GL10 gl10){
    gl10.glMatrixMode(GL10.GL_PROJECTION);
    gl10.glLoadIdentity();
    GLU.gluPerspective(gl10, 45.0f, mAspect, 0.01f, 100.0f);
    GLU.gluLookAt(gl10, 0, 2.5f, 5.0f, 0, 0, 0.0f, 0.0f, 1.0f, 0.0f);
  }
  

  /**
   * set spin size.
   * @param x spin size of x.
   * @param y spin size of y.
   */
  public void setSpinMovement(float x, float y){
    final float MOVEMENT_MAX = 1000.0f;
    
    // !! swap x and y.
//    if(x > MOVEMENT_MAX){
//      mSpinMovementX = MOVEMENT_MAX;
//    }else if(x < -MOVEMENT_MAX){
//      mSpinMovementX = -MOVEMENT_MAX;
//    }else{
//      mSpinMovementX = x;
//    }

    if(x > MOVEMENT_MAX){
      mSpinMovementY = MOVEMENT_MAX;
    }else if(x < -MOVEMENT_MAX){
      mSpinMovementY = -MOVEMENT_MAX;
    }else{
      mSpinMovementY = x;
    }
    
    //spinCube.rotateY += y;
  }

  
  /**
   * Spun cube object.
   * @author Tochika
   *
   */
  class SpinCube {
    public float posX = 0.0f;
    public float posY = 0.0f;
    public float posZ = 0.0f;
    public float rotateX = 30.0f;
    public float rotateY = 0.0f;


    public void spin(GL10 gl10) {

      gl10.glMatrixMode(GL10.GL_MODELVIEW);
      gl10.glLoadIdentity();

      // Set positions, rotate.
      gl10.glTranslatef(posX, posY, posZ);
      gl10.glRotatef(rotateY, 0, 1, 0);
//      gl10.glRotatef(rotateX, 1, 0, 0);
      gl10.glScalef(1.0f, 1.0f, 1.0f);
      
      // Set color.
      gl10.glColor4f(1.0f, 0.5f, 1.0f, 1.0f);
      
      
      GL11 gl11 = (GL11)gl10;

      // Bind cube vertices.
      gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, GLRendererMain.this.mVertices);
      gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);

      // Draw.
      gl11.glDrawElements(GL10.GL_TRIANGLES, mIndicesLength, GL10.GL_UNSIGNED_BYTE, 0);
    } 
  }

}




Java Source Code List

st.kirara.tochikashobby.GLRendererMain.java
st.kirara.tochikashobby.MainActivity.java
st.kirara.tochikashobby.debug.MyD.java