Adding multiple GLEventListener to GLCanvas

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding multiple GLEventListener to GLCanvas

alicana
I have different types of data (e.g. point, line) that should be drawn. I created separate classes for each like below:

public class Point implements GLEventListener
public class Line implements GLEventListener

Is that a good idea that bind and unbind these listeners?

public void drawListener(GLEventListener obj) {
        glCanvas.addGLEventListener(obj);
        glCanvas.display();
        glCanvas.removeGLEventListener(obj);
        }
}

If you have any recommendation, I'll be very pleased.
Reply | Threaded
Open this post in threaded view
|

Re: Adding multiple GLEventListener to GLCanvas

gouessej
Administrator
Hi

Don't overcomplicate something so simple, use a single GLEventListener. I don't understand what you're trying to achieve.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Adding multiple GLEventListener to GLCanvas

alicana
I was doing a code review, I guess its made for modularity. There is a main GLEventListener class and a simple thread to invoke its display method.

User creates different objects (which are implements GLEventListener) Main display thread adds/removes objects sequentially.So different display callback methods are getting called.

To be clear, I'm sharing this pseudo below:
======================================
Class Main implements GLEventListener{
 ...
 glCanvas.addGLEventListener(this);
 glCanvas.setAutoSwapBufferMode(false);
 ...

 init();
 display();
 reshape();
 dispose();
}
======================================
Class Point implements GLEventListener{

 init(); // nothing done
 display();
 reshape(); // nothing done
 dispose(); // nothing done

}

=====================================

 DrawThread extends Thread{

        while(true){

             main.glCanvas.addEventListener(point_obj_1);
             main.glCanvas.display();
             main.glCanvas.removeEventListener();

             main.glCanvas.swapBuffers();

       }

}
Reply | Threaded
Open this post in threaded view
|

Re: Adding multiple GLEventListener to GLCanvas

gouessej
Administrator
There is no need to create a thread, rather use an animator. GLEventListener instances can but shouldn't be used that way. Rather create a single GLEventListener, add it once for all into the canvas and this single GLEventListener should loop on all objects. These objects should have some drawing code.

You can use a scenegraph API instead of reinventing the wheel except if you want to use only JOGL.

Edit.: You shouldn't have to swap the buffers by yourself especially if you haven't called setAutoSwapBufferMode(false). The more you go away from the helpers of a framework, the more you become responsible for doing the things the "right" way.

Edit.2: You can look at this very simple example.
Julien Gouesse | Personal blog | Website