Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android OpenGLES Render-to-Texture

I write graphics apps for the iPhone and I'm looking to port my most recent app, 'Layers,' to the Android platform. Layers is painting app that allows users to draw on the screen and create multi-layered paintings with different brushes, colors, etc... and export to PSD. It's got desktop sync, a smudge tool, lots of good stuff... http://www.layersforiphone.com/

I started looking at the Android platform Monday and I've run into a major problem. I use OpenGL to do all the drawing because it offers the best performance. However, there are several places where I need to render into a texture and then use the texture. For example:

  1. Use brush texture and a line of sprites to create a black paint stroke in texture A
  2. Put brush color+alpha in glColor4f and then draw texture A onto the screen.

On the iPhone, I do this continually as the user's finger moves and I am able to achieve 12-15fps on a 1st gen iPod Touch. It's necessary because applying color and alpha to the individual sprites making up the brush stroke doesn't produce the right result (since sprites overlap and make the stroke too dark).

The android platform supports OpenGL ES 1.0 but seems to omit key functions for dealing with framebuffers. I can't find a way to bind a texture to the framebuffer and draw into it using OpenGL.

Here's how I'd normally go about it on the iPhone:

// generate a secondary framebuffer and bind it to OpenGL so we don't mess with existing output.
glGenFramebuffers(1, &compositingFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, compositingFramebuffer);

// attach a texture to the framebuffer.
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

// subsequent drawing operations are rendered into the texture

// restore the primary framebuffer and (possibly) draw the rendered texture
glBindFramebuffer(GL_FRAMEBUFFER, originalActiveFramebuffer);

Is there anything like this on the Android platform? On the iPhone, OpenGL is significantly faster than software-based drawing and is the only viable solution (trust me - I wrote a CoreGraphics painting app too...). Maybe there's another route I could take on Android? I'm willing to jump through whatever hoops are necessary, but I won't publish the app unless performance is good (10fps+ as you're drawing on the screen).

like image 905
Ben Gotow Avatar asked Nov 11 '09 17:11

Ben Gotow


People also ask

What is render to texture why we use?

You can use the Render to Texture component to render the scene from a specific camera to a texture. You can use this feature to create rear-view mirrors, security camera screens, and draw 3D models in the viewport. To enable this component, you must enable the Render to Texture gem.

What is OpenGL texture?

A texture is an OpenGL Object that contains one or more images that all have the same image format. A texture can be used in two ways: it can be the source of a texture access from a Shader, or it can be used as a render target.


1 Answers

I'm skeptical that all Android devices would support the exact same set of OpenGL ES extensions, or that they'd all support the exact same version of OpenGL ES.

Framebuffer objects (FBOs) are a part of the OpenGL ES 2.0 core specification; if your android device supports ES 2.0, then it supports FBOs.

If your android device supports ES 1.1, then check the extension string for GL_OES_framebuffer_object using glGetString(GL_EXTENSIONS).

like image 197
prideout Avatar answered Sep 21 '22 00:09

prideout