How to refer GL context in AWT Mouse Event

classic Classic list List threaded Threaded
22 messages Options
12
nj
Reply | Threaded
Open this post in threaded view
|

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
|

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.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

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
|

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
Why do you need to call repaint()?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

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
|

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
|

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.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

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
|

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.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

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
|

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
Maybe you should take into account the scale when HiDpi is enabled.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

n.j.
Dear gouessej,

I am very afraid to say, I have to check if the HiDPI mode is enabled and
scale like
 int x = mouseEvent.getX();
 int y = mouseEvent.getY();
 if (isOSX() && isHiDPIenabled() && isHiDPImode()) {
   x = x / 2;
   y = y / 2;
 }

I have to do like this?
or do you have cool and generic answers?
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
Maybe you can disable HiDpi as a first step to see whether it affects your code:
https://github.com/gouessej/Ardor3D/blob/master/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtCanvas.java#L65
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
In reply to this post by n.j.
You can call ScalableSurface.getCurrentSurfaceScale() to get the surface scale, note that GLJPanel implements this interface.
Julien Gouesse | Personal blog | Website
nj
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

nj
Thank you for you answer.

Then i should wrote like

public void mousePressed(MouseEvent mouseEvent) {
   float[] scale = new float[2];
   /*void*/getCurrentSurfaceScale(scale);
   float x = scale[0] * mouseEvent.getX();
   float y = scale[1] * mouseEvent.getY();
   setMousePressedLocation(x,y);
}


by the way,
can I refer GL context in SwingUtilities#invokeLater()
how can i use it safely ?

Regards,
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
nj wrote
Thank you for you answer.

Then i should wrote like

public void mousePressed(MouseEvent mouseEvent) {
   float[] scale = new float[2];
   /*void*/getCurrentSurfaceScale(scale);
   float x = scale[0] * mouseEvent.getX();
   float y = scale[1] * mouseEvent.getY();
   setMousePressedLocation(x,y);
}
It makes sense. It seems to be correct.

nj wrote
by the way,
can I refer GL context in SwingUtilities#invokeLater()
how can i use it safely ?

Regards,
No you can't use a GL instance in SwingUtilities.invokeLater(). Rather call GLAutoDrawable.invoke(boolean, GLRunnable). You mustn't store any GL instance in non local variables or fields. If you use an invalidated GL instance or if you use it when the OpenGL context isn't current on the current thread, bad things will happen.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

n.j.
Dear gouessej,

thank you very much.
thank you!
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

gouessej
Administrator
You're welcome.

Keep in mind that this workaround might become useless with Java 1.9 on other platforms:
http://openjdk.java.net/jeps/263
Julien Gouesse | Personal blog | Website
nj
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

nj
Dear gouessej,

Is it difficult to implement like GLJComponent#setHiDPIMouseEventBehavior(enum MouseEventBehavior) ?

I am very happy if I can use GLJPanel.setHiDPIMouseEventBehavior(MouseEventBehavior.SCALED);;
only once at creation time.


I will be happy, how about you?

regards,
nj
Reply | Threaded
Open this post in threaded view
|

Re: How to refer GL context in AWT Mouse Event

nj
I know the rumors of Java9,
and I am expecting 1.9.

i am afraid that whether it is portable feasible
on different systems.

(。ŏ﹏ŏ)
12