csnewb
csnewb

Reputation: 1270

How to create offscreen Texture and use it on Android with OpenGL ES 2

I am very new in OpenGL.

How can I create an offscreen texture, so that I can get some Informations of it (e.g. PixelColor by clicking on view texture) ?

I tried many many things for example FBO, but it did't work. I have no imagination how to load the Bitmap to a offscreen texture and get information of it. I am very frustated :/

Task

Edit: Took this Tutorial as Example, don't know if it fits to my problem. createFramebuffer

public void createFramebuffer(GL10 gl) {

    FloatBuffer uvBuffer2;
    int texture[] = new int[1];
    GLES20.glGenTextures(1, texture, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE0, texture[0]);

    float uvs2[] = {
               0.0f, 0.0f,
               0.0f, 1.0f,
               1.0f, 1.0f,
               1.0f, 0.0f
       };

    ByteBuffer bb = ByteBuffer.allocateDirect(uvs2.length * 4);
    bb.order(ByteOrder.nativeOrder());
    uvBuffer2 = bb.asFloatBuffer();
    uvBuffer2.put(uvs2);
    uvBuffer2.position(0);

    gl.glTexCoordPointer(2, GLES20.GL_BYTE, 0, uvBuffer2);

    //load image
    int idTest = mContext.getResources().getIdentifier("drawable/testgroesse", null, mContext.getPackageName());
    Bitmap wood = BitmapFactory.decodeResource(mContext.getResources(), idTest);

    GLES20.glTexParameterf(GLES20.GL_TEXTURE0, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE0, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    GLUtils.texImage2D(GLES20.GL_TEXTURE0, 0, wood, 0); 

    GLES20.glEnable(GLES20.GL_TEXTURE0);
    gl.glEnableClientState(GLES20.GL_TEXTURE0);

    //color picking background start
    ByteBuffer bytePickedColor = ByteBuffer.allocate(4);
    bytePickedColor.order(ByteOrder.nativeOrder());
    GLES20.glFlush();

    GLES20.glReadPixels(touchXint, touchYint, 1, 1, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, bytePickedColor);

    byte b[] = new byte[4];
    bytePickedColor.get(b);
    String key = "" + b[0] + " " + b[1] + " " + b[2] + " " + b[3];
    Log.d("Color_Farbe: ", key);
    bytePickedColor.rewind();
    //color picking background end
}

Upvotes: 0

Views: 1649

Answers (1)

Matic Oblak
Matic Oblak

Reputation: 16774

To oversimplify:

The FBO you created is a BUFFER that contains RGBA + 16-bit depth. By attaching a texture to it you can then reuse the texture as it will have the same RGBA data as the FBO. At the beginning the buffer itself is still empty, you still need to clear it and draw to it as with any other render buffer.

So to put the image/texture on to that buffer you need to create the texture (with that image) and draw a textured rectangle on this FBO.

It would seem this is not what you are after:

  • "How can I create a offscreen texture?" What is an onscreen texture? All textures are offscreen.
  • "get some Informations of it"? Of what, the texture?! There are basically 2 ways of creating the texture: first is with an image in which case rather lookup the texel from the source image. Second is by drawing which is the FBO procedure where you need to read pixels (try glReadPixels).
  • "e.g. PixelColor by clicking on view texture" Clicking on what?! You can click on view/screen but not the texture. If you again need the sample of the rendered view that you see before you then all you need to do is read the pixels as described before.

It looks like all you are looking for is glReadPixels to get the colour values from the current buffer. If this is not the case please rephrase your question and explain what is your goal here.

Upvotes: 2

Related Questions