View Full Version : Transform to eye-space with shader

05-14-2011, 05:27 AM

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 epos.xyz 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?

05-14-2011, 06:02 AM
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.

05-14-2011, 07:08 AM

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.

05-14-2011, 09:20 AM
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.

Alfonse Reinheart
05-14-2011, 01:54 PM
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.

05-14-2011, 02:40 PM
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. :)
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.