您可以在顶点着色器中旋转 OpenGL 输出,如下所示:
#version 300 es
in vec4 position;
in mediump vec4 texturecoordinate;
in vec4 color;
uniform float preferredRotation;
out mediump vec2 coordinate;
void main()
{
//const float pi = 4.0 * atan(1.0);
//float radians = (( -90.0 ) / 180.0 * pi );
// Preferred rotation of video acquired, for example, by:
// AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
// CGAffineTransform preferredTransform = [videoTrack preferredTransform];
// self.glKitView.preferredRotation = -1 * atan2(preferredTransform.b, preferredTransform.a);
// Preferred rotation for both portrait and landscape
mat4 rotationMatrix = mat4( cos(preferredRotation), -sin(preferredRotation), 0.0, 0.0,
sin(preferredRotation), cos(preferredRotation), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// Mirror vertical (portrait only)
mat4 rotationMatrix = mat4( cos(preferredRotation), sin(preferredRotation), 0.0, 0.0,
-sin(preferredRotation), cos(preferredRotation), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// Mirror horizontal (landscape only)
mat4 rotationMatrix = mat4( 1.0, 0.0, 0.0, 0.0,
0.0, cos(preferredRotation), -sin(preferredRotation), 0.0,
0.0, sin(preferredRotation), cos(preferredRotation), 0.0,
0.0, 0.0, 0.0, 1.0);
// Mirror vertical (landscape only)
mat4 rotationMatrix = mat4( cos(preferredRotation), 0.0, sin(preferredRotation), 0.0,
0.0, 1.0, 0.0, 0.0,
-sin(preferredRotation), 0.0, cos(preferredRotation), 0.0,
0.0, 0.0, 0.0, 1.0);
gl_Position = position * rotationMatrix;
coordinate = texturecoordinate.xy;
}
对于每个 vSync,您可以为 preferredRotation 传递一个新值,这将旋转视图而不进行拉伸。
显然,您只选择一个矩阵4,这取决于视频的方向,然后是它的旋转。每个 matrix4 都会翻转视频窗口——而不是旋转它。对于旋转,您必须首先根据方向选择 matrix4,然后将 preferredRotation 变量替换为度数(以弧度为单位,还提供了公式)。
有很多方法可以旋转视图、图层、对象等;但是,如果您通过 OpenGL 渲染图像,那么您应该选择这种方法,并且只能选择这种方法。