Advertisement
atm959

Renderer.java

May 11th, 2019
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1. package com.atm959.openglgame;
  2.  
  3. import android.opengl.GLES20;
  4. import android.opengl.Matrix;
  5.  
  6. import java.nio.ByteBuffer;
  7. import java.nio.ByteOrder;
  8. import java.nio.FloatBuffer;
  9.  
  10. public class Renderer {
  11.  
  12.     private String vertexShaderCode =
  13.                     "attribute vec2 aPos;" +
  14.                     "attribute vec2 aTexCoord;" +
  15.                     "uniform mat4 projection;" +
  16.                     "uniform mat4 model;" +
  17.                     "varying vec2 vTexCoord;" +
  18.                     "void main(){" +
  19.                     "    gl_Position = projection * model * vec4(aPos, 0.0, 1.0);" +
  20.                     "    vTexCoord = aTexCoord;" +
  21.                     "}";
  22.  
  23.     private String fragmentShaderCode =
  24.                     "precision mediump float;" +
  25.                     "varying vec2 vTexCoord;" +
  26.                     "uniform sampler2D tex;" +
  27.                     "void main(){" +
  28.                     "    gl_FragColor = texture2D(tex, vTexCoord);" +
  29.                     "}";
  30.  
  31.     static final float[] vertexCoords = {   // in counterclockwise order:
  32.             0.0f, 0.0f,
  33.             0.0f, 1.0f,
  34.             1.0f, 1.0f,
  35.             0.0f, 0.0f,
  36.             1.0f, 1.0f,
  37.             1.0f, 0.0f,
  38.     };
  39.  
  40.     static final float[] textureCoords = {
  41.             0.0f, 0.0f,
  42.             0.0f, 1.0f,
  43.             1.0f, 1.0f,
  44.             0.0f, 0.0f,
  45.             1.0f, 1.0f,
  46.             1.0f, 0.0f
  47.     };
  48.  
  49.     static final int COORDS_PER_VERTEX = 2;
  50.     private final int vertexCount = vertexCoords.length / COORDS_PER_VERTEX;
  51.     private final int vertexStride = COORDS_PER_VERTEX * 4;
  52.  
  53.     private FloatBuffer vertexBuffer;
  54.     private FloatBuffer textureCoordsBuffer;
  55.     private int mProgram;
  56.  
  57.     private int mPositionHandle;
  58.     private int mTexCoordHandle;
  59.     private int mProjectionMatrixHandle;
  60.     private int mModelMatrixHandle;
  61.     private int mTexHandle;
  62.  
  63.     float[] projection = new float[16];
  64.     float[] model = new float[16];
  65.  
  66.     int screenWidth;
  67.     int screenHeight;
  68.  
  69.     public Renderer(int screenWidth, int screenHeight){
  70.         this.screenWidth = screenWidth;
  71.         this.screenHeight = screenHeight;
  72.         Matrix.setIdentityM(this.projection, 0);
  73.         Matrix.setIdentityM(this.model, 0);
  74.  
  75.         ByteBuffer bb = ByteBuffer.allocateDirect(vertexCoords.length * 4);
  76.         bb.order(ByteOrder.nativeOrder());
  77.         this.vertexBuffer = bb.asFloatBuffer();
  78.         this.vertexBuffer.put(vertexCoords);
  79.         this.vertexBuffer.position(0);
  80.  
  81.         bb = ByteBuffer.allocateDirect(textureCoords.length * 4);
  82.         bb.order(ByteOrder.nativeOrder());
  83.         this.textureCoordsBuffer = bb.asFloatBuffer();
  84.         this.textureCoordsBuffer.put(textureCoords);
  85.         this.textureCoordsBuffer.position(0);
  86.  
  87.         int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
  88.         int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
  89.         this.mProgram = GLES20.glCreateProgram();
  90.         GLES20.glAttachShader(this.mProgram, vertexShader);
  91.         GLES20.glAttachShader(this.mProgram, fragmentShader);
  92.         GLES20.glLinkProgram(this.mProgram);
  93.  
  94.         this.mPositionHandle = GLES20.glGetAttribLocation(this.mProgram, "aPos");
  95.         this.mTexCoordHandle = GLES20.glGetAttribLocation(this.mProgram, "aTexCoord");
  96.         this.mProjectionMatrixHandle = GLES20.glGetUniformLocation(this.mProgram, "projection");
  97.         this.mModelMatrixHandle = GLES20.glGetUniformLocation(this.mProgram, "model");
  98.         this.mTexHandle = GLES20.glGetUniformLocation(this.mProgram, "tex");
  99.     }
  100.  
  101.     public void setScreenSize(int width, int height){
  102.         this.screenWidth = width;
  103.         this.screenHeight = height;
  104.     }
  105.  
  106.     public void update(){
  107.         Matrix.setIdentityM(this.projection, 0);
  108.         Matrix.orthoM(this.projection, 0, 0.0f, this.screenWidth, this.screenHeight, 0.0f, -1.0f, 1.0f);
  109.     }
  110.  
  111.     public void renderImageBasic(int x, int y, int width, int height, int texUnit){
  112.         ByteBuffer bb = ByteBuffer.allocateDirect(textureCoords.length * 4);
  113.         bb.order(ByteOrder.nativeOrder());
  114.         this.textureCoordsBuffer = bb.asFloatBuffer();
  115.         this.textureCoordsBuffer.put(textureCoords);
  116.         this.textureCoordsBuffer.position(0);
  117.  
  118.         Matrix.setIdentityM(this.model, 0);
  119.         Matrix.translateM(this.model, 0, x, y, 0.0f);
  120.         Matrix.scaleM(this.model, 0, width, height, 1.0f);
  121.  
  122.         GLES20.glUseProgram(this.mProgram);
  123.  
  124.         GLES20.glEnableVertexAttribArray(this.mPositionHandle);
  125.         GLES20.glEnableVertexAttribArray(this.mTexCoordHandle);
  126.  
  127.         GLES20.glVertexAttribPointer(this.mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, this.vertexBuffer);
  128.         GLES20.glVertexAttribPointer(this.mTexCoordHandle, 2, GLES20.GL_FLOAT, false, 2*4, this.textureCoordsBuffer);
  129.  
  130.         GLES20.glUniformMatrix4fv(this.mProjectionMatrixHandle, 1, false, this.projection, 0);
  131.         GLES20.glUniformMatrix4fv(this.mModelMatrixHandle, 1, false, this.model, 0);
  132.  
  133.         GLES20.glUniform1i(this.mTexHandle, texUnit);
  134.  
  135.         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
  136.  
  137.         GLES20.glDisableVertexAttribArray(this.mPositionHandle);
  138.         GLES20.glDisableVertexAttribArray(this.mTexCoordHandle);
  139.     }
  140.  
  141.     public void renderImageTexCoords(int x, int y, int width, int height, float u1, float v1, float u2, float v2, int texUnit){
  142.         float[] texCoords = {
  143.                 u1, v1,
  144.                 u1, v2,
  145.                 u2, v2,
  146.                 u1, v1,
  147.                 u2, v2,
  148.                 u2, v1
  149.         };
  150.  
  151.         ByteBuffer bb = ByteBuffer.allocateDirect(texCoords.length * 4);
  152.         bb.order(ByteOrder.nativeOrder());
  153.         this.textureCoordsBuffer = bb.asFloatBuffer();
  154.         this.textureCoordsBuffer.put(texCoords);
  155.         this.textureCoordsBuffer.position(0);
  156.  
  157.         Matrix.setIdentityM(this.model, 0);
  158.         Matrix.translateM(this.model, 0, x, y, 0.0f);
  159.         Matrix.scaleM(this.model, 0, width, height, 1.0f);
  160.  
  161.         GLES20.glUseProgram(this.mProgram);
  162.  
  163.         GLES20.glEnableVertexAttribArray(this.mPositionHandle);
  164.         GLES20.glEnableVertexAttribArray(this.mTexCoordHandle);
  165.  
  166.         GLES20.glVertexAttribPointer(this.mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, this.vertexBuffer);
  167.         GLES20.glVertexAttribPointer(this.mTexCoordHandle, 2, GLES20.GL_FLOAT, false, 2*4, this.textureCoordsBuffer);
  168.  
  169.         GLES20.glUniformMatrix4fv(this.mProjectionMatrixHandle, 1, false, this.projection, 0);
  170.         GLES20.glUniformMatrix4fv(this.mModelMatrixHandle, 1, false, this.model, 0);
  171.  
  172.         GLES20.glUniform1i(this.mTexHandle, texUnit);
  173.  
  174.         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
  175.  
  176.         GLES20.glDisableVertexAttribArray(this.mPositionHandle);
  177.         GLES20.glDisableVertexAttribArray(this.mTexCoordHandle);
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement