5

c#とdirectxを使用してポリゴンを描画しようとしています

私が得るのはファイルからのポイントの順序付きリストだけであり、3Dワールドでフラットポリゴンを描画する必要があります。

三角形のファンとdrawuserprimitivesを使用して、ポイントをロードし、凸形状を描画できます。

ポリゴンが非常に凹面である場合(これは明らかに不正確な結果につながります)。

この問題に取り組むのは私だけだとは想像できません(私はgfx /directxの新人です-私のバックグラウンドはgui\windowsアプリケーション開発です)。

誰かが私を助けるかもしれないリソース\チュートリアル\アルゴリズムに従う簡単なものに私を向けることができますか?

4

4 に答える 4

4

Direct3D can only draw triangles (well, it can draw lines and points as well, but that's besides the point). So if you want to draw any shape that is more complex than a triangle, you have to draw a bunch of touching triangles that equal to that shape.

In your case, it's a concave polygon triangulation problem. Given a bunch of vertices, you can keep them as is, you just need to compute the "index buffer" (in simplest case, three indices per triangle that say which vertices the triangle uses). Then draw that by putting into vertex/index buffers or using DrawUserPrimitives.

Some algorithms for triangulating simple (convex or concave, but without self-intersections or holes) polygons are at VTerrain site.

I have used Ratcliff's code in the past; very simple and works well. VTerrain has a dead link to it; the code can be found here. It's C++, but porting that over to C# should be straightforward.

Oh, and don't use triangle fans. They are of very limited use, inefficient and are going away soon (e.g. Direct3D 10 does not support them anymore). Just use triangle lists.

于 2008-09-18T09:35:01.463 に答える
3

三角測量は彼の明白な答えですが、堅実な三角測量機を書くのは難しいです。無駄にする 2 か月の時間がない限り、試してはいけません。

役立つコードがいくつかあります。

GPC ライブラリ。非常に使いやすいですが、ライセンスが気に入らないかもしれません:

http://www.cs.man.ac.uk/~toby/alan/software/gpc.html

三角形もあります:

http://www.cs.cmu.edu/~quake/triangle.html

そして拳:

http://www.cosy.sbg.ac.at/~held/projects/triang/triang.html

別の (そして私が好む) オプションは、GLU テッセレータを使用することです。DirectX プログラムから GLU ライブラリを読み込んで使用することができます。それを使用するために OpenGL コンテキストは必要なく、すべての Windows マシンにプリインストールされています。ソースが必要な場合は、SGI 参照実装から三角測量コードを持ち上げることができます。私はそれを一度やっただけで、数時間しかかかりませんでした。

ここまで三角測量。別の方法もあります。ステンシル トリックを使用できます。

一般的なアルゴリズムは次のようになります。

  1. カラー書き込みと深度書き込みを無効にします。ステンシル書き込みを有効にし、現在のステンシル値を反転するようにステンシル バッファーを設定します。ステンシルは 1 ビットで十分です。ああ、ステンシル バッファもクリアする必要があります。

  2. 画面上のランダムなポイントを選択します。どれでも構いません。この点をアンカーと呼びます。

  3. ポリゴンの各エッジに対して、エッジとアンカーを構築する 2 つの頂点から三角形を構築します。その三角形を描きます。

  4. これらすべての三角形を描画したら、ステンシル書き込みをオフにし、ステンシル テストとカラー書き込みをオンにして、選択した色でフルスクリーン クワッドを描画します。これにより、凸多角形内のピクセルだけが塗りつぶされます。

アンカーを多角形の中央に配置し、多角形の境界ボックスと同じ大きさの長方形を描くことをお勧めします。これにより、フィルレートが少し節約されます。

ところで - ステンシル手法は、自己交差ポリゴンにも機能します。

お役に立てば幸いです、ニルス

于 2008-09-18T10:02:44.627 に答える
3

ステンシル バッファーを使用できる場合は、難しくはないはずです。一般的なアルゴリズムは次のとおりです。

Clear the stencil buffer to 1.
Pick an arbitrary vertex v0, probably somewhere near the polygon to reduce floating-point errors.
For each vertex v[i] of the polygon in clockwise order:
    let s be the segment v[i]->v[i+1] (where i+1 will wrap to 0 when the last vertex is reached)
    if v0 is to the "right" of s:
        draw a triangle defined by s, v[i], v[i+1] that adds 1 to the stencil buffer
    else
        draw a triangle defined by s, v[i], v[i+1] that subtracts 1 from the stencil buffer
end for
fill the screen with the desired color/texture, testing for stencil buffer values >= 2.

「s の右」とは、v[i] の上に立って v[i+1] に面している誰かの視点を意味します。これはクロス積を使用してテストできます。

cross(v0 - v[i], v[i+1] - v[i]) > 0

于 2008-12-07T00:26:42.477 に答える
0

私はプロジェクトのためにこれをしなければなりませんでした。私が見つけた最も単純なアルゴリズムは、「Ear Clipping」と呼ばれます。それに関する素晴らしい論文はここにあります: TriangulationByEarClipping.pdf

約 250 行の C++ コードと、ブルート フォース バージョンを実装するのに 4 時間かかりました。他のアルゴリズムの方がパフォーマンスは優れていますが、これは実装と理解が簡単でした。

于 2009-07-30T18:47:38.580 に答える