### OpenGL and Perspective Matrix

Posted:

**Thu Jan 28, 2016 8:25 pm**
by **Benjamin100**

I've been getting back into graphics programming, which I tried before but never really took seriously.

Regarding perspective, I don't understand what I'm doing with the "w" variable in the matrix. What IS the w variable? What does it represent?

Why do I use it for perspective? What is it doing?

### Re: OpenGL and Perspective Matrix

Posted:

**Fri Jan 29, 2016 3:36 am**
by **Benjamin100**

Ok. This is interesting. I have a couple books on computer graphics, both of which discuss the concept. It is just a little difficult to understand.

I think I understand a bit better. Looking through my books, the issue is that the w isn't really to be thought of as a coordinate in the program. It is just another variable that is placed in the vector to simplify the transformation process. Is that accurate?

### Re: OpenGL and Perspective Matrix

Posted:

**Tue Feb 09, 2016 2:27 pm**
by **Falco Girgis**

Basically the 'w' is an extra parameter that when multiplied by the perspective matrix gives a ratio for scaling the X and Y vector components by the Z component (which results in scaling the size of objects based on their distance from the camera). The point of the w and this ratio is so that it can be used in something called "perspective division." Once you've multiplied whatever <x,y,z,w> vector by the perspective matrix and have gotten a new, transformed vector, the <x,y,z> components are all divided by w. This is what produces the effect of distant objects being small and close objects being large in a 3D scene. It's because the X and Y terms are essentially being scaled as a function of the Z term, which is distance from camera when perspective division is applied.

An "orthographic projection" matrix in 2D mode is a matrix whose transform simply results in a 1.0 in the w term. Now when perspective division happens, there is no scaling based on distance, and you're left with a uniform or orthographic perspective. Perspective division was still happening before in 2D, it just wasn't doing anything since you were dividing by 1.0.