Android Open Source - glestest G L E S Test Geometry






From Project

Back to project page glestest.

License

The source code is released under:

GNU General Public License

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

/*
 *    GLESTestGeometry.java : Geometry generation and drawing code.
 *    (C)2013 Marisa Kirisame, UnSX Team.
 */*from   w ww.jav  a 2  s. c o  m*/
 *    This file is part of GLEStest.
 *
 *    GLEStest is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    GLEStest is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with GLEStest.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sayachan.glestest;

import android.opengl.GLES20;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/* clase principal para elementos geomtricos */
class GLESTestGeometry
{
  /* buffers de vrtices, normales e ndices */
  protected int[] geomBuffers;
  /* nmero de elementos */
  protected int numElements;
  /* buffers internos */
  protected FloatBuffer bufVerts,bufNormals;
  protected ShortBuffer bufIndices;
  /* arrays internos */
  protected float[] vertices,normals;
  protected short[] indices;

  /* dibujar elementos */
  public void Draw( GLESTestShader shader )
  {
    /* atributos que pasar al shader */
    int attribVertex =
      GLES20.glGetAttribLocation(shader.shaderProgram,
      "vertex");
    int attribNormal =
      GLES20.glGetAttribLocation(shader.shaderProgram,
      "normal");
    /* enlazado de arrays */
    if ( attribVertex != -1 )
    {
      GLES20.glEnableVertexAttribArray(attribVertex);
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,
        geomBuffers[0]);
      GLES20.glVertexAttribPointer(attribVertex,3,
        GLES20.GL_FLOAT,false,0,0);
    }
    if ( attribNormal != -1 )
    {
      GLES20.glEnableVertexAttribArray(attribNormal);
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,
        geomBuffers[1]);
      GLES20.glVertexAttribPointer(attribNormal,3,
        GLES20.GL_FLOAT,false,0,0);
    }
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,
      geomBuffers[2]);
    /* dibujado */
    GLES20.glDrawElements(GLES20.GL_TRIANGLES,numElements,
      GLES20.GL_UNSIGNED_SHORT,0);
    /* esperar a que finalice la GPU */
    GLES20.glFinish();
  }

  /* generar buffers */
  protected void GenBuffers()
  {
    geomBuffers = new int[3];
    GLES20.glGenBuffers(3,geomBuffers,0);
    bufVerts = ByteBuffer.allocateDirect(vertices.length*4)
      .order(ByteOrder.nativeOrder()).asFloatBuffer();
    bufNormals = ByteBuffer.allocateDirect(normals.length*4)
      .order(ByteOrder.nativeOrder()).asFloatBuffer();
    bufIndices = ByteBuffer.allocateDirect(indices.length*2)
      .order(ByteOrder.nativeOrder()).asShortBuffer();
    bufVerts.put(vertices).position(0);
    bufNormals.put(normals).position(0);
    bufIndices.put(indices).position(0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,geomBuffers[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER,
      bufVerts.capacity()*4,bufVerts,GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,geomBuffers[1]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER,
      bufNormals.capacity()*4,bufNormals,
      GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,
      geomBuffers[2]);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER,
      bufIndices.capacity()*2,bufIndices,
      GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,0);
  }
}

/** FIGURAS PRIMITIVAS **/

/* plano */
class GLESTestFlat extends GLESTestGeometry
{
  public GLESTestFlat( float sz )
  {
    numElements = 6;
    vertices = new float[]
    {
      -0.5f*sz, -0.5f*sz,  0.0f,
       0.5f*sz, -0.5f*sz,  0.0f,
       0.5f*sz,  0.5f*sz,  0.0f,
      -0.5f*sz,  0.5f*sz,  0.0f
    };
    normals = new float[]
    {
       0.0f,  0.0f,  1.0f,
       0.0f,  0.0f,  1.0f,
       0.0f,  0.0f,  1.0f,
       0.0f,  0.0f,  1.0f
    };
    indices = new short[]
    {
      0,1,3,
      1,2,3
    };
    /* generar buffers */
    GenBuffers();
  }
}

/* tetraedro */
class GLESTestTetra extends GLESTestGeometry
{
  public GLESTestTetra( float sz )
  {
    numElements = 12;
    vertices = new float[]
    {
       0.5f*sz,  0.5f*sz,  0.5f*sz,
      -0.5f*sz, -0.5f*sz,  0.5f*sz,
       0.5f*sz, -0.5f*sz, -0.5f*sz,

       0.5f*sz,  0.5f*sz,  0.5f*sz,
      -0.5f*sz,  0.5f*sz, -0.5f*sz,
      -0.5f*sz, -0.5f*sz,  0.5f*sz,

       0.5f*sz,  0.5f*sz,  0.5f*sz,
       0.5f*sz, -0.5f*sz, -0.5f*sz,
      -0.5f*sz,  0.5f*sz, -0.5f*sz,

      -0.5f*sz, -0.5f*sz,  0.5f*sz,
      -0.5f*sz,  0.5f*sz, -0.5f*sz,
       0.5f*sz, -0.5f*sz, -0.5f*sz,

    };
    normals = new float[]
    {
       0.5f, -0.5f,  0.5f,
       0.5f, -0.5f,  0.5f,
       0.5f, -0.5f,  0.5f,

      -0.5f,  0.5f,  0.5f,
      -0.5f,  0.5f,  0.5f,
      -0.5f,  0.5f,  0.5f,

       0.5f,  0.5f, -0.5f,
       0.5f,  0.5f, -0.5f,
       0.5f,  0.5f, -0.5f,

      -0.5f, -0.5f, -0.5f,
      -0.5f, -0.5f, -0.5f,
      -0.5f, -0.5f, -0.5f,
    };
    indices = new short[]
    {
       0, 1, 2,
       3, 4, 5,
       6, 7, 8,
       9,10,11
    };
    /* generar buffers */
    GenBuffers();
  }
}

