Thread problem

Greetings!
Help please to me to solve a small problem with streams in JOGL.
I wish to make loading of textures and geometry in a separate stream, for this purpose I use Runnable() and do so:


    static GL gl;
    static GLUT glut;
    static GLU glu;
.....
public void init(GLAutoDrawable drawable) {	
      gl = drawable.getGL();
      glu = new GLU();
      glut = new GLUT();
.....
public void run()  {
            for ( int i = 0;i < 5; i ++){
            try {
                gl.glBindTexture(GL.GL_TEXTURE_2D, menuTex[i]);    
                tl.loadTexture("" +i,"menu");
                glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, 4,  tl.getWidth(),tl.getHeight(), gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, tl.getTextureBuf());
                } catch (IOException ex) {
                ex.printStackTrace();
                }
            gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
            gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
            gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP);
            gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP);
            } 
}

When I run the program it cast exception:

Exception in thread "Thread-12" javax.media.opengl.GLException: No OpenGL context current on this thread
        at javax.media.opengl.glu.GLU.getCurrentGL(GLU.java:234)
        at javax.media.opengl.glu.GLU.gluBuild2DMipmapsJava(GLU.java:1525)
        at javax.media.opengl.glu.GLU.gluBuild2DMipmaps(GLU.java:1581)
        at Metods.renderMenu.run(renderMenu.java:283)
        at java.lang.Thread.run(Thread.java:595)

Help me to solve this problem please :-
PS.I ask to forgive my English :wink:

All opengl calls have to be on the same thread (which is the one in which init() and display() methods are called for the GLEventListener). If you try to use the GL methods on another thread, it can’t handle it and you’ll get the exception. Short answer is that the actual creation of an opengl texture can’t be in a separate thread (however you can load the byte data into memory in a different thread and once it’s ready, create the texture in the next display() call on the GL thread).

Hopefully that helps.

Thanks for the answer! Now I know as to make :slight_smile: