首页 > 解决方案 > 挣扎于while循环(计算机图形学)

问题描述

做一个与计算机图形有关的小项目。到目前为止,我已经(我认为)一切都井井有条,因为下面的代码可以很好地将我的世界修补到屏幕上。

但是,我还有最后一个障碍要跳过:我希望代码能够针对不同的 theta 值进行修补。在代码中,它设置为 2*pi/4,但我想迭代和修补 0:pi/4:2*pi 之间的每个角度。但是,当我尝试将代码放在 a fororwhile循环中时,它似乎并没有达到我的预期,即先修补一个角度,然后再修补另一个角度,等等。

真的卡住了我已经尝试了很多东西,现在我只是没有任何想法。非常感谢任何帮助或建议。

function world()
% Defining House Vertices 

house_verts = [-5, 0, -5;
    5, 0, -5;
    5, 10, -5;
    0,15,-5;
    -5,10,-5;
    -5,0,5;
    5,0,5;
    5,10,5;
    0,15,5;
    -5,10,5];        

% Sorting out the homeogenous co-ordinates
ones = [1,1,1,1,1,1,1,1,1,1];
ones=transpose(ones);
house_verts = [house_verts, ones];
house_verts = transpose(house_verts);

% House faces
house_faces = [1,2,3,4,5;
    2,7,8,3,3;
    6,7,8,9,10;
    1,6,10,5,5;
    3,4,9,8,8;
    4,5,10,9,9;
    1,2,7,6,6];

world_pos = [];

% creating a street
street_vector = [1,0,1]; % the direction of the street
orthog_street_vector = [-1,0,1];

for i = 1:15
   % current_pos1 and 2 will be the positions of the two houses
   % opposite each other on the street
   current_pos1 = 30*i*street_vector + 50*orthog_street_vector;
   current_pos2 = 30*i*street_vector - 50*orthog_street_vector;
   world_pos = [world_pos;current_pos1;current_pos2];
end

% initialising world vertices and faces
world_verts = [];
world_faces = [];

% Populating the street
for i =1:size(world_pos,1)
    T = transmatrix(world_pos(i,:)); % a translation matrix
    s = [1,1/2 + rand(),1];
    S=scalmatrix(s); % a matrix for a random scaling of the height (y-coordinate)
    Ry = rotymatrix(rand()*2*pi); % a matrix for a random rotation about the y-axis
    A = T*Ry*S; % the compound transformation matrix to take the house into the world
    obj_faces = size(world_verts,2) + house_faces; %increments the basic house faces to match the current object
    obj_verts = A*house_verts;

    world_verts = [world_verts, obj_verts]; % adds the vertices to the world
    world_faces = [world_faces; obj_faces]; % adds the faces to the world
end

% initialising aligned vertices
align_verts = [];

% Aligning the vertices to the particular camera at angle theta
for elm = world_verts
    x = 350 + 350*cos(2*pi/4);
    z = 350 + 350*sin(2*pi/4);
    y = 80;

    u = [x,y,z];
    v = [250,0,250];


    d = v - u;
    phiy = atan2(d(1),d(3));
    phix = -atan2(d(2),sqrt(d(1)^2+d(3)^2));

    T = transmatrix([-u(1),-u(2),-u(3)]);
    Ry = rotymatrix(phiy);
    Rx = rotxmatrix(phix);  
    A = Rx*Ry*T; 
    align_verts = [align_verts, A*elm];
end


% initialising projected vertices 
proj_verts=[];

% Performing the projection
for elm = align_verts
    proj = projmatrix(10);
    projverts = proj*elm;
    projverts = ((10/projverts(3))*projverts);
    proj_verts = [proj_verts,projverts]; 
end

% Displaying the world
for i = 1:size(world_faces,1)
    for j = 1:size(world_faces,2)
        x(j) =proj_verts(1,world_faces(i,j));
        z(j) = proj_verts(2,world_faces(i,j));
    end
    patch(x,z,'w')
end
end



function T = transmatrix(p)
T = [1 0 0 p(1) ; 0 1 0 p(2) ; 0 0 1 p(3) ; 0 0 0 1];
end

function S = scalmatrix(s)
S = [s(1) 0 0 0 ; 0 s(2) 0 0 ; 0 0 s(3) 0 ; 0 0 0 1];
end

function Ry = rotymatrix(theta)
Ry = [cos(theta), 0, -sin(theta),0;
    0,1,0,0;
    sin(theta),0,cos(theta),0;
    0,0,0,1];
end

function Rx = rotxmatrix(phi)
Rx = [1,     0,          0,     0;
      0,  cos(phi),  -sin(phi), 0;
      0,  sin(phi),   cos(phi), 0;
      0,     0,          0,     1];
end

function P = projmatrix(f)
P = [1,0,0,0
     0,1,0,0
     0,0,1,0
     0,0,1/f,0];
end

