Android Open Source - Tanks Shader






From Project

Back to project page Tanks.

License

The source code is released under:

MIT License

If you think the Android project Tanks 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.ThirtyNineEighty.Renderable;
//from  ww w .  j a  va 2  s  .  c o  m
import android.opengl.GLES20;
import android.util.Log;

import com.ThirtyNineEighty.System.GameContext;

import java.io.IOException;
import java.io.InputStream;

public abstract class Shader
{
  protected static Shader current;
  private static Shader shader2D;
  private static Shader shader3D;

  protected int vertexShaderHandle;
  protected int fragmentShaderHandle;
  protected int programHandle;

  public abstract void compile();
  protected abstract void getLocations();

  public static Shader getCurrent()
  {
    return current;
  }

  public static void initShader3D()
  {
    if (shader3D != null)
      shader3D.deleteProgram();

    shader3D = new Shader3D();
    shader3D.compile();
  }

  public static void initShader2D()
  {
    if (shader2D != null)
      shader2D.deleteProgram();

    shader2D = new Shader2D();
    shader2D.compile();
  }

  public static void setShader3D()
  {
    if (current == shader3D)
      return;

    current = shader3D;
    GLES20.glUseProgram(shader3D.programHandle);
  }

  public static void setShader2D()
  {
    if (current == shader2D)
      return;

    current = shader2D;
    GLES20.glUseProgram(shader2D.programHandle);
  }

  public boolean validateProgram()
  {
    if (!GameContext.isDebuggable())
      return true;

    int[] result = new int[1];

    GLES20.glValidateProgram(programHandle);
    GLES20.glGetProgramiv(programHandle, GLES20.GL_VALIDATE_STATUS, result, 0);

    boolean validated = result[0] != 0;
    if (!validated)
    {
      Log.e("Error", "Program do not validated!");

      if (!GLES20.glIsProgram(programHandle))
        Log.e("Error", "Program handle deprecated!");
    }

    return validated;
  }

  protected void compile(String vertexFileName, String fragmentFileName)
  {
    Log.d("Shader", String.format("compiler called for [vertex: %s, fragment: %s]", vertexFileName, fragmentFileName));

    vertexShaderHandle    = compileShader(GLES20.GL_VERTEX_SHADER, vertexFileName);
    fragmentShaderHandle  = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentFileName);
    programHandle = GLES20.glCreateProgram();

    GLES20.glAttachShader(programHandle, vertexShaderHandle);
    GLES20.glAttachShader(programHandle, fragmentShaderHandle);
    GLES20.glLinkProgram(programHandle);

    GLES20.glReleaseShaderCompiler();

    getLocations();
  }

  private int compileShader(int type, String path)
  {
    int shaderHandle = 0;

    try
    {
      //load shader source
      shaderHandle = GLES20.glCreateShader(type);
      InputStream stream = GameContext.getAppContext().getAssets().open(path);
      int size = stream.available();
      byte[] buffer = new byte[size];
      int readCount = stream.read(buffer);

      if (readCount != size)
        Log.e("Error", "file read not fully: " + path);

      String source = new String(buffer);
      stream.close();

      //compile shader
      GLES20.glShaderSource(shaderHandle, source);
      GLES20.glCompileShader(shaderHandle);

      //check for errors
      int[] compiled = new int[1];
      GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compiled, 0);
      if (compiled[0] == 0)
      {
        Log.e("Error", GLES20.glGetShaderInfoLog(shaderHandle));
        GLES20.glDeleteShader(shaderHandle);
      }
    }
    catch(IOException e)
    {
      Log.e("Error", e.getMessage());
    }

    return shaderHandle;
  }

  private void deleteProgram()
  {
    GLES20.glDeleteShader(vertexShaderHandle);
    GLES20.glDeleteShader(fragmentShaderHandle);
    GLES20.glDeleteProgram(programHandle);
  }
}




Java Source Code List

com.ThirtyNineEighty.Game.EngineObject.java
com.ThirtyNineEighty.Game.IEngineObject.java
com.ThirtyNineEighty.Game.Collisions.Collidable.java
com.ThirtyNineEighty.Game.Collisions.Collision2D.java
com.ThirtyNineEighty.Game.Collisions.Collision3D.java
com.ThirtyNineEighty.Game.Collisions.CollisionManager.java
com.ThirtyNineEighty.Game.Collisions.Collision.java
com.ThirtyNineEighty.Game.Collisions.ICollidable.java
com.ThirtyNineEighty.Game.Gameplay.Bullet.java
com.ThirtyNineEighty.Game.Gameplay.GameObject.java
com.ThirtyNineEighty.Game.Gameplay.Tank.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.CharacteristicFactory.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.Characteristic.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.Upgrade.java
com.ThirtyNineEighty.Game.Menu.BaseMenu.java
com.ThirtyNineEighty.Game.Menu.GameMenu.java
com.ThirtyNineEighty.Game.Menu.IMenu.java
com.ThirtyNineEighty.Game.Menu.Controls.Button.java
com.ThirtyNineEighty.Game.Menu.Controls.IControl.java
com.ThirtyNineEighty.Game.Worlds.GameWorld.java
com.ThirtyNineEighty.Game.Worlds.IWorld.java
com.ThirtyNineEighty.Helpers.Plane.java
com.ThirtyNineEighty.Helpers.Vector2.java
com.ThirtyNineEighty.Helpers.Vector3.java
com.ThirtyNineEighty.Helpers.VectorUtils.java
com.ThirtyNineEighty.Helpers.Vector.java
com.ThirtyNineEighty.Renderable.Renderable.java
com.ThirtyNineEighty.Renderable.Shader2D.java
com.ThirtyNineEighty.Renderable.Shader3D.java
com.ThirtyNineEighty.Renderable.Shader.java
com.ThirtyNineEighty.Renderable.Renderable2D.I2DRenderable.java
com.ThirtyNineEighty.Renderable.Renderable2D.Label.java
com.ThirtyNineEighty.Renderable.Renderable2D.Sprite.java
com.ThirtyNineEighty.Renderable.Renderable3D.I3DRenderable.java
com.ThirtyNineEighty.Renderable.Renderable3D.Model3D.java
com.ThirtyNineEighty.System.ConfigChooser.java
com.ThirtyNineEighty.System.Content.java
com.ThirtyNineEighty.System.GameActivity.java
com.ThirtyNineEighty.System.GameContext.java
com.ThirtyNineEighty.System.IContent.java
com.ThirtyNineEighty.System.ISubprogram.java