0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jack
  • 2,625
  • 5
  • 33
  • 56

1 Answers1

1

I doubt that the Context constructor gets called anywhere in your code; if it did it would crash at that point:

  • You cast gl to an EGL11 type, while it doesn't implement that interface.
  • You use the config member in EGLContext glContext = ((EGL11) gl).eglCreateContext(dpy, config, EGL11.EGL_NO_CONTEXT, attribList);, while it's not initialized (it's null as the entries of your configs member are not constructed)
  • Where is gl assigned? From outside the Context class?
  • You use the gl member and then reassign it in gl = (GL11)glContext.getGL() ?
  • ...

In short, I don't think gl was assigned a value in the first place, but it's hard to tell without the rest of the code.

Aert
  • 1,989
  • 2
  • 15
  • 17
  • okay i'm obviously approaching this completely wrong. Any good resources that could get me on the right track? – Jack Aug 16 '11 at 20:42
  • I think so; but can you let me know what you intended to do with this class in the first place? I think it's best to just let Android worry about the context creation and destruction, once you specified what type of surface you want. I.e. why isn't the standard `eglSetConfigChooser()` & `setRenderer()` in the constructor of your `glSurfaceView`, and using the `gl` variable in your renderer class as it is passed in the `onSurfaceCreated()`, `onSurfaceChanged()` and `draw()` methods sufficient? – Aert Aug 18 '11 at 02:16
  • the entire system is a simple app that draws just a couple of squares on screen. When the user touches the screen on square should move to that location. In my renderer I have a method that implements unProject (which is necessary to convert the coordinates). That is the method that returns a null pointer exception because its arguments are `(GL11 gl){` When I call this method elsewhere I cannot just put `vector3(gl);`(returns an error) so I was told to create contexts. If you want a little more info then the questions are still on my user profile. – Jack Aug 18 '11 at 11:00
  • 1
    I think your issue has to do with you wanting to access `gl` from the UI thread `onTouchEvent()`. You can't. The simplest way to achieve what you want is to sync buffer the touch events (UI thread) and sync evaluate them in the `draw()` (GL thread) where your matrices have just been updated. You then take action in that GL thread to move your object. There are other better ways, but that should work. – Aert Aug 22 '11 at 02:55