更新的代码:设法让循环工作,但现在有一些我不明白的错误,当它再次进行完全旋转时,任何帮助都会很棒。

function world()
% Defining House Vertices 

house_verts = [-5, 0, -5;
    5, 0, -5;
    5, 10, -5;
    0,15,-5;
    -5,10,-5;
    -5,0,5;
    5,0,5;
    5,10,5;
    0,15,5;
    -5,10,5]; 

% Sorting out the homeogenous co-ordinates
ones = [1,1,1,1,1,1,1,1,1,1];
ones=transpose(ones);
house_verts = [house_verts, ones];
house_verts = transpose(house_verts);

% House faces
house_faces = [1,2,3,4,5;
    2,7,8,3,3;
    6,7,8,9,10;
    1,6,10,5,5;
    3,4,9,8,8;
    4,5,10,9,9;
    1,2,7,6,6];

world_pos = [];

% creating a street
street_vector = [1,0,1]; % the direction of the street
orthog_street_vector = [-1,0,1];

for i = 1:15
   % current_pos1 and 2 will be the positions of the two houses
   % opposite each other on the street
   current_pos1 = 30*i*street_vector + 50*orthog_street_vector;
   current_pos2 = 30*i*street_vector - 50*orthog_street_vector;
   world_pos = [world_pos;current_pos1;current_pos2];
end

% initialising world vertices and faces
world_verts = [];
world_faces = [];

% Populating the street
for i =1:size(world_pos,1)
    T = transmatrix(world_pos(i,:)); % a translation matrix
    s = [1,1/2 + rand(),1];
    S=scalmatrix(s); % a matrix for a random scaling of the height (y-coordinate)
    Ry = rotymatrix(rand()*2*pi); % a matrix for a random rotation about the y-axis
    A = T*Ry*S; % the compound transformation matrix to take the house into the world
    obj_faces = size(world_verts,2) + house_faces; %increments the basic house faces to match the current object
    obj_verts = A*house_verts;

    world_verts = [world_verts, obj_verts]; % adds the vertices to the world
    world_faces = [world_faces; obj_faces]; % adds the faces to the world
end

% initialising aligned vertices
align_verts = [];
% initialising projected vertices 
proj_verts=[];
% Aligning the vertices to the particular camera at angle theta
theta = 0;
t = 0;
while t < 100
    proj_verts=[];
    align_verts = [];
    for elm = world_verts
        x = 300 + 300*cos(theta);
        z = 300 + 300*sin(theta);
        y = 80;

        u = [x,y,z];
        v = [200,0,200];


        d = v - u;
        phiy = atan2(d(1),d(3));
        phix = -atan2(d(2),sqrt(d(1)^2+d(3)^2));

        T = transmatrix([-u(1),-u(2),-u(3)]);
        Ry = rotymatrix(phiy);
        Rx = rotxmatrix(phix);  
        A = Rx*Ry*T; 
        align_verts = [align_verts, A*elm];
    end

    % Performing the projection
    for elm = align_verts
        proj = projmatrix(6);
        projverts = proj*elm;
        projverts = ((6/projverts(3))*projverts);
        proj_verts = [proj_verts,projverts]; 
    end

    % Displaying the world
    for i = 1:size(world_faces,1)
        for j = 1:size(world_faces,2)
            x(j) = proj_verts(1,world_faces(i,j));
            z(j) = proj_verts(2,world_faces(i,j));
        end
        patch(x,z,'w')
        pbaspect([1,1,1]); % adjusts the aspect ratio of the figure

    end


title('Projected Space', 'fontsize', 16, 'interpreter', 'latex')
xlabel('$x$', 'fontsize', 16, 'interpreter', 'latex')
ylabel('$z$', 'fontsize', 16, 'interpreter', 'latex')
zlabel('$y$', 'fontsize', 16, 'interpreter', 'latex')

axis([-5,5,-5,2,0,5]) % sets the axes limits 
view(0,89)
pause(0.0000001)
theta = theta + 0.01;
clf
end

end



function T = transmatrix(p)
T = [1 0 0 p(1) ; 0 1 0 p(2) ; 0 0 1 p(3) ; 0 0 0 1];
end

function S = scalmatrix(s)
S = [s(1) 0 0 0 ; 0 s(2) 0 0 ; 0 0 s(3) 0 ; 0 0 0 1];
end

function Ry = rotymatrix(theta)
Ry = [cos(theta), 0, -sin(theta),0;
    0,1,0,0;
    sin(theta),0,cos(theta),0;
    0,0,0,1];
end

function Rx = rotxmatrix(phi)
Rx = [1,     0,          0,     0;
      0,  cos(phi),  -sin(phi), 0;
      0,  sin(phi),   cos(phi), 0;
      0,     0,          0,     1];
end

function P = projmatrix(f)
P = [1,0,0,0
     0,1,0,0
     0,0,1,0
     0,0,1/f,0];
end

标签: matlabwhile-loop

解决方案


推荐阅读