Advertisement
ginkage

Video Texture

Nov 27th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. @@ -3,11 +3,16 @@
  2.  import android.content.Context;
  3.  import android.graphics.Bitmap;
  4.  import android.graphics.BitmapFactory;
  5. +import android.graphics.SurfaceTexture;
  6. +import android.media.MediaPlayer;
  7. +import android.opengl.GLES11Ext;
  8.  import android.opengl.GLES20;
  9.  import android.opengl.GLSurfaceView.Renderer;
  10.  import android.opengl.GLUtils;
  11. +import android.opengl.Matrix;
  12.  import android.os.SystemClock;
  13.  import android.util.Log;
  14. +import android.view.Surface;
  15.  
  16.  import javax.microedition.khronos.egl.EGLConfig;
  17.  import javax.microedition.khronos.opengles.GL10;
  18. @@ -16,7 +21,7 @@
  19.  import java.nio.FloatBuffer;
  20.  import java.nio.IntBuffer;
  21.  
  22. -public class PlanetRenderer implements Renderer {
  23. +public class PlanetRenderer implements Renderer, SurfaceTexture.OnFrameAvailableListener {
  24.     private final String quadVS =
  25.         "precision mediump float;\n" +
  26.         "attribute vec4 vPosition;\n" +
  27. @@ -29,11 +34,13 @@
  28.         "}\n";
  29.  
  30.     private final String quadFS =
  31. +       "#extension GL_OES_EGL_image_external : require\n" +
  32.         "precision mediump float;\n" +
  33. -       "uniform sampler2D uTexture0;\n" +
  34. +       "uniform samplerExternalOES uTexture0;\n" +
  35.         "uniform sampler2D uTexture1;\n" +
  36.         "uniform sampler2D uTexture2;\n" +
  37.         "uniform vec3 uRotate;\n" +
  38. +       "uniform mat4 uSTMatrix;\n" +
  39.         "varying vec4 Position;\n" +
  40.  
  41.         "void main() {\n" +
  42. @@ -66,7 +73,9 @@
  43.         "       }\n" +
  44.  
  45.         "       vCoord.x += uRotate.x;\n" +
  46. +       "       if (vCoord.x >= 1.0) { vCoord.x -= 1.0; }\n" +
  47.  
  48. +       "       vec2 vTexCoord = (uSTMatrix * vec4(vCoord.x, -vCoord.y, 0, 1)).xy;\n" +
  49.         "       vec3 vCol = texture2D(uTexture0, vCoord).rgb;\n" +
  50.         "       gl_FragColor = vec4(vCol * sz, 1.0);\n" +
  51.         "   } else {\n" +
  52. @@ -81,6 +90,7 @@
  53.     private int quTexture1;
  54.     private int quTexture2;
  55.     private int quRotate;
  56. +   private int quSTMatrix;
  57.  
  58.     float ratioX, ratioY;
  59.  
  60. @@ -108,6 +118,12 @@
  61.  
  62.     private final Context mContext;
  63.  
  64. +   private SurfaceTexture mSurface;
  65. +   private boolean updateSurface = false;
  66. +   private MediaPlayer mMediaPlayer;
  67. +   private int mTextureID;
  68. +   private float[] mSTMatrix = new float[16];
  69. +
  70.     public PlanetRenderer(Context context)
  71.     {
  72.         super();
  73. @@ -117,6 +133,14 @@
  74.     @Override
  75.     public void onDrawFrame(GL10 arg0)
  76.     {
  77. +       synchronized(this) {
  78. +           if (updateSurface) {
  79. +               mSurface.updateTexImage();
  80. +               mSurface.getTransformMatrix(mSTMatrix);
  81. +               updateSurface = false;
  82. +           }
  83. +       }
  84. +
  85.         long curTime = SystemClock.uptimeMillis();
  86.  
  87.         if (curTime > start_frame + 1000) {
  88. @@ -142,7 +166,7 @@
  89.         GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  90.  
  91.         GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  92. -       GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, planetTex);
  93. +       GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
  94.         GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
  95.         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, offsetTex1);
  96.         GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
  97. @@ -159,6 +183,7 @@
  98.         GLES20.glUniform1i(quTexture0, 0);
  99.         GLES20.glUniform1i(quTexture1, 1);
  100.         GLES20.glUniform1i(quTexture2, 2);
  101. +       GLES20.glUniformMatrix4fv(quSTMatrix, 1, false, mSTMatrix, 0);
  102.  
  103.         double ta = tiltAngle * Math.PI;
  104.         GLES20.glUniform3f(quRotate, rotateAngle, (float) Math.sin(ta), (float) Math.cos(ta));
  105. @@ -342,6 +367,7 @@
  106.         quTexture1 = GLES20.glGetUniformLocation(quadProgram, "uTexture1");
  107.         quTexture2 = GLES20.glGetUniformLocation(quadProgram, "uTexture2");
  108.         quRotate = GLES20.glGetUniformLocation(quadProgram, "uRotate");
  109. +       quSTMatrix = GLES20.glGetUniformLocation(quadProgram, "uSTMatrix");
  110.  
  111.         final float quad[] = {
  112.             -1,  1,
  113. @@ -351,5 +377,40 @@
  114.         };
  115.  
  116.         quadVB = createBuffer(quad);
  117. +
  118. +       Matrix.setIdentityM(mSTMatrix, 0);
  119. +
  120. +       GLES20.glGenTextures(1, genbuf, 0);
  121. +       mTextureID = genbuf[0];
  122. +       GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
  123. +       GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
  124. +       GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
  125. +       GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
  126. +       GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
  127. +
  128. +       mSurface = new SurfaceTexture(mTextureID);
  129. +       mSurface.setOnFrameAvailableListener(this);
  130. +       Surface surface = new Surface(mSurface);
  131. +
  132. +       mMediaPlayer = MediaPlayer.create(mContext, R.raw.small);
  133. +       mMediaPlayer.setSurface(surface);
  134. +       mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  135. +           @Override
  136. +           public void onCompletion(MediaPlayer mp) {
  137. +               mp.start();
  138. +           }
  139. +       });
  140. +       mMediaPlayer.start();
  141. +
  142. +       surface.release();
  143. +
  144. +       synchronized(this) {
  145. +           updateSurface = false;
  146. +       }
  147. +   }
  148. +
  149. +   @Override
  150. +   synchronized public void onFrameAvailable(SurfaceTexture surfaceTexture) {
  151. +       updateSurface = true;
  152.     }
  153.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement