Transform to eye-space with shader


Im reading the orange book at the moment and i have a
small problem.

To transform incoming vertices to eye-space they suggest
the following method:

in vec4 vertex;

uniform mat4 mvmatrix;
vec4 epos;
vec3 epos3;

void main() {
epos = mvmatrix * vertex;
epos3 = (vec3(epos)) / epos.w


What does the last line do? My vertices are available in homogeneous
coordinates, but the fourth component is always 1 so i could just
write to get the same result or am i wrong?

Later in the book there are several examples where the author use this
method. But from time to time he divided the transformed vertex by w
and i don’t know why.

Can someone help me out?

If you are sure that w is always 1 in your data, than you can skip that line. Generally, it shouldn’t always be 1. That’s why Randi wrote so.


In my OpenGL client code i store only vertices with 3 coordinates
in a vbo. Since i read out a vec4 in the vertex shader i guess opengl sets the homogeneous coordinates for me. Can i assume in this case that they are always equal to one?

Thanks for your help so far.

Whenever you need 4 coordinates, just call vec4 constructor

in  vec3 in_Position;
void main()
   gl_Position = matPMV * vec4(in_Position, 1.0);

AFAIK, OpenGL shouldn’t provide implicit conversion.

Generally, it shouldn’t always be 1. That’s why Randi wrote so.

Actually, it is usually one. Eye space is generally not a homogeneous coordinate system. At least, I seriously doubt that the lighting equations take into account the possibility that the coordinate system will be a non-linear homogeneous coordinate system.

The division is there for the sake of completeness. In any practical application, you won’t need it.

That was my error. English is not my native language, so the reason is obvious, and I even didn’t read what I had written. Shame on me. :slight_smile:
W can be different from 1 for some spatial curves, when it is more convenient, and for vectors when it is 0. But, as you’ve said, usually it is 1.