opengl esは三角形を描きます。
勉強を始めたばかりです。呼び出しの順番を記録してください。コースは全部同じです。
記事の参考:http://blog.csdn.net/column/details/openges.html
1、ShaderUtilを作成してglslをコンパイルします。
記事の参考:http://blog.csdn.net/column/details/openges.html
1、ShaderUtilを作成してglslをコンパイルします。
public class ShaderUtil {
public static int compileVertexShader(String vertexShaderCode) throws RuntimeException {
return compileShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
}
public static int compileFragmentShader(String fragmentShaderCode) throws RuntimeException {
return compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
}
private static int compileShader(int type, String shaderCode) throws RuntimeException {
// 1、 shader
int shaderID = GLES20.glCreateShader(type);
if (shaderID == 0) {
throw new RuntimeException(" ,type:" + type);
}
// 2、
GLES20.glShaderSource(shaderID, shaderCode);
// 3、
GLES20.glCompileShader(shaderID);
// 4、
int[] compileStatus = new int[1];
GLES20.glGetShaderiv(shaderID, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
// 5、 ,
if (compileStatus[0] == 0) {
GLES20.glDeleteShader(shaderID);
throw new RuntimeException(" :" + GLES20.glGetShaderInfoLog(shaderID));
}
return shaderID;
}
public static int buildProgram(String vertexShaderCode, String fragmentShaderCode) throws RuntimeException {
// 1、
int vertexShader = compileVertexShader(vertexShaderCode);
int fragmentShader = compileFragmentShader(fragmentShaderCode);
// 2、 program
int program = linkProgram(vertexShader, fragmentShader);
// 3、 program
validateProgram(program);
return program;
}
private static void validateProgram(int program) throws RuntimeException {
}
private static int linkProgram(int vertexShader, int fragmentShader) throws RuntimeException {
// 1、 program
int program = GLES20.glCreateProgram();
if (program == 0) {
throw new RuntimeException(" program ");
}
// 2、attach
GLES20.glAttachShader(program, vertexShader);
//3、attach
GLES20.glAttachShader(program, fragmentShader);
// 4、 program
GLES20.glLinkProgram(program);
// 5、
int[] linkStatus = new int[1];
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
//
if (linkStatus[0] == 0) {
GLES20.glDeleteProgram(program);
throw new RuntimeException(" ");
}
return program;
}
}
2、Tranglel 1クラスを作成して、三角形を表します。public class Triangle1 {
private float[] vertexs = {
0, 0.5f,
-0.5f, -0.5f,
0.5f, -0.5f
};
private int vertexLocationCount = 2;//
private FloatBuffer vertexBuffer;
private Context context;
private int program;
private int vertexPosition;
private int colorPosition;
public Triangle1(Context context) {
this.context = context;
// :
vertexBuffer = ByteBuffer.allocateDirect(vertexs.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexBuffer.put(vertexs);
vertexBuffer.position(0);
// :
initShader();
// : id
vertexPosition = GLES20.glGetAttribLocation(program, "a_Position");
colorPosition = GLES20.glGetUniformLocation(program, "u_Color");
// :
GLES20.glVertexAttribPointer(vertexPosition, vertexLocationCount, GLES20.GL_FLOAT, false, 0, vertexBuffer);
GLES20.glEnableVertexAttribArray(vertexPosition);
}
private void initShader() {
String vertexCode = TextResourceReader.readTextFileFromResource(context, R.raw.simple_vertex_shader);
String fragmentCode = TextResourceReader.readTextFileFromResource(context, R.raw.simple_fragment_shader);
program = ShaderUtil.buildProgram(vertexCode, fragmentCode);
GLES20.glUseProgram(program);
}
public void draw() {
GLES20.glUniform4f(colorPosition, 0, 0, 1, 1);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
}
3、GLOSurefaceViewを作成します。@SuppressLint("WrongViewCast")
public class GLSureViewActivity extends Activity {
private GLSurfaceView surfaceView;
private Triangle1 triangle;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opengl_es);
surfaceView = findViewById(R.id.gl);
surfaceView.setEGLContextClientVersion(2);
surfaceView.setRenderer(renderer);
surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
private GLSurfaceView.Renderer renderer = new GLSurfaceView.Renderer() {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// , , alpha
glClearColor(1, 0, 0, 0);
triangle = new Triangle1(GLSureViewActivity.this);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// , opengl surface
glViewport(0, 0, width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
//Third: , , glClearColor
glClear(GL_COLOR_BUFFER_BIT);
//
triangle.draw();
}
};
}