How to normalize the screen height and width?

Hello just wanted to know the simplest and most efficient way to normalize
a value of screen width in this case GLfloat WIN_W = 640.0f;
and screen height in this case GLfloat WIN_H = 480.0f to a range of [-1.0,1.0].

I want or would like this GLfloat value between the range of [-1.0,1.0] please
for use in OpenGL modelveiw matrix translation column
as the centering value for my 2D Shapes modelview matrix like so:

1
2
3
4
5
6
std::vector<GLfloat> modelviewmatrix[] ={
xx,yx,zx,centerx //<- centering x with normalized [-1.0,1.0] GLfloat
xy,yy,zy,centery //<- centering y with normalized [-1.0,1.0] GLfloat
xz,yz,zz,tz,
xw,yw,zw,tw
};


If you could tell me where you learn this if you know how
Id greatly appreciate that too so I can study the hell out of it thanks.

I should also note that I tried

1
2
GLfloat centerx = WIN_H/WIN_H;
GLfloat centery = WIN_W/WIN_W;

However that did not center the 2D shape it instead
moved it about -5 units too the top right how flustering.


Last edited on
So you want to take a number which could be from zero to 640, and map it to a new number between -1 and 1.

Take the original number and divide it by 320. This turns the original number into a number from 0 to 2. Then subtract 1; now the number is in the range -1 to 1.

Let's check. We know that original number zero should become -1.

0 / 320 = 0 , and then subtract 1 to get -1. So that works.

Let's check with the other extreme; original 640 should become 1.

640 / 320 = 2. Then subtact 1, to get 1. So that works.

Let's check with the central value, original 320. This should become 0.

320 / 320 = 1, then subtract 1 to get 0. So that works.

To convert the range 0 to 480 to the range -1 to +1, do likewise but divide by 240.


As to how this is learned? It's technically some basic geometry and a little algebra, but to a large extent you can work this out with a little experience and drawing it on a piece of paper.
Very interesting response ...

I must look over all of this and get more aquanted with geometry maths
and mysterious algebra.


I see so you are dividing by half of the size which would give you 1 or 2 then subtract by 1 too get the real value .

Makes total sense I must learn more ha!.

The way to think about this is two steps.

First, scale. You have a line 640 units long, and you need to shrink it to exactly fit in a line 2 units long. So suddenly, the first part is obvious; divide by 320.

Now, you've got a line that goes from zero to two, but you want it to go from -1 to 1. Now, the second part is obvious; subtract 1.
Topic archived. No new replies allowed.