shader版龙书第20章,射线与三角形相交,作者为了显示相交,会对点击到的三角形进行单独Solid渲染一次,而整体用Wireframe渲染。
对于D3DXIntersect函数用来检测射线和网格的相交情况。
它有个参数,FaceIndex:
[out] Pointer to an index value of the face closest to the ray origin
(指向最靠近射线起源的面(三角形)索引值。)
作者从网格得到顶点缓冲,索引缓冲,准备渲染。样式为:
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, pMesh->GetNumVertices(), FaceIndex*3, 1);
该方法有个争议参数,NumVertices;
NumVertices[in] Number of vertices used during this call. The first vertex is located at index: BaseVertexIndex + MinIndex.
(在调用期间要使用的顶点数。第一个顶点是基于:BaseVertexIndex+MinIndex)
我在仿写作者的DEMO时,写的为:
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 3, FaceIndex*3, 1);
因为要只会显示一个三角形,所以,我将NumVertices设为3;
结果,是渲染不出。
如果将争议参数NumVertiecs设置为整个网格的顶点数-》pMesh->GetNumVertices();
结果,可以渲染。
可见,该参数不是SDK中描述的那样。
。。。。。
按照参数描述我的理解又为:
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 3, pMesh->GetNumVertices(), 1);
结果,渲染和点击是不统一的。这不对。
为什么FaceIndex要乘上3呢?