Android openGL渲染器返回空指针异常

时间:2013-03-01 13:12:37

标签: java android opengl-es nullpointerexception

我正在创建一个使用GL10的应用程序,它由三个类组成,

A类扩展了Activity。 B类实现Renderer。 C类扩展了活动。

C类包含3D立方体的数据,B类是渲染器,A类显示它。

要显示它,我在A类中使用以下方法,

GLCubeRenderer ourSurface = new GLCubeRenderer();

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



    GLSurfaceView glSurfaceView =
        (GLSurfaceView) findViewById(R.id.ourCube);
    glSurfaceView.setRenderer(ourSurface);
    setContentView(R.layout.cubelayout);

}

XML是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.mastermind.GLCubeRenderer
    android:id="@+id/ourCube"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />


</RelativeLayout>

问题是在A类的Java代码中行'glSurfaceView.setRenderer(ourSurface);'返回空指针异常。

B类(渲染器)代码:

  private GLCube cube = new GLCube();
static Context context;
public static float xAngle;

public static float yAngle;

final float[] ambient = { 0.1f, 1, 1, 1 };
final float[] position = { 45, 20, 0, 1 };
final float[] direction = { 0, -1, 0 };

public GLCubeRenderer() {
    cube = new GLCube();



}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub
    gl.glDisable(GL10.GL_DITHER);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    gl.glEnable(GL10.GL_LIGHT1);
    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, ambient, 0);
    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, position, 0);
    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPOT_DIRECTION, direction, 0);
    gl.glLightf(GL10.GL_LIGHT1, GL10.GL_SPOT_CUTOFF, 30.0f);
    gl.glClearColor(.0f, 0, .0f, 0);
    gl.glClearDepthf(1f);


}

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    gl.glDisable(GL10.GL_DITHER);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);

    gl.glRotatef(xAngle, 0, xAngle, 0);
    // gl.glRotatef(yAngle, yAngle, 0, yAngle);

    gl.glActiveTexture(GL10.GL_TEXTURE0);
    gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,     GL10.GL_MODULATE);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    cube.draw(gl);
}



@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub
    gl.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1f, 1, 25);

}

C类代码:

private float vertices[] = {
        1, 1, -1, // p0-top front right
        1, -1, -1, // p1-bottom front right
        -1, -1, -1, // p2-bottom front left
        -1, 1, -1, // p3-front top left
        1, 1, 1, // p4-top back right
        1, -1, 1, // p5-bottom back right
        -1, -1, 1, // p6-bottom back left
        -1, 1, 1 // p7-front back left

};

// Buffer for our vertices
private FloatBuffer vertBuff;

// Index for our points e.g. p0 = 0f, 1f, in vert Index
private short[] pIndex = { 
        3, 4, 0,
        0, 4, 1,
        3, 0, 1,
        3, 7, 4,
        7, 6, 4,
        7, 3, 6,
        3, 1, 2,
        1, 6, 2,
        6, 3, 2,
        1, 4, 5,
        5, 6, 1,
        6, 5, 4 
        };

// Buffer for points index
private ShortBuffer pBuff;

// Triangle constructor
public GLCube() {

    // Construction of vertices
    // byte buffer for vertices
    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);

    // Construction of points
    // point byte buffer
    ByteBuffer pointByteBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
    pointByteBuff.order(ByteOrder.nativeOrder());
    pBuff = pointByteBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);
}



public void draw(GL10 gl) {

    gl.glFrontFace(GL10.GL_CW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_BACK);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length,   GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
}

请原谅问题的长度,但有人有任何解决方案吗?

1 个答案:

答案 0 :(得分:1)

我不明白,您在XML中将myCube定义为GLCubeRenderer,它与ourSurface属于同一类。

GLSurfaceView glSurfaceView =
    (GLSurfaceView) findViewById(R.id.ourCube); 

<com.mastermind.GLCubeRenderer
     android:id="@+id/ourCube"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     />

然后再次获取myCube的引用时,将其转换为GLSurfaceView。

GLCubeRenderer是否扩展了GLSurfaceView?在这种情况下,您是否尝试将此surfaceview的渲染器设置为同一类的实例?

我想你可能会在这里混淆课程。