/* * Copyright (C) 2021 pedroSG94. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.pedro.encoder.input.gl.render.filters; import android.content.Context; import android.opengl.GLES20; import android.opengl.Matrix; import android.os.Build; import androidx.annotation.RequiresApi; import com.pedro.encoder.R; import com.pedro.encoder.input.video.CameraHelper; import com.pedro.encoder.utils.gl.GlUtil; import java.nio.ByteBuffer; import java.nio.ByteOrder; /** * Created by pedro on 9/07/18. */ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public class RotationFilterRender extends BaseFilterRender { //rotation matrix private final float[] squareVertexDataFilter = { // X, Y, Z, U, V -1f, -1f, 0f, 0f, 0f, //bottom left 1f, -1f, 0f, 1f, 0f, //bottom right -1f, 1f, 0f, 0f, 1f, //top left 1f, 1f, 0f, 1f, 1f, //top right }; private int program = -1; private int aPositionHandle = -1; private int aTextureHandle = -1; private int uMVPMatrixHandle = -1; private int uSTMatrixHandle = -1; private int uSamplerHandle = -1; private int rotation = 0; private float[] rotationMatrix = new float[16]; public RotationFilterRender() { squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES) .order(ByteOrder.nativeOrder()) .asFloatBuffer(); squareVertex.put(squareVertexDataFilter).position(0); Matrix.setIdentityM(MVPMatrix, 0); Matrix.setIdentityM(STMatrix, 0); Matrix.setIdentityM(rotationMatrix, 0); } @Override protected void initGlFilter(Context context) { String vertexShader = GlUtil.getStringFromRaw(context, R.raw.simple_vertex); String fragmentShader = GlUtil.getStringFromRaw(context, R.raw.simple_fragment); program = GlUtil.createProgram(vertexShader, fragmentShader); aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition"); aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord"); uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix"); uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix"); uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler"); } @Override protected void drawFilter() { GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); GLES20.glUseProgram(program); squareVertex.position(SQUARE_VERTEX_DATA_POS_OFFSET); GLES20.glVertexAttribPointer(aPositionHandle, 3, GLES20.GL_FLOAT, false, SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex); GLES20.glEnableVertexAttribArray(aPositionHandle); squareVertex.position(SQUARE_VERTEX_DATA_UV_OFFSET); GLES20.glVertexAttribPointer(aTextureHandle, 2, GLES20.GL_FLOAT, false, SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex); GLES20.glEnableVertexAttribArray(aTextureHandle); GLES20.glUniformMatrix4fv(uMVPMatrixHandle, 1, false, MVPMatrix, 0); GLES20.glUniformMatrix4fv(uSTMatrixHandle, 1, false, STMatrix, 0); GLES20.glUniform1i(uSamplerHandle, 4); GLES20.glActiveTexture(GLES20.GL_TEXTURE4); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, previousTexId); } @Override public void release() { GLES20.glDeleteProgram(program); } public int getRotation() { return rotation; } public void setRotation(int rotation) { this.rotation = rotation; //Set rotation Matrix.setRotateM(rotationMatrix, 0, rotation, 0, 0, 1.0f); Matrix.scaleM(rotationMatrix, 0, 1f, 1f, 0f); //Translation //Matrix.translateM(rotationMatrix, 0, 0f, 0f, 0f); // Combine the rotation matrix with the projection and camera view Matrix.multiplyMM(MVPMatrix, 0, rotationMatrix, 0, MVPMatrix, 0); } /** * Keep aspect ratio if you rotate 90º or 270º. * @param rotation value * @param width width of stream (prepareVideo method) if you are streaming or preview (startPreview method) if you aren't streaming. * @param height height of stream (prepareVideo method) if you are streaming or preview (startPreview method) if you aren't streaming. */ public void setRotationFixed(int rotation, int width, int height, boolean isPortrait) { this.rotation = rotation; //Set rotation Matrix.setRotateM(rotationMatrix, 0, rotation, 0, 0, 1.0f); if (rotation == 90 || rotation == 270) { float value = (float) height / (float) width; if (isPortrait) { Matrix.scaleM(rotationMatrix, 0, value, 1f, 0f); } else { Matrix.scaleM(rotationMatrix, 0, 1f, value, 0f); } } else { Matrix.scaleM(rotationMatrix, 0, 1f, 1f, 0f); } // Combine the rotation matrix with the projection and camera view Matrix.multiplyMM(MVPMatrix, 0, rotationMatrix, 0, MVPMatrix, 0); } }