Commit e8f3fe64 authored by zhaozl's avatar zhaozl

feature: 验证RGBA to NV21成功 (恢复默认顶点和纹理坐标的对应关系发现部分区域不糊了,为什么?).

parent b54be368
......@@ -7,13 +7,10 @@ uniform sampler2D sTexture;
uniform float uWidth;
uniform float uHeight;
/**
*
*/
float cY(float x, float y) {
vec4 c = texture2D(sTexture, vec2(x, y));
// return c.r * 0.257 + c.g * 0.504 + c.b * 0.098 + 0.0625;
return 0.299 * c.r + 0.587 * c.g + 0.114 * c.b;
return c.r * 0.257 + c.g * 0.504 + c.b * 0.098 + 0.0625;
}
vec4 cC(float x, float y, float dx, float dy) {
......@@ -26,14 +23,12 @@ vec4 cC(float x, float y, float dx, float dy) {
float cU(float x, float y, float dx, float dy) {
vec4 c = cC(x, y, dx, dy);
// return -0.148 * c.r - 0.291 * c.g + 0.439 * c.b + 0.5000;
return -0.14713 * c.r - 0.28886 * c.g + 0.436 * c.b;
return -0.148 * c.r - 0.291 * c.g + 0.439 * c.b + 0.5000;
}
float cV(float x, float y, float dx, float dy) {
vec4 c = cC(x, y, dx, dy);
// return 0.439 * c.r - 0.368 * c.g - 0.071 * c.b + 0.5000;
return 0.615 * c.r - 0.51499 * c.g - 0.10001 * c.b;
return 0.439 * c.r - 0.368 * c.g - 0.071 * c.b + 0.5000;
}
vec2 cPos(float t, float shiftx, float gy) {
......@@ -50,7 +45,7 @@ vec2 cPos(float t, float shiftx, float gy) {
* 输入: x[0, 1920] y[0, 270)
* 输出: x[0, 1920] y[0, 1080)
* [0, 0]=>[0, 0]; [1, 0]=>[4, 0]; [2, 0]=>[8, 0]; ...;[479, 0]=>[1916, 0]; [480, 0]=>[0, 1]??; [481, 0]=>[4, 1];...;
* [1919, 0]=>[1916, 3]; [1920, 0]=>[0, 4]??
* [1919, 0]=>[1916, 3]; [1920, 0]=>[0, 4] TODO: 是否需要闭1920 ??
*
* [0, 1]=>[0, 4]; [1, 1]=>[4, 4];
*/
......@@ -118,21 +113,21 @@ vec4 calculateVU(float dx, float dy) {
return oColor;
}
void main() {
if (vTextureCoord.x >= 0. && vTextureCoord.x < 1.) {
if (vTextureCoord.y >= 0.625 && vTextureCoord.y < 0.875) {
gl_FragColor = calculateY();
} else if (vTextureCoord.y >= 0.875) {
gl_FragColor = calculateVU(1. / uWidth, 1. / uHeight);
} else {
gl_FragColor = vec4(0, 0, 0, 0);
}
if (vTextureCoord.y >= 0.625 && vTextureCoord.y < 0.875) {
gl_FragColor = calculateY();
} else if (vTextureCoord.y >= 0.875) {
gl_FragColor = calculateVU(1. / uWidth, 1. / uHeight);
} else {
gl_FragColor = vec4(0, 0, 0, 0);
}
// if (vTextureCoord.y >= 0.625 && vTextureCoord.y < 0.875) {
// gl_FragColor = calculateY();
// } else if (vTextureCoord.y >= 0.875) {
// gl_FragColor = calculateVU(1. / uWidth, 1. / uHeight);
// if (vTextureCoord.x >= 0. && vTextureCoord.x < 1.) {
// if (vTextureCoord.y >= 0.625 && vTextureCoord.y < 0.875) {
// gl_FragColor = calculateY();
// } else if (vTextureCoord.y >= 0.875) {
// gl_FragColor = calculateVU(1. / uWidth, 1. / uHeight);
// } else {
// gl_FragColor = vec4(0, 0, 0, 0);
// }
// } else {
// gl_FragColor = vec4(0, 0, 0, 0);
// }
......
......@@ -64,7 +64,8 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
/* Set up alpha blending and an Android background color */
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ZERO);
// GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
GLES20.glClearColor(0.643f, 0.776f, 0.223f, 1.0f);
/* Set up shaders and handles to their variables */
......@@ -127,7 +128,7 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
0f, 1.0f, 0.0f);
Log.v(TAG, "mVMatrix: " + Arrays.toString(mVMatrix));
mPixelBuffer = ByteBuffer.allocateDirect(NV21_SIZE); // RGBA format
mPixelBuffer = ByteBuffer.allocateDirect(RGBA_SIZE);
mPixelBuffer.order(ByteOrder.nativeOrder());
}
......@@ -140,13 +141,17 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
renderTexture();
mPixelBuffer.clear();
GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);
GLES20.glReadPixels(0, 0, mWidth, mHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, mPixelBuffer);
checkGlError("glReadPixels");
mPixelBuffer.rewind();
mPixelBuffer.position(Y_SIZE / 2);
// mPixelBuffer.position(Y_SIZE / 2);
mPixelBuffer.position(NV21_OFFSET);
mPixelBuffer.get(mImageBytes);
saveImage(mImageBytes, "gray");
// mPixelBuffer.position(0);
// mPixelBuffer.get(mImageBytes, Y_SIZE, Y_SIZE / 2);
saveImage(mImageBytes, "nv21");
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
checkGlError("glBindFramebuffer 0");
......@@ -243,16 +248,6 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffer);
checkGlError("glBindFramebuffer mFrameBuffer: " + mFrameBuffer);
// 创建纹理
// int[] textures = new int[1];
// GLES20.glGenTextures(1, textures, 0);
// mFrameTextureID = textures[0];
// GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFrameTextureID);
// checkGlError("glBindTexture mFrameTextureID");
// GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
// checkGlError("glTexImage2D mFrameTextureID");
mFrameTextureID = createTexture(null);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFrameTextureID);
checkGlError("glBindTexture mFrameTextureID");
......@@ -330,22 +325,22 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
private static final int VERTICES_DATA_POS_OFFSET = 0;
private static final int VERTICES_DATA_UV_OFFSET = 3;
// private final float[] mVerticesData = {
// // X, Y, Z, U, V
// -1.0f, -1.0f, 0, 0.f, 0.f, // 左下
// 1.0f, -1.0f, 0, 1.f, 0.f, // 右下
// -1.0f, 1.0f, 0, 0.f, 1.f, // 左上
// 1.0f, 1.0f, 0, 1.f, 1.f, // 右上
// };
private final float[] mVerticesData = {
// X, Y, Z, U, V
-1.0f, -1.0f, 0 /* 左下 */, 0.f, 1.f, /* 左上 */
1.0f, -1.0f, 0 /* 右下 */, 1.f, 1.f, /* 右上 */
-1.0f, 1.0f, 0 /* 左上 */, 0.f, 0.f, /* 左下 */
1.0f, 1.0f, 0 /* 右上 */, 1.f, 0.f, /* 右下 */
-1.0f, -1.0f, 0, 0.f, 0.f, // 左下
1.0f, -1.0f, 0, 1.f, 0.f, // 右下
-1.0f, 1.0f, 0, 0.f, 1.f, // 左上
1.0f, 1.0f, 0, 1.f, 1.f, // 右上
};
// private final float[] mVerticesData = {
// // X, Y, Z, U, V
// -1.0f, -1.0f, 0 /* 左下 */, 0.f, 1.f, /* 左上 */
// 1.0f, -1.0f, 0 /* 右下 */, 1.f, 1.f, /* 右上 */
// -1.0f, 1.0f, 0 /* 左上 */, 0.f, 0.f, /* 左下 */
// 1.0f, 1.0f, 0 /* 右上 */, 1.f, 0.f, /* 右下 */
// };
private FloatBuffer mVertices;
private final String mVertexShader =
......@@ -420,10 +415,10 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
private static final int RGBA_SIZE = mWidth * mHeight * 4;
private static final int NV21_SIZE = RGBA_SIZE * 3 / 8;
private static final int NV21_OFFSET = RGBA_SIZE * 5 / 8;
private static final int Y_SIZE = mWidth * mHeight;
private byte[] mImageBytes = new byte[Y_SIZE];
private byte[] mImageBytes = new byte[NV21_SIZE];
private static final String TAG = "ImageRenderer";
......@@ -459,22 +454,21 @@ public class ImageRenderer implements GLSurfaceView.Renderer {
}
}
private int index = 0;
private void saveImage(byte[] buffer, String suffix) {
Log.v(TAG, "saveImage buffer length: " + buffer.length);
FileOutputStream fos = null;
try {
// String path = "/storage/9016-4EF8/dump_rgba";
String path = "/data/data/tv.yunxi.imgfmt/files/dump_rgba";
File filePath = new File(path);
if (!filePath.exists()) {
filePath.mkdirs();
}
File file = new File(path + "/image_" + index++ + "." + suffix);
if (!file.exists()) {
File file = new File(path + "/image." + suffix);
// 只保存一张
if (file.exists()) {
return;
} else {
file.createNewFile();
}
// 写入文件
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment