android - 如何 "apply fragment shader to GLSurfaceView/TextureView"?

标签 android opengl-es exoplayer

我的目标是让 ExoPlayer 将彩色视频显示为黑白。

根据这个this Github issue我应该能够使用 Open GL 实现这一点:

ExoPlayer will render video to any Surface. You could use SurfaceTexture, at which point you'd have video rendered into an OpenGL ES texture. Once you have that you can do anything that OpenGL lets you do, including using a pixel shader to transform the video to black and white.

an older, but related, discussion in the Android Developers Google group Romain Guy 给出了一些关于应该如何完成的细节:

  • Create an OpenGL context
  • Generate an OpenGL texture name
  • Create a SurfaceTexture with the texture name
  • Pass the SurfaceTexture to Camera
  • Listen for updates On SurfaceTexture update, draw the texture with OpenGL using the shader you want

Simple :)

通过玩转 Google's Cardboard example project我确定像下面这样的 fragment 着色器应该是正确的:

precision mediump float;
varying vec4 v_Color;

void main() {
    float grayscale = v_Color[0] * 0.3 + v_Color[1] * 0.59 + v_Color[2] * 0.11;
    gl_FragColor = vec4(grayscale, grayscale, grayscale, 0.1);
}

我还设法让 ExoPlayer 渲染到 TextureView 而不是通常的 SurfaceView:

mPlayer.sendMessage(
    videoRenderer,
    MediaCodecVideoTrackRenderer.MSG_SET_SURFACE,
    new Surface(mVideoTextureView.getSurfaceTexture())
);

现在,如何将所有内容连接在一起?

在哪里可以将着色器“应用”到 TextureView?这甚至可能还是我应该使用 GLSurfaceView 代替?我需要在 Renderer 中做什么?

最佳答案

我之前将 fragment 着色器应用于 ExoPlayer 播放的视频。最有效的方法似乎是在 GLSurfaceView 上播放视频,因为它们对自定义渲染器有很好的支持。

将 OpenGL 着色器应用于 GLSurfaceView 的工作示例可从 here 获得。 .它甚至已经有一个灰度 fragment 着色器作为其 effects library 的一部分。 .现有代码需要进行一些小的重构,因为它使用 MediaPlayer 而不是 ExoPlayer

关于android - 如何 "apply fragment shader to GLSurfaceView/TextureView"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35656146/

相关文章:

java - JSON 格式的字符串到字符串数组

android - ImageView 没有填满屏幕

iOS视频播放

iphone - 如何使用 cocos2d for iPhone 绘制实心圆

android - GLSL - 用于图像处理/混合的 fragment 着色器

android - 当另一个应用程序的音频进入时停止播放音频

android - 在 Debug模式下设置 minifyEnabled true 后 api 调用无法正常工作

java - 为什么 getViewTypeCount() 返回不同的值?

video-streaming - android 中的 HLS 自适应流仅在 10 秒后更改流?

android - TextureView 获取 Surface