• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.mediafx;
18 
19 import android.opengl.GLES20;
20 
21 public class GLToolbox {
22 
loadShader(int shaderType, String source)23     public static int loadShader(int shaderType, String source) {
24         int shader = GLES20.glCreateShader(shaderType);
25         if (shader != 0) {
26             GLES20.glShaderSource(shader, source);
27             GLES20.glCompileShader(shader);
28             int[] compiled = new int[1];
29             GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
30             if (compiled[0] == 0) {
31                 String info = GLES20.glGetShaderInfoLog(shader);
32                 GLES20.glDeleteShader(shader);
33                 shader = 0;
34                 throw new RuntimeException("Could not compile shader " +
35                 shaderType + ":" + info);
36             }
37         }
38         return shader;
39     }
40 
createProgram(String vertexSource, String fragmentSource)41     public static int createProgram(String vertexSource,
42             String fragmentSource) {
43         int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
44         if (vertexShader == 0) {
45             return 0;
46         }
47         int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
48         if (pixelShader == 0) {
49             return 0;
50         }
51 
52         int program = GLES20.glCreateProgram();
53         if (program != 0) {
54             GLES20.glAttachShader(program, vertexShader);
55             checkGlError("glAttachShader");
56             GLES20.glAttachShader(program, pixelShader);
57             checkGlError("glAttachShader");
58             GLES20.glLinkProgram(program);
59             int[] linkStatus = new int[1];
60             GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus,
61                     0);
62             if (linkStatus[0] != GLES20.GL_TRUE) {
63                 String info = GLES20.glGetProgramInfoLog(program);
64                 GLES20.glDeleteProgram(program);
65                 program = 0;
66                 throw new RuntimeException("Could not link program: " + info);
67             }
68         }
69         return program;
70     }
71 
checkGlError(String op)72     public static void checkGlError(String op) {
73         int error;
74         while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
75             throw new RuntimeException(op + ": glError " + error);
76         }
77     }
78 
initTexParams()79     public static void initTexParams() {
80         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
81                 GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
82         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
83                 GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
84         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
85                 GLES20.GL_CLAMP_TO_EDGE);
86         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
87                 GLES20.GL_CLAMP_TO_EDGE);
88    }
89 
90 }
91