View Full Version : Sub Image on Texture

Hylke Donker

03-23-2005, 08:58 AM

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

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.

Hylke Donker

03-24-2005, 01:52 AM

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.

charliejay

03-24-2005, 05:14 AM

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

Hylke Donker

03-25-2005, 01:06 AM

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.

CThat sounds kinda complicated.

Can you give a small example?

Thanx Hylke

charliejay

03-25-2005, 07:00 AM

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

charliejay

03-25-2005, 07:23 AM

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

Hylke Donker

03-25-2005, 07:32 AM

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.

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

Thanx Hylke

charliejay

03-25-2005, 10:45 AM

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

knackered

03-25-2005, 01:14 PM

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.

Hylke Donker

03-25-2005, 11:51 PM

Thank you for your replies.

I've now got this code:

-87260

6720

-169891

44968

-139810

42889

-8142

1865

-2924

2859

-298791

31769

Can anyone tell me what I'm doing wrong?

Sorry for beging sucha math noob.

But they haven't teached me such things with math yet(if i'm right next year i'm going to learn such things with chinese like charachters :-p)

Hylke Donker

03-28-2005, 02:16 AM

No body?

charliejay

03-28-2005, 04:11 AM

DX and DY are going to be relative to the lengths of your texture axes.

For instance, a DX value of 1.0 means that the mouse point is on one of the vertical edges of your texture (probably the right hand edge).

So you need to scale it up before you convert to integer, in order to get a decent value in pixels.

Something like (int) (DX*textureWidth) should work...

Plus, you might get negative values, too, which would be outside your texture rectangle.

You may need to map these to a point inside the texture rectangle somehow - exactly how will depend on what you want to do, subImage will probably only work properly with input that ends up being within the range 0 to textureWidth-1, 0 to textureHeight-1.

CJ

Hylke Donker

03-29-2005, 05:43 AM

Ok thanx.

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.