简体   繁体   中英

Setting up Android OpenGl ES Environment fails

I want to learn how to use OpenGL for Android. Therefore I followed this guide to set up the environment. I have done everything like they say: I got a OpenGLActivity which initiates my MyGLSurfaceView :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    glSurfaceView = new MyGLSurfaceView(this);
    setContentView(R.layout.activity_open_gl);
}

This is my MyGLSurfaceView :

class MyGLSurfaceView extends GLSurfaceView {

private final MyGLRenderer mRenderer;

public MyGLSurfaceView(Context context) {
    super(context);

    // Create an OpenGL ES 2.0 context
    setEGLContextClientVersion(2);

    mRenderer = new MyGLRenderer();

    // Set the Renderer for drawing on the GLSurfaceView
    setRenderer(mRenderer);
}
}

And Thats my MyGLRenderder class which should set the background to black:

public class MyGLRenderer implements Renderer {

public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    // Set the background frame color
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

public void onDrawFrame(GL10 unused) {
    // Redraw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}

public void onSurfaceChanged(GL10 unused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);
}
}

And I've added this line to my AndroidManifest.xml (right at the beginning, before <application> tag)

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

The problem is that the background isn't black, its white. Am I missing something?

You're not actually displaying your OpenGL view. In this code here:

glSurfaceView = new MyGLSurfaceView(this);
setContentView(R.layout.activity_open_gl);

you're setting the content view to be a view from your layout, while you're not using the GLSurfaceView you just created. It should be:

glSurfaceView = new MyGLSurfaceView(this);
setContentView(glSurfaceView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM