# gl_ClipDistance

Hi!
I do this on a moving shipmodel:

``````gl_ClipDistance = dot(gl_Vertex * modelMatrix, cPlane);
``````

`cPlane` is a vector4f that I send in as a `uniform vec4(0.0, 1.0, 0.0 0.0)`.
The clipplane seems to be stuck in the object space and follows the moving ships movements and do not stay at the world pos.
Any idea what’s wrong?

Cheers

Either reverse the order of the multiplication or transpose the matrix. OpenGL uses the usual mathematical convention of multiplying with the matrix on the left and a column vector on the right, not the DirectX convention (row vector on the left, matrix on the right). The two are equivalent up to transposition of the matrix: (M.x)T = xT·MT. Matrices generated by GLM or similar will use the OpenGL convention.

If that doesn’t fix it, check that you’re using the correct matrix and that it contains what you expect.

So I imagine some matrix is wrong.
The shader is applied on a ship model which rolls/pitches in the water, and the cPlane is the flat water plane.
Which matrix should be applied on the gl_Vertex and/or cPlane?

Thanks for the help!

Assuming that you’re calculating gl_Position as:

``````gl_Position = projectionMatrix * viewMatrix * modelMatrix * gl_Vertex;
``````

then it should be

``````gl_ClipDistance = dot(modelMatrix * gl_Vertex, cPlane);
``````

where `cPlane` is in the coordinate system to which `modelMatrix` transforms (presumably world space).

Yeah, something must be wrong with my modelMatrix.
Let’s say I would define the cPlane as vec4(0.0, 1.0, 0.0, 0.0) directly in the shader, the code would still be the same right?

But in that case, it would affect the model itself, not just the clip plane.

Although, it’s possible that transformations which should be part of the view matrix are part of the model matrix. Without the clip plane, the individual matrices wouldn’t matter, only their product. So there wouldn’t be any difference between `view=A*B,model=C` and `view=A,model=B*C`, as both would give `view*model=A*B*C`.

Yes.

I wonder if such behavior would work fine on some ATI graphic card. I well remember the clip plane intrinsic did not work on some ATI hardware, and I made a pure programmable shader to solve that. So just a reminder, beware the compatible issues.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.