# Thread: Sub Image on Texture

1. ## Sub Image on Texture

Hello
I'm trying to create a sub image on a texture at the point of my cursor, when my cursor is in a texture.
So far, I've managed to know when I'm in a texture.
I also know that i need to use glTexSubImage* to create a sub image.
But i don't know how to calculate the position of my cursor in the texture.
I've been trying to figure it out for hours but i still don't know how to do it.
This is the information i have:
the window x and y coordinates of my cursor
The world x and y coordinates of my cursor
the position of my texture
the size of my texture(in world coordinates).
So can somebody help me and tell me how to calculate it?
Thanx Hylke

2. ## Re: Sub Image on Texture

Leaving out the texture, do you need to have it working for arbitrary geometry or just for special cases?
Can the texture-mapped geometry be transformed in arbitrary ways or it's guaranteed it'll always be in a certain position (say, facing parallel the near plane)?
The first case is definetly more involved and I'm not sure it has real applications, at least at basic level.

3. ## Re: Sub Image on Texture

Originally posted by Obli:
Leaving out the texture, do you need to have it working for arbitrary geometry or just for special cases?
Can the texture-mapped geometry be transformed in arbitrary ways or it's guaranteed it'll always be in a certain position (say, facing parallel the near plane)?
The first case is definetly more involved and I'm not sure it has real applications, at least at basic level.
Does arbitrary mean random?
But the texture doesn't always have the same place, but a random place.The random coordinates are saved in a x and y variable.

4. ## Re: Sub Image on Texture

If you're looking at the texture square on, meaning that it's as if the texture is a piece of paper on a table, and you're looking at the table from above, then this is quite easy to calculate, even if the texture is rotated, because it's a 2 dimensional calculation.

Regardless of the number of dimensions, you're just expressing the coordinates of the mouse point projected onto the texture's plane, in the texture's coordinates.

You just take the dot product of the vector from the texture's origin to the projected mouse coordinate, with the texture's unit X and Y axes.

C

5. ## Re: Sub Image on Texture

Originally posted by charliejay:
If you're looking at the texture square on, meaning that it's as if the texture is a piece of paper on a table, and you're looking at the table from above, then this is quite easy to calculate, even if the texture is rotated, because it's a 2 dimensional calculation.
Yup, that's the case, it's completly 2D
Regardless of the number of dimensions, you're just expressing the coordinates of the mouse point projected onto the texture's plane, in the texture's coordinates.

You just take the dot product of the vector from the texture's origin to the projected mouse coordinate, with the texture's unit X and Y axes.

C
That sounds kinda complicated.
Can you give a small example?
Thanx Hylke

6. ## Re: Sub Image on Texture

http://homepage.mac.com/cjj/index.html

You can just go strarght to the dot product steps because the mousepoint is already projected on the texture's plane.

All values, including those for the texture's axes and origin, should be expressed in the same coordinate system, probably window coordinates.

CJ

7. ## Re: Sub Image on Texture

Look up the dot product on any maths site, mathworld.wolfram.com is good.

Basically, you use the dot product anywhere you want to express one vector in term of some others (which is what you want to do), provided all these vectors are expressed in the same coordinate system.

CJ

8. ## Re: Sub Image on Texture

Originally posted by charliejay:
http://homepage.mac.com/cjj/index.html

You can just go strarght to the dot product steps because the mousepoint is already projected on the texture's plane.

All values, including those for the texture's axes and origin, should be expressed in the same coordinate system, probably window coordinates.

CJ
My English is not so good, so could you give a small code sample to demonstrate what i have to do?
Thanx Hylke

9. ## Re: Sub Image on Texture

OK, it's pure math, so here are the calculations, can't do code, because I don't program where I have internet access.

We have 3 vectors, each with an x and a y component, because we're in 2 dimensions.

All values are defined in world coordinate space.

The vectors are,

TX, TY, and OM.
Each of these is an (x,y) pair.

Referring to my diagram, where O is the meeting point of TX and TY

OM = M.x-O.x , M.y-O.y

These are the two (2 dimensional) dot product calculations
DX=TX.x*OM.x + TX.y*OM.y
DY=TY.x*OM.x + TY.y*OM.y

They describe the mouse point in terms of the coordinate system defined by the texture origin, and the texture axes.

You can then test that point against the texture rectangle, convert it to integer coordinates, etc, and then use those coordinates for the Sub Image...

CJ

10. ## Re: Sub Image on Texture

Give him the .h/.c files, that's all he wants. He wants to copy and paste, he doesn't want to know how it works.
BTW, don't mind us - we're just reading this because we thought it was an advanced opengl topic....can't think why we'd think that.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•