When using the perspective matrix in the vertex shader, should I write code to divide by w, or is this done automatically at a later stage?
The reason for my question is that I saw a lot of vertex shaders using:
gl_Position = matrix * pos;
which makes sense if there is a later stage that divides the vector with its w-component.
However, I never worked until I used the following in my vertex shader:
gl_Position = matrix * pos;
gl_Position = gl_Position / gl_Position.w;
Is the second example correct or may some other setting be missing?
Put another thing: which of the steps shown in OpenGL Vertex transform (first image) do I need to write to the vertex shader
I know for sure that the matrix ModelView and Projection belong (or merge the two). Converting a viewport is not part of the vertex shader, but what about divide by w?
My setup is simple triangles with coordinates inside [-1 1] for x / y / z.
The prospective Prospect matrix should project the coordinates from z = -1 to -10 on z = -1, x = [- 1,1], y = [- 1,1].
-1.0 0.0 0.0 0.0
0.0 -1.0 0.0 0.0
0.0 0.0 -1.2 -2.2
0.0 0.0 1.0 0.0
It was generated:
x = 2.0f * zNear / (xMax - xMin);
y = 2.0f * zNear / (yMax - yMin);
a = -(xMax + xMin) / (xMax - xMin);
b = -(yMax + yMin) / (yMax - yMin);
c = (zFar + zNear) / (zNear - zFar);
d = -(2.0f * zFar * zNear) / (zNear - zFar);
To make the matrix P:
x, 0, a, 0
0, y, b, 0
0, 0, c, d
0, 0, 1, 0;
Finally, I create the final matrix with the matrix = P * T, where T is the shift (0,0, -2)
I tried to do the math on the processor and it seems to work with expected results, however there I also do split w manually.
Update : solved, but you need to understand
( -1), .
, z- , .
, , .