Determine the color of pixel

Hi,

If the user clicks on my GLCanvas, is there a way to determine the color of the pixel that was clicked?

Thanks,

Pahidla

Yes you can, try using glReadPixels(), which takes in
absolute coordinates in pixels.

Yes, I tried that, but apparently in too naive a way:

public void mouseMoved(MouseEvent inME) {
  int x = inME.getX(), y = inME.getY();

  GL gl = myCanvas3D.getGL();
  int[] l = new int[3];
  gl.glReadPixels(x, y, 1, 1, GL.GL_RGB, GL.GL_INT, l);
  System.out.println(l[0] + " " + l[1] + " " + l[2]);
}

Maybe you could tell me easily what I’m doing wrong?

Could it be that your calling glReadPixels from the wrong thread? Try to do it the thread you do all the other gl calls.

Ouch. I was hoping I was doing something completely stupid.

Yes, I’ve checked and I’m trying to read pixels in the same thread as other gl commands. (Actually, I only have one thread.)

I remember vaguely having to do something like glPixelStorei(GL_PACK_ALIGNMENT, 1) and other preparatory commands.

Are you sure I’m not forgetting anything obvious? You are seeing my entire code!

Pahidla

My apologies!!!

It was in a different thread!

Trying to do it in the same thread now causes “Unable to lock surface” exceptions so I’ll try to figure those out before I continue with this thread.

Let me guess, it’s not a good idea to initialize gl stuff from a button’s callback?

Pahidla

Yes, be very careful about threads when using JOGL.
It’s not a fault of JOGL, but the different factors
involving ATI driver’s thread model, AWT and OpenGL
being inherently single-threaded-by-design.

When in doubt, always use the static method:
Thread.currentThread().

.rex

Where can I read about what it means to be careful.

Also, what do you mean by using Thread.currentThread().

Use how?

Dummy Pahidla.

…and

does the callback of a button count as another thread?

(BTW, still having trouble reading pixels even though now I’m pretty sure I only have a single thread - except for that callback question.)

Pahidla (still feeling like a dummy)

[quote]…and

does the callback of a button count as another thread?
[/quote]
Yes, in the AWT/Swing world, the button event is executed in the GUI thread, which probably is not the same thread you’re using JOGL from right now.

To go around this, it is easiest to “delay” and delegate the button event to the rendering thread, using a queue of events, for example. So, in general:

  1. Event occurs (button was pressed, mouse was moved, etc.)

  2. Delegate the event to the queue (you can simply put the event object to the queue)

  3. Rendering thread sees that the queue is non-empty and processes the events in the queue (responds to the pressed button, etc.)

I hope this helps

Boy…

It does help a little, but maybe it creates more questions than it answers. (Is there a tutorial on jogl and threads?)

Basically, are we talking about 3 threads here?
a. the main thread
b. the gui thread
c. the jogl rendering thread.

And the questions that got created are these:
I understand there’s only one gui thread. Can I do all of my rendering from there? Isn’t that how all virtually all 3D applications work - something is drawn in response to the users key strokes and mouse actions?

If you are suggesting that I have a third thread for rendering, do I have to take care of everything? I.E.

a. Create the thread.
b. Create the queue
c. Create a class that encapsulates what’s to be done in response to gui events.
d. The thread sleeps for 10ms and then checks whether there’s something in the queue.

And above all I have to make sure no gl commend is given from any other thread?

This seems to be so much work that if this is necessary there’s gotta be a standard implementation of it.

If I’m confused, please straigten me out.

Pahidla