DirectX 9如何找到交点?

时间:2013-04-03 09:27:53

标签: delphi directx directx-9 jedi d3dx

我找到了交叉点与函数'D3DXIntersectTri'的距离。 现在,使用距离值,我如何找到分值?

IDE:Delphi - JEDI

语言:Pascal

DirectX 9

编辑: 实际上我有2个圆柱体,并且想要在三维中仅渲染相交的部分。见图: enter image description here

2 个答案:

答案 0 :(得分:1)

MSDN article中所述,您可以使用重心坐标计算点:

p = p1 + pU * (p2 - p1) + pV(p3 - p1)

答案 1 :(得分:0)

渲染到屏幕的某些部分是模板缓冲区的任务。除非你想从交集创建一个新的顶点缓冲区(可以通过剪切部分来创建,这并不容易),使用模板缓冲区会更有效。

模板缓冲区是一个保存整数值的缓冲区。您必须使用深度缓冲区创建它,指定正确的格式(例如D24S8)。然后,您可以指定何时丢弃像素。这是一个想法:

Clear stencil buffer to 0
Enable solid rendering
Enable stencil buffer
Set blend states to not draw anything (Souce: 0, Destination: 1)
Disable depth testing, enable backface culling
Set the following stencil states:
    CompareFunc to Always
    StencilRef to 1
    StencilWriteMask to 255
    StencilFail to Replace
    StencilPass to Replace
    //this will set value 1 to every pixel that will be drawn
Draw the first cylinder
Now set the following stencil states:
    CompareFunc to Equal
    StencilFail to Keep //this keeps the value where the stencil test fails
    StencilPass to Increment //this increments the value to 2 where stencil test passes
Draw the second cylinder
//Now there is a 2 in the stencil buffer where the cylinders intersect
Reset blend states
Reenable depth testing
Set StencilRef to 2 //render only pixels where stencil value == 2
Draw both cylinders

您可能需要在最后一次渲染过程之前将compare函数更改为GreaterEqual。如果像素重叠,则可能存在大于2的值。