Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.nk33785.project3;
- import android.content.Context;
- import android.opengl.GLES20;
- import android.opengl.GLSurfaceView;
- import android.opengl.Matrix;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import yuku.ambilwarna.AmbilWarnaDialog;
- public class MyRenderer implements GLSurfaceView.Renderer {
- //region Fields
- private FloatBuffer triangleVB;
- public float angle;
- private final String vertexShaderCode =
- "uniform mat4 uMVPMatrix; \n"
- + "attribute vec4 vPosition; \n"
- + "void main(){ \n"
- +" gl_Position = uMVPMatrix * vPosition; \n"
- + "} \n";
- private final String fragmentShaderCode =
- "precision mediump float; \n"
- + "void main(){ \n"
- + " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n"
- + "} \n";
- private int program, positionHandle, mvpMatrixHandle;
- private float[] projMatrix = new float[16];
- private float[] vMatrix = new float[16];
- private float[] mMatrix = new float[16];
- private float[] mvMatrix = new float[16];
- private float[] mvpMatrix = new float[16];
- int defaultColor = 000000;
- Context context;
- //endregion
- //region Properties
- //endregion
- //region Constructor
- public MyRenderer() {
- }
- //endregion
- //region Methods
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
- initShapes();
- int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
- int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
- program = GLES20.glCreateProgram();
- GLES20.glAttachShader(program, vertexShader);
- GLES20.glAttachShader(program, fragmentShader);
- GLES20.glLinkProgram(program);
- positionHandle = GLES20.glGetAttribLocation(program, "vPosition");
- }
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height) {
- GLES20.glViewport(0, 0, width, height);
- float ratio = (float) width/height;
- Matrix.frustumM(projMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
- mvpMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
- Matrix.setLookAtM(vMatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
- }
- @Override
- public void onDrawFrame(GL10 gl) {
- GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
- GLES20.glUseProgram(program);
- Matrix.setRotateM(mMatrix, 0, -angle, 0, 0, 1.0f);
- Matrix.multiplyMM(mvMatrix, 0, vMatrix, 0, mMatrix, 0);
- Matrix.multiplyMM(mvpMatrix, 0, projMatrix, 0, mvMatrix, 0);
- GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0);
- triangleVB.position(0);
- GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, triangleVB);
- GLES20.glEnableVertexAttribArray(positionHandle);
- GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
- }
- private void initShapes() {
- float triangleCoords[] = { -0.5f, -0.25f, 0, 0.5f, -0.25f, 0, 0.0f, 0.559016994f, 0 };
- ByteBuffer vbb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
- vbb.order(ByteOrder.nativeOrder());
- triangleVB = vbb.asFloatBuffer();
- triangleVB.put(triangleCoords);
- triangleVB.position(0);
- }
- private int loadShader(int type, String shaderCode) {
- int shader = GLES20.glCreateShader(type);
- GLES20.glShaderSource(shader, shaderCode);
- GLES20.glCompileShader(shader);
- return shader;
- }
- //endregion
- public void openColorPicker(){
- AmbilWarnaDialog colorPicker = new AmbilWarnaDialog(context, defaultColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
- @Override
- public void onCancel(AmbilWarnaDialog dialog) {
- }
- @Override
- public void onOk(AmbilWarnaDialog dialog, int color) {
- defaultColor = color;
- }
- });
- colorPicker.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement