Advertisement
Guest User

myRenderer

a guest
Jan 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. package com.example.nk33785.project3;
  2.  
  3. import android.content.Context;
  4. import android.opengl.GLES20;
  5. import android.opengl.GLSurfaceView;
  6. import android.opengl.Matrix;
  7.  
  8. import java.nio.ByteBuffer;
  9. import java.nio.ByteOrder;
  10. import java.nio.FloatBuffer;
  11.  
  12. import javax.microedition.khronos.egl.EGLConfig;
  13. import javax.microedition.khronos.opengles.GL10;
  14. import yuku.ambilwarna.AmbilWarnaDialog;
  15.  
  16. public class MyRenderer implements GLSurfaceView.Renderer {
  17.  
  18. //region Fields
  19.  
  20. private FloatBuffer triangleVB;
  21. public float angle;
  22. private final String vertexShaderCode =
  23. "uniform mat4 uMVPMatrix; \n"
  24. + "attribute vec4 vPosition; \n"
  25. + "void main(){ \n"
  26. +" gl_Position = uMVPMatrix * vPosition; \n"
  27. + "} \n";
  28. private final String fragmentShaderCode =
  29. "precision mediump float; \n"
  30. + "void main(){ \n"
  31. + " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n"
  32. + "} \n";
  33. private int program, positionHandle, mvpMatrixHandle;
  34. private float[] projMatrix = new float[16];
  35. private float[] vMatrix = new float[16];
  36. private float[] mMatrix = new float[16];
  37. private float[] mvMatrix = new float[16];
  38. private float[] mvpMatrix = new float[16];
  39. int defaultColor = 000000;
  40. Context context;
  41. //endregion
  42.  
  43. //region Properties
  44. //endregion
  45.  
  46. //region Constructor
  47.  
  48. public MyRenderer() {
  49.  
  50. }
  51.  
  52. //endregion
  53.  
  54. //region Methods
  55.  
  56. @Override
  57. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  58. GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
  59. initShapes();
  60.  
  61. int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
  62. int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
  63.  
  64. program = GLES20.glCreateProgram();
  65. GLES20.glAttachShader(program, vertexShader);
  66. GLES20.glAttachShader(program, fragmentShader);
  67. GLES20.glLinkProgram(program);
  68.  
  69. positionHandle = GLES20.glGetAttribLocation(program, "vPosition");
  70. }
  71.  
  72. @Override
  73. public void onSurfaceChanged(GL10 gl, int width, int height) {
  74. GLES20.glViewport(0, 0, width, height);
  75. float ratio = (float) width/height;
  76.  
  77. Matrix.frustumM(projMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
  78. mvpMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
  79. Matrix.setLookAtM(vMatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
  80. }
  81.  
  82. @Override
  83. public void onDrawFrame(GL10 gl) {
  84. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  85. GLES20.glUseProgram(program);
  86.  
  87. Matrix.setRotateM(mMatrix, 0, -angle, 0, 0, 1.0f);
  88. Matrix.multiplyMM(mvMatrix, 0, vMatrix, 0, mMatrix, 0);
  89. Matrix.multiplyMM(mvpMatrix, 0, projMatrix, 0, mvMatrix, 0);
  90.  
  91. GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0);
  92.  
  93. triangleVB.position(0);
  94. GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, triangleVB);
  95. GLES20.glEnableVertexAttribArray(positionHandle);
  96. GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
  97. }
  98.  
  99. private void initShapes() {
  100. float triangleCoords[] = { -0.5f, -0.25f, 0, 0.5f, -0.25f, 0, 0.0f, 0.559016994f, 0 };
  101.  
  102. ByteBuffer vbb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
  103. vbb.order(ByteOrder.nativeOrder());
  104. triangleVB = vbb.asFloatBuffer();
  105. triangleVB.put(triangleCoords);
  106. triangleVB.position(0);
  107. }
  108.  
  109. private int loadShader(int type, String shaderCode) {
  110. int shader = GLES20.glCreateShader(type);
  111. GLES20.glShaderSource(shader, shaderCode);
  112. GLES20.glCompileShader(shader);
  113.  
  114. return shader;
  115. }
  116.  
  117. //endregion
  118. public void openColorPicker(){
  119. AmbilWarnaDialog colorPicker = new AmbilWarnaDialog(context, defaultColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
  120. @Override
  121. public void onCancel(AmbilWarnaDialog dialog) {
  122.  
  123. }
  124.  
  125. @Override
  126. public void onOk(AmbilWarnaDialog dialog, int color) {
  127. defaultColor = color;
  128.  
  129. }
  130. });
  131. colorPicker.show();
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement