package com.martin.ads.panoramaopengltutorial;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.opengl.GLES10;
import android.opengl.GLES11;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.GLES30;
import android.opengl.GLES31Ext;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.Surface;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
/**
* Created by Ads on 2016/11/13.
*/
public class GLRenderer implements GLSurfaceView.Renderer
, SurfaceTexture.OnFrameAvailableListener,
MediaPlayer.OnVideoSizeChangedListener,
SensorEventListener {
private static final String TAG = "GLRenderer";
private Context context;
private int aPositionHandle;
private int programId;
private float[] modelMatrix = new float[16];
private float[] projectionMatrix=new float[16];
private float[] viewMatrix = new float[16];
private float[] modelViewMatrix = new float[16];
private float[] mMVPMatrix = new float[16];
private int uMatrixHandle;
private int uTextureSamplerHandle;
private int aTextureCoordHandle;
private int textureId;
private SurfaceTexture surfaceTexture;
private MediaPlayer mediaPlayer;
private float[] mSTMatrix = new float[16];
private int uSTMMatrixHandle;
private boolean updateSurface;
private boolean playerPrepared;
private int screenWidth,screenHeight;
//private Plain plain;
private Sphere sphere;
//private SphereNoTexture sphereNoTexture;
public GLRenderer(Context context,String videoPath) {
this.context = context;
playerPrepared=false;
synchronized(this) {
updateSurface = false;
}
//plain=new Plain();
sphere=new Sphere(18,100,200);
//sphereNoTexture=new SphereNoTexture(18,100,200);
mediaPlayer=new MediaPlayer();
try{
mediaPlayer.setDataSource(context, Uri.parse(videoPath));
}catch (IOException e){
e.printStackTrace();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(true);
mediaPlayer.setOnVideoSizeChangedListener(this);
Matrix.setIdentityM(modelMatrix,0);
initSensor();
initGestureHandler();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
String vertexShader = ShaderUtils.readRawTextFile(context, R.raw.vertex_shader);
String fragmentShader= ShaderUtils.readRawTextFile(context, R.raw.fragment_shader);
programId=ShaderUtils.createProgram(vertexShader,fragmentShader);
aPositionHandle= GLES20.glGetAttribLocation(programId,"aPosition");
uMatrixHandle=GLES20.glGetUniformLocation(programId,"uMatrix");
uSTMMatrixHandle = GLES20.glGetUniformLocation(programId, "uSTMatrix");
uTextureSamplerHandle=GLES20.glGetUniformLocation(programId,"sTexture");
aTextureCoordHandle=GLES20.glGetAttribLocation(programId,"aTexCoord");
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
textureId = textures[0];
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId);
ShaderUtils.checkGlError("glBindTexture 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);
surfaceTexture = new SurfaceTexture(textureId);
surfaceTexture.setOnFrameAvailableListener(this);
Surface surface = new Surface(surfaceTexture);
mediaPlayer.setSurface(surface);
surface.release();
if (!playerPrepared){
try {
mediaPlayer.prepare();
playerPrepared=true;
} catch (IOException t) {
Log.e(TAG, "media player prepare failed");
}
mediaPlayer.start();
playerPrepared=true;
}
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.d(TAG, "onSurfaceChanged: "+width+" "+height);
screenWidth=width; screenHeight=height;
float ratio=(float)width/height;
Matrix.perspectiveM(projectionMatrix, 0, 90, ratio/2, 1f, 500f);
Matrix.setLookAtM(viewMatrix, 0,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f,-1.0f,
0.0f, 1.0f, 0.0f);
}
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
synchronized (this){
if (updateSurface){
surfaceTexture.updateTexImage();
surfaceTexture.getTransformMatrix(mSTMatrix);
updateSurface = false;
}
}
GLES20.glUseProgram(programId);
Matrix.setIdentityM(modelMatrix, 0);
Matrix.rotateM(modelMatrix, 0, mDeltaY, 1.0f, 0.0f, 0.0f);
Matrix.rotateM(modelMatrix, 0, mDeltaX, 0.0f, 1.0f, 0.0f);
//视角从90度到14度
float currentDegree= (float) (Math.toDegrees(Math.atan(mScale))*2);
Matrix.perspectiveM(projectionMatrix, 0, currentDegree,(float)screenWidth/screenHeight/2, 1f, 500f);
Matrix.multiplyMM(modelViewMatrix, 0, viewMatrix, 0, modelMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, projectionMatrix, 0, modelViewMatrix, 0);
GLES20.glUniformMatrix4fv(uMatrixHandle,1,false,mMVPMatrix,0);
GLES20.glUniformMatrix4fv(uSTMMatrixHandle, 1, false, mSTMatrix, 0);
sphere.uploadVerticesBuffer(aPositionHandle);
sphere.uploadTexCoordinateBuffer(aTextureCoordHandle);
//sphereNoTexture.uploadVerticesBuffer(aPositionHandle);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,textureId);
GLES20.glUniform1i(uTextureSamplerHandle,0);
//GLES20.glViewport(0,0,screenWidth,screenHeight);
//sphereNoTexture.draw();
//sphere.draw();
GLES20.glViewport(0,0,screenWidth/2,screenHeight);
sphere.draw();
GLES20.glViewport(screenWidth/2,0,screenWidth-screenWidth/2,screenHeight);
sphere.draw();
}
@Override
synchronized public void onFrameAvailable(SurfaceTexture surface) {
updateSurface = true;
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Log.d(TAG, "onVideoSizeChanged: "+width+" "+height);
//updateProjection(width,height);
}
private void updateProjection(int videoWidth, int videoHeight){
float screenRatio=(float)screenWidth/screenHeight;
float videoRatio=(float)videoWidth/videoHeight;
if (videoRatio>screenRatio){
Matrix.orthoM(projectionMatrix,0,-1f,1f,-videoRatio/screenRatio,videoRatio/screenRatio,-1f
没有合适的资源?快使用搜索试试~ 我知道了~
Android平台下的全景视频播放器配套教程—— 项目示例代码
共39个文件
java:9个
xml:8个
png:5个
5星 · 超过95%的资源 需积分: 37 124 下载量 20 浏览量
2017-01-29
19:18:59
上传
评论 1
收藏 108KB RAR 举报
温馨提示
Android平台下的全景视频播放器配套教程,博客地址http://blog.csdn.net/Martin20150405/article/details/53149578
资源推荐
资源详情
资源评论
收起资源包目录
PanoramaOpenGLTutorial up to 5.5.rar (39个子文件)
PanoramaOpenGLTutorial
gradlew.bat 2KB
gradlew 5KB
gradle.properties 872B
PanoramaOpenGLTutorial.iml 953B
projectFilesBackup
.idea
workspace.xml 154KB
app
proguard-rules.pro 680B
src
test
java
com
martin
ads
panoramaopengltutorial
ExampleUnitTest.java 344B
androidTest
java
com
martin
ads
panoramaopengltutorial
ApplicationTest.java 380B
main
res
raw
fragment_shader.glsl 205B
vertex_shader.glsl 226B
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 214B
strings.xml 88B
styles.xml 394B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
layout
activity_main.xml 756B
values-w820dp
dimens.xml 364B
java
com
martin
ads
panoramaopengltutorial
Plain.java 2KB
TextureHelper.java 2KB
SphereNoTexture.java 4KB
ShaderUtils.java 3KB
Sphere.java 5KB
MainActivity.java 2KB
GLRenderer.java 11KB
AndroidManifest.xml 827B
app.iml 10KB
.gitignore 8B
build.gradle 664B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 233B
.gitignore 105B
local.properties 516B
settings.gradle 16B
.gitattributes 395B
build.gradle 521B
共 39 条
- 1
Martin20150405
- 粉丝: 298
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页