I wanted to make a simple image viewer using opengl. I have the opengl super bible ed. 2, followed their example for drawing a simple bitmap to screen using glDrawPixels, works nicely this is just what I want.
I’d like to determine which pixel of my on screen image the user has clicked on, assuming the user could have scaled/zoomed/panned the on screen image. Say the user has zoomed to a level in which only two pixels take up the entire viewport, and they click on one, is there a way to figure which pixel that is from my original image? I know how to get the mouse coords clicked, just need to know how to translate them into my original image coordinates.
Thanks for any suggestions,
Moving to the beginner’s section.
don’t know for a way in opengl but you could use some relationships between zoomfactor - viewport size and zoomfactor - pixelsize …
Mouse movement and windowing is outside the domain of OpenGL.
However you should know where you have drawn an image through calls like viewport using your window dimensions and ortho projection calls etc.
When you get a mouse click you use the mouse x and y positions to tell you where you are in the window and use that value as a straight interpolation through your glViewport and your glOrtho (or equivalent) projection to tell you where you are relative to where you drew your image.
Hello Dorbie & Powerpad, thanks for your replies. I see how it can be done since I know the information necessary to calculate the final position. I have a few more basic questions before tackling my picking problem.
I set up middle mouse button + move to pan the image around the screen, that works nicely. I am having a bit of trouble with zooming in on the image. I tried using glRasterPos3f() to alter the Z location, which it does, but seems to have no effect since I guess I’m in orthogonal projection mode. I tried the glPixelZoom() function but it looks a little funky since it’s just scaling the image from its origin, instead of dollying the camera forward. Is there a way to maintain orthogonal projection while moving the camera forward?
Thanks again for any suggestions,
While zooming is naturaly bind to distance to the object, in case of ortho projection the thing are different (as you already guessed). So you can zoom in orthographic projection changing left, right, bottom and top values.
edit: But this will not work well with glDrawPixel, which you use, as I see now. You must use textured quad for that porposes in order for my advise to be valid.
Yeah I finally switched to using a textured quad which eliminates some of the problems I was having with glDrawPixels(). I can do the zooming/panning now nicely.
It seems though that my original image is getting a little distorted due to interpolation by the texture mapper somehow, even though I set all the filtering to GL_NEAREST. I took 2 screen shots of a small portion of my image that exhibits this behavior:
The actual image region:
How this region looks textured:
Can I not achieve a 1 to 1 relationship with the original image, or could this be due to something else? (Other parts of the image are fine!)