Java で 3D レンダラーを作成していますが、ソリッド カラーの塗りつぶしでポリゴンをレンダリングしようとすると問題が発生します。それは完全に正常に動作しますが、頻繁に引き裂かれますが、それがアルゴリズムが非効率的であるためなのか、それとも頂点だけが引き裂かれているために何か他のものなのかはわかりません。ここに写真があります:
ワイヤーフレーム:
ポリゴンの頂点またはポイントの近くで引き裂かれていることがわかります。ピクセルの色を 2 次元配列に格納し、それを循環してレンダリングしています。ポリゴンを非常に小さくしても破れるので、パフォーマンスの問題ではないと思います。Bresham アルゴリズムを使用してピクセルを 2 次元配列に格納し、次にポリゴンでピクセルを取得して 1 つの大きな配列にし、ピクセルに到達するまで y を下ってから x を横切ります。それが beginLine として設定され、最後のものが endLine として設定されます。次に、ポイント間に線を引きます。
public void render()
{
int tempPixels[][] = new int[(int) Math.max(vertex_1.getX(), Math.max(vertex_2.getX(), vertex_3.getX())) + 30][(int) Math.max(vertex_1.getY(), Math.max(vertex_2.getY(), vertex_3.getY())) + 30];
for (int x = 0; x < vector_1.getWidth(); x++)
{
for (int y = 0; y < vector_1.getHeight(); y++)
{
if (vector_1.getPixels()[x][y] == 1)
{
tempPixels[(int) (x + Math.min(vertex_1.getX(), vertex_2.getX()))][(int) (y + Math.min(vertex_1.getY(), vertex_2.getY()))] = 1;
}
}
}
for (int x = 0; x < vector_2.getWidth(); x++)
{
for (int y = 0; y < vector_2.getHeight(); y++)
{
if (vector_2.getPixels()[x][y] == 1)
{
tempPixels[(int) (x + Math.min(vertex_2.getX(), vertex_3.getX()))][(int) (y + Math.min(vertex_2.getY(), vertex_3.getY()))] = 1;
}
}
}
for (int x = 0; x < vector_3.getWidth(); x++)
{
for (int y = 0; y < vector_3.getHeight(); y++)
{
if (vector_3.getPixels()[x][y] == 1)
{
tempPixels[(int) (x + Math.min(vertex_3.getX(), vertex_1.getX()))][(int) (y + Math.min(vertex_3.getY(), vertex_1.getY()))] = 1;
}
}
}
for (int y = 0; y < (int) Math.max(vertex_1.getY(), Math.max(vertex_2.getY(), vertex_3.getY())) + 4; y++)
{
int beginLine = -1;
int endLine = -1;
for (int x = 0; x < (int) Math.max(vertex_1.getX(), Math.max(vertex_2.getX(), vertex_3.getX())) + 4; x++)
{
if (tempPixels[x][y] == 1)
{
if (beginLine == -1)
{
beginLine = x;
}
else
{
endLine = x;
}
}
}
for (int i = beginLine; i < endLine; i++)
{
pixels[i][y] = 1;
colors[i][y] = Color.PINK;
}
}
vector_1.render();
vector_2.render();
vector_3.render();
vertex_1.render();
vertex_2.render();
vertex_3.render();
}
したがって、基本的に私の質問は次のとおりです。このアルゴリズムは非効率的ですか?もしそうなら、より良い方法は何でしょうか? 頂点の近くで裂けているのはなぜですか?