Quantcast

How to refer GL context in AWT Mouse Event

classic Classic list List threaded Threaded
11 messages Options
nj
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to refer GL context in AWT Mouse Event

nj
I have to convert local coordinate to global one when Mouse Event happens.
Then I have to refer GL context in AWT EVENT THREAD.
How can I do it? any reference?

Regards,
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
Hi

As I told you on StackOverflow, you shouldn't even try to do so, it's a bad idea, it would require to make the OpenGL context current on the AWT thread in your mouse event and release it when you're done, you should rather use GLAutoDrawable.invoke(boolean, GLRunnable) instead.

Please show some source code, I'm not sure that you really need a GL context to perform your conversion.

By the way, some engines store and treat the mouse and key events at a more appropriate time (not directly in the listeners) to solve this kind of problem.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

n.j.
I am very sorry for me too late.
I'd been in hospital cause of my stomach ulcer.

code skel is


public class MyPanel extends GLJPanel implements GLEventListener, MouseListener {
    MouseEvent mouseEvent = null;
    public void mousePressed(MouseEvent e) {
        if(SwingUtilities.isLeftMouseButton(e)) {
            mouseEvent = e;
            repaint();
        }
    }
    public void display(GLAutoDrawable arg0) {
        GL2 gl2 = arg0.getGL().getGL2();
        if (mouseEvent != null) {
            pickup(mouseEvent, gl2, glu);
            mouseEvent = null;
        } else {
         ...
        }
    }
    private void pickup(MouseEvent e, GL2 gl, GLUgl2 glu) {
        int x = e.getX();
        int y = e.getY();
        DoubleBuffer model = DoubleBuffer.allocate(16);
        DoubleBuffer proj = DoubleBuffer.allocate(16);
        IntBuffer view = IntBuffer.allocate(4);
        gl.glGetDoublev(GL2.GL_MODELVIEW_MATRIX, model);
        gl.glGetDoublev(GL2.GL_PORJECTION_MATRIX, proj);
        gl.glGetIntegerv(GL2.GL_VIEWPORT, view);
       DoubleBuffer loc = DoubleBuffer.allocate(3);
       FloatBuffer z = FloatBuffer.allocate(1);
       gl.glReadPixels(x, view.get(3)-y-1, 1, 1, GL2.GL_DEPTH_COMPONENT, GL2.GL_FLOAT, z);
       glu.gluUnProject(x, view.get(3)-y-1, z.get(0), model, proj, view, loc);
       DisplayCompartment selected = getDisplayCompartment(loc.get(0), loc.get(1), loc.get(2));
       if (selected != null) {
         selected.setSelected(true);
         repaint();
       }
    }
}


I got a problem using Mac Book Pro.
As a local coordinate leaves the starting point,
the return Global Coord is differ.
Another mac, Mac Book Air and Mini I'd tested,
the problem does not occur.
So, the way to unproject I'd written could be doubtful, I thought.

I should be very much obliged if you teach me good solution method.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
Why do you need to call repaint()?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

n.j.
 When mouse clicked, I have to change the color of selected DisplayCompartment.
getDisplayCompartment() also do it.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

n.j.
I made a mistake.
DisplayCompartment#setSelected() do it.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
You should create a Runnable to call repaint() and pass this runnable to SwingUtilities.invokeLater(). Moreover, creating a buffer at each call of glReadPixels is a bad idea, create a direct NIO buffer once and reuse it.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

n.j.
I am sorry, but can I refer the GL Context in SwingUtilities#invokeLater()?

like,

class MouseWorker extends Runnable {
  private MouseEvent mouseEvent;
  private GL2 gl;
  MouseThread(MouseEvent mouseEvent) {
     this.mouseEvent = mouseEvent;
  }
  void setGL2(GL2 gl) {
    this.gl = gl;
  }
  public void run() {
     int x = mouseEvent.getX(), y = mouseEvent.getY();
     NIO nio = getNIO();
     gl.glGetDoublev(GL2.GL_MODELVIEW_MATRIX, nio.model);
     gl.glGetDoublev(GL2.GL_PROJECTION_MATRIX, nio.proj);
     gl.glGetIntegerv(GL2.GL_VIEWPORT, noi.view);
     gl.glReadPixels(x, nio.view.get(3)-y-1, 1, 1, GL2.GL_DEPTH_COMPONENt, GL2.GL_FLOAT, nio.z);
     glu.gluUnProject(x, nio.view.get(3)-y-1, z.get(0), nio.model,nio.proj,nio.view,nio.loc);
     DisplayCompartment selected = getDisplayCompartment(nio.loc.get(0),nio.loc.get(1),nio.loc.get(2));
     if (selected != nulll) {
       setSelected(selected);
       this.MyPanel.repaint();
     }
  }
}
public void mousePressed(MouseEvent e) {
   Runnable r = new MouseWorker(e);
   SwingUtilities.invokeLater(r);
}

You mean like this?
or am i confused?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
This post was updated on .
I meant that you could use a Runnable that just calls repaint() and nothing else.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

n.j.
I can try it.
thank you.
But the problem i face, the difference of local coordination to global one.
As far as the local mouse pointed from the origin of the local coord,
with using Mac Book Pro returns invalid global coord
How can i tell you,

regards,
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
Maybe you should take into account the scale when HiDpi is enabled.
Loading...