Aaaaa988

RenderProgram.java

Dec 11th, 2020
447
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.user.newcurswork;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6.  
  7. import android.content.Context;
  8. import android.opengl.GLES20;
  9. import android.util.Log;
  10.  
  11. class RenderProgram {
  12.  
  13.     private int mProgram;
  14.  
  15.     private String mVertexS, mFragmentS;
  16.  
  17.     RenderProgram(int vID, int fID, Context context) {
  18.         StringBuilder vs = new StringBuilder();
  19.         StringBuilder fs = new StringBuilder();
  20.  
  21.         try {
  22.             InputStream inputStream = context.getResources().openRawResource(vID);
  23.             BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
  24.  
  25.             String read = in.readLine();
  26.             while (read != null) {
  27.                 vs.append(read).append("\n");
  28.                 read = in.readLine();
  29.             }
  30.  
  31.             vs.deleteCharAt(vs.length() - 1);
  32.  
  33.             inputStream = context.getResources().openRawResource(fID);
  34.             in = new BufferedReader(new InputStreamReader(inputStream));
  35.  
  36.             read = in.readLine();
  37.             while (read != null) {
  38.                 fs.append(read).append("\n");
  39.                 read = in.readLine();
  40.             }
  41.  
  42.             fs.deleteCharAt(fs.length() - 1);
  43.         } catch (Exception e) {
  44.             Log.d("RenderProgram", "Could not read shader: " + e.getLocalizedMessage());
  45.         }
  46.         setup(vs.toString(), fs.toString());
  47.     }
  48.  
  49.     private void setup(String vs, String fs) {
  50.         this.mVertexS = vs;
  51.         this.mFragmentS = fs;
  52.  
  53.         if (createProgram() != 1) {
  54.             throw new RuntimeException("Error at creating shaders");
  55.         };
  56.     }
  57.  
  58.     private int createProgram() {
  59.         int mVertexShader = loadShader(GLES20.GL_VERTEX_SHADER, mVertexS);
  60.         if (mVertexShader == 0) {
  61.             return 0;
  62.         }
  63.  
  64.         int mPixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, mFragmentS);
  65.         if (mPixelShader == 0) {
  66.             return 0;
  67.         }
  68.  
  69.         mProgram = GLES20.glCreateProgram();
  70.         if (mProgram != 0) {
  71.             GLES20.glAttachShader(mProgram, mVertexShader);
  72.             GLES20.glAttachShader(mProgram, mPixelShader);
  73.             GLES20.glLinkProgram(mProgram);
  74.             int[] linkStatus = new int[1];
  75.             GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus, 0);
  76.             if (linkStatus[0] != GLES20.GL_TRUE) {
  77.                 Log.e("RenderProgram", "Could not link _program: ");
  78.                 Log.e("RenderProgram", GLES20.glGetProgramInfoLog(mProgram));
  79.                 GLES20.glDeleteProgram(mProgram);
  80.                 mProgram = 0;
  81.                 return 0;
  82.             }
  83.         }
  84.         else
  85.             Log.d("CreateProgram", "Could not create program");
  86.  
  87.         return 1;
  88.     }
  89.  
  90.     private int loadShader(int shaderType, String source) {
  91.         int shader = GLES20.glCreateShader(shaderType);
  92.         if (shader != 0) {
  93.             GLES20.glShaderSource(shader, source);
  94.             GLES20.glCompileShader(shader);
  95.             int[] compiled = new int[1];
  96.             GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
  97.             if (compiled[0] == 0) {
  98.                 Log.e("RenderProgram", "Could not compile shader " + shaderType + ":");
  99.                 Log.e("RenderProgram", GLES20.glGetShaderInfoLog(shader));
  100.                 GLES20.glDeleteShader(shader);
  101.                 shader = 0;
  102.             }
  103.         }
  104.         return shader;
  105.     }
  106.  
  107.     int getProgram() {
  108.         return mProgram;
  109.     }
  110. }
  111.  
RAW Paste Data