Android上简单的OpenGL ES 2.0应用程序显示空白屏幕:为什么?

时间:2014-04-03 02:57:52

标签: java android opengl-es

所以我跟随Kevin Brothaler的“Open GL ES 2 for Android”这本书,我正在尝试第一章的项目,它基本上将屏幕着色为红色。我是由三星Galaxy Note 3设置的,所以我可以在那里调试我的应用程序,还可以设置一个使用主机GPU进行渲染的模拟器。另外,我强制在手机上进行GPU渲染。我将他完全复制的代码复制到了我的eclipse项目中。以下是参考代码:

package com.firstopenglproject.android;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
//import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class FirstOpenGLProjectActivity extends Activity {

private static final String TAG = FirstOpenGLProjectActivity.class.getSimpleName();
private GLSurfaceView glSurfaceView;
private boolean rendererSet = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "Before calling super.onCreate()");
    super.onCreate(savedInstanceState);
    Log.d(TAG, "Before creating a new GLSurfaceView");
    glSurfaceView = new GLSurfaceView(this);

    Log.d(TAG, "Creating activity manager");
    final ActivityManager activityManager = 
            (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    Log.d(TAG, "Creating configuration info");
    final ConfigurationInfo configurationInfo =
            activityManager.getDeviceConfigurationInfo();

    Log.d(TAG, "Getting supportsEs2 boolean");
    final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
//              || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1
//              && (Build.FINGERPRINT.startsWith("generic")
//              || Build.FINGERPRINT.startsWith("unknown")
//              || Build.MODEL.contains("google_sdk")
//              || Build.MODEL.contains("Emulator")
//              || Build.MODEL.contains("Android SDK built for x86")));
    Log.d(TAG, "supportsEs2 = " + supportsEs2);
    if (supportsEs2) {
        // Request an OpenGLES2 compatible context
        glSurfaceView.setEGLContextClientVersion(2);

        glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);

        // Assign our renderer
        glSurfaceView.setRenderer(new FirstOpenGLProjectRenderer());
        rendererSet = true;
        Toast.makeText(this, "Device supports OpenGL ES 2.0", Toast.LENGTH_LONG).show();
        Log.d(TAG, "rendererSet is true");
    } else {
        Log.d(TAG, "OGLES2.0 not supported");
        Toast.makeText(this, "This device does not support OpenGL ES 2.0", Toast.LENGTH_LONG).show();
        return;
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (rendererSet) {
        glSurfaceView.onPause();
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (rendererSet) {
        glSurfaceView.onResume();
    }
}

}

日志记录任务是我添加的,以便我可以看到代码是否正确执行,它是:我在LogCat中看到所有这些日志消息。这是渲染器的代码:

package com.firstopenglproject.android;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;

public class FirstOpenGLProjectRenderer implements Renderer {

private static final String TAG = FirstOpenGLProjectRenderer.class.getSimpleName();
/*
 * GLSurfaceView calls this when its time to draw a frame. We must
 * draw something, even if its only to clear the screen. The rendering buffer
 * will be swapped and displayed on the screen after this method returns, 
 * so if we don't draw anything, we'll probably get a bad flickering effect.
 * */
@Override
public void onDrawFrame(GL10 arg0) {
    // clear the rendering surface
    glClear(GL_COLOR_BUFFER_BIT);
}

/*
 * GLSurfaceView calls this after the surface is created and whenever the size has 
 * changed. A size change can occur when switcheing from portrait to landscape
 * and vice versa.
 */
@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    // set the openGL viewport to fill the entire surface
    glViewport(0, 0, width, height);
}

/*
 * GLSurfaceView calls this when the surface is created. This happens the first
 * time our application is run, and it may also be called when the device wakes 
 * up or when the user switches back to our activity. In practice, this means that
 * this method may be called multiple times while our application is still running.
 */
@Override
public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
}

}

如您所见,我只在GLES20包中使用3次静态方法调用。当我在手机和模拟器上运行此应用程序时,我得到一个空白屏幕:发生了什么?在过去的2个小时里,我一直在桌子上敲打着这个,这很简单。它应该显示一个红色的屏幕。非常感谢!

1 个答案:

答案 0 :(得分:1)

您没有在该方法结尾处的OnCreate()方法中设置视图添加:

setContentView(glSurfaceView);
相关问题