Android Open Source - glestest G L E S Test Render






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 : Base renderer and FBO handling.
 *    (C)2013 Marisa Kirisame, UnSX Team.
 */*from   www  . ja  v  a 2 s . com*/
 *    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.os.SystemClock;
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import java.nio.ShortBuffer;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/* vista de aplicacin */
class GLESTestView extends GLSurfaceView
{
  public GLESTestView( Context ctx )
  {
    super(ctx);
    /* usar OpenGL ES 2.0 */
    setEGLContextClientVersion(2);
    /* motor de renderizado */
    GLESTestRenderer render = new GLESTestRenderer();
    setRenderer(render);
  }
}

/* motor de renderizado + escena */
class GLESTestRenderer implements GLSurfaceView.Renderer
{
  /* objetos */
  private GLESTestGeometry[] Objs;
  /* superficie plana usada como pantalla virtual */
  private GLESTestGeometry Screen;
  /* conjunto de FBO y texturas para la pantalla virtual */
  private GLESTestScreen VScreen;
  /* shaders */
  private GLESTestShader[] BaseShader;
  private GLESTestShader[] ScreenShader;
  /* variables internas */
  private int[] screenRes = {256,256};
  private float[] mmat, vmat, pmat;

  @Override public void onSurfaceCreated( GL10 unused, EGLConfig cfg )
  {
    /* preparar escena */
    GLES20.glClearColor(0.25f,0.25f,0.25f,1.0f);
    GLES20.glClearDepthf(1.0f);
    GLES20.glEnable(GLES20.GL_TEXTURE_2D);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
    /* generar objetos */
    Objs = new GLESTestGeometry[]
    {
      new GLESTestFlat(1.0f),
      new GLESTestTetra(1.0f),
      new GLESTestCube(1.0f),
      new GLESTestOcta(1.0f),
      new GLESTestIcosa(1.0f),
    };
    Screen = new GLESTestFlat(2.0f);
    /* preparar shaders */
    int i;
    int numbase = GLESTest.bSha.length;
    BaseShader = new GLESTestShader[numbase];
    for ( i=0; i<numbase; i++ )
      BaseShader[i] = new GLESTestShader(GLESTest.bSha[i]);
    int numscreen = GLESTest.sSha.length;
    ScreenShader = new GLESTestShader[numscreen];
    for ( i=0; i<numscreen; i++ )
      ScreenShader[i] = new GLESTestShader(GLESTest.sSha[i]);
    /* crear pantalla virtual */
    VScreen = new GLESTestScreen();
    /* crear matrices */
    mmat = new float[16];
    vmat = new float[16];
    pmat = new float[16];
  }

  @Override public void onDrawFrame( GL10 unused )
  {
    /* renderizar cubo en la pantalla virtual */
    VScreen.Enable();
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT
      |GLES20.GL_DEPTH_BUFFER_BIT);
    BaseShader[GLESTest.tex].Use();
    Matrix.setIdentityM(mmat,0);
    Matrix.setIdentityM(vmat,0);
    Matrix.setIdentityM(pmat,0);
    float timer = (float)SystemClock.elapsedRealtime();
    timer /= 1000.0f;
    Matrix.rotateM(mmat,0,timer*90.0f,0.0f,1.0f,0.0f);
    Matrix.rotateM(mmat,0,timer*90.0f,1.0f,0.0f,0.0f);
    Matrix.setLookAtM(vmat,0,0.0f,0.0f,2.0f,0.0f,0.0f,-1.0f,0.0f,
      1.0f,0.0f);
    float ar = (float)screenRes[0]/screenRes[1];
    Matrix.frustumM(pmat,0,-ar,ar,-1.0f,1.0f,1.0f,100.0f);
    int uniformTim =
      GLES20.glGetUniformLocation(BaseShader[GLESTest.tex]
      .shaderProgram,"timer");
    if ( uniformTim != -1 )
      GLES20.glUniform1f(uniformTim,timer);
    int uniformMM = 
      GLES20.glGetUniformLocation(BaseShader[GLESTest.tex]
      .shaderProgram,"mmat");
    if ( uniformMM != -1 )
      GLES20.glUniformMatrix4fv(uniformMM,1,false,mmat,0);
    int uniformVM = 
      GLES20.glGetUniformLocation(BaseShader[GLESTest.tex]
      .shaderProgram,"vmat");
    if ( uniformVM != -1 )
      GLES20.glUniformMatrix4fv(uniformVM,1,false,vmat,0);
    int uniformPM = 
      GLES20.glGetUniformLocation(BaseShader[GLESTest.tex]
      .shaderProgram,"pmat");
    if ( uniformPM != -1 )
      GLES20.glUniformMatrix4fv(uniformPM,1,false,pmat,0);
    Objs[GLESTest.fig].Draw(BaseShader[GLESTest.tex]);
    VScreen.Disable();
    /* renderizar la pantalla virtual */
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT
      |GLES20.GL_DEPTH_BUFFER_BIT);
    ScreenShader[GLESTest.sha].Use();
    uniformTim =
      GLES20.glGetUniformLocation(ScreenShader[GLESTest.sha]
      .shaderProgram,"timer");
    if ( uniformTim != -1 )
      GLES20.glUniform1f(uniformTim,timer);
    int uniformColor =
      GLES20.glGetUniformLocation(ScreenShader[GLESTest.sha]
      .shaderProgram,"color");
    int uniformDepth =
      GLES20.glGetUniformLocation(ScreenShader[GLESTest.sha]
      .shaderProgram,"depth");
    int uniformRes = 
      GLES20.glGetUniformLocation(ScreenShader[GLESTest.sha]
      .shaderProgram,"screenres");
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,
      VScreen.screenTextures[0]);
    if ( uniformColor != -1 )
      GLES20.glUniform1i(uniformColor,0);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,
      VScreen.screenTextures[1]);
    if ( uniformDepth != -1 )
      GLES20.glUniform1i(uniformDepth,1);
    if ( uniformRes != -1 )
      GLES20.glUniform2f(uniformRes,screenRes[0],
      screenRes[1]);
    Screen.Draw(ScreenShader[GLESTest.sha]);
  }
  @Override public void onSurfaceChanged( GL10 gl, int w, int h )
  {
    /* reajustar ventana */
    GLES20.glViewport(0,0,w,h);
    screenRes[0] = w;
    screenRes[1] = h;
    /* (re)generar FBO y texturas */
    VScreen.Update(w,h);
  }
}

/* clase para manejar FBO (renderizado a textura) */
class GLESTestScreen
{
  /* texturas de color y profundidad */
  public int[] screenTextures = {0,0};
  /* objeto de framebuffer */
  public int[] screenFBO = {0};
  /* resolucion interna */
  private int[] screenRes = {0,0};

  public GLESTestScreen()
  {
    /* generar texturas */
    GLES20.glGenTextures(2,screenTextures,0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,screenTextures[0]);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
      GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
      GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
      GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
      GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,screenTextures[1]);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
      GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
      GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
      GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
      GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
    /* generar FBO */
    GLES20.glGenFramebuffers(1,screenFBO,0);
    Enable();
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,
      GLES20.GL_COLOR_ATTACHMENT0,GLES20.GL_TEXTURE_2D,
      screenTextures[0],0);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,
      GLES20.GL_DEPTH_ATTACHMENT,GLES20.GL_TEXTURE_2D,
      screenTextures[1],0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
    Disable();
  }

  public void Update( int w, int h )
  {
    if ( (w == screenRes[0]) | (h == screenRes[1]) )
      return;
    FloatBuffer BlankBuffer0 = FloatBuffer.allocate(w*h*4);
    ShortBuffer BlankBuffer1 = ShortBuffer.allocate(w*h*2);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,screenTextures[0]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGBA,w,h,
      0,GLES20.GL_RGBA,GLES20.GL_FLOAT,
      BlankBuffer0.position(0));
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,screenTextures[1]);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,
      GLES20.GL_DEPTH_COMPONENT,w,h,0,
      GLES20.GL_DEPTH_COMPONENT,GLES20.GL_UNSIGNED_SHORT,
      BlankBuffer1.position(0));
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
  }

  public void Enable()
  {
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,screenFBO[0]);
  }

  public void Disable()
  {
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,0);
  }
}




Java Source Code List

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