问题
Given a slice plane in the world, I want triangles to be seen if they were on the positive side of the plane, and those who intersect with the plane are split and the part on positive plane can be seen too.
If I slice the triangles meshes every time when plane is changed, it may be very slow when there are tons of triangles, and texture coordinate will be re-generated too.
If I set the camera(view) and projection carefully, it may generate similar result, but this result is view dependent.
So, is there render techniques which can utilize clipping result from GPU to render the view independent cross section?
cross section like this:
回答1:
using shaders is this really easy. In Opengl/GLSL you just add an if
statement to fragment shader determining if your fragment is on the correct side of your plane and if not discard
it.
That will cut your object by your condition so you will get the contours. However you still have to handle the filling somehow possibly from depth buffer in second pass or by using stencil buffer but both are just single GL_QUAD
covering whole screen.
The math to do this is simple you need to define your half space. The simplest thing I can think of is:
p0=(x0,y0,z0)
- 3D point belonging to cutting plane
n0=(nx,ny,nz)
- normal to cutting plane pointing to wards viewed side
so any fragment p=(x,y,z)
is visible if
dot_product(p-p0,n0) >= 0.0
so
if (nx*(x-x0)+ny*(y-y0)+nz*(z-z0)<0.0f) discard;
Do not forget to disable GL_CULL_FACE
and using double sided matherials/lighting. Also all the coordinates must be in the same coordinate system !!!
来源:https://stackoverflow.com/questions/42992084/how-to-render-cross-section-without-slicing-the-mesh-geometry