Search code examples
androidopengl-esglsurfaceview

opengl - how to keep updating a scene forever


For every incoming tweet, I was asked to put a point (corresponding to tweet location) on world map (opengl scene). Some thing like this. I tried to learn OpenGL. I cried. With time, OpenGL started feeling like my friend. He was on the way to become my best friend. Now, I used GLSurfaceView on Android and assumed like I can simply draw points one by one, one after another with out ever calling glCear(). This way I only have to keep current tweet coordinate in memory. Told to myself, you are a genius. I suffered like others, whose have explained there pains here, here and here. Now comes a master, who taught me the basics and told me, kiddo, you have to call glClear no matter what.

I cannot keep adding/storing coordinates of all points in memory and call glDrawArrarys since this has to keep running for ever on mobile device. So the choices now are

  1. Draw n points. After that, try some thing like 'render to texture' and use that texture as background for the next drawing next n points.
  2. Use EGL_BUFFER_PRESERVED as in EGL Preserve sample at Mali sdk . Needs NDK as Android EGL14 cannot be used before Android API 17. Khronos EGL11 is not yet implemented in Android. People closest to my new friend tells, he doesn't like this at all.

Do I have any other choices or am I solving the wrong problem ? I would like to know what is OpenGL's approach to updating a scene for ever.


Solution

  • I believe EGL_BUFFER_PRESERVED is not supported on all devices. So even if you could use API level 17, that might not be your solution.

    You could do the primary rendering to a texture, using a FBO. You would then clear that texture once while you initialize the whole thing, after you finished setting up the FBO. Then, every time you get a new point, you draw it to the texture, and then copy the texture to the primary framebuffer.

    Roughly, you have the following steps on startup:

    1. Create texture, and set it up with the desired format/size, parameters, etc.
    2. Create FBO, and set the texture as color attachment.
    3. Prepare rendering to FBO, calling glViewport(), etc.
    4. Call glClear().

    Then each time a point is added:

    1. Pass the new point into your Renderer implementation, and trigger a redraw.
    2. In onDrawFrame(), bind FBO for rendering.
    3. Render the point.
    4. Bind default framebuffer for rendering.
    5. Bind shader program for simple texturing.
    6. Bind texture for sampling.
    7. Draw screen filling quad.