您好,我只是想在Matlab中编写一个背面剔除功能,因为我不确定如何最好地获取多边形列表,因此很难。
我希望能够从“顶点矩阵”和“面矩阵”开始,并以某种方式获得一种结构,在该结构中,我有一个对象之类的列表,其中包含可以迭代的多边形。下面是我对特定已知矩阵V和F非常幼稚的方法。
%Vertices Matrix
V = [0.5,-0.86,0.36,-0.5;
-0.5,-0.36,0.86,0.05;
10.71,9.65,9.65,10.71;];
% Faces Matrix
F = [1,2,3;
2,4,3;
3,4,1;
1,4,2;];
% Camera Position
P = [1,1,2]';
% Getting Size of F to inform us of the number of polygons
% Currently useless as the program is very specific
[n,m] = size(F);
% Defining Polygons from Faces and vertices
Poly1 = [V(:,1),V(:,2),V(:,3)];
Poly2 = [V(:,2),V(:,4),V(:,3)];
Poly3 = [V(:,3),V(:,4),V(:,1)];
Poly4 = [V(:,1),V(:,4),V(:,2)];
% Trying to create some sort of polygon list not sure how to automate
% this from just V and F.
Polygons = [];
Polygons(:,:,1) = Poly1;
Polygons(:,:,2) = Poly2;
Polygons(:,:,3) = Poly3;
Polygons(:,:,4) = Poly4;
% Call Function
BackFaceCull(Polygons,P,n)
function F = BackFaceCull(Polygons,P,n)
% Front Facing Polygons
F = [];
% So we can label Polygons A-Z (Though in practise we would not use
% However for my use will be very simple set ups with less than 25
% Objects
k = 65;
for i = 1:n
% Create each point on polygon
Polygon = Polygons(:,:,i);
P1 = Polygon(:,1);
P2 = Polygon(:,2);
P3 = Polygon(:,3);
% Find the Normal for this polygon
N = cross((P2 -P1),(P3-P2));
% Find the centre of the polygon ( Triangle) What if it's not a
% triangle?
C = Centroid(P1,P2,P3);
% Apply Test for if forward facing if so add associated polygon
%letter to Front list
if dot((C-P),N) < 0
F = [F char(k)];
end
% update k
k = k+1;
end
end
function C = Centroid(p1,p2,p3)
P1 = (p1(1)+p2(1)+p3(1))/3;
P2 = (p1(2)+p2(2)+p3(2))/3;
P3 = (p1(3)+p2(3)+p3(3))/3;
C = [P1,P2,P3]';
end
TLDR:我要如何在Matlab中实现背面剔除算法,前提是矩阵V包含所有顶点,矩阵F包含绘制顶点的顶点。