Доброе утро.
C++, Directx 9.
Вычислен номер треугольного полигона (даже номера всех таких полигонов), которые пересекаются с лучом, испущенным мышью (ортогонально экрану), записаны в буфере ID3DXBuffer * InterBuffer размера DWORD numInterBuffer.
Необходимо теперь "подсветить" ближайший пересеченный полигон (его номер записан в DWORD FaceIndex) меша Mesh.
Вот http://www.mdxinfo.com/tutorials/picking.php предложена такая функция "подсветки" на C#:
| Код | private void HighlightIntersectedFace(IntersectInformation ii) { // create an array to hold the indices for the intersected face short[] intersectedIndices = new short[3]; // fetch indices for the intersected face from the mesh short[] indices = (short[])scannerMesh.LockIndexBuffer( typeof(short), LockFlags.ReadOnly, scannerMesh.NumberFaces * 3); Array.Copy( indices, ii.FaceIndex * 3, intersectedIndices, 0, 3 ); scannerMesh.UnlockIndexBuffer(); // NOTE: we could also use these indices to render the intersected // face, by simply reusing the vertexbuffer of the mesh for our // vertex stream source. We'll continue to extract this data below. // create an array to hold the vertices for the intersected face CustomVertex.PositionNormalTextured[] tempIntersectedVertices = new CustomVertex.PositionNormalTextured[3]; // extract vertex data from mesh, using our indices we obtained earlier CustomVertex.PositionNormalTextured[] meshVertices = (CustomVertex.PositionNormalTextured[])scannerMesh.LockVertexBuffer(typeof(CustomVertex.PositionNormalTextured), LockFlags.ReadOnly, scannerMesh.NumberVertices); tempIntersectedVertices[0] = meshVertices[ intersectedIndices[0] ]; tempIntersectedVertices[1] = meshVertices[ intersectedIndices[1] ]; tempIntersectedVertices[2] = meshVertices[ intersectedIndices[2] ]; scannerMesh.UnlockVertexBuffer(); // we'll wait till here before updating the vertices used // for rendering, so we don't render incomplete data this.intersectedVertices = tempIntersectedVertices; }
|
Проблема состоит в переводе этого кода на C++, так как никак не удается найти аналогичные типы данных и соответственно функции для них.
Вот начало того, что кое-как перевёл, да и то наверняка ошибочно:
| Код | void HighlightIntersectedFace(const ID3DXBuffer * InterBuffer, const DWORD numInterBuffer, DWORD FaceIndex) { short intersectedIndices[3]; short* indicies; Mesh -> LockIndexBuffer(D3DLOCK_READONLY, (LPVOID)indicies); for (int i = 3*FaceIndex; i < 3*(FaceIndex + 1); ++i) intersectedIndices[i] = indicies[i]; Mesh -> UnlockIndexBuffer(); // ... }
|
А вот с вершинами и иже с ними — проблема. Как понял, структура CUSTOMVERTEX объявляется самостоятельно, например, так:
| Код | struct CUSTOMVERTEX { D3DXVECTOR3 position; D3DXVECTOR3 normal; };
|
Но вот с помощью каких функций затронуть текстуры — непонятно.
И можно ли вместо текстуры менять материал полигона? Если да, то какими функциями?
Заранее благодарен.
P.S. В Directx SDK смотрел пример, но там все выглядит сложнее/запутаннее. |