Advertisement
Aaaaa988

lab3

Nov 3rd, 2020
2,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.44 KB | None | 0 0
  1. package sibsutis.lab3;
  2.  
  3. import android.opengl.GLES20;
  4. import android.opengl.GLSurfaceView;
  5. import android.opengl.Matrix;
  6. import android.os.SystemClock;
  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.  
  15. public class Renderer implements GLSurfaceView.Renderer {
  16.     private float[] mModelMatrix = new float[16];
  17.     private float[] mViewMatrix = new float[16];
  18.     private float[] mProjectionMatrix = new float[16];
  19.     private float[] mMVPMatrix = new float[16];
  20.     private float[] mLightModelMatrix = new float[16];
  21.  
  22.     private FloatBuffer mVertexBuffer;
  23.     private FloatBuffer textureBuffer;
  24.     private FloatBuffer normalBuffer;
  25.     private FloatBuffer colorBuffer;
  26.     private int n;
  27.  
  28.     private int mMVPMatrixHandle;
  29.     private int mMVMatrixHandle;
  30.     private int mLightPosHandle;
  31.     private int mPositionHandle;
  32.     private int mColorHandle;
  33.     private int mNormalHandle;
  34.  
  35.     private final int mPositionDataSize = 3;
  36.     private final int mColorDataSize = 4;
  37.     private final int mNormalDataSize = 3;
  38.  
  39.     private final float[] mLightPosInModelSpace = new float[]{0.0f, 0.0f, 0.0f, 1.0f};
  40.     private final float[] mLightPosInWorldSpace = new float[4];
  41.     private final float[] mLightPosInEyeSpace = new float[4];
  42.  
  43.     private int mPerVertexProgramHandle;
  44.     private int mPointProgramHandle;
  45.  
  46.     public Renderer() {
  47.         float R = 1;
  48.         n = 0;
  49.         int dtheta = 15, dphi = 15;
  50.  
  51.         float DTOR = (float)(Math.PI / 180.0f);
  52.  
  53.         ByteBuffer byteBuf = ByteBuffer.allocateDirect(5000 * 3 * 4);
  54.         byteBuf.order(ByteOrder.nativeOrder());
  55.         mVertexBuffer = byteBuf.asFloatBuffer();
  56.  
  57.         byteBuf = ByteBuffer.allocateDirect(5000 * 3 * 4);
  58.         byteBuf.order(ByteOrder.nativeOrder());
  59.         normalBuffer = byteBuf.asFloatBuffer();
  60.  
  61.         byteBuf = ByteBuffer.allocateDirect(5000 * 2 * 4);
  62.         byteBuf.order(ByteOrder.nativeOrder());
  63.         textureBuffer = byteBuf.asFloatBuffer();
  64.         for (int theta = -90; theta <= 90 - dtheta; theta += dtheta) {
  65.             for (int phi = 0; phi <= 360 - dphi; phi += dphi){
  66.  
  67.                 mVertexBuffer.put((float)(Math.cos(theta*DTOR) * Math.cos(phi*DTOR))*R);
  68.                 mVertexBuffer.put((float)(Math.cos(theta*DTOR) * Math.sin(phi*DTOR))*R);
  69.                 mVertexBuffer.put((float)(Math.sin(theta*DTOR))*R);
  70.  
  71.                 mVertexBuffer.put((float)(Math.cos((theta+dtheta)*DTOR) * Math.cos(phi*DTOR))*R);
  72.                 mVertexBuffer.put((float)(Math.cos((theta+dtheta)*DTOR) * Math.sin(phi*DTOR))*R);
  73.                 mVertexBuffer.put((float)(Math.sin((theta+dtheta)*DTOR))*R);
  74.  
  75.                 mVertexBuffer.put((float)(Math.cos((theta+dtheta)*DTOR) * Math.cos((phi+dphi)*DTOR))*R);
  76.                 mVertexBuffer.put((float)(Math.cos((theta+dtheta)*DTOR) * Math.sin((phi+dphi)*DTOR))*R);
  77.                 mVertexBuffer.put((float)(Math.sin((theta+dtheta)*DTOR))*R);
  78.  
  79.  
  80.                 mVertexBuffer.put((float)(Math.cos(theta*DTOR) * Math.cos((phi+dphi)*DTOR))*R);
  81.                 mVertexBuffer.put((float)(Math.cos(theta*DTOR) * Math.sin((phi+dphi)*DTOR))*R);
  82.                 mVertexBuffer.put((float)(Math.sin(theta*DTOR))*R);
  83.                 n += 4;
  84.  
  85.                 normalBuffer.put((float)(Math.cos(theta*DTOR) * Math.cos(phi*DTOR))*R);
  86.                 normalBuffer.put((float)(Math.cos(theta*DTOR) * Math.sin(phi*DTOR))*R);
  87.                 normalBuffer.put((float)(Math.sin(theta*DTOR))*R);
  88.  
  89.                 normalBuffer.put((float)(Math.cos((theta+dtheta)*DTOR) * Math.cos(phi*DTOR))*R);
  90.                 normalBuffer.put((float)(Math.cos((theta+dtheta)*DTOR) * Math.sin(phi*DTOR))*R);
  91.                 normalBuffer.put((float)(Math.sin((theta+dtheta)*DTOR))*R);
  92.  
  93.                 normalBuffer.put((float)(Math.cos((theta+dtheta)*DTOR) * Math.cos((phi+dphi)*DTOR))*R);
  94.                 normalBuffer.put((float)(Math.cos((theta+dtheta)*DTOR) * Math.sin((phi+dphi)*DTOR))*R);
  95.                 normalBuffer.put((float)(Math.sin((theta+dtheta)*DTOR))*R);
  96.  
  97.                 normalBuffer.put((float)(Math.cos(theta*DTOR) * Math.cos((phi+dphi)*DTOR))*R);
  98.                 normalBuffer.put((float)(Math.cos(theta*DTOR) * Math.sin((phi+dphi)*DTOR))*R);
  99.                 normalBuffer.put((float)(Math.sin(theta*DTOR))*R);
  100.  
  101.                 textureBuffer.put((float)(phi/360.0f));
  102.                 textureBuffer.put((float)((90+theta)/180.0f));
  103.  
  104.                 textureBuffer.put((float)(phi/360.0f));
  105.                 textureBuffer.put((float)((90+theta+dtheta)/180.0f));
  106.  
  107.                 textureBuffer.put((float)((phi+dphi)/360.0f));
  108.                 textureBuffer.put((float)((90+theta+dtheta)/180.0f));
  109.  
  110.                 textureBuffer.put((float)((phi+dphi)/360.0f));
  111.                 textureBuffer.put((float)((90+theta)/180.0f));
  112.             }
  113.         }
  114.         mVertexBuffer.position(0);
  115.         textureBuffer.position(0);
  116.         normalBuffer.position(0);
  117.     }
  118.  
  119.     protected String getVertexShader() {
  120.         final String vertexShader =
  121.                 "uniform mat4 u_MVPMatrix;      \n"
  122.                         + "uniform mat4 u_MVMatrix;       \n"
  123.                         + "uniform vec3 u_LightPos;       \n"
  124.  
  125.                         + "attribute vec4 a_Position;     \n"
  126.                         + "attribute vec4 a_Color;        \n"
  127.                         + "attribute vec3 a_Normal;       \n"
  128.  
  129.                         + "varying vec4 v_Color;          \n"
  130.  
  131.                         + "void main()                    \n"
  132.                         + "{                              \n"
  133.                         + "   vec3 modelViewVertex = vec3(u_MVMatrix * a_Position);              \n"
  134.                         + "   vec3 modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));     \n"
  135.                         + "   float distance = length(u_LightPos - modelViewVertex);             \n"
  136.                         + "   vec3 lightVector = normalize(u_LightPos - modelViewVertex);        \n"
  137.                         + "   float diffuse = max(dot(modelViewNormal, lightVector), 0.1);       \n"
  138.                         + "   vec3 lookvector = normalize(u_LightPos - modelViewVertex);\n"
  139.                         + "   float specular = pow(max(dot(modelViewNormal, lookvector), 0.0), 40.0);\n"
  140.                         + "   float ambient = 0.2;\n"
  141.                         + "   diffuse = diffuse * (3.0 / (1.0 + (0.25 * distance * distance)));  \n"
  142.                         //+ "   v_Color = vec4(diffuse, diffuse, diffuse, 1);                                       \n"
  143.                         + "   vec4 one = vec4(1.0, 1.0, 1.0, 1.0);"
  144.                         + "   v_Color = (ambient + specular + diffuse) * one;\n"
  145.                         + "   gl_Position = u_MVPMatrix * a_Position;                            \n"
  146.                         + "}                                                                     \n";
  147.  
  148.         return vertexShader;
  149.     }
  150.  
  151.     protected String getFragmentShader() {
  152.         final String fragmentShader =
  153.                 "precision mediump float;       \n"
  154.                         + "varying vec4 v_Color;          \n"
  155.                         + "void main()                    \n"
  156.                         + "{                              \n"
  157.                         + "   gl_FragColor = v_Color;     \n"
  158.                         + "}                              \n";
  159.  
  160.         return fragmentShader;
  161.     }
  162.  
  163.     @Override
  164.     public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
  165.         GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  166.         GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  167.  
  168.         final float eyeX = 0.0f;
  169.         final float eyeY = 0.0f;
  170.         final float eyeZ = -0.5f;
  171.  
  172.         final float lookX = 0.0f;
  173.         final float lookY = 0.0f;
  174.         final float lookZ = -5.0f;
  175.  
  176.         final float upX = 0.0f;
  177.         final float upY = 1.0f;
  178.         final float upZ = 0.0f;
  179.  
  180.         Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
  181.         final String vertexShader = getVertexShader();
  182.         final String fragmentShader = getFragmentShader();
  183.         final int vertexShaderHandle = compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
  184.         final int fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
  185.         mPerVertexProgramHandle = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
  186.                 new String[]{"a_Position", "a_Color", "a_Normal"});
  187.  
  188.         final String pointVertexShader =
  189.                 "uniform mat4 u_MVPMatrix;      \n"
  190.                         + "attribute vec4 a_Position;     \n"
  191.                         + "void main()                    \n"
  192.                         + "{                              \n"
  193.                         + "   gl_Position = u_MVPMatrix   \n"
  194.                         + "               * a_Position;   \n"
  195.                         + "   gl_PointSize = 10.0;         \n"
  196.                         + "}                              \n";
  197.  
  198.         final String pointFragmentShader =
  199.                 "precision mediump float;       \n"
  200.                         + "void main()                    \n"
  201.                         + "{                              \n"
  202.                         + "   gl_FragColor = vec4(1.0,    \n"
  203.                         + "   1.0, 1.0, 1.0);             \n"
  204.                         + "}                              \n";
  205.  
  206.         final int pointVertexShaderHandle = compileShader(GLES20.GL_VERTEX_SHADER, pointVertexShader);
  207.         final int pointFragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, pointFragmentShader);
  208.         mPointProgramHandle = createAndLinkProgram(pointVertexShaderHandle, pointFragmentShaderHandle,
  209.                 new String[]{"a_Position"});
  210.     }
  211.  
  212.     @Override
  213.     public void onSurfaceChanged(GL10 glUnused, int width, int height) {
  214.         GLES20.glViewport(0, 0, width, height);
  215.         final float ratio = (float) width / height;
  216.         final float left = -ratio;
  217.         final float right = ratio;
  218.         final float bottom = -1.0f;
  219.         final float top = 1.0f;
  220.         final float near = 1.0f;
  221.         final float far = 10.0f;
  222.         Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
  223.     }
  224.  
  225.     @Override
  226.     public void onDrawFrame(GL10 glUnused) {
  227.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  228.         long time = SystemClock.uptimeMillis() % 10000L;
  229.         float angleInDegrees = (360.0f / 10000.0f) * ((int) time);
  230.         GLES20.glUseProgram(mPerVertexProgramHandle);
  231.         mMVPMatrixHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_MVPMatrix");
  232.         mMVMatrixHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_MVMatrix");
  233.         mLightPosHandle = GLES20.glGetUniformLocation(mPerVertexProgramHandle, "u_LightPos");
  234.         mPositionHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Position");
  235.         mColorHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Color");
  236.         mNormalHandle = GLES20.glGetAttribLocation(mPerVertexProgramHandle, "a_Normal");
  237.  
  238.         Matrix.setIdentityM(mLightModelMatrix, 0);
  239.         Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, -5.0f);
  240.         Matrix.rotateM(mLightModelMatrix, 0, angleInDegrees, 0.0f, 1.0f, 0.0f);
  241.         Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, 2.0f);
  242.         Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0);
  243.         Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);
  244.  
  245.         Matrix.setIdentityM(mModelMatrix, 0);
  246.         Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -7.0f);
  247.         Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 0.0f, 0.0f);
  248.         drawSphere();
  249.         GLES20.glUseProgram(mPointProgramHandle);
  250.         drawLight();
  251.     }
  252.  
  253.     private void drawSphere() {
  254.         mVertexBuffer.position(0);
  255.         GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
  256.                 0, mVertexBuffer);
  257.         GLES20.glEnableVertexAttribArray(mPositionHandle);
  258.         GLES20.glEnableVertexAttribArray(mColorHandle);
  259.         normalBuffer.position(0);
  260.         GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false,
  261.                 0, normalBuffer);
  262.         GLES20.glEnableVertexAttribArray(mNormalHandle);
  263.         Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
  264.         GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);
  265.         Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
  266.         GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
  267.         GLES20.glUniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);
  268.         for (int i = 0; i < n; i += 4)
  269.             GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, i,4);
  270.     }
  271.  
  272.     private void drawLight() {
  273.         final int pointMVPMatrixHandle = GLES20.glGetUniformLocation(mPointProgramHandle, "u_MVPMatrix");
  274.         final int pointPositionHandle = GLES20.glGetAttribLocation(mPointProgramHandle, "a_Position");
  275.         GLES20.glVertexAttrib3f(pointPositionHandle, mLightPosInModelSpace[0], mLightPosInModelSpace[1], mLightPosInModelSpace[2]);
  276.         GLES20.glDisableVertexAttribArray(pointPositionHandle);
  277.         Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mLightModelMatrix, 0);
  278.         Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
  279.         GLES20.glUniformMatrix4fv(pointMVPMatrixHandle, 1, false, mMVPMatrix, 0);
  280.         GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
  281.     }
  282.  
  283.     private int compileShader(final int shaderType, final String shaderSource) {
  284.         int shaderHandle = GLES20.glCreateShader(shaderType);
  285.  
  286.         if (shaderHandle != 0) {
  287.             GLES20.glShaderSource(shaderHandle, shaderSource);
  288.  
  289.             GLES20.glCompileShader(shaderHandle);
  290.             final int[] compileStatus = new int[1];
  291.             GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
  292.             if (compileStatus[0] == 0) {
  293.                 GLES20.glDeleteShader(shaderHandle);
  294.                 shaderHandle = 0;
  295.             }
  296.         }
  297.         if (shaderHandle == 0)
  298.             throw new RuntimeException("Error creating shader.");
  299.         return shaderHandle;
  300.     }
  301.  
  302.     private int createAndLinkProgram(final int vertexShaderHandle, final int fragmentShaderHandle, final String[] attributes) {
  303.         int programHandle = GLES20.glCreateProgram();
  304.  
  305.         if (programHandle != 0) {
  306.             GLES20.glAttachShader(programHandle, vertexShaderHandle);
  307.             GLES20.glAttachShader(programHandle, fragmentShaderHandle);
  308.             if (attributes != null) {
  309.                 for (int i = 0; i < attributes.length; i++)
  310.                     GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
  311.             }
  312.             GLES20.glLinkProgram(programHandle);
  313.             final int[] linkStatus = new int[1];
  314.             GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);
  315.             if (linkStatus[0] == 0) {
  316.                 GLES20.glDeleteProgram(programHandle);
  317.                 programHandle = 0;
  318.             }
  319.         }
  320.         if (programHandle == 0)
  321.             throw new RuntimeException("Error creating program.");
  322.         return programHandle;
  323.     }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement