📄 object3d_persp1.m
字号:
% A simple program to demonstrate perspective projection
% on wire-frame objects.
close all;
clear all;
% make a model; first define the points...
cube.points = [ % a unit cube
[ 1/2 1/2 1/2 1]; % 1
[-1/2 1/2 1/2 1]; % 2
[-1/2 -1/2 1/2 1]; % 3
[ 1/2 -1/2 1/2 1]; % 4
[ 1/2 1/2 -1/2 1]; % 5
[-1/2 1/2 -1/2 1]; % 6
[-1/2 -1/2 -1/2 1]; % 7
[ 1/2 -1/2 -1/2 1]; % 8
]';
% now define the edges - using point indices
cube.edges = [
[1 2]; [2 3]; [3 4]; [4 1];...
[5 6]; [6 7]; [7 8]; [8 5];...
[1 5];...
[2 6];...
[3 7];...
[4 8]
];
% Make up a few (4x4) matrix transforms
% translation
t = [1 -2 6]';
T = [
1 0 0 t(1);
0 1 0 t(2);
0 0 1 t(3);
0 0 0 1;
];
% rotate about y
theta = pi/6;
Ry = [
cos(theta) 0 sin(theta) 0;
0 1 0 0;
-sin(theta) 0 cos(theta) 0;
0 0 0 1
];
% an overall scale
s = 10;
S = [
s 0 0 0;
0 s 0 0;
0 0 s 0;
0 0 0 1;
];
% perspective transform
P = [
1 0 0 0;
0 1 0 0;
0 0 1 0;
0 0 1 0;
];
% transform and project the cube
cube.image = P * S* T * Ry * cube.points;
% convert homogeneous coords to physical coords
cube.image = cube.image ./ repmat( cube.image(4,:), 4, 1 );
% draw the result
figure;
axis equal;
hold on;
%plot( 1/2,1/2,'w');
%plot( -1/2,-1/2,'w');
for i = 1:size(cube.edges,1)
% get the coordinates of the edge
p = [cube.image( :, cube.edges(i,1) ), cube.image( :, cube.edges(i,2) ) ];
% draw the egde
plot( p(1,:), p(2,:) );
end
% do it again
t = [0 0 5]';
T = [
1 0 0 t(1);
0 1 0 t(2);
0 0 1 t(3);
0 0 0 1;
];
cube.image = P * T *cube.points;
cube.image = cube.image ./ repmat( cube.image(4,:), 4, 1 );
for i = 1:size(cube.edges,1)
% get the coordinates of the edge
p = [cube.image( :, cube.edges(i,1) ), cube.image( :, cube.edges(i,2) ) ];
% draw the egde
plot( p(1,:), p(2,:) );
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -