how to convert pixel to [-1..1] coordinate in opengl

how can i convert pixel to [-1..1]coordinate in opengl?
witch function?
i want to write a program that is work with glutmousefunc(),
when i click on the screen a ball is appear on it where clicked. but mouse work with pixel and circle work with coordinate between -1 and 1
x = ((2 / screen-width) * mouse-x) - 1
y = ((2 / screen-height) * mouse-y) - 1
it doesnt work for me
when i clicked on (0,400) pixel it draw circle in (-1,-1) coordinate and i just can see a quarter of circle
Last edited on
You have to use float or double to do that right. like:
double v = 2.0 / (double)screen-width;
int x = round(v * (double)mouse-x - 1.0);

I know it's a bit messy with those back and forth casting, but if you divide integer values you quickly just get 0.

Also, I wrote that in two lines to make it less messy. I write this in one line in my own programs.

Also, also, the function round() doesn't really exist/work this way, but I think you know what I mean. I usually just cast back to int, I don't really care for a half-pixel error.
Last edited on
thanks so much
Topic archived. No new replies allowed.