![]() |
![]() |
![]() |
凹面を含む図形(トーラス) |
法線ベクトル法による表示 表示が失敗している |
Zソート法による表示 本来表示されるべき状態 |
public void zSort(Graphics g,Shape shape) {
g.setColor(Color.white);
g.fillRect(0, 0, screenW, screenH);
//面の重心から視点の距離を求める
double[] dist=new double[shape.faces.length];
for(int i=0;i<shape.faces.length;i++) {
double[] fg=new double[3];
fg=viewOrtho(shape.fg[i]);
dist[i]=(fg[0]-vp[0])*(fg[0]-vp[0])+
(fg[1]-vp[1])*(fg[1]-vp[1])+
(fg[2]-vp[2])*(fg[2]-vp[2]);
}
int[] faceList=new int[shape.faces.length];
for(int i=0;i<shape.faces.length;i++) {
faceList[i]=i;
}
for(int i=0;i<shape.faces.length-1;i++) {
for(int j=i+1;j<shape.faces.length;j++) {
if(dist[i]<dist[j]) {
double wd=dist[i];
dist[i]=dist[j];
dist[j]=wd;
int wl=faceList[i];
faceList[i]=faceList[j];
faceList[j]=wl;
}
}
}
double[] p=new double[3];
int[] px=new int[shape.faces[0].length];
int[] py=new int[shape.faces[0].length];
for(int i=0;i<shape.faces.length;i++) {
for(int j=0;j<shape.faces[i].length;j++) {
p=viewOrtho(shape.vertices[shape.faces[faceList[i]][j]]);
px[j]=(int)p[0];
py[j]=(int)p[1];
}
g.setColor(c);
g.fillPolygon(px, py, shape.faces[i].length );
}
}