Android Open Source - splott Shader






From Project

Back to project page splott.

License

The source code is released under:

MIT License

If you think the Android project splott 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.wordsaretoys.rise.glwrapper;
//from www.  j  a v  a  2s .  co m
import android.opengl.GLES20;
import android.util.Log;


/**
 * maintains a shader program consisting of one
 * vertex shader function and one fragment shader
 * function. 
 * 
 * @author chris
 *
 */
public class Shader {

  private int vobj;
  private int fobj;
  private int program;
  
  public Shader() {
  }

  /**
   * generates GL shader program from source code
   * 
   * @param vertex source code for vertex shader
   * @param fragment source code for fragment shader
   * @throws Exception 
   */
  public boolean build(String vertex, String fragment) {
    String error;
    int[] status = new int[1];
    
    // compile the vertex shader
    vobj = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vobj, vertex);
    GLES20.glCompileShader(vobj);
    
    // check the compilation
    GLES20.glGetShaderiv(vobj, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
      error = GLES20.glGetShaderInfoLog(vobj);
      String reason = "vertex shader compile error: " + error;
      Log.e("shader", reason);
      return false;
    }

    // compile the fragment shader
    fobj =  GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fobj, fragment);
    GLES20.glCompileShader(fobj);

    // check the compilation
    GLES20.glGetShaderiv(fobj, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == 0) {
      error = GLES20.glGetShaderInfoLog(fobj);
      String reason = "fragment shader compile error: " + error;
      Log.e("shader", reason);
      return false;
    }

    // create and link the shader program
    program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vobj);
    GLES20.glAttachShader(program, fobj);
    GLES20.glLinkProgram(program);

    // check the linkage
    GLES20.glGetShaderiv(program, GLES20.GL_LINK_STATUS, status, 0);
    if (status[0] == 0) {
      error = GLES20.glGetProgramInfoLog(program);
      String reason = "shader program link error: " + error;
      Log.e("shader", reason);
      return false;
    }
    
    return true;
  }
  
  /**
   * get attribute location id
   * @param name attribute variable name
   * @return id
   */
  public int getAttributeId(String name) {
    return GLES20.glGetAttribLocation(program, name);
  }
  
  /**
   * get uniform/sampler location id
   * @param name uniform variable name
   * @return id
   */
  public int getUniformId(String name) {
    return GLES20.glGetUniformLocation(program, name);
  }
  
  /**
   * activate the shader program
   */
  public void activate() {
    GLES20.glUseProgram(program);
  }
  
  /**
   * release the shader program
   */
  public void release() {
    GLES20.glDeleteShader(vobj);
    GLES20.glDeleteShader(fobj);
    GLES20.glDeleteProgram(program);
  }
  
  /**
   * set up a matrix
   */
  public void setMatrix(String label, float[] m) {
    GLES20.glUniformMatrix4fv(
        getUniformId(label), 1, false, m, 0);
  }
}




Java Source Code List

com.wordsaretoys.rise.geometry.Camera.java
com.wordsaretoys.rise.geometry.Geom.java
com.wordsaretoys.rise.geometry.Mote.java
com.wordsaretoys.rise.geometry.Ortho.java
com.wordsaretoys.rise.geometry.Quaternion.java
com.wordsaretoys.rise.geometry.Vector.java
com.wordsaretoys.rise.glwrapper.Mesh.java
com.wordsaretoys.rise.glwrapper.Shader.java
com.wordsaretoys.rise.glwrapper.Texture.java
com.wordsaretoys.rise.meshutil.HeightMapper.java
com.wordsaretoys.rise.meshutil.IndexBuffer.java
com.wordsaretoys.rise.meshutil.SurfaceMapper.java
com.wordsaretoys.rise.meshutil.VertexBuffer.java
com.wordsaretoys.rise.meshutil.Vindexer.java
com.wordsaretoys.rise.pattern.Bitmap.java
com.wordsaretoys.rise.pattern.F2FSumMap.java
com.wordsaretoys.rise.pattern.I2FCutMap.java
com.wordsaretoys.rise.pattern.I2FMap.java
com.wordsaretoys.rise.pattern.I2IMap.java
com.wordsaretoys.rise.pattern.Pattern.java
com.wordsaretoys.rise.pattern.Ring.java
com.wordsaretoys.rise.utility.Asset.java
com.wordsaretoys.rise.utility.Board.java
com.wordsaretoys.rise.utility.Dbg.java
com.wordsaretoys.rise.utility.Interval.java
com.wordsaretoys.rise.utility.Misc.java
com.wordsaretoys.rise.utility.Needle.java
com.wordsaretoys.splott.MainActivity.java
com.wordsaretoys.splott.parser.Compiler.java
com.wordsaretoys.splott.parser.SurfaceBaseListener.java
com.wordsaretoys.splott.parser.SurfaceLexer.java
com.wordsaretoys.splott.parser.SurfaceListener.java
com.wordsaretoys.splott.parser.SurfaceParser.java
com.wordsaretoys.splott.parser.SyntaxChecker.java
com.wordsaretoys.splott.parser.Vm.java
com.wordsaretoys.splott.plotter.GlView.java
com.wordsaretoys.splott.plotter.Render.java
com.wordsaretoys.splott.plotter.Surface.java