Exemplo n.º 1
0
  /**
   * Creates a new program from the supplied vertex and fragment shaders.
   *
   * @return A handle to the program, or 0 on failure.
   */
  public static int createProgram(String vertexSource, String fragmentSource) {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader == 0) {
      return 0;
    }
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (pixelShader == 0) {
      return 0;
    }

    int program = GLES20.glCreateProgram();
    checkGlError("glCreateProgram");
    if (program == 0) {
      Log.e(TAG, "Could not create program");
    }
    GLES20.glAttachShader(program, vertexShader);
    checkGlError("glAttachShader");
    GLES20.glAttachShader(program, pixelShader);
    checkGlError("glAttachShader");
    GLES20.glLinkProgram(program);
    int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] != GLES20.GL_TRUE) {
      Log.e(TAG, "Could not link program: ");
      Log.e(TAG, GLES20.glGetProgramInfoLog(program));
      GLES20.glDeleteProgram(program);
      program = 0;
    }
    return program;
  }
Exemplo n.º 2
0
  /**
   * Creates a texture from raw data.
   *
   * @param data Image data, in a "direct" ByteBuffer.
   * @param width Texture width, in pixels (not bytes).
   * @param height Texture height, in pixels.
   * @param format Image data format (use constant appropriate for glTexImage2D(), e.g. GL_RGBA).
   * @return Handle to texture.
   */
  public static int createImageTexture(ByteBuffer data, int width, int height, int format) {
    int[] textureHandles = new int[1];
    int textureHandle;

    GLES20.glGenTextures(1, textureHandles, 0);
    textureHandle = textureHandles[0];
    GlUtil.checkGlError("glGenTextures");

    // Bind the texture handle to the 2D texture target.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle);

    // Configure min/mag filtering, i.e. what scaling method do we use if what we're rendering
    // is smaller or larger than the source image.
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GlUtil.checkGlError("loadImageTexture");

    // Load the data from the buffer into the texture handle.
    GLES20.glTexImage2D(
        GLES20.GL_TEXTURE_2D, /*level*/
        0,
        format,
        width,
        height, /*border*/
        0,
        format,
        GLES20.GL_UNSIGNED_BYTE,
        data);
    GlUtil.checkGlError("loadImageTexture");

    return textureHandle;
  }
Exemplo n.º 3
0
 /**
  * Compiles the provided shader source.
  *
  * @return A handle to the shader, or 0 on failure.
  */
 public static int loadShader(int shaderType, String source) {
   int shader = GLES20.glCreateShader(shaderType);
   checkGlError("glCreateShader type=" + shaderType);
   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) {
     Log.e(TAG, "Could not compile shader " + shaderType + ":");
     Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
     GLES20.glDeleteShader(shader);
     shader = 0;
   }
   return shader;
 }