Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use rotateM() of Matrix to rotate matrix from SurfaceTexture but corrupt the video output

I managed to play video with opengl es, I used the way of grafika's ContinuousCaptureActivity, my data source is MediaPlayer rather than Camera which makes no difference. MediaPlayer produces video frames continuously and I draw each frame to screen in onFrameAvailable callback. The code is as follows which works well:

    mVideoTexture.updateTexImage();
    mVideoTexture.getTransformMatrix(mTmpMatrix);
    mDisplaySurface.makeCurrent();
    int viewWidth = getWidth();
    int viewHeight = getHeight();
    GLES20.glViewport(0, 0, viewWidth, viewHeight);
    mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);
    mDisplaySurface.swapBuffers();

Now I want to rotate video frames with 270 degrees, so I changed the code:

        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    mVideoTexture.updateTexImage();
    mVideoTexture.getTransformMatrix(mTmpMatrix);
    mDisplaySurface.makeCurrent();
    int viewWidth = getWidth();
    int viewHeight = getHeight();
    GLES20.glViewport(0, 0, viewWidth, viewHeight);
    Matrix.rotateM(mTmpMatrix, 0, 270, 1f, 0, 0);
    mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);
    mDisplaySurface.swapBuffers();

But the result is weird, take a look at the picture below: enter image description here

But I can flip video frame successfully with the code below:

        mVideoTexture.updateTexImage();
    mVideoTexture.getTransformMatrix(mTmpMatrix);
    mDisplaySurface.makeCurrent();
    int viewWidth = getWidth();
    int viewHeight = getHeight();
    GLES20.glViewport(0, 0, viewWidth, viewHeight);
    mTmpMatrix[5] = -1 * mTmpMatrix[5];
    mTmpMatrix[13] = 1.0f - mTmpMatrix[13];
    mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);
    mDisplaySurface.swapBuffers();

How to achieve the rotation, Could anyone give me some help?

ADD:

At first, I want to tell that I always used this code for each draw action:

        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

I use this demo to do my test, it is a very good demo for this test. https://github.com/izacus/AndroidOpenGLVideoDemo

The matrix got from the surfacetexture is:

1.0, 0.0, 0.0, 0.0

0.0, -1.0, 0.0, 0.0

0.0, 0.0, 1.0, 0.0

0.0, 1.0, 0.0, 1.0

After "Matrix.setRotateM(videoTextureTransform, 0, 270 , 0, 0, 1);",

it became:

1.1924881E-8, -1.0, 0.0, 0.0

1.0, 1.1924881E-8, 0.0, 0.0

0.0, 0.0, 1.0, 0.0

0.0, 0.0, 0.0, 1.0

And this video effect is: enter image description here

like image 416
dragonfly Avatar asked Nov 18 '15 07:11

dragonfly


2 Answers

fadden's rotation matrix about the z-axis needs to be followed by the correct translation to bring it back on-screen, so to speak. I've tested all 3 rotations below on SurfaceTexture video:

ROTATE 90

Matrix.rotateM(mTmpMatrix, 0, 90, 0, 0, 1);
Matrix.translateM(mTmpMatrix, 0, 0, -1, 0);

ROTATE 180

Matrix.rotateM(mTmpMatrix, 0, 180, 0, 0, 1);
Matrix.translateM(mTmpMatrix, 0, -1, -1, 0);

ROTATE 270

Matrix.rotateM(mTmpMatrix, 0, 270, 0, 0, 1);
Matrix.translateM(mTmpMatrix, 0, -1, 0, 0);
like image 100
matteo411 Avatar answered Oct 17 '22 14:10

matteo411


You're rotating about the X axis:

Matrix.rotateM(mTmpMatrix, 0, 270, 1f, 0, 0);

By convention that runs from left to right. The axis acts like an axle; by flipping it 270 degrees, you're rotating the plane so you're viewing it edge-on, and it's effectively vanishing. I think what you're seeing is essentially uninitialized data, and if you call glClear() you'll see the background color instead.

Try rotating about the Z axis, which is a line pointing out of the screen:

Matrix.rotateM(mTmpMatrix, 0, 270, 0, 0, 1);

(It might also be interesting to experiment with a rotation of about 15 degrees about the X axis just to see how that looks. When fiddling with matrices it's often useful to start with small values.)

like image 22
fadden Avatar answered Oct 17 '22 13:10

fadden