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

Posted on 2005-05-14
Last Modified: 2013-12-26
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?

Question by:rossryan
    LVL 10

    Expert Comment

    by:Andrew Beers
    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.


    Author Comment

    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?


    LVL 17

    Accepted Solution

    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...)

    LVL 4

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
    As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now