Android Open Source - OpenGLProject Tronco






From Project

Back to project page OpenGLProject.

License

The source code is released under:

MIT License

If you think the Android project OpenGLProject 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 com.example.opengl2;
/*from  w  w  w . ja va 2 s  .co  m*/
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;

public class Tronco {
  
  /** The buffer holding the vertices */
  private FloatBuffer vertexBuffer;
  /** The buffer holding the color values */
  private FloatBuffer colorBuffer;
  /** The buffer holding the indices */
  private ByteBuffer  indexBuffer;
  
  /** 
   * The initial vertex definition
   * 
   * It defines the eight vertices a cube has
   * based on the OpenGL coordinate system
   */
  private float vertices[] = {
                  -0.3f, -1.0f, -0.3f,  //lower back left (0)
                  0.3f, -1.0f, -0.3f,    //lower back right (1)
                  0.3f,  1.0f, -0.3f,    //upper back right (2)
                  -0.3f, 1.0f, -0.3f,    //upper back left (3)
                  -0.3f, -1.0f,  0.3f,  //lower front left (4)
                  0.3f, -1.0f,  0.3f,    //lower front right (5)
                  0.3f,  1.0f,  0.3f,    //upper front right (6)
                  -0.3f,  1.0f,  0.3f    //upper front left (7)
                        };
    
    /** The initial color definition */  
  private float colors[] = {
                  0.0f,  1.0f,  0.0f,  1.0f,
                  0.0f,  1.0f,  0.0f,  1.0f,
                  1.0f,  0.5f,  0.0f,  1.0f,
                  1.0f,  0.5f,  0.0f,  1.0f,
                  1.0f,  0.0f,  0.0f,  1.0f,
                  1.0f,  0.0f,  0.0f,  1.0f,
                  0.0f,  0.0f,  1.0f,  1.0f,
                  1.0f,  0.0f,  1.0f,  1.0f
                          };
  
  /** 
     * The initial indices definition
     * 
     * The indices define our triangles.
     * Always two define one of the six faces
     * a cube has.
     */  
  private byte indices[] = {
              /*
               * Example: 
               * Face made of the vertices lower back left (lbl),
               * lfl, lfr, lbl, lfr, lbr
               */
                  0, 4, 5,    0, 5, 1,
                  //and so on...
                  1, 5, 6,    1, 6, 2,
                  2, 6, 7,    2, 7, 3,
                  3, 7, 4,    3, 4, 0,
                  4, 7, 6,    4, 6, 5,
                  3, 0, 1,    3, 1, 2
                        };
  
  /**
   * The Cube constructor.
   * 
   * Initiate the buffers.
   */
  public Tronco() {
    //
    ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuf.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);
    
    //
    byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
    byteBuf.order(ByteOrder.nativeOrder());
    colorBuffer = byteBuf.asFloatBuffer();
    colorBuffer.put(colors);
    colorBuffer.position(0);
    
    //
    indexBuffer = ByteBuffer.allocateDirect(indices.length);
    indexBuffer.put(indices);
    indexBuffer.position(0);
  }
  
  /**
   * The object own drawing function.
   * Called from the renderer to redraw this instance
   * with possible changes in values.
   * 
   * @param gl - The GL Context
   */
  public void draw(GL10 gl) {    
    //Set the face rotation
    gl.glFrontFace(GL10.GL_CW);
    
    //Point to our buffers
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    //gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
    
    //Enable the vertex and color state
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glColor4f(0.804f, 0.522f, 0.247f, 1);    
    
    //Draw the vertices as triangles, based on the Index Buffer information
    gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE, indexBuffer);
    
    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
  }
}




Java Source Code List

com.example.opengl2.Casa.java
com.example.opengl2.Hojas.java
com.example.opengl2.MainActivity.java
com.example.opengl2.Puerta.java
com.example.opengl2.Render.java
com.example.opengl2.Square.java
com.example.opengl2.Techo.java
com.example.opengl2.Triangle.java
com.example.opengl2.Tronco.java