1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| public class MyRenderer implements GLSurfaceView.Renderer {
public static final int recWidth = 320;
public static final int recHeight = 240;
private static final int U_INDEX = recWidth * recHeight;
private static final int V_INDEX = recWidth * recHeight * 5 / 4;
private static final int LENGTH = recWidth * recHeight;
private static final int LENGTH_4 = recWidth * recHeight / 4;
private int previewFrameWidth = 320;
private int previewFrameHeight = 240;
private int[] yTextureNames;
private int[] uTextureNames;
private int[] vTextureNames;
private FloatBuffer mVertices;
private ShortBuffer mIndices;
private int mProgramObject;
private int mPositionLoc;
private int mTexCoordLoc;
private int yTexture;
private int uTexture;
private int vTexture;
private final float[] mVerticesData = {-1.f, 1.f, 0.0f, // Position 0
0.0f, 0.0f, // TexCoord 0
-1.f, -1.f, 0.0f, // Position 1
0.0f, 1.0f, // TexCoord 1
1.f, -1.f, 0.0f, // Position 2
1.0f, 1.0f, // TexCoord 2
1.f, 1.f, 0.0f, // Position 3
1.0f, 0.0f // TexCoord 3
};
private final short[] mIndicesData = {0, 1, 2, 0, 2, 3};
private ByteBuffer yBuffer;
private ByteBuffer uBuffer;
private ByteBuffer vBuffer;
private IntBuffer frameBuffer;
private IntBuffer renderBuffer;
private IntBuffer parameterBufferWidth;
private IntBuffer parameterBufferHeigth;
byte[] ydata = new byte[LENGTH];
byte[] uData = new byte[LENGTH_4];
byte[] vData = new byte[LENGTH_4];
public MyRenderer() {
mVertices = ByteBuffer.allocateDirect(mVerticesData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
mVertices.put(mVerticesData).position(0);
mIndices = ByteBuffer.allocateDirect(mIndicesData.length * 2)
.order(ByteOrder.nativeOrder()).asShortBuffer();
mIndices.put(mIndicesData).position(0);
yBuffer = ByteBuffer.allocateDirect(LENGTH);//MyGraphUtils.makeByteBuffer(LENGTH);
uBuffer = ByteBuffer.allocateDirect(LENGTH_4);//MyGraphUtils.makeByteBuffer(LENGTH_4/* * 2*/);
vBuffer = ByteBuffer.allocateDirect(LENGTH_4);//MyGraphUtils.makeByteBuffer(LENGTH_4);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.d("debug", "onSurfaceChanged");
GLES30.glActiveTexture(GLES30.GL_ACTIVE_TEXTURE);
// CameraGLRenderer.checkGlError("toto");
GLES30.glViewport(0, 0, width, height);
// CameraGLRenderer.checkGlError("toto");
}
@Override
public void onSurfaceCreated(GL10 gl10, javax.microedition.khronos.egl.EGLConfig eglConfig) {
// public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.d("debug", "on surface created");
// Define a simple shader program for our point.
//Our vertex shader code; nothing special
String vShaderStr =
"attribute vec4 a_position; \n" +
"attribute vec2 a_texCoord; \n" +
"varying vec2 v_texCoord; \n" +
"void main(){ \n" +
" gl_Position = a_position; \n" +
" v_texCoord = a_texCoord; \n" +
"} \n";
//Our fragment shader code; takes Y,U,V values for each pixel and calculates R,G,B colors,
//Effectively making YUV to RGB conversion
String fShaderStr =
"#ifdef GL_ES \n" +
"precision highp float; \n" +
"#endif \n" +
"varying vec2 v_texCoord; \n" +
"uniform sampler2D y_texture; \n" +
"uniform sampler2D u_texture; \n" +
"uniform sampler2D v_texture; \n" +
//
"void main (void){ \n" +
" float r, g, b, y, u, v; \n" +
//
// //We had put the Y values of each pixel to the R,G,B components by GL_LUMINANCE,
//// //that's why we're pulling it from the R component, we could also use G or B
" y = texture2D(y_texture, v_texCoord).r; \n" +
//
//// //We had put the U and V values of each pixel to the A and R,G,B components of the
//// //texture respectively using GL_LUMINANCE_ALPHA. Since U,V bytes are interspread
//// //in the texture, this is probably the fastest way to use them in the shader
" u = texture2D(u_texture, v_texCoord).a - 0.5; \n" +
" v = texture2D(v_texture, v_texCoord).r - 0.5; \n" +
//
//
// //The numbers are just YUV to RGB conversion constants
" r = y;\n" +
" g = u;\n" +
" b = y ;\n" +
//We finally set the RGB color of our pixel
" gl_FragColor = vec4(r, g, b, 0.0);\n" +
"} \n";
frameBuffer = IntBuffer.allocate(1);
renderBuffer= IntBuffer.allocate(1);
GLES30.glEnable(GLES30.GL_TEXTURE_2D);
//
GLES30.glGenFramebuffers(1, frameBuffer);
GLES30.glGenRenderbuffers(1, renderBuffer);
GLES30.glActiveTexture(GLES30.GL_ACTIVE_TEXTURE);
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, frameBuffer.get(0));
GLES30.glClear(0);
GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, renderBuffer.get(0));
GLES30.glRenderbufferStorage(GLES30.GL_RENDERBUFFER, GLES30.GL_DEPTH_COMPONENT16,
320, 240);
//
parameterBufferHeigth = IntBuffer.allocate(1);
parameterBufferWidth = IntBuffer.allocate(1);
GLES30.glGetRenderbufferParameteriv(GLES30.GL_RENDERBUFFER, GLES30.GL_RENDERBUFFER_WIDTH, parameterBufferWidth);
GLES30.glGetRenderbufferParameteriv(GLES30.GL_RENDERBUFFER, GLES30.GL_RENDERBUFFER_HEIGHT, parameterBufferHeigth);
GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_RENDERBUFFER, renderBuffer.get(0));
if (GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER)!=GLES30.GL_FRAMEBUFFER_COMPLETE){
Log.d("debug", "gl frame buffer status != frame buffer complete");
}
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
GLES30.glClear(0);
mProgramObject = loadProgram(vShaderStr, fShaderStr);
//
// // Get the attribute locations
mPositionLoc = GLES30.glGetAttribLocation(mProgramObject, "a_position");
mTexCoordLoc = GLES30.glGetAttribLocation(mProgramObject, "a_texCoord");
//
GLES30.glEnable(GLES30.GL_TEXTURE_2D);
yTexture = GLES30.glGetUniformLocation(mProgramObject, "y_texture");
yTextureNames = new int[1];
GLES30.glGenTextures(1, yTextureNames, 0);
int yTextureName = yTextureNames[0];
GLES30.glEnable(GLES30.GL_TEXTURE_2D);
uTexture = GLES30.glGetUniformLocation(mProgramObject, "u_texture");
uTextureNames = new int[1];
GLES30.glGenTextures(1, uTextureNames, 0);
int uTextureName = uTextureNames[0];
GLES30.glEnable(GLES30.GL_TEXTURE_2D);
vTexture = GLES30.glGetUniformLocation(mProgramObject, "v_texture");
vTextureNames = new int[1];
GLES30.glGenTextures(1, vTextureNames, 0);
int vTextureName = vTextureNames[0];
GLES30.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
//
}
float val = 0.01f;
@Override
public final void onDrawFrame(GL10 gl) {
Log.d("debug", "on Draw frame");
// // Clear the color buffer
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
val+= 0.05;
if(val>1.0f)
val = 0.f;
GLES30.glClearColor(val, 0.0f, 0.0f, 0.0f);
// if(val < 0.5f)
// return;
yBuffer.clear();
Log.d("draw", "Draw " + (val *255.0));
for(int i=0;i<LENGTH;i++) {
yBuffer.put((byte)(val *255.0));
}
yBuffer.position(0);
for(int i=0;i<LENGTH_4;i++) {
uBuffer.put((byte)(val *255.0));
}
uBuffer.position(0);
// Use the program object
GLES30.glUseProgram(mProgramObject);
// Load the vertex position
mVertices.position(0);
GLES30.glVertexAttribPointer(mPositionLoc, 3, GLES30.GL_FLOAT, false, 5*4, mVertices);
mVertices.position(3);
GLES30.glVertexAttribPointer(mTexCoordLoc, 2, GLES30.GL_FLOAT, false, 5*4, mVertices);
GLES30.glEnableVertexAttribArray(mPositionLoc);
GLES30.glEnableVertexAttribArray(mTexCoordLoc);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, yTextureNames[0]);
GLES30.glTexImage2D( GLES30.GL_TEXTURE_2D, 0, GLES30.GL_LUMINANCE,
320, 240, 0, GLES30.GL_LUMINANCE, GLES30.GL_UNSIGNED_BYTE, yBuffer);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, yTextureNames[0]);
GLES30.glUniform1i(yTexture, 0);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, uTextureNames[0]);
GLES30.glTexImage2D( GLES30.GL_TEXTURE_2D, 0, GLES30.GL_LUMINANCE,
160, 120, 0, GLES30.GL_LUMINANCE, GLES30.GL_UNSIGNED_BYTE, uBuffer);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glActiveTexture(GLES30.GL_TEXTURE1+2);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, uTextureNames[0]);
GLES30.glUniform1i(uTexture, 2);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, vTextureNames[0]);
GLES30.glTexImage2D( GLES30.GL_TEXTURE_2D, 0, GLES30.GL_LUMINANCE,
160, 120, 0, GLES30.GL_LUMINANCE, GLES30.GL_UNSIGNED_BYTE, vBuffer);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glActiveTexture(GLES30.GL_TEXTURE1+1);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, vTextureNames[0]);
GLES30.glUniform1i(vTexture, 1);
GLES30.glDrawElements(GLES30.GL_TRIANGLES, 6, GLES30.GL_UNSIGNED_SHORT, mIndices);
// CameraGLRenderer.checkGlError("toto");
}
} |
Partager