I am trying to nail down some details of my tessellation code. I determine the tessellation levels in the tessellation control shader by evaluating the length of each primitive’s edges in screen space. My equation to transform the normalized device coordinates stored in gl_in[n].gl_Position is as follows:

```
screenvertex[n] = BufferSize.xy * (1.0 + gl_in[n].gl_Position.xy / gl_in[n].gl_Position.w) / 2.0;
if (gl_in[n].gl_Position.z < 0.0f)
{
screenvertex[n].x *= -1.0f;
}
```

This works most of the time, but there are some camera angles at which the tessellation goes way too high, presumably because my edge length is wrong.

Conceptually I am having some difficult with the idea of vertex screen coordinates that are behind the camera, and how they should be handled.

If these calculations are not perfect, I will end up with moments where there is very dense subdivision, like this:

Not necessarily. If an endpoint lies on the Z_{eye}=0 plane, the line will extend to infinity in screen space. If an endpoint is close to that plane, the line will be extremely long.

Bear in mind that tessellation occurs prior to clipping (in fact, clipping might only exist conceptually; it’s possible to perform rasterisation without an explicit clipping step).

Also, this:

```
if (gl_in[n].gl_Position.z < 0.0f)
```

is probably wrong.

A point on the near plane satisfies Z_{clip}=-W_{clip}. Note that for a conventional projection matrix (e.g. `glFrustum`

or `glm::perpective`

), W_{clip} will be -Z_{eye}. So if you’re looking for points which are behind the viewpoint, the test is

```
if (gl_in[n].gl_Position.w > 0.0f)
```

In that situation, you can’t reasonably determine a suitable scale factor without actually clipping the edge to the top/bottom/left/right edges of the clip volume.

Also: tessellation produces roughly equal steps in eye space. If the patch is close (relative to its size) to the viewpoint, the steps will be quite unequal in NDC. If you choose the subdivisions based upon the farthest point, the tessellation will be coarse close to the viewpoint. If you choose the subdivisions based upon the nearest point, the number of subdivisions may be impractical and you have to deal with the case where the nearest point is actually behind the viewpoint.

1 Like