0

I am creating an app that makes use of GL10, it's made up f three classes,

Class A extends Activity. Class B implements Renderer. Class C extends Activity.

Class C contains the data for a 3D cube, Class B is the renderer and Class A displays it.

To display it i am using the following method in class 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);

}

And the XML is:

<?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>

the problem is that in the Java code for class A the line 'glSurfaceView.setRenderer(ourSurface);' returns a null pointer exception.

Class B (Renderer) code:

  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);

}

And the class C code:

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);
}

Pardon the length of the question but does anyone have any solutions?

September
  • 45
  • 8

1 Answers1

1

I don't understand, you define ourCube in your XML as GLCubeRenderer, which is the same class as ourSurface.

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

and

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

Then again when getting hold of the reference of ourCube you cast it to GLSurfaceView.

Is GLCubeRenderer extending GLSurfaceView? In that case, are you trying to set the Renderer of this surfaceview to an instance of the same class?

I think you might be mixing up classes here.

M Rajoy
  • 4,028
  • 14
  • 54
  • 111
  • maybe, I was trying to figure this out by using this answer: http://stackoverflow.com/questions/4120689/android-glsurfaceview-with-overlay-using-xml-java – September Mar 01 '13 at 13:26
  • create the xml element as GLSurfaceView, make your glSurfaceView extend Renderer, then use the same instance. GLSurfaceView glSurfaceView = (GLSurfaceView) findViewById(R.id.ourCube); glSurfaceView.setRenderer(glSurfaceView); – M Rajoy Mar 01 '13 at 13:29