Aaaaa988

ShadowsRenderer.java

Dec 11th, 2020 (edited)
145
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.user.newcurswork;
  2.  
  3. import javax.microedition.khronos.egl.EGLConfig;
  4. import javax.microedition.khronos.opengles.GL10;
  5.  
  6. import android.content.Context;
  7. import android.opengl.GLES20;
  8. import android.opengl.GLSurfaceView;
  9. import android.opengl.Matrix;
  10. import android.util.Log;
  11.  
  12. public class ShadowsRenderer implements GLSurfaceView.Renderer {
  13.  
  14.     private final MainActivity mShadowsActivity;
  15.  
  16.     private RenderProgram mSimpleShadowProgram;
  17.  
  18.     private RenderProgram mDepthMapProgram;
  19.  
  20.     private int mActiveProgram;
  21.  
  22.     private final float[] mMVPMatrix = new float[16];
  23.     private final float[] mMVMatrix = new float[16];
  24.     private final float[] mNormalMatrix = new float[16];
  25.     private final float[] mProjectionMatrix = new float[16];
  26.     private final float[] mViewMatrix = new float[16];
  27.     private final float[] mModelMatrix = new float[16];
  28.  
  29.     private final float[] mLightMvpMatrix = new float[16];
  30.  
  31.     private final float[] mLightProjectionMatrix = new float[16];
  32.  
  33.     private final float[] mLightViewMatrix = new float[16];
  34.  
  35.     private final float[] mLightPosInEyeSpace = new float[16];
  36.  
  37.     private final float[] mLightPosModel = new float[]
  38.             {0.1f, 10.0f, 0.1f, 1.0f};
  39.     private float[] mActualLightPosition = new float[4];
  40.  
  41.     private int mDisplayWidth;
  42.     private int mDisplayHeight;
  43.     private float s = 0;
  44.  
  45.     private int mShadowMapWidth;
  46.     private int mShadowMapHeight;
  47.  
  48.     private int[] fboId;
  49.     private int[] renderTextureId;
  50.  
  51.     private int scene_mvpMatrixUniform;
  52.     private int scene_mvMatrixUniform;
  53.     private int scene_normalMatrixUniform;
  54.     private int scene_lightPosUniform;
  55.     private int scene_shadowProjMatrixUniform;
  56.     private int scene_textureUniform;
  57.     private int scene_mapStepXUniform;
  58.     private int scene_mapStepYUniform;
  59.  
  60.     private int shadow_mvpMatrixUniform;
  61.  
  62.     private int scene_positionAttribute;
  63.     private int scene_normalAttribute;
  64.     private int scene_colorAttribute;
  65.  
  66.     private int shadow_positionAttribute;
  67.  
  68.     private Objects Table;
  69.  
  70.     private Context c;
  71.  
  72.     private Plane mPlane;
  73.     private Objects Teapot;
  74.     private Objects Torch;
  75.     private Objects Apple;
  76.     private Objects Cup;
  77.     private Objects Ufo;
  78.     private Objects Mug;
  79.     private Objects Title;
  80.     private Objects Mug2;
  81.     private Objects blackChess;
  82.     private Objects whiteChess;
  83.  
  84.     ShadowsRenderer(final MainActivity shadowsActivity, Context c) {
  85.         mShadowsActivity = shadowsActivity;
  86.         this.c = c;
  87.     }
  88.  
  89.     @Override
  90.     public void onSurfaceCreated(GL10 unused, EGLConfig config) {
  91.         GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  92.  
  93.         GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  94.  
  95.         GLES20.glEnable(GLES20.GL_CULL_FACE);
  96.  
  97.         Table = new Objects(c, new float[]{0.6f, 0.3f, 0.2f, 1.0f}, "sasha_table.obj");
  98.         Teapot = new Objects(c, new float[]{0.5f, 0.5f, 0.6f, 1.0f}, "sasha_tarelka.obj");
  99.         Torch = new Objects(c, new float[]{0.8f, 0.6f, 0.3f, 1.0f}, "sasha_candle.obj");
  100.         Apple = new Objects(c, new float[]{0.9f, 0.2f, 0.2f, 1.0f}, "sasha_apple.obj");
  101.         Cup = new Objects(c, new float[]{0.0f, 0.5f, 0.0f, 1.0f}, "sasha_bottle.obj");
  102.         Title = new Objects(c, new float[]{0f, 0f, 0f, 1.0f}, "sasha_name.obj");
  103.  
  104.  
  105.         Table = new Objects(c, new float[]{0.6f, 0.3f, 0.2f, 1.0f}, "Table.obj");
  106.         Teapot = new Objects(c, new float[]{0.5f, 0.5f, 0.6f, 1.0f}, "teapot.obj");
  107.         Torch = new Objects(c, new float[]{0.8f, 0.6f, 0.3f, 1.0f}, "torch.obj");
  108.         Apple = new Objects(c, new float[]{0.9f, 0.2f, 0.2f, 1.0f}, "apple.obj");
  109.         Cup = new Objects(c, new float[]{0.4f, 0.2f, 0.3f, 1.0f}, "cup.obj");
  110.         Title = new Objects(c, new float[]{0f, 0f, 0f, 1.0f}, "Title.obj");
  111.  
  112.         mPlane = new Plane();
  113.  
  114.         mSimpleShadowProgram = new RenderProgram(R.raw.depth_tex_v_with_shadow, R.raw.depth_tex_f_with_simple_shadow, mShadowsActivity);
  115.         mDepthMapProgram = new RenderProgram(R.raw.depth_tex_v_depth_map, R.raw.depth_tex_f_depth_map, mShadowsActivity);
  116.         mActiveProgram = mSimpleShadowProgram.getProgram();
  117.     }
  118.  
  119.     @Override
  120.     public void onSurfaceChanged(GL10 unused, int width, int height) {
  121.         mDisplayWidth = width;
  122.         mDisplayHeight = height;
  123.         GLES20.glViewport(0, 0, mDisplayWidth, mDisplayHeight);
  124.  
  125.         float ratio = (float) mDisplayWidth / mDisplayHeight;
  126.         float bottom = -1.0f;
  127.         float top = 1.0f;
  128.         float near = 1.0f;
  129.         float far = 100.0f;
  130.  
  131.         Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, bottom, top, near, far);
  132.         Matrix.frustumM(mLightProjectionMatrix, 0, -1.1f * ratio, 1.1f * ratio, 1.1f * bottom, 1.1f * top, near, far);
  133.     }
  134.  
  135.     @Override
  136.     public void onDrawFrame(GL10 unused) {
  137.         mActiveProgram = mSimpleShadowProgram.getProgram();
  138.  
  139.         Matrix.setLookAtM(mViewMatrix, 0,
  140.                 5, 4, 0,
  141.                 0, 0, 0,
  142.                 -1,0,0);
  143.  
  144.         scene_mvpMatrixUniform = GLES20.glGetUniformLocation(mActiveProgram, "uMVPMatrix");
  145.         scene_mvMatrixUniform = GLES20.glGetUniformLocation(mActiveProgram, "uMVMatrix");
  146.         scene_normalMatrixUniform = GLES20.glGetUniformLocation(mActiveProgram, "uNormalMatrix");
  147.         scene_lightPosUniform = GLES20.glGetUniformLocation(mActiveProgram, "uLightPos");
  148.  
  149.         scene_positionAttribute = GLES20.glGetAttribLocation(mActiveProgram, "aPosition");
  150.         scene_normalAttribute = GLES20.glGetAttribLocation(mActiveProgram, "aNormal");
  151.         scene_colorAttribute = GLES20.glGetAttribLocation(mActiveProgram, "aColor");
  152.  
  153.         float[] basicMatrix = new float[16];
  154.  
  155.         Matrix.setIdentityM(basicMatrix, 0);
  156.         Matrix.multiplyMV(mActualLightPosition, 0, basicMatrix, 0, mLightPosModel, 0);
  157.  
  158.         Matrix.setIdentityM(mModelMatrix, 0);
  159.  
  160.         Matrix.setLookAtM(mLightViewMatrix, 0,
  161.                 mActualLightPosition[0], mActualLightPosition[1], mActualLightPosition[2],
  162.                 mActualLightPosition[0], -mActualLightPosition[1], mActualLightPosition[2],
  163.                 -mActualLightPosition[0], 0, -mActualLightPosition[2]);
  164.  
  165.         GLES20.glCullFace(GLES20.GL_FRONT);
  166.  
  167.         s+=0.3f;
  168.         if (s >= 360) s-=360;
  169.         Matrix.rotateM(mModelMatrix, 0, s, 0,1,0);
  170.  
  171.         GLES20.glCullFace(GLES20.GL_BACK);
  172.  
  173.         renderScene();
  174.  
  175.  
  176.     }
  177.  
  178.     private void renderScene() {
  179.         GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
  180.  
  181.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  182.  
  183.         GLES20.glUseProgram(mActiveProgram);
  184.  
  185.         GLES20.glViewport(0, 0, mDisplayWidth, mDisplayHeight);
  186.  
  187.  
  188.  
  189.         float[] tempResultMatrix = new float[16];
  190.  
  191.  
  192.         Matrix.multiplyMM(tempResultMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
  193.         System.arraycopy(tempResultMatrix, 0, mMVMatrix, 0, 16);
  194.  
  195.         GLES20.glUniformMatrix4fv(scene_mvMatrixUniform, 1, false, mMVMatrix, 0);
  196.  
  197.         Matrix.invertM(tempResultMatrix, 0, mMVMatrix, 0);
  198.         Matrix.transposeM(mNormalMatrix, 0, tempResultMatrix, 0);
  199.  
  200.         GLES20.glUniformMatrix4fv(scene_normalMatrixUniform, 1, false, mNormalMatrix, 0);
  201.  
  202.         Matrix.multiplyMM(tempResultMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0);
  203.         System.arraycopy(tempResultMatrix, 0, mMVPMatrix, 0, 16);
  204.  
  205.         GLES20.glUniformMatrix4fv(scene_mvpMatrixUniform, 1, false, mMVPMatrix, 0);
  206.  
  207.         Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mActualLightPosition, 0);
  208.  
  209.         GLES20.glUniform3f(scene_lightPosUniform, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);
  210.  
  211.         GLES20.glUniformMatrix4fv(scene_shadowProjMatrixUniform, 1, false, mLightMvpMatrix, 0);
  212.  
  213.         Table.render(scene_positionAttribute, scene_normalAttribute, scene_colorAttribute,  false);
  214.         Teapot.render(scene_positionAttribute, scene_normalAttribute, scene_colorAttribute,  false);
  215.         Cup.render(scene_positionAttribute, scene_normalAttribute, scene_colorAttribute,  false);
  216.         Apple.render(scene_positionAttribute, scene_normalAttribute, scene_colorAttribute,  false);
  217.         Torch.render(scene_positionAttribute, scene_normalAttribute, scene_colorAttribute,  false);
  218.         Title.render(scene_positionAttribute, scene_normalAttribute, scene_colorAttribute,  false);
  219.         Apple.render(scene_positionAttribute, scene_normalAttribute, scene_colorAttribute,  false);
  220.  
  221.         mPlane.render(scene_positionAttribute, scene_normalAttribute, scene_colorAttribute, false);
  222.     }
  223. }
  224.  
RAW Paste Data