How to keep a current GLContext?

Greetings everyone!

I am working on an application which shall be able to use different GUIs.

My JOGLGUI.java is structured like this (I removed everything that cant relate to my problem):


public class JOGLGUI implements GLEventListener {
	
	private Frame frame;
	private GL gl;
	private GLU glu;
	private float camPosX = 0, camPosY = 0, camPosZ = -5, camAngle = 29;
	private GraphicsDevice graphicsDevice;
	private Texture texture;
	private GLContext glcontext;

	public JOGLGUI() {
		frame = new Frame("JOGLGUI");
		graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		if (graphicsDevice.isFullScreenSupported()) {
			frame.setUndecorated(true);
			graphicsDevice.setFullScreenWindow(frame);
		} else {
			frame.setSize(800, 600);
		}
		glu = new GLU();
		GLCanvas canvas = new GLCanvas();
		glcontext = canvas.getContext();
		canvas.addGLEventListener(this);
		frame.add(canvas);
		final Animator animator = new Animator(canvas);
		frame.setVisible(true);
		animator.start();
	}
	
	public void drawItems() {
		glcontext.makeCurrent();
		texture.bind();

		gl.glNewList(1, GL.GL_COMPILE);
			gl.glBegin(GL.GL_POLYGON);
				gl.glTexCoord2i(0, 0); gl.glVertex2i(0, 0);
				gl.glTexCoord2i(1, 0); gl.glVertex2i(1, 0);
				gl.glTexCoord2i(1, 1); gl.glVertex2i(1, 1);
				gl.glTexCoord2i(0, 1); gl.glVertex2i(0, 1);
			gl.glEnd();
		gl.glEndList();
	}

	public void init(GLAutoDrawable drawable) {
		gl = drawable.getGL();
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glEnable(GL.GL_TEXTURE_2D);

		File texFile = new File("floor.bmp");
		try{
			texture = TextureIO.newTexture(texFile, true); 
		} catch (IOException e) { System.out.println("Texture could not be loaded!"); }
		
	}	// init
	
	public void display(GLAutoDrawable drawable) {
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		
		gl.glPushMatrix();
		glu.gluLookAt(camPosX, camPosY, camPosZ, camPosX, camPosY, 0, 0, -1, 0);
		
		gl.glPushMatrix();
		gl.glCallList(1);
		gl.glPopMatrix();
		
		gl.glPopMatrix();
		
		gl.glFlush();
	}
	
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(camAngle, (double)width/(double)height, 1.0, 20.0);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
	}
	
	public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}

}

Now my problem is:
The method drawItems is called from the main class and is supposed to add or change items to be displayed. Therefore it needs to use loaded textures or generally even load new textures given. But when calling texture.bind() I get the Exception:

Exception in thread “main” javax.media.opengl.GLException: No OpenGL context current on this thread

How can I get around this? Also I dont really understand the GLContext concept, I guess.

I tried to save the GLContext as a class variable and make it current in drawItems() (red code), but then my program completely crashes and I have to abort it by CTRL-ALT-DEL. Also some other approaches failed, and I don’t know how to solve this.

Any help greatly appreciated, and thanks in advance! :smiley:

Eric

I just saw that the code somehow cannot be marked red. Anyway, the three lines that I meant are surrounded by tags.

Too bad that this doesnt work, would be extremely helpful to mark code lines…

Eric

Here is how I got around the no context in this thread issue. Create a list of textures to be loaded and a boolean flag in your GUI class. Add textures to the list, set the flag to true, then at the start of the display method load the textures, clear the list, and set the flag to false. It works for my tile based game, I don’t know if this would cause problems if you were loading large textures.

Edit: oops, just saw your problem was about binding not loading, nothing to see here, move along…

Hej there again!

I hope this thread did not get forgotten over the weekend?

Please just some short explanation or remarks!

Thanks, Eric

Hi again!

Please, someone help me with this. I really couldn’t find anything helpful anywhere, so please just some hint how to solve this! ???

Eric

The only suggestion I can offer you is to not do what you are doing. Your manipulation of the OpenGL context is not correct. Rather than going down the route you are currently going, I would strongly recommend you use the GLEventListener paradigm as it is intended and only do rendering inside your display() callback. Set up state in your GLEventListener from your GUI indicating what is to be drawn next, and call repaint() / display() on the GLCanvas.

Maybe you want to read this slightly related discussion

Thanks, great description in this other discussion!

Although I have the feeling that this is far more complicated than I suspected it to be, this solution seems to work quite well for me. Then again, I am far from being an expert…

Anyway, thanks a lot!

Eric