I am trying to create a context in opengl-es for android and I'm a bit confused about it. I have initialized my context in a completely separate class from my renderer and my launch activity like so.
import javax.microedition.khronos.egl.EGL11;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL11;
public class Context {
public static GL11 gl;
EGLConfig[] configs = new EGLConfig[1];
EGLConfig config = configs[0];
EGLDisplay dpy;
int attribList;
EGLSurface surf;
static EGLContext glContext;
public Context() {
int attribList [] =
{
EGL11.EGL_DEPTH_SIZE, 15, // z-buffer
EGL11.EGL_NONE
};
dpy = ((EGL11) gl).eglGetDisplay(EGL11.EGL_DEFAULT_DISPLAY);
EGLContext glContext = ((EGL11) gl).eglCreateContext(dpy, config, EGL11.EGL_NO_CONTEXT, attribList);
gl = (GL11)glContext.getGL();
}
}
My app is a simple thing that draws two squares to the screen on launch, one moves in a random direction while the other stays on the spot and spins. In theory the user should be able to touch the screen and the stationary square should move to the location touched on screen. But my app crashes when my UnProject method (which I need to translate coordinates) is called, possibly because my context
GL11 gl
(which is the argument for my UnProject class) returns null (NullPointerException is thrown when my app crashes).
Is my context initialized badly so that it returns null or do you think the problem is elsewhere?