Window Coordinates (OpenGL)-> coordinates with regards to a rotated texture

This question pertains to OpenGL.

I have a rotated texture. The coordinates I have for some other part of the program are in Windows coordinates (full on view, zero rotation). How can I tranform these coordinates to fall within the rotated textures object coordinates?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Andrew BeersTechnology LeadCommented:
Ok for starters read up a lil: is your most valuable tool for begining OpenGL

Second, remember that openGL is based off a series of state changes.  You apply a texture giving an object tex coordinates, if in 2D you give it coordinates of a plane then you give the texture properties like repeat, stretch, etc.  Since you read state changes in opengl from the bottom up, meaning what is lowest happens first... well sortof... at least with rotates and translates any how...

You want to apply the texture within your glBegin(<something>);  But your structure should look something like this:

   //draw me

With of course your glPushMatrix() glPushAttrib() and cooresponding pops surrounding the declaration of the object so the rotate and translates do not move your entire world.

Not quite sure if I hit your question there but... if I didn't clarify your question a tad.  If you are asking about detecting a mouse click from 2D window coords to 3D open gl coords... specify that.

rossryanAuthor Commented:
I've read the NeHe tutorials quite extensively, so perhaps I am phrasing (or thinking) about the problem incorrectly. I have a hit buffer, it works fine, as do the textures.

Here's some background (may or may not be useful):

I am writing a program that copies a MS Window into OpenGL, and allows full keyboard and mouse input (think Project Looking Glass or MS Task Gallery). Keyboard redirection is currently slotted in (works, but I have not mapped all the keys, just the alphanumerics for testing), mouse redirection will be soon. Now, through a series of APIs, a given window is copied and converted to a texture, and mapped to a QUAD, of which a single face (the front face) is drawn.

The program copies only the parent windows of a thread, though not the child windows of the parent windows. The child window coordinates (Windows coordinates), however, are copied into an array for use with its corresponding parent window. This is done because input redirection may require that redirection is sent to a child window of the parent window, instead of a parent window (just the way Windows works). For instance in Notepad.exe, the actual keyboard input in Windows is not to the parent window, but to the Edit control. This may be confirmed by opening Notepad, and firing up Spy++, observing the parent and child windows.

Now in Spy++, you may observe that Notepad has coordinates of (233, 239) - (782, 590) 549x351. The Edit control (a child window of Notepad) has coordinates of (237, 281) - (778, 586) 541x301. Now, my program detects when a user clicks on a texture, grabs the window coordinates of mouse click, and tries to find out if the area that the user clicked falls within a child window. If it does, it redirects input to that child window, instead of the parent window. The detection is done by taking the Windows coordinates of the parent window, subtracting these coordinates from the child window coordinates and the mouse coordinates (to get relative Windows coordinates with regards to the Parent Window), and seeing if the relative mouse coordinates falls the relative coordinates of the child window. If it does, it redirects output accordingly.

The problem is two fold. One, the texture is not precisly the same size as the Windows window, so the coordinates are a bit off, but this may be fixed by applying a few more transforms, by making the Windows coordinates of the window relative to the object coordinates. The big problem lies in that this only works if the object is rectangular. If you rotate it, the object is a rotated rectangle, whose height and width vary at any point. It becomes more of a trapezoid or something. How do I map the Windows coordinates to the object then? The object coordinates have changed, but the Windows coordinates have not. The object is a trapezoid, but the Windows coordinates are a rectangle. Right now, I calculate the bottom, top, left and right borders in Windows coordinates, and see if the the mouse coordinates fall within them. When rotated, these coordinates become meaningless.

Any ideas?


Might want to look at the glut functions gluProject/gluUnProject:
bottom of:

Useful for translating, with the current matrices, between window coords and object coords, and back.  In this case, your object is a rectangular QUAD (or two triangles forming a quad).  You may need to do some interpolation to get the '3d window panel' coordinates proper (as you yourself noted).

you may also want to look around for GL 'picking' examples, which help determine when an object has been clicked.

(Even without the picking code, you can always project the four corners into window space, and test that the mouse is within the quad/trapezoid formed by them...)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
To convert a picture to a texture you should do like this

PicSize.x/ScreenWidth = 1.0 - 0.0
picSize.y/ScreenHeight = 1.0 - 0.0

You should skip the rotation. Instead rotate the quad you are using back to 0 rotation and insert the texture. Then you can rotate the quad instead of rotating the texture.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.