4
\$\begingroup\$

When does OpenGL divide by W? Is this done automatically within the vertex shader? It is my understanding that the vertex shader outputs the final vertex position, so I presume no transformations can be made after this point...

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Another thought: why does OpenGL divide by W automatically? Shouldn't this be left to developer to decide if it's needed in the shader? \$\endgroup\$
    – Edward
    Commented Nov 26, 2017 at 10:58
  • \$\begingroup\$ Should it be left to the developer to decide if they need clipping too? Or if they need window-space coordinates? Or any number of other transformations and fixed-function stuff used to render? \$\endgroup\$ Commented Nov 26, 2017 at 14:39
  • \$\begingroup\$ It divides by w automatically because that performs the perspective projection and that's what's generally needed for all vertices. It's effectively converting your 4D vertex to a 3D one in this step. If you don't want perspective projection, then make sure w is 1 on all your vertices. See tomdalling.com/blog/modern-opengl/… and sections 6.4 and 6.4 of 3D Math Primer for Graphics and Game Development (Dunn & Parberry). \$\endgroup\$
    – Doug Voss
    Commented Dec 30, 2017 at 20:08

1 Answer 1

4
\$\begingroup\$

It's somewhere between the vertex and fragment shader (after the geometry shader and right before the fragment shader if you want to be specific). Since the fragment shader runs for each drawn pixel, the positions have to be divided by W by then. The division is automatic, you don't need to do it yourself.

Even if you wanted to, doing transformations after this is simply not possible. The fragment shader already has a set of pixels it needs to draw, nothing can change that.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .