Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FBO to record screen in Android

I'm developing the Android plugin that record Unity gameplay screen.To achieve this, I used OpenGL FBO. Pseudo Code is so simple like this below :

// Bind frame buffer as a render target
mFrameBuffer.bind();

// Render scene to frame buffer
renderScene();

// Restore rendering target, unbind FBO
mFrameBuffer.unbind();

// Draw texture into display
mTexture.draw(mFrameBuffer.getTexture());

// Make video surface a rendering target
videoCapture.captureFrame(mFrameBuffer.getTexture());

The output video is ok but i had a problem when render to window display using offscreen texture that get from FBO. I checked my code and recognize that the calls to glDrawArray fails with GL_INVALID_OPERATION error code
.

My render code

   public void draw(int textureId) {
    this.shader.use();

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    Log.getPossibleGLError("active texture0");
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
    Log.getPossibleGLError("bind texture when redraw");

    int uOrientationM = this.shader.getAttributeLocation("uOrientationM");
    Log.getPossibleGLError("get orientation matrix");
    int uTransformM = this.shader.getAttributeLocation("uTransformM");
    Log.getPossibleGLError("get transform matrix");

    GLES20.glUniformMatrix4fv(uOrientationM, 1, false, this.orientationMatrix, 0);

    Log.getPossibleGLError("set orientation matrix when redraw");

    GLES20.glUniformMatrix4fv(uTransformM, 1, false, this.transformMatrix, 0);

    Log.getPossibleGLError("set uniform matrix when redraw");

    renderQuad(this.shader.getAttributeLocation("aPosition"));

    Log.getPossibleGLError("render quad when redraw");

    this.shader.unUse();
   }

   private void renderQuad(int aPosition) {
      GLES20.glVertexAttribPointer(aPosition, 2, GLES20.GL_BYTE, false, 0, this.fullQuadVertices);
      Log.getPossibleGLError("get vertex attrib pointer");
      GLES20.glEnableVertexAttribArray(aPosition);
      Log.getPossibleGLError("enable vertex array");
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
      // error occurs after execute glDrawArrays
      Log.getPossibleGLError("draw array ");
   } 

This code worked fine when render to videoSurface but failed when render to window display.
Any solutions for my issue?

Render to display log : GL_INVALID_OPERATION error occurs

glUseProgram(program = 31)
glActiveTexture(texture = GL_TEXTURE0)
glGetError(void) = (GLenum) GL_NO_ERROR
glBindTexture(target = GL_TEXTURE_2D, texture = 13)
glGetError(void) = (GLenum) GL_NO_ERROR
glGetError(void) = (GLenum) GL_NO_ERROR
glGetError(void) = (GLenum) GL_NO_ERROR
glUniformMatrix4fv(location = 0, count = 1, transpose = false, value = [1.0, 0.0, 0.0, 0.0, -0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
glGetError(void) = (GLenum) GL_NO_ERROR
glUniformMatrix4fv(location = 1, count = 1, transpose = false, value = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
glGetError(void) = (GLenum) GL_NO_ERROR
glVertexAttribPointer(indx = 0, size = 2, type = GL_BYTE, normalized = false, stride = 0, ptr = 0x42b1ae38)
glGetError(void) = (GLenum) GL_NO_ERROR
glEnableVertexAttribArray(index = 0)
glGetError(void) = (GLenum) GL_NO_ERROR
glValidateProgram(program = 31)
glGetError(void) = (GLenum) GL_NO_ERROR
glGetProgramiv(program = 31, pname = GL_VALIDATE_STATUS, params = [1])
glGetProgramiv(program = 31, pname = GL_LINK_STATUS, params = [1])
glDrawArrays(mode = GL_TRIANGLE_STRIP, first = 0, count = 4)
glGetError(void) = (GLenum) GL_INVALID_OPERATION
glGetError(void) = (GLenum) GL_NO_ERROR

Render to video log : run fine with no error

glUseProgram(program = 31)
glActiveTexture(texture = GL_TEXTURE0)
glGetError(void) = (GLenum) GL_NO_ERROR
glBindTexture(target = GL_TEXTURE_2D, texture = 13)
glGetError(void) = (GLenum) GL_NO_ERROR
glGetAttribLocation(program = 31, name = uOrientationM) = (GLint) -1
glGetError(void) = (GLenum) GL_NO_ERROR
glGetUniformLocation(program = 31, name = uOrientationM) = (GLint) 0
glGetError(void) = (GLenum) GL_NO_ERROR
glGetError(void) = (GLenum) GL_NO_ERROR
glGetAttribLocation(program = 31, name = uTransformM) = (GLint) -1
glGetError(void) = (GLenum) GL_NO_ERROR
glGetUniformLocation(program = 31, name = uTransformM) = (GLint) 1
glGetError(void) = (GLenum) GL_NO_ERROR
glGetError(void) = (GLenum) GL_NO_ERROR
glUniformMatrix4fv(location = 0, count = 1, transpose = false, value = [1.0, 0.0, 0.0, 0.0, -0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
glGetError(void) = (GLenum) GL_NO_ERROR
glUniformMatrix4fv(location = 1, count = 1, transpose = false, value = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
glGetError(void) = (GLenum) GL_NO_ERROR
glGetAttribLocation(program = 31, name = aPosition) = (GLint) 0
glGetError(void) = (GLenum) GL_NO_ERROR
glVertexAttribPointer(indx = 0, size = 2, type = GL_BYTE, normalized = false, stride = 0, ptr = 0x42b1ae38)
glGetError(void) = (GLenum) GL_NO_ERROR
glEnableVertexAttribArray(index = 0)
glGetError(void) = (GLenum) GL_NO_ERROR
glValidateProgram(program = 31)
glGetError(void) = (GLenum) GL_NO_ERROR
glGetProgramiv(program = 31, pname = GL_VALIDATE_STATUS, params = [1])
glGetProgramiv(program = 31, pname = GL_LINK_STATUS, params = [1])
glVertexAttribPointerData(indx = 0, size = 2, type = GL_BYTE, normalized = false, stride = 2, ptr = 0x??, minIndex = 0, maxIndex = 4)
glDrawArrays(mode = GL_TRIANGLE_STRIP, first = 0, count = 4)
glGetError(void) = (GLenum) GL_NO_ERROR
glGetError(void) = (GLenum) GL_NO_ERROR
glUseProgram(program = 0)
like image 233
knighthedspi Avatar asked Aug 01 '14 06:08

knighthedspi


1 Answers

Finally after many investigations about OpenGl, I fixed this issue. Problem is when I attempted to redraw texture to screen, the array buffer and element array buffer were wrongly pointed.Solution is so simple, I created my own array buffer and element array buffer then bind them before attempt to redraw to screen. It worked fine with no errors :D

like image 89
knighthedspi Avatar answered Nov 16 '22 07:11

knighthedspi