/* cubo */
class GLESTestCube extends GLESTestGeometry
{
  public GLESTestCube( float sz )
  {
    numElements = 36;
    vertices = new float[]
    {
      -0.5f, -0.5f,  0.5f,
       0.5f, -0.5f,  0.5f,
       0.5f,  0.5f,  0.5f,
      -0.5f,  0.5f,  0.5f,

       0.5f,  0.5f,  0.5f,
       0.5f,  0.5f, -0.5f,
       0.5f, -0.5f, -0.5f,
       0.5f, -0.5f,  0.5f,

      -0.5f,  0.5f,  0.5f,
       0.5f,  0.5f,  0.5f,
       0.5f,  0.5f, -0.5f,
      -0.5f,  0.5f, -0.5f,

       0.5f,  0.5f, -0.5f,
      -0.5f,  0.5f, -0.5f,
      -0.5f, -0.5f, -0.5f,
       0.5f, -0.5f, -0.5f,

      -0.5f, -0.5f, -0.5f,
      -0.5f, -0.5f,  0.5f,
      -0.5f,  0.5f,  0.5f,
      -0.5f,  0.5f, -0.5f,

       0.5f, -0.5f, -0.5f,
      -0.5f, -0.5f, -0.5f,
      -0.5f, -0.5f,  0.5f,
       0.5f, -0.5f,  0.5f

    };
    normals = new float[]
    {
       0.0f,  0.0f,  1.0f,
       0.0f,  0.0f,  1.0f,
       0.0f,  0.0f,  1.0f,
       0.0f,  0.0f,  1.0f,

       1.0f,  0.0f,  0.0f,
       1.0f,  0.0f,  0.0f,
       1.0f,  0.0f,  0.0f,
       1.0f,  0.0f,  0.0f,

       0.0f,  1.0f,  0.0f,
       0.0f,  1.0f,  0.0f,
       0.0f,  1.0f,  0.0f,
       0.0f,  1.0f,  0.0f,

       0.0f,  0.0f, -1.0f,
       0.0f,  0.0f, -1.0f,
       0.0f,  0.0f, -1.0f,
       0.0f,  0.0f, -1.0f,

      -1.0f,  0.0f,  0.0f,
      -1.0f,  0.0f,  0.0f,
      -1.0f,  0.0f,  0.0f,
      -1.0f,  0.0f,  0.0f,

       0.0f, -1.0f,  0.0f,
       0.0f, -1.0f,  0.0f,
       0.0f, -1.0f,  0.0f,
       0.0f, -1.0f,  0.0f
    };
    indices = new short[]
    {
       0, 1, 3,
       1, 2, 3,

       4, 5, 7,
       5, 6, 7,

       8, 9,11,
       9,10,11,

      12,13,15,
      13,14,15,

      16,17,19,
      17,18,19,

      20,21,23,
      21,22,23
    };
    /* generar buffers */
    GenBuffers();
  }
}

/* TODO octaedro */

class GLESTestOcta extends GLESTestGeometry
{
  public GLESTestOcta( float sz )
  {
    numElements = 24;
    vertices = new float[]
    {
       1.0f,  0.0f,  0.0f,
       0.0f,  1.0f,  0.0f,
       0.0f,  0.0f,  1.0f,

       1.0f,  0.0f,  0.0f,
       0.0f, -1.0f,  0.0f,
       0.0f,  0.0f, -1.0f,

       1.0f,  0.0f,  0.0f,
       0.0f,  0.0f,  1.0f,
       0.0f, -1.0f,  0.0f,

       1.0f,  0.0f,  0.0f,
       0.0f,  0.0f, -1.0f,
       0.0f,  1.0f,  0.0f,

      -1.0f,  0.0f,  0.0f,
       0.0f,  1.0f,  0.0f,
       0.0f,  0.0f, -1.0f,

      -1.0f,  0.0f,  0.0f,
       0.0f, -1.0f,  0.0f,
       0.0f,  0.0f,  1.0f,

      -1.0f,  0.0f,  0.0f,
       0.0f,  0.0f,  1.0f,
       0.0f,  1.0f,  0.0f,

      -1.0f,  0.0f,  0.0f,
       0.0f,  0.0f, -1.0f,
       0.0f, -1.0f,  0.0f
    };
    normals = new float[]
    {
       0.5f,  0.5f,  0.5f,
       0.5f,  0.5f,  0.5f,
       0.5f,  0.5f,  0.5f,

       0.5f, -0.5f, -0.5f,
       0.5f, -0.5f, -0.5f,
       0.5f, -0.5f, -0.5f,

       0.5f, -0.5f,  0.5f,
       0.5f, -0.5f,  0.5f,
       0.5f, -0.5f,  0.5f,

       0.5f,  0.5f, -0.5f,
       0.5f,  0.5f, -0.5f,
       0.5f,  0.5f, -0.5f,

      -0.5f,  0.5f, -0.5f,
      -0.5f,  0.5f, -0.5f,
      -0.5f,  0.5f, -0.5f,

      -0.5f, -0.5f,  0.5f,
      -0.5f, -0.5f,  0.5f,
      -0.5f, -0.5f,  0.5f,

      -0.5f,  0.5f,  0.5f,
      -0.5f,  0.5f,  0.5f,
      -0.5f,  0.5f,  0.5f,

      -0.5f, -0.5f, -0.5f,
      -0.5f, -0.5f, -0.5f,
      -0.5f, -0.5f, -0.5f
    };
    indices = new short[]
    {
       0, 1, 2,
       3, 4, 5,
       6, 7, 8,
       9,10,11,
      12,13,14,
      15,16,17,
      18,19,20,
      21,22,23,
    };
    /* generar buffers */
    GenBuffers();
  }
}

/* TODO icosaedro */

class GLESTestIcosa extends GLESTestGeometry
{
  public GLESTestIcosa( float sz )
  {
    numElements = 60;
    float a, b, c, d;
    a = 0.000000f;
    b = 0.525731f;
    c = 0.850650f;
    vertices = new float[]
    {
      a,b,c,
      a,-b,c,
      c,a,b,
      a,b,c,
      b,c,a,
      -b,c,a,
      a,b,c,
      -b,c,a,
      -c,a,b,
      a,b,c,
      c,a,b,
      b,c,a,
      a,b,c,
      -c,a,b,
      a,-b,c,
      a,b,-c,
      a,-b,-c,
      -c,a,-b,
      a,b,-c,
      b,c,a,
      c,a,-b,
      a,b,-c,
      -b,c,a,
      b,c,a,
      a,b,-c,
      c,a,-b,
      a,-b,-c,
      a,b,-c,
      -c,a,-b,
      -b,c,a,
      a,-b,c,
      b,-c,a,
      c,a,b,
      a,-b,c,
      -b,-c,a,
      b,-c,a,
      a,-b,c,
      -c,a,b,
      -b,-c,a,
      a,-b,-c,
      b,-c,a,
      -b,-c,a,
      a,-b,-c,
      -b,-c,a,
      -c,a,-b,
      a,-b,-c,
      c,a,-b,
      b,-c,a,
      b,c,a,
      c,a,b,
      c,a,-b,
      -b,c,a,
      -c,a,-b,
      -c,a,b,
      b,-c,a,
      c,a,-b,
      c,a,b,
      -b,-c,a,
      -c,a,b,
      -c,a,-b
    };
    a = 0.356822f;
    b = 0.000000f;
    c = 0.934172f;
    d = 0.577350f;
    normals = new float[]
    {
      a,b,c,a,b,c,a,b,c,
      b,c,a,b,c,a,b,c,a,
      -d,d,d,-d,d,d,-d,d,d,
      d,d,d,d,d,d,d,d,d,
      -a,b,c,-a,b,c,-a,b,c,
      -a,b,-c,-a,b,-c,-a,b,-c,
      d,d,-d,d,d,-d,d,d,-d,
      b,c,-a,b,c,-a,b,c,-a,
      a,b,-c,a,b,-c,a,b,-c,
      -d,d,-d,-d,d,-d,-d,d,-d,
      d,-d,d,d,-d,d,d,-d,d,
      b,-c,a,b,-c,a,b,-c,a,
      -d,-d,d,-d,-d,d,-d,-d,d,
      b,-c,-a,b,-c,-a,b,-c,-a,
      -d,-d,-d,-d,-d,-d,-d,-d,-d,
      d,-d,-d,d,-d,-d,d,-d,-d,
      c,a,b,c,a,b,c,a,b,
      -c,a,b,-c,a,b,-c,a,b,
      c,-a,b,c,-a,b,c,-a,b,
      -c,-a,b,-c,-a,b,-c,-a,b
    };
    indices = new short[]
    {
       0, 1, 2,
       3, 4, 5,
       6, 7, 8,
       9,10,11,
      12,13,14,
      15,16,17,
      18,19,20,
      21,22,23,
      24,25,26,
      27,28,29,
      30,31,32,
      33,34,35,
      36,37,38,
      39,40,41,
      42,43,44,
      45,46,47,
      48,49,50,
      51,52,53,
      54,55,56,
      57,58,59,
    };
    /* generar buffers */
    GenBuffers();
  }
}




Java Source Code List

org.sayachan.glestest.GLESTestGeometry.java
org.sayachan.glestest.GLESTestRender.java
org.sayachan.glestest.GLESTestShaders.java
org.sayachan.glestest.GLESTest.java