Exemple #1
0
  /**
   * load, compile and link shader
   *
   * @param vss source of vertex shader
   * @param fss source of fragment shader
   * @return
   */
  public static int loadShader(final String vss, final String fss) {
    if (DEBUG) Log.v(TAG, "loadShader:");
    int vs = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vs, vss);
    GLES20.glCompileShader(vs);
    final int[] compiled = new int[1];
    GLES20.glGetShaderiv(vs, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
      if (DEBUG) Log.e(TAG, "Failed to compile vertex shader:" + GLES20.glGetShaderInfoLog(vs));
      GLES20.glDeleteShader(vs);
      vs = 0;
    }

    int fs = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fs, fss);
    GLES20.glCompileShader(fs);
    GLES20.glGetShaderiv(fs, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
      if (DEBUG) Log.w(TAG, "Failed to compile fragment shader:" + GLES20.glGetShaderInfoLog(fs));
      GLES20.glDeleteShader(fs);
      fs = 0;
    }

    final int program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vs);
    GLES20.glAttachShader(program, fs);
    GLES20.glLinkProgram(program);

    return program;
  }
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vertexShaderHandle, loadShader(R.raw.vertex_shader));
    GLES20.glCompileShader(vertexShaderHandle);
    int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fragmentShaderHandle, loadShader(R.raw.fragment_shader));
    GLES20.glCompileShader(fragmentShaderHandle);
    int programHandle = GLES20.glCreateProgram();
    GLES20.glAttachShader(programHandle, vertexShaderHandle);
    GLES20.glAttachShader(programHandle, fragmentShaderHandle);
    GLES20.glLinkProgram(programHandle);
    GLES20.glUseProgram(programHandle);
    Object3D.init(programHandle);
    mLightPosHandle = GLES20.glGetUniformLocation(programHandle, "uLightPos");

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glCullFace(GLES20.GL_BACK);

    //		GLES20.glEnable(GLES20.GL_BLEND);
    //		GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

    mPrism.init();
    mCylinder.init();
    mSphere.init();
  }
Exemple #3
0
  public static int getProgram(String vsSrc, String fsSrc) {
    // vertexShaderのコンパイル
    int vs = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vs, vsSrc);
    GLES20.glCompileShader(vs);

    // コンパイルチェック
    final int[] vsCompileStatus = new int[1];
    GLES20.glGetShaderiv(vs, GLES20.GL_COMPILE_STATUS, vsCompileStatus, 0);

    if (vsCompileStatus[0] == 0) {
      // コンパイル失敗
      GLES20.glDeleteShader(vs);
      vs = 0;
      // ログにエラーを出力
      Log.d("debug", "VERTEXSHADER_COMPILE_ERROR");
    }

    // fragmentShaderのコンパイル
    int fs = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fs, fsSrc);
    GLES20.glCompileShader(fs);

    // コンパイルチェック
    final int[] fsCompileStatus = new int[1];
    GLES20.glGetShaderiv(fs, GLES20.GL_COMPILE_STATUS, fsCompileStatus, 0);

    if (fsCompileStatus[0] == 0) {
      // コンパイル失敗
      GLES20.glDeleteShader(fs);
      fs = 0;
      Log.d("debug", "FRAGMENTSHADER_COMPILE_ERROR");
    }

    // shaderをリンクしてProgramを作る
    int shaderProgram;
    shaderProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(shaderProgram, vs);
    GLES20.glAttachShader(shaderProgram, fs);
    GLES20.glLinkProgram(shaderProgram);

    // リンク結果をチェック
    final int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(shaderProgram, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] == 0) {
      // リンク失敗
      GLES20.glDeleteProgram(shaderProgram);
      shaderProgram = 0;
      Log.d("debug", "LINK_ERROR");
    }
    return shaderProgram;
  }
Exemple #4
0
  /**
   * Create a vertex shader type (GLES20.GL_VERTEX_SHADER) or a fragment shader type
   * (GLES20.GL_FRAGMENT_SHADER)
   */
  public static int loadShader(int type, String shaderCode) {
    int shaderHandle = GLES20.glCreateShader(type);

    if (shaderHandle != 0) {
      GLES20.glShaderSource(shaderHandle, shaderCode);
      GLES20.glCompileShader(shaderHandle);

      // Get the compilation status.
      final int[] compileStatus = new int[1];
      GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        Log.v(
            OpenGLUtil.class.getCanonicalName(),
            "Shader fail info: " + GLES20.glGetShaderInfoLog(shaderHandle));
        GLES20.glDeleteShader(shaderHandle);
        shaderHandle = 0;
      }
    }

    if (shaderHandle == 0) {
      throw new RuntimeException("Error creating shader " + type);
    }

    return shaderHandle;
  }
  private int compileShader(final int shaderType, final String shaderSource) {
    int shaderHandle = GLES20.glCreateShader(shaderType);

    if (shaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(shaderHandle, shaderSource);

      // Compile the shader.
      GLES20.glCompileShader(shaderHandle);

      // Get the compilation status.
      final int[] compileStatus = new int[1];
      GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(shaderHandle);
        shaderHandle = 0;
      }
    }

    if (shaderHandle == 0) {
      throw new RuntimeException("Error creating shader.");
    }

    return shaderHandle;
  }
  public static int loadShader(int type, String shaderCode) {

    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    // Get the compilation status.
    final int[] compileStatus = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

    // If the compilation failed, delete the shader.
    if (compileStatus[0] == 0) {
      GLES20.glDeleteShader(shader);
      shader = 0;
    }

    if (shader == 0) {
      throw new RuntimeException("Error creating shader.");
    }

    return shader;
  }
Exemple #7
0
  int load(int type) throws GLException {
    if (loaded) {
      return handle;
    }
    handle = GLES20.glCreateShader(type);
    if (handle == 0) {
      int error = GameRenderer.logError("glCreateShader(" + type + ")");
      throw new GLException(error, "CreateShader failed");
    }
    GLES20.glShaderSource(handle, source);
    GameRenderer.logError("glShaderSource(handle, source)");
    GLES20.glCompileShader(handle);

    // Check compile status
    int[] compiled = {0};
    GLES20.glGetShaderiv(handle, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] != GLES20.GL_TRUE) {
      Log.e(TAG, "Error compiling shader. \n" + GLES20.glGetShaderInfoLog(handle));

      // Undo the shader
      GLES20.glDeleteShader(handle);
      return 0;
    }

    Log.d(TAG, "shader successfully compiled!");

    return handle;
  }
 // 加载制定shader的方法
 public static int loadShader(
     int shaderType, // shader的类型  GLES20.GL_VERTEX_SHADER   GLES20.GL_FRAGMENT_SHADER
     String source // shader的脚本字符串
     ) {
   // 创建一个新shader
   int shader = GLES20.glCreateShader(shaderType);
   // 若创建成功则加载shader
   if (shader != 0) {
     // 加载shader的源代码
     GLES20.glShaderSource(shader, source);
     // 编译shader
     GLES20.glCompileShader(shader);
     // 存放编译成功shader数量的数组
     int[] compiled = new int[1];
     // 获取Shader的编译情况
     GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
     if (compiled[0] == 0) { // 若编译失败则显示错误日志并删除此shader
       Log.e("ES20_ERROR", "Could not compile shader " + shaderType + ":");
       Log.e("ES20_ERROR", GLES20.glGetShaderInfoLog(shader));
       GLES20.glDeleteShader(shader);
       shader = 0;
     }
   }
   return shader;
 }
Exemple #9
0
  public void compile() {
    GLES20.glCompileShader(handle);

    int[] status = new int[1];
    GLES20.glGetShaderiv(handle, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] == GLES20.GL_FALSE) {
      throw new Error(GLES20.glGetShaderInfoLog(handle));
    }
  }
Exemple #10
0
 @Override
 public boolean compileShader(final int shader, final String source) {
   checkOpenGLThread();
   GLES20.glShaderSource(shader, source);
   GLES20.glCompileShader(shader);
   final int[] compiled = new int[1];
   GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
   return (compiled[0] != 0);
 }
Exemple #11
0
  public static int loadShader(int type, String shaderCode) {
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
  }
 // Compile & attach a |type| shader specified by |source| to |program|.
 private static void addShaderTo(int type, String source, int program) {
   int[] result = new int[] {GLES20.GL_FALSE};
   int shader = GLES20.glCreateShader(type);
   GLES20.glShaderSource(shader, source);
   GLES20.glCompileShader(shader);
   GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, result, 0);
   abortUnless(
       result[0] == GLES20.GL_TRUE, GLES20.glGetShaderInfoLog(shader) + ", source: " + source);
   GLES20.glAttachShader(program, shader);
   GLES20.glDeleteShader(shader);
   checkNoGLES2Error();
 }
  public static int loadShader(int type, String shaderCode) {

    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
  }
 public static int createAndCompileShader(int type, String code) throws ShaderException {
   int shader = GLES20.glCreateShader(type);
   GLES20.glShaderSource(shader, code);
   GLES20.glCompileShader(shader);
   String info = GLES20.glGetShaderInfoLog(shader);
   if (shader == 0)
     throw new ShaderException(
         "Failed to compile shader of type: " + String.valueOf(type) + "\n" + info);
   if (!info.isEmpty())
     throw new ShaderException(
         "Failed to compile shader of type: " + String.valueOf(type) + "\n" + info);
   return shader;
 }
 public static int LoadShader(String strSource, int iType) {
   Log.d("Utils", "LoadShader");
   int[] compiled = new int[1];
   int iShader = GLES20.glCreateShader(iType);
   GLES20.glShaderSource(iShader, strSource);
   GLES20.glCompileShader(iShader);
   GLES20.glGetShaderiv(iShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
   if (compiled[0] == 0) {
     Log.d("Load Shader Failed", "Compilation\n" + GLES20.glGetShaderInfoLog(iShader));
     return 0;
   }
   return iShader;
 }
 public static int compileShader(int type, String code) {
   int shader = GLES20.glCreateShader(type);
   GLES20.glShaderSource(shader, code);
   GLES20.glCompileShader(shader);
   final int[] status = new int[1];
   GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, status, 0);
   Log.e(TAG, "shader log " + GLES20.glGetShaderInfoLog(shader));
   if (status[0] == 0) {
     GLES20.glDeleteShader(shader);
     return 0;
   }
   return shader;
 }
Exemple #17
0
 private int loadShader(int type, String shaderCode) {
   int shader = GLES20.glCreateShader(type);
   GLES20.glShaderSource(shader, shaderCode);
   GLES20.glCompileShader(shader);
   int[] compiled = new int[1];
   GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
   if (compiled[0] == 0) { // もしコンパイルに失敗していたら
     android.util.Log.e("TAG", GLES20.glGetShaderInfoLog(shader)); // 何行目がどんなふうに間違ってるか吐き出す。
     GLES20.glDeleteShader(shader);
     return 0;
   }
   return shader;
 }
Exemple #18
0
  public int loadShader(int type, String shaderCode) {
    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    String logmsg = GLES20.glGetProgramInfoLog(shader);
    Log.d("ShaderCompile", logmsg);

    return shader;
  }
 /**
  * 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;
 }
  /** Compiles a shader, returning the OpenGL object ID. */
  private static int compileShader(int type, String shaderCode) {
    // Create a new shader object.
    final int shaderObjectId = GLES20.glCreateShader(type);

    if (shaderObjectId == 0) {
      if (LoggerConfig.ON) {
        Log.w(TAG, "Could not create new shader.");
      }

      return 0;
    }

    // Pass in the shader source.
    GLES20.glShaderSource(shaderObjectId, shaderCode);

    // Compile the shader.
    GLES20.glCompileShader(shaderObjectId);

    // Get the compilation status.
    final int[] compileStatus = new int[1];
    GLES20.glGetShaderiv(shaderObjectId, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

    if (LoggerConfig.ON) {
      // Print the shader info log to the Android log output.
      Log.v(
          TAG,
          "Results of compiling source:"
              + "\n"
              + shaderCode
              + "\n:"
              + GLES20.glGetShaderInfoLog(shaderObjectId));
    }

    // Verify the compile status.
    if (compileStatus[0] == 0) {
      // If it failed, delete the shader object.
      GLES20.glDeleteShader(shaderObjectId);

      if (LoggerConfig.ON) {
        Log.w(TAG, "Compilation of shader failed.");
      }

      return 0;
    }

    // Return the shader object ID.
    return shaderObjectId;
  }
 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) {
       Log.e(TAG, "Could not compile shader " + shaderType + ":");
       Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
       GLES20.glDeleteShader(shader);
       shader = 0;
     }
   }
   return shader;
 }
 private static int loadShader(int shaderType, String shaderSource) {
   int shader = GLES20.glCreateShader(shaderType);
   if (shader != 0) {
     GLES20.glShaderSource(shader, shaderSource);
     GLES20.glCompileShader(shader);
     int[] result = new int[1];
     GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, result, 0);
     if (result[0] == 0) {
       Log.w(TAG, "加载Shader失败 shaderTyp=" + shaderType);
       Log.w(TAG, GLES20.glGetShaderInfoLog(shader));
       GLES20.glDeleteShader(shader);
       shader = 0;
     }
   }
   return shader;
 }
  private int loadGLShader(int type, String code) {
    int shader = GLES20.glCreateShader(type);
    GLES20.glShaderSource(shader, code);
    GLES20.glCompileShader(shader);

    // Get the compilation status.
    final int[] compileStatus = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

    // If the compilation failed, delete the shader.
    if (compileStatus[0] == 0) {
      GLES20.glDeleteShader(shader);
      shader = 0;
    }

    if (shader == 0) {
      throw new RuntimeException("Error creating shader.");
    }

    return shader;
  }
Exemple #24
0
 /**
  * Loads the shader from a text string and then compiles it.
  *
  * @param shaderType
  * @param source
  * @return
  */
 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) {
       RajLog.e(
           "["
               + getClass().getName()
               + "] Could not compile "
               + (shaderType == GLES20.GL_FRAGMENT_SHADER ? "fragment" : "vertex")
               + " shader:");
       RajLog.e("Shader log: " + GLES20.glGetShaderInfoLog(shader));
       GLES20.glDeleteShader(shader);
       shader = 0;
     }
   }
   return shader;
 }
 private int loadShader(final int shaderType, final String source) {
   int shader = GLES20.glCreateShader(shaderType);
   if (shader != 0) {
     GLES20.glShaderSource(shader, source);
     GLES20.glCompileShader(shader);
     final int[] compiled = {0};
     GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
     if (compiled[0] == 0) {
       Log.e(
           "DistortionRenderer",
           new StringBuilder(37)
               .append("Could not compile shader ")
               .append(shaderType)
               .append(":")
               .toString());
       Log.e("DistortionRenderer", GLES20.glGetShaderInfoLog(shader));
       GLES20.glDeleteShader(shader);
       shader = 0;
     }
   }
   return shader;
 }
  /**
   * Utility method for compiling a OpenGL shader.
   *
   * <p><strong>Note:</strong> When developing shaders, use the checkGlError() method to debug
   * shader coding errors.
   *
   * @param type - Vertex or fragment shader type.
   * @param shaderCode - String containing the shader code.
   * @return - Returns an mId for the shader.
   */
  public static int loadShader(int type, String shaderCode) {

    // Create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);
    checkGlError("glCreateShader type=" + type);

    // Add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    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 " + type + ":");
      Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader));
      GLES20.glDeleteShader(shader);
      shader = 0;
    }

    return shader;
  }
Exemple #27
0
 @Override
 public void glCompileShader(int shader) {
   GLES20.glCompileShader(shader);
 }
  /** Method for creating the most basic of shaders. */
  private void createShaders() {
    final String vertexShader =
        "uniform mat4 u_MVPMatrix;      \n"
            + "attribute vec4 a_Position;     \n"
            + "attribute vec4 a_Color;        \n"
            + "varying vec4 v_Color;          \n"
            + "void main()                    \n"
            + "{                              \n"
            + "   v_Color = a_Color;          \n"
            + "   gl_Position = u_MVPMatrix   \n"
            + "               * a_Position;   \n"
            + "}                              \n";

    final String fragmentShader =
        "precision mediump float;       \n"
            + "varying vec4 v_Color;          \n"
            + "void main()                    \n"
            + "{                              \n"
            + "   gl_FragColor = v_Color;     \n"
            + "}                              \n";

    // Load in the vertex shader.
    int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);

    if (vertexShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(vertexShaderHandle, vertexShader);

      // Compile the shader.
      GLES20.glCompileShader(vertexShaderHandle);

      // Get the compilation status.
      final int[] compileStatus = new int[1];
      GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(vertexShaderHandle);
        vertexShaderHandle = 0;
      }
    }

    if (vertexShaderHandle == 0) {
      throw new RuntimeException("Error creating vertex shader.");
    }

    // Load in the fragment shader shader.
    int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

    if (fragmentShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);

      // Compile the shader.
      GLES20.glCompileShader(fragmentShaderHandle);

      // Get the compilation status.
      final int[] compileStatus = new int[1];
      GLES20.glGetShaderiv(fragmentShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(fragmentShaderHandle);
        fragmentShaderHandle = 0;
      }
    }

    if (fragmentShaderHandle == 0) {
      throw new RuntimeException("Error creating fragment shader.");
    }

    // Create a program object and store the handle to it.
    int programHandle = GLES20.glCreateProgram();

    if (programHandle != 0) {
      // Bind the vertex shader to the program.
      GLES20.glAttachShader(programHandle, vertexShaderHandle);

      // Bind the fragment shader to the program.
      GLES20.glAttachShader(programHandle, fragmentShaderHandle);

      // Bind attributes
      GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
      GLES20.glBindAttribLocation(programHandle, 1, "a_Color");

      // Link the two shaders together into a program.
      GLES20.glLinkProgram(programHandle);

      // Get the link status.
      final int[] linkStatus = new int[1];
      GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

      // If the link failed, delete the program.
      if (linkStatus[0] == 0) {
        GLES20.glDeleteProgram(programHandle);
        programHandle = 0;
      }
    }

    if (programHandle == 0) {
      throw new RuntimeException("Error creating program.");
    }

    // Set program handles. These will later be used to pass in values to the program.
    mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
    mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
    mColorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");

    // Tell OpenGL to use this program when rendering.
    GLES20.glUseProgram(programHandle);
  }
  @Override
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {

    SurfaceCreated(glUnused, config);
    // Set the background clear color to gray.
    GLES20.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);

    // Position the eye behind the origin.
    final float eyeX = 0.0f;
    final float eyeY = 0.0f;
    final float eyeZ = 1.5f;

    // We are looking toward the distance
    final float lookX = 0.0f;
    final float lookY = 0.0f;
    final float lookZ = -5.0f;

    // Set our up vector. This is where our head would be pointing were we holding the camera.
    final float upX = 0.0f;
    final float upY = 1.0f;
    final float upZ = 0.0f;

    // Set the view matrix. This matrix can be said to represent the camera position.
    // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
    // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
    Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);

    final String vertexShader =
        "uniform mat4 u_MVPMatrix;      \n" // A constant representing the combined
            // model/view/projection matrix.
            + "attribute vec4 a_Position;     \n" // Per-vertex position information we will pass
            // in.
            + "attribute vec4 a_Color;        \n" // Per-vertex color information we will pass in.
            + "varying vec4 v_Color;          \n" // This will be passed into the fragment shader.
            + "void main()                    \n" // The entry point for our vertex shader.
            + "{                              \n"
            + "   v_Color = a_Color;          \n" // Pass the color through to the fragment shader.
            // It will be interpolated across the triangle.
            + "   gl_Position = u_MVPMatrix   \n" // gl_Position is a special variable used to store
            // the final position.
            + "               * a_Position;   \n" // Multiply the vertex by the matrix to get the
            // final point in
            + "}                              \n"; // normalized screen coordinates.

    final String fragmentShader =
        "precision mediump float;       \n" // Set the default precision to medium. We don't need as
            // high of a
            // precision in the fragment shader.
            + "varying vec4 v_Color;          \n" // This is the color from the vertex shader
            // interpolated across the
            // triangle per fragment.
            + "void main()                    \n" // The entry point for our fragment shader.
            + "{                              \n"
            + "   gl_FragColor = v_Color;     \n" // Pass the color directly through the pipeline.
            + "}                              \n";

    // Load in the vertex shader.
    int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);

    if (vertexShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(vertexShaderHandle, vertexShader);

      // Compile the shader.
      GLES20.glCompileShader(vertexShaderHandle);

      // Get the compilation status.
      final int[] compileStatus = new int[1];
      GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(vertexShaderHandle);
        vertexShaderHandle = 0;
      }
    }

    if (vertexShaderHandle == 0) {
      throw new RuntimeException("Error creating vertex shader.");
    }

    // Load in the fragment shader shader.
    int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);

    if (fragmentShaderHandle != 0) {
      // Pass in the shader source.
      GLES20.glShaderSource(fragmentShaderHandle, fragmentShader);

      // Compile the shader.
      GLES20.glCompileShader(fragmentShaderHandle);

      // Get the compilation status.
      final int[] compileStatus = new int[1];
      GLES20.glGetShaderiv(fragmentShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

      // If the compilation failed, delete the shader.
      if (compileStatus[0] == 0) {
        GLES20.glDeleteShader(fragmentShaderHandle);
        fragmentShaderHandle = 0;
      }
    }

    if (fragmentShaderHandle == 0) {
      throw new RuntimeException("Error creating fragment shader.");
    }

    // Create a program object and store the handle to it.
    int programHandle = GLES20.glCreateProgram();

    if (programHandle != 0) {
      // Bind the vertex shader to the program.
      GLES20.glAttachShader(programHandle, vertexShaderHandle);

      // Bind the fragment shader to the program.
      GLES20.glAttachShader(programHandle, fragmentShaderHandle);

      // Bind attributes
      GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
      GLES20.glBindAttribLocation(programHandle, 1, "a_Color");

      // Link the two shaders together into a program.
      GLES20.glLinkProgram(programHandle);

      // Get the link status.
      final int[] linkStatus = new int[1];
      GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

      // If the link failed, delete the program.
      if (linkStatus[0] == 0) {
        GLES20.glDeleteProgram(programHandle);
        programHandle = 0;
      }
    }

    if (programHandle == 0) {
      throw new RuntimeException("Error creating program.");
    }

    // Set program handles. These will later be used to pass in values to the program.
    mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
    mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
    mColorHandle = GLES20.glGetAttribLocation(programHandle, "a_Color");

    // Tell OpenGL to use this program when rendering.
    GLES20.glUseProgram(programHandle);
  }
Exemple #30
0
 public static int loadShader(int type, String shaderCode) {
   int shader = GLES20.glCreateShader(type);
   GLES20.glShaderSource(shader, shaderCode);
   GLES20.glCompileShader(shader);
   return shader;
 }