java - Android 上的简单 OpenGL ES 2.0 应用程序显示黑屏 : why?

标签 java android opengl-es

所以我正在阅读 Kevin Brothaler 的 Open GL ES 2 for Android 这本书,并且正在尝试第一章的项目,它基本上是将屏幕着色为红色。我通过 Samsung 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);
}

}

如您所见,我只调用了 3 次 GLES20 包中的静态方法。当我在我的手机和我的模拟器上运行这个应用程序时,我得到一个空白屏幕:发生了什么事?在过去的 2 个小时里,我一直在为此苦思冥想,这已经是最简单的事情了。它应该显示红色屏幕。非常感谢!

最佳答案

您没有在该方法末尾的 OnCreate() 方法中设置 View 添加:

setContentView(glSurfaceView);

关于java - Android 上的简单 OpenGL ES 2.0 应用程序显示黑屏 : why?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22827025/

相关文章:

java - 如何阻止 IntelliJ IDEA 在启用 "External Libraries"的情况下扩展 "Autoscroll from Source"?

java - 我应该更喜欢代码复制以获得更好的性能吗?

java - 安卓工作室 : how to use the fbc live template

android - 如何更改 SwitchCompat 的轨道颜色

c - 绘制到 FBO - Sprite 上下颠倒问题

java - 非法字符 < :> at index 40: com. 识别.app-mergeDebugResources-33 :/values/values. xml)

java.util.NoSuchElementException : Read words from a file

java - Android Studio 不断在运行 OneDrive 的计算机上的错误位置生成 .android 文件夹

android - 当任何中断发生时纹理都会丢失

ios - SpriteKit 是否支持 sprite/texture/shape 的密集镶嵌,以便可以自由变形?