Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @@ -3,11 +3,16 @@
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- +import android.graphics.SurfaceTexture;
- +import android.media.MediaPlayer;
- +import android.opengl.GLES11Ext;
- import android.opengl.GLES20;
- import android.opengl.GLSurfaceView.Renderer;
- import android.opengl.GLUtils;
- +import android.opengl.Matrix;
- import android.os.SystemClock;
- import android.util.Log;
- +import android.view.Surface;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- @@ -16,7 +21,7 @@
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- -public class PlanetRenderer implements Renderer {
- +public class PlanetRenderer implements Renderer, SurfaceTexture.OnFrameAvailableListener {
- private final String quadVS =
- "precision mediump float;\n" +
- "attribute vec4 vPosition;\n" +
- @@ -29,11 +34,13 @@
- "}\n";
- private final String quadFS =
- + "#extension GL_OES_EGL_image_external : require\n" +
- "precision mediump float;\n" +
- - "uniform sampler2D uTexture0;\n" +
- + "uniform samplerExternalOES uTexture0;\n" +
- "uniform sampler2D uTexture1;\n" +
- "uniform sampler2D uTexture2;\n" +
- "uniform vec3 uRotate;\n" +
- + "uniform mat4 uSTMatrix;\n" +
- "varying vec4 Position;\n" +
- "void main() {\n" +
- @@ -66,7 +73,9 @@
- " }\n" +
- " vCoord.x += uRotate.x;\n" +
- + " if (vCoord.x >= 1.0) { vCoord.x -= 1.0; }\n" +
- + " vec2 vTexCoord = (uSTMatrix * vec4(vCoord.x, -vCoord.y, 0, 1)).xy;\n" +
- " vec3 vCol = texture2D(uTexture0, vCoord).rgb;\n" +
- " gl_FragColor = vec4(vCol * sz, 1.0);\n" +
- " } else {\n" +
- @@ -81,6 +90,7 @@
- private int quTexture1;
- private int quTexture2;
- private int quRotate;
- + private int quSTMatrix;
- float ratioX, ratioY;
- @@ -108,6 +118,12 @@
- private final Context mContext;
- + private SurfaceTexture mSurface;
- + private boolean updateSurface = false;
- + private MediaPlayer mMediaPlayer;
- + private int mTextureID;
- + private float[] mSTMatrix = new float[16];
- +
- public PlanetRenderer(Context context)
- {
- super();
- @@ -117,6 +133,14 @@
- @Override
- public void onDrawFrame(GL10 arg0)
- {
- + synchronized(this) {
- + if (updateSurface) {
- + mSurface.updateTexImage();
- + mSurface.getTransformMatrix(mSTMatrix);
- + updateSurface = false;
- + }
- + }
- +
- long curTime = SystemClock.uptimeMillis();
- if (curTime > start_frame + 1000) {
- @@ -142,7 +166,7 @@
- GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
- GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
- - GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, planetTex);
- + GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
- GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, offsetTex1);
- GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
- @@ -159,6 +183,7 @@
- GLES20.glUniform1i(quTexture0, 0);
- GLES20.glUniform1i(quTexture1, 1);
- GLES20.glUniform1i(quTexture2, 2);
- + GLES20.glUniformMatrix4fv(quSTMatrix, 1, false, mSTMatrix, 0);
- double ta = tiltAngle * Math.PI;
- GLES20.glUniform3f(quRotate, rotateAngle, (float) Math.sin(ta), (float) Math.cos(ta));
- @@ -342,6 +367,7 @@
- quTexture1 = GLES20.glGetUniformLocation(quadProgram, "uTexture1");
- quTexture2 = GLES20.glGetUniformLocation(quadProgram, "uTexture2");
- quRotate = GLES20.glGetUniformLocation(quadProgram, "uRotate");
- + quSTMatrix = GLES20.glGetUniformLocation(quadProgram, "uSTMatrix");
- final float quad[] = {
- -1, 1,
- @@ -351,5 +377,40 @@
- };
- quadVB = createBuffer(quad);
- +
- + Matrix.setIdentityM(mSTMatrix, 0);
- +
- + GLES20.glGenTextures(1, genbuf, 0);
- + mTextureID = genbuf[0];
- + GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
- + GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
- + GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
- + GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
- + GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
- +
- + mSurface = new SurfaceTexture(mTextureID);
- + mSurface.setOnFrameAvailableListener(this);
- + Surface surface = new Surface(mSurface);
- +
- + mMediaPlayer = MediaPlayer.create(mContext, R.raw.small);
- + mMediaPlayer.setSurface(surface);
- + mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
- + @Override
- + public void onCompletion(MediaPlayer mp) {
- + mp.start();
- + }
- + });
- + mMediaPlayer.start();
- +
- + surface.release();
- +
- + synchronized(this) {
- + updateSurface = false;
- + }
- + }
- +
- + @Override
- + synchronized public void onFrameAvailable(SurfaceTexture surfaceTexture) {
- + updateSurface = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement