• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2332
  • Last Modified:

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?

Thanks,
Ryan
0
rossryan
Asked:
rossryan
1 Solution
 
Andrew BeersCommented:
Ok for starters read up a lil:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06

nehe.gamedev.net 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:

gltranslatef(MoveMe);
glrotatef(RotateMe);
glBegin(<something>);
   //draw me
glEnd();

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.

~Aqua
0
 
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?

Ryan

0
 
davebytesCommented:
Might want to look at the glut functions gluProject/gluUnProject:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glufnc01_4mno.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glufnc01_3xv8.asp
bottom of: http://www.opengl.org/resources/faq/technical/glu.htm
http://nehe.gamedev.net/data/articles/article.asp?article=13

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

-d
0
 
joachimcCommented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now