Android Open Source - android_glsl Glsl Shader






From Project

Back to project page android_glsl.

License

The source code is released under:

Apache License

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

/*
   Copyright 2011 Harri Sm?tt/*from   w  w w .jav  a 2s. co  m*/

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package fi.harism.glsl;

import java.util.HashMap;

import android.opengl.GLES20;
import android.util.Log;

/**
 * Helper class for handling shaders.
 */
public final class GlslShader {

  // Shader program handle.
  private int mProgram = -1;
  // HashMap for storing uniform/attribute handles.
  private final HashMap<String, Integer> mShaderHandleMap = new HashMap<String, Integer>();

  /**
   * Get id for given handle name. This method checks for both attribute and
   * uniform handles.
   * 
   * @param name
   *            Name of handle.
   * @return Id for given handle or -1 if none found.
   */
  public int getHandle(String name) {
    if (mShaderHandleMap.containsKey(name)) {
      return mShaderHandleMap.get(name);
    }
    int handle = GLES20.glGetAttribLocation(mProgram, name);
    if (handle == -1) {
      handle = GLES20.glGetUniformLocation(mProgram, name);
    }
    if (handle == -1) {
      // One should never leave log messages but am not going to follow
      // this rule. This line comes handy if you see repeating 'not found'
      // messages on LogCat - usually for typos otherwise annoying to
      // spot from shader code.
      Log.d("GlslShader", "Could not get attrib location for " + name);
    } else {
      mShaderHandleMap.put(name, handle);
    }
    return handle;
  }

  /**
   * Get array of ids with given names. Returned array is sized to given
   * amount name elements.
   * 
   * @param names
   *            List of handle names.
   * @return array of handle ids.
   */
  public int[] getHandles(String... names) {
    int[] res = new int[names.length];
    for (int i = 0; i < names.length; ++i) {
      res[i] = getHandle(names[i]);
    }
    return res;
  }

  /**
   * Compiles vertex and fragment shaders and links them into a program one
   * can use for rendering. Once OpenGL context is lost and onSurfaceCreated
   * is called, there is no need to reset existing GlslShader objects but one
   * can simply reload shader.
   * 
   * @param vertexSource
   *            String presentation for vertex shader
   * @param fragmentSource
   *            String presentation for fragment shader
   */
  public void setProgram(String vertexSource, String fragmentSource) {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
        fragmentSource);
    int program = GLES20.glCreateProgram();
    if (program != 0) {
      GLES20.glAttachShader(program, vertexShader);
      GLES20.glAttachShader(program, fragmentShader);
      GLES20.glLinkProgram(program);
      int[] linkStatus = new int[1];
      GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
      if (linkStatus[0] != GLES20.GL_TRUE) {
        String error = GLES20.glGetProgramInfoLog(program);
        GLES20.glDeleteProgram(program);
        throw new RuntimeException(error);
      }
    }
    mProgram = program;
    mShaderHandleMap.clear();
  }

  /**
   * Activates this shader program.
   */
  public void useProgram() {
    GLES20.glUseProgram(mProgram);
  }

  /**
   * Helper method for compiling a shader.
   * 
   * @param shaderType
   *            Type of shader to compile
   * @param source
   *            String presentation for shader
   * @return id for compiled shader
   */
  private int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    if (shader != 0) {
      GLES20.glShaderSource(shader, source);
      GLES20.glCompileShader(shader);
      int[] compiled = new int[1];
      GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
      if (compiled[0] == 0) {
        String error = GLES20.glGetShaderInfoLog(shader);
        GLES20.glDeleteShader(shader);
        throw new RuntimeException(error);
      }
    }
    return shader;
  }

}




Java Source Code List

fi.harism.glsl.GlslActivity.java
fi.harism.glsl.GlslCamera.java
fi.harism.glsl.GlslFbo.java
fi.harism.glsl.GlslFilter.java
fi.harism.glsl.GlslMatrix.java
fi.harism.glsl.GlslRenderer.java
fi.harism.glsl.GlslShader.java
fi.harism.glsl.prefs.GlslPreferenceActivity.java
fi.harism.glsl.prefs.GlslSliderPreference.java
fi.harism.glsl.scene.GlslAnimator.java
fi.harism.glsl.scene.GlslBox.java
fi.harism.glsl.scene.GlslLight.java
fi.harism.glsl.scene.GlslObject.java
fi.harism.glsl.scene.GlslScene.java
fi.harism.glsl.scene.GlslShaderIds.java