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!
Eric