1. ## Math help anyone?

I am makeing a library to deal with all my opengl general stuff, and i need help with my mouse interface.

See windows assumes that 0,0 is the upper left corner, and OpenGL assumes its the lower left. so i need to flip the y axis value.

it is stored here

loc.cur.y

ive been trying, and have this, but it dosent work

int tempy;
if(loc.cur.y > (glh.height/2)) tempy = (glh.height/2) - (loc.cur.y - (glh.height/2));
if(loc.cur.x < (glh.height/2)) tempy = (glh.height/2) + (loc.cur.y - (glh.height/2));

I have attached a simple program to demonstrate the problem.
just click on a sector to turn the floating box that color.

Given a 600 y resolution, if the mouse is at 500, it needs to be at 100.

2. ## Re: Math help anyone?

Originally posted by Eber Kain
I am makeing a library to deal with all my opengl general stuff, and i need help with my mouse interface.

See windows assumes that 0,0 is the upper left corner, and OpenGL assumes its the lower left. so i need to flip the y axis value.

it is stored here

loc.cur.y

ive been trying, and have this, but it dosent work

int tempy;
if(loc.cur.y > (glh.height/2)) tempy = (glh.height/2) - (loc.cur.y - (glh.height/2));
if(loc.cur.x < (glh.height/2)) tempy = (glh.height/2) + (loc.cur.y - (glh.height/2));

I have attached a simple program to demonstrate the problem.
just click on a sector to turn the floating box that color.

Given a 600 y resolution, if the mouse is at 500, it needs to be at 100.
Well, if you want to go from (maxheight-1)..0 instead of 0..maxheight-1, just do:

(i'm assuming tempy is where you want the result, glh.height is the height, and loc.cur.y is the y-value you want to convert.

tempy = glh.height - loc.cur.y - 1;

To test this:

if loc.cur.y = 0; tempy = glh.height - 1 (good!)
if loc.cur.y = glh.height - 1; tempy = 0 (good!)

Because this is a linear system of one indep. variable, if two points map correctly, all points map correctly, so this will work unless I misunderstand the problem.

3. You are talking about wrapping i think, i need to invert the mouses current y position.

if its 500 make it 100, 400 = 200, 110 = 490.

run the program so you can see what im talking about please

4. Originally posted by Eber Kain
You are talking about wrapping i think, i need to invert the mouses current y position.

if its 500 make it 100, 400 = 200, 110 = 490.

run the program so you can see what im talking about please
Try that function -- it DOES invert it.

In fact, plug in your examples, and you'll see they differ from the function I gave by only 1 (which is your mistake, I believe, because the lines would be numbered 0...599 not 0..600, so 0 maps to 599, 100 maps to 499, etc.)

my function, evaluated at your examples:

tempy = glh.height - loc.cur.y - 1;

loc.curr.y = 500, then tempy = 600 - 500 - 1 = 99;
loc.curr.y = 400, then tempy = 600 - 400 - 1 = 199;
loc.curr.y = 110, then tempy = 600 - 110 - 1 = 489;

No matter which approach I take to your problem, I always get tempy = glh.height - loc.curr.y - 1 as the answer.

Another approach is to look at how you want tempy to change for a given change in loc.curr.y. We can prove that an inversion mapping like this is a linear equation of the form tempy = m * loc.curr.y + b. I'll leave out this proof, but from this, we can analyze the system.

If loc.curr.y increases by one, tempy should decrease by one. This means the slope (change in output for unit change of input) is equal in magnitude (one pixel) but opposite in direction, so m = -1. Once we have this, we can substitute any point whose values we know -- for example, if loc.curr.y = 0, we want tempy = glh.height - 1. From this we can compute b, and we'll get the same equation as above.

I know this method works, because I, too, have needed to invert y-values, for reading certain types of images. And this will invert the y-values -- test it if you don't believe me.

5. The code I use (and it may help you, it may not) is ->

x = ((mouseX*2/640 - 1) * (z/2));
y = ((mouseY*2/480 - 1) * -(z/2));

mouseX is your windows mouse x co-ordinate
mouseY is your windows mouse y co-ordinate

z is your openGL depth value

x should give you the openGL x co-ordinate
y should give you the openGL y co-ordinate

Hope this helps

6. well ill be damned, that works perfectly, it still dosent look right, but works great, thanks alot man.

7. ## :)

my theory is "whatever works", it took my brother and I about 6 hours to work that out. Glad it helped