1

请记住,我是 android 开发新手,之前没有使用 OpenGL 的经验。我学习了这一课:http: //developer.android.com/training/graphics/opengl/environment.html

我当前的代码是:

OpenGLESTestActivity.java:

package com.KML;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.opengl.*;

import com.KML.MyGLRenderer;

public class OpenGLESTestActivity extends Activity {
    /** Called when the activity is first created. */

    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGLView = new MyGLSurfaceView(this);
        setContentView(mGLView);
    }
}

class MyGLSurfaceView extends GLSurfaceView {


    public MyGLSurfaceView(Context context) {
        super(context);
        setRenderer(new MyGLRenderer());
        this.setEGLContextClientVersion(2);
        this.setRenderMode(RENDERMODE_WHEN_DIRTY);
    }
}

在 MyGLRenderer.java 中:

package com.KML;

import android.opengl.GLES20;

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

import android.opengl.GLSurfaceView;


public class MyGLRenderer implements GLSurfaceView.Renderer {

    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    }

    public void onDrawFrame(GL10 unused) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    }

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

}

这让我“不幸的是,OpenGLESTest 已停止。” 在我的安卓设备上。

谢谢

4

1 回答 1

3

Android 上的 OpenGLES - IllegalStateException:已为此实例调用 setRenderer

public MyGLSurfaceView(Context context) 
{
    super(context);
    setEGLContextClientVersion(2);
    setRenderer (new MyRenderer());
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

这是您设置所有错误的顺序。

于 2013-02-09T20:48:51.473 回答