gpt4 book ai didi

android - 绘制前裁剪 GLES 纹理

转载 作者:行者123 更新时间:2023-11-29 19:44:26 24 4
gpt4 key购买 nike

我正在使用 SurfaceView 通过 GLES 将相机预览渲染到屏幕上。是否可以在渲染之前用下面的方法裁剪纹理?我想裁剪 16:9 的纹理以在屏幕上显示为 4:3。

public void drawFrame(int width, int height, float sourceAspectRatio, float targetAspectRatio, boolean flipHorizontally, boolean flipVertically) {

//Aspect ratio correction. Source Aspect ratio is taken from Camera.Parameters.getPictureSize()
//assuming that value is the native resolution of the camera. Sometimes the native camera aspect ratio
//doesn't match the display's aspect ratio.
float scaleX, scaleY;
if (sourceAspectRatio >= targetAspectRatio) {
scaleX = sourceAspectRatio/targetAspectRatio;
scaleY = 1;
} else {
scaleX = 1;
scaleY = targetAspectRatio/sourceAspectRatio;
}

if (flipHorizontally) scaleX = -scaleX;
if (flipVertically) scaleY = -scaleY;

checkGlError("onDrawFrame start");
mSurfaceTexture.getTransformMatrix(mSTMatrix);

GLES20.glViewport(0,0,width,height);

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

GLES20.glUseProgram(mProgram);
checkGlError("glUseProgram");

/**
* Explanation of double binding: http://stackoverflow.com/questions/20386515/glsurfaceview-framerate-issues-on-nexus-5
*
* I was able to replicate the behavior, and my GL wizard office-mate figured out the problem.
* Basically, one of the EGL contexts isn't noticing that the texture contents have changed, so it keeps rendering older data.
* We think it's getting occasional updates because it has a set of buffers that it cycles through,
* so eventually it re-uses the buffer you're looking at.
* I was able to fix the problem in my code by updating the texture renderer class, changing this:
*
* GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID)
*
* to this
*
* GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
* GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
*
* The un-bind and re-bind causes the driver to pick up the right buffer.
*/
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);

mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maPosition");
GLES20.glEnableVertexAttribArray(maPositionHandle);
checkGlError("glEnableVertexAttribArray maPositionHandle");

mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maTextureHandle");
GLES20.glEnableVertexAttribArray(maTextureHandle);
checkGlError("glEnableVertexAttribArray maTextureHandle");
Matrix.setIdentityM(mMVPMatrix, 0);
mMVPMatrix[0] = scaleX;
mMVPMatrix[5] = scaleY;
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);

GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
checkGlError("glDrawArrays");

GLES20.glDisableVertexAttribArray(maTextureHandle);
GLES20.glDisableVertexAttribArray(maPositionHandle);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
GLES20.glUseProgram(0);
}

最佳答案

只需更改纹理坐标(选择要在屏幕上显示的内容)和顶点坐标(决定在屏幕上绘制的位置)。

关于android - 绘制前裁剪 GLES 纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38079164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com