|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 pedroSG94. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.pedro.encoder.input.gl.render.filters |
| 17 | + |
| 18 | +import android.content.Context |
| 19 | +import android.opengl.GLES20 |
| 20 | +import android.opengl.Matrix |
| 21 | +import android.os.Build |
| 22 | +import androidx.annotation.RequiresApi |
| 23 | +import com.pedro.common.TimeUtils.getCurrentTimeMillis |
| 24 | +import com.pedro.encoder.R |
| 25 | +import com.pedro.encoder.utils.gl.GlUtil |
| 26 | +import java.nio.ByteBuffer |
| 27 | +import java.nio.ByteOrder |
| 28 | + |
| 29 | +/** |
| 30 | + * Created by pedro on 4/02/18. |
| 31 | + */ |
| 32 | +@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) |
| 33 | +class ChromaticAberrationFilterRender: BaseFilterRender() { |
| 34 | + //rotation matrix |
| 35 | + private val squareVertexDataFilter = floatArrayOf( |
| 36 | + // X, Y, Z, U, V |
| 37 | + -1f, -1f, 0f, 0f, 0f, //bottom left |
| 38 | + 1f, -1f, 0f, 1f, 0f, //bottom right |
| 39 | + -1f, 1f, 0f, 0f, 1f, //top left |
| 40 | + 1f, 1f, 0f, 1f, 1f, //top right |
| 41 | + ) |
| 42 | + |
| 43 | + private var program = -1 |
| 44 | + private var aPositionHandle = -1 |
| 45 | + private var aTextureHandle = -1 |
| 46 | + private var uMVPMatrixHandle = -1 |
| 47 | + private var uSTMatrixHandle = -1 |
| 48 | + private var uSamplerHandle = -1 |
| 49 | + private var uTimeHandle = -1 |
| 50 | + |
| 51 | + private var START_TIME = getCurrentTimeMillis() |
| 52 | + |
| 53 | + init { |
| 54 | + squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.size * FLOAT_SIZE_BYTES) |
| 55 | + .order(ByteOrder.nativeOrder()) |
| 56 | + .asFloatBuffer() |
| 57 | + squareVertex.put(squareVertexDataFilter).position(0) |
| 58 | + Matrix.setIdentityM(MVPMatrix, 0) |
| 59 | + Matrix.setIdentityM(STMatrix, 0) |
| 60 | + } |
| 61 | + |
| 62 | + override fun initGlFilter(context: Context) { |
| 63 | + val vertexShader = GlUtil.getStringFromRaw(context, R.raw.simple_vertex) |
| 64 | + val fragmentShader = GlUtil.getStringFromRaw(context, R.raw.chromatic_aberration_fragment) |
| 65 | + |
| 66 | + program = GlUtil.createProgram(vertexShader, fragmentShader) |
| 67 | + aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition") |
| 68 | + aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord") |
| 69 | + uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix") |
| 70 | + uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix") |
| 71 | + uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler") |
| 72 | + uTimeHandle = GLES20.glGetUniformLocation(program, "uTime") |
| 73 | + } |
| 74 | + |
| 75 | + override fun drawFilter() { |
| 76 | + GLES20.glUseProgram(program) |
| 77 | + |
| 78 | + squareVertex.position(SQUARE_VERTEX_DATA_POS_OFFSET) |
| 79 | + GLES20.glVertexAttribPointer( |
| 80 | + aPositionHandle, 3, GLES20.GL_FLOAT, false, |
| 81 | + SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex |
| 82 | + ) |
| 83 | + GLES20.glEnableVertexAttribArray(aPositionHandle) |
| 84 | + |
| 85 | + squareVertex.position(SQUARE_VERTEX_DATA_UV_OFFSET) |
| 86 | + GLES20.glVertexAttribPointer( |
| 87 | + aTextureHandle, 2, GLES20.GL_FLOAT, false, |
| 88 | + SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex |
| 89 | + ) |
| 90 | + GLES20.glEnableVertexAttribArray(aTextureHandle) |
| 91 | + |
| 92 | + GLES20.glUniformMatrix4fv(uMVPMatrixHandle, 1, false, MVPMatrix, 0) |
| 93 | + GLES20.glUniformMatrix4fv(uSTMatrixHandle, 1, false, STMatrix, 0) |
| 94 | + val time = ((getCurrentTimeMillis() - START_TIME).toFloat()) / 1000f |
| 95 | + if (time >= 10) START_TIME += 10000 |
| 96 | + GLES20.glUniform1f(uTimeHandle, time) |
| 97 | + |
| 98 | + GLES20.glUniform1i(uSamplerHandle, 0) |
| 99 | + GLES20.glActiveTexture(GLES20.GL_TEXTURE0) |
| 100 | + GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, previousTexId) |
| 101 | + } |
| 102 | + |
| 103 | + override fun disableResources() { |
| 104 | + GlUtil.disableResources(aTextureHandle, aPositionHandle) |
| 105 | + } |
| 106 | + |
| 107 | + override fun release() { |
| 108 | + GLES20.glDeleteProgram(program) |
| 109 | + } |
| 110 | +} |
0 commit comments