为什么我的OpenGL代码失败了?

时间:2012-08-24 19:41:32

标签: java android opengl-es

请记住,我是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已经停止了。”在我的Android设备上。

由于

1 个答案:

答案 0 :(得分:3)

OpenGLES on Android - IllegalStateException: setRenderer has already been called for this instance

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

这是你设置错误的顺序。