Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSurfaceView.Renderer possible to swap buffers multiple times in onDrawFrame()?

In my openGL game, I draw my scene normally using a GLSurfaceView.Renderer class in the onDrawFrame(). However, when I am displaying a loading screen I would like to force the screen to draw after each item of data is loaded so the loading bar can be displayed.

Is it possible to force a bufferswap during this draw call somehow? My only alternative is to stagger my loading across multiple frames which means a lot of rework..

I guess what I am trying to call is eglSwapBuffers() but I cannot find a way to access the egl context from the GLSurfaceView or GLSurfaceView.Renderer.

Thank you for your time.

like image 475
DJPJ Avatar asked Jun 23 '11 15:06

DJPJ


3 Answers

You can add also this method to your GLSurfaceView.Renderer class:

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;

public void swapBuffers()
{
    EGL10 curEgl = (EGL10)EGLContext.getEGL();

    EGLDisplay curDisplay = curEgl.eglGetCurrentDisplay();
    if (curDisplay == EGL10.EGL_NO_DISPLAY) { Log.e("myApp","No default display"); return; }    

    EGLSurface curSurface = curEgl.eglGetCurrentSurface(EGL10.EGL_DRAW);
    if (curSurface == EGL10.EGL_NO_SURFACE) { Log.e("myApp","No current surface"); return; }

    curEgl.eglSwapBuffers(curDisplay, curSurface);
}

Much the same as OpenUserX03's answer, just in Java.

like image 128
Aidan Avatar answered Nov 09 '22 05:11

Aidan


No you can't (or shouldn't) force swapping buffers in the onDraw method of your Renderer.

What you should do is to make the loading of your data in a separate Thread. Your onDraw method will still be called regularly, which will let you ask to the loading thread how many items were loadede to display a progress bar / message accordingly.

like image 34
XGouchet Avatar answered Nov 09 '22 04:11

XGouchet


It's been awhile since the answer has been accepted but you can (and there is no reason you shouldn't) force swapping the buffers in the onDrawFrame() method of your Renderer.

I had the exact same problem in my OpenGL app - I needed to render a loading screen while data was being loaded. Here is my pseudo-code example of calling eglSwapBuffers() during a load (I use JNI):

public void onDrawFrame(GL10 gl)
{
    MyJNILib.render();
}

MyJNILib native pseudo-code:

#include <EGL\egl.h>

...

void render()
{
    ...

    while (loading)
    {
        // Do loading stuff
        ...
        eglSwapBuffers( eglGetCurrentDisplay(), eglGetCurrentSurface( EGL_DRAW ) );
    }

    ...
}
like image 22
OpenUserX03 Avatar answered Nov 09 '22 03:11

OpenUserX03