显示摄像头流经由一个表面纹理GLSurfaceView纹理、摄像头、表面、GLSurfaceView

2023-09-05 03:22:10 作者:如此悲伤″

我想通过trasmitted到OpenGL ES 2.0的着色器表面纹理显示在GLSurfaceView相机流。

I'm trying to display the camera stream in a GLSurfaceView via a SurfaceTexture trasmitted to OpenGL ES 2.0 shaders.

我把灵感来自于该post.

图片是完整的,但不是我的平板电脑正确显示。 屏幕似乎分为2x2的部分。而其他三个部分是黑色的图像显示在左上角部分

The image is complete but is not correctly displayed on my tablet. The screen seems divided in 2x2 parts. The image is displayed in the upper-left part whereas the other three parts are black.

我怀疑问题来自于我的使用由序列记录here

I suspect that the problem comes from my usage of the transformation matrix returned by the sequence documented here

updateTexImage();
getTransformMatrix(...);

我发这个矩阵中的顶点着色器来生成纹理坐标片段着色器。

I transmit this matrix in the vertex shader to generate the texture coordinates for the fragment shader.

顶点着色器:

attribute vec3 aPosition;
uniform mat4 uMvpTransform;
// Matrix retrieved by getTransformMatrix(...);
uniform mat4 uTexMatTransform;
varying vec2 vTexCoord;

void main(void)
{
  gl_Position = uMvpTransform *vec4(aPosition.xyz, 1);
  vec4 l_tex =  uTexMatTransform*vec4(aPosition.xyz, 1);
  vTexCoord=l_tex.xy;
}

片段着色器:

fragment shader:

#extension GL_OES_EGL_image_external : require
varying mediump vec2 vTexCoord;
uniform samplerExternalOES uSampler; 

void main(void) 
{ 
    mediump vec4 l_tex = texture2D(uSampler, vTexCoord);
    gl_FragColor=l_tex;
}

纹理附着于以下方

The texture is attached to the following square:

 // Image container
 GLfloat l_vertices[] = {
  -1.0f, 1.0f,  0.0f,
  -1.0f, -1.0f,  0.0f,
  1.0f,  -1.0f,  0.0f,
  1.0f,  1.0f,  0.0f };

有没有人达到类似的东西?

Did anyone achieve something similar ?

主编2012年11月3日:

Edited nov 3, 2012:

顶点着色器的修正:

attribute vec3 aPosition;
attribute vec2 aTexCoord;
uniform mat4 uMvpTransform;
// Matrix retrieved by getTransformMatrix(...);
uniform mat4 uTexMatTransform;
varying vec2 vTexCoord;

void main(void)
{
  gl_Position = uMvpTransform *vec4(aPosition.xyz, 1);
  vec4 l_tex =  uTexMatTransform*vec4(aTexCoord.xy,0, 1);
  vTexCoord=l_tex.xy;
}

// Texture
GLfloat l_texCoord[] = {
   0.0f, 1.0f,
   0.0f, 0.0f,
   1.0f,  0.0f,
   1.0f,  1.0f

};

推荐答案

我已经使用了成功地表面纹理上绘制一个自定义OpenGL纹理的相机帧不利用机器人提供的转换矩阵。

I have used the SurfaceTexture succesfully to draw camera frames on a custom opengl texture without using the transformation matrix provided by android.

刚刚尝试定义你指数的顶点和纹理,你会做一个正常的纹理绘制的方式。像这样的例子。

Just try defining your indices vertices and textures the way you would do it for a normal texture draw. Like this for example.

const GLfloat Vertices[] = {0.5, -0.5, 0,
                            0.5, 0.5, 0,
                           -0.5, 0.5, 0,
                           -0.5, -0.5, 0};

const GLubyte Indices[] = { 0, 1, 2,
                            2, 3, 0 };

const GLfloat Textures[] = { 1.,0.,
                             0.,0.,
                             0.,1.,
                             1.,1. };

您应该能够使用表面纹理使用一个正常的纹理的方法。

You should be able to use the surfacetexture the way you use a normal texture.

如果您想进行某种形式的3D投影,这是一个的好帖子如何生成正确的MVP矩阵。你可以用它来乘以在顶点着色器的位置。

In case you want to perform some sort of 3D projection, this is a good post on how to generate a proper MVP matrix. Which you could use to multiply with the position in the vertex shader.