Advertisement
dawrehxyz

Untitled

Aug 6th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. public class MyGLRenderer implements GLSurfaceView.Renderer
  2. {
  3.     private GameClock gameClock;
  4.     private float elapsedTime;
  5.     private Context context;
  6.     private Camera camera;
  7.  
  8.     private ArrayList<StaticObject> maze;
  9.     private Player player;
  10.  
  11.     private int mazeTexture;
  12.     private int playerTexture;
  13.  
  14.     private TextureShaderProgram textureShaderProgram;
  15.  
  16.     private float[] projectionMatrix = new float[16];
  17.     private float[] modelMatrix = new float[16];
  18.     private float[] viewMatrix = new float[16];
  19.     private float[] worldView = new float[16];
  20.     private float[] worldViewProjection = new float[16];
  21.     private float[] scalingMatrix = new float[16];
  22.     private float[] rotationMatrix = new float[16];
  23.     private float[] translationMatrix = new float[16];
  24.     private float rotate = 0;
  25.  
  26.     public MyGLRenderer(Context context)
  27.     {
  28.         this.context = context;
  29.         gameClock = new GameClock();
  30.     }
  31.  
  32.     @Override
  33.     public void onSurfaceCreated(GL10 gl, EGLConfig config)
  34.     {
  35.         GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  36.         GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  37.  
  38.         GLES20.glEnable(GLES20.GL_CULL_FACE); //Enables cull face
  39.         GLES20.glCullFace(GLES20.GL_BACK); //specifies whether front- or back-facing polygons can be culled
  40.         GLES20.glFrontFace(GLES20.GL_CCW); //defines the font facing polygon, whether they are drawn in CW order or CCW order.
  41.  
  42.         LoadShaderPrograms();
  43.         LoadObjects();
  44.  
  45.         playerTexture = TextureHelper.loadTexture(context, R.drawable.chararacter);
  46.         player = new Player(context, R.raw.character);
  47.         camera = new Camera(player);
  48.  
  49.         gameClock.Initialize();
  50.     }
  51.  
  52.     @Override
  53.     public void onSurfaceChanged(GL10 gl, int width, int height)
  54.     {
  55.         GLES20.glViewport(0, 0, width, height);
  56.         Matrix.setIdentityM(worldViewProjection, 0);
  57.         float aspectRatio;
  58.         if(width > height)
  59.         {
  60.             aspectRatio = (float)width/(float)height;
  61.             //Matrix.frustumM(projectionMatrix, 0, -aspectRatio, aspectRatio, -1, 1, 0.1f, 150);
  62.             Matrix.perspectiveM(projectionMatrix, 0, 90, aspectRatio, 0.1f, 150);
  63.         }
  64.         else
  65.         {
  66.             aspectRatio = (float)height/(float)width;
  67.             Matrix.perspectiveM(projectionMatrix, 0, 90, aspectRatio, 0.1f, 150);
  68.             //Matrix.frustumM(projectionMatrix, 0, -1, 1, -aspectRatio, aspectRatio, 1, 150);
  69.         }
  70.         viewMatrix = camera.getViewMatrix();
  71.     }
  72.  
  73.     @Override
  74.     public void onDrawFrame(GL10 gl)
  75.     {
  76.         elapsedTime = gameClock.CalculateElapsedTime();
  77.         System.out.println(elapsedTime);
  78.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  79.         GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT);
  80.         drawPlayer();
  81.         drawMaze();
  82.         updateCamera();
  83.     }
  84.  
  85.     private void drawMaze()
  86.     {
  87.         for(int a = 0; a < maze.size(); a++)
  88.         {
  89.             Matrix.setIdentityM(modelMatrix, 0);
  90.             Matrix.multiplyMM(worldView, 0, viewMatrix, 0, modelMatrix, 0);
  91.             Matrix.multiplyMM(worldViewProjection, 0, projectionMatrix, 0, worldView, 0);
  92.  
  93.             textureShaderProgram.useProgram();
  94.             textureShaderProgram.setUniforms(worldViewProjection, mazeTexture);
  95.  
  96.             maze.get(a).bindData(textureShaderProgram);
  97.             maze.get(a).draw();
  98.         }
  99.     }
  100.  
  101.     private void drawPlayer()
  102.     {
  103.  
  104.         Matrix.setIdentityM(translationMatrix, 0);
  105.         Vector3 playerWorldMidPoint = player.getWorldMidPoint();
  106.         Matrix.translateM(translationMatrix, 0, playerWorldMidPoint.x, playerWorldMidPoint.y, playerWorldMidPoint.z);
  107.         rotationMatrix = player.getIncrementalRotationMatrix();
  108.  
  109.         Matrix.multiplyMM(modelMatrix, 0, translationMatrix, 0, rotationMatrix, 0);
  110.  
  111.         Matrix.multiplyMM(worldView, 0, viewMatrix, 0, modelMatrix, 0);
  112.         Matrix.multiplyMM(worldViewProjection, 0, projectionMatrix, 0, worldView, 0);
  113.  
  114.         textureShaderProgram.useProgram();
  115.         textureShaderProgram.setUniforms(worldViewProjection, playerTexture);
  116.  
  117.         player.bindData(textureShaderProgram);
  118.         player.draw();
  119.         player.updatePlayerPosition(elapsedTime);
  120.     }
  121.  
  122.     private void updateCamera()
  123.     {
  124.         camera.updateCamera();
  125.         viewMatrix = camera.getViewMatrix();
  126.     }
  127.  
  128.     private void LoadShaderPrograms()
  129.     {
  130.         textureShaderProgram = new TextureShaderProgram(context);
  131.     }
  132.  
  133.     private void LoadObjects()
  134.     {
  135.         LoadMaze();
  136.     }
  137.  
  138.     //I am loading in a wavefront file and the maze consists of several objects so I have to load them all since I stored them
  139.     //separately.
  140.     private void LoadMaze()
  141.     {
  142.         mazeTexture = TextureHelper.loadTexture(context, R.drawable.dark_texture);
  143.         InputStream path = context.getResources().openRawResource(R.raw.maze);
  144.         LoadOBJModel mazeOBJLoader = new LoadOBJModel();
  145.         ArrayList<OBJFileData> mazeOBJFileData = mazeOBJLoader.LoadData(path);
  146.         maze = new ArrayList<>();
  147.         for(int a = 0; a < mazeOBJFileData.size(); a++)
  148.         {
  149.             maze.add(new StaticObject(mazeOBJFileData.get(a)));
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement