Last active
November 20, 2016 01:15
-
-
Save turingbirds/590ff17d88fe4a48ce373fc6839e69a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function camera_matrix = lookAt(eyePos, target, up) | |
% lookAt function for right-handed coordinate system | |
% Returns the 4x4 camera matrix that maps homogeneous 3D world coordinates | |
% (xW, yW, zW, W) to homogeneous 2D view coordinates (xV, yV, V) | |
zaxis = eyePos - target; % The "forward" vector. | |
zaxis = zaxis / norm(zaxis); | |
xaxis = cross(up, zaxis); % The "right" vector. | |
xaxis = xaxis / norm(xaxis); | |
yaxis = cross(zaxis, xaxis); % The "up" vector. | |
orientation = [xaxis(1), yaxis(1), zaxis(1), 0; | |
xaxis(2), yaxis(2), zaxis(2), 0; | |
xaxis(3), yaxis(3), zaxis(3), 0; | |
0, 0, 0, 1]; | |
translation = eye(4); | |
translation(4,1:3) = -eyePos; | |
camera_matrix = translation * orientation; | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% eye = [...]; | |
target = [0 0 0]; | |
up = [0 0 1]; | |
% | |
% generate some dummy data | |
% | |
figure(1); | |
clf; | |
subplot(1, 2, 1); | |
scatter3(0,0,0,'x'); | |
hold on; | |
n_pts = 100; | |
points = zeros(3, n_pts); | |
for i = 1:n_pts | |
points(:, i) = [cos(i/n_pts*2*pi), .25 * rand(), sin(i/n_pts*2*pi)]; | |
scatter3(points(1, i), points(2, i), points(3, i)); | |
end | |
xlabel('X'); | |
ylabel('Y'); | |
zlabel('Z'); | |
daspect([1 1 1]); | |
xlim([-4, 4]); | |
ylim([-4, 4]); | |
% | |
% animate the camera | |
% | |
n_camera_pos = 32; | |
filename1 = 'camera_anim.gif'; | |
clear imind | |
for i = 1:n_camera_pos | |
eye = [4 * sin((i-1)/n_camera_pos*2*pi), 3 * cos((i-1)/n_camera_pos*2*pi), 1]; | |
subplot(1, 2, 1); | |
scatter3(eye(1), eye(2), eye(3), '+'); | |
hold on | |
camera_matrix = lookAt(eye, target, up); | |
camera_matrix = camera_matrix(:,1:3); | |
subplot(1, 2, 2); | |
cla | |
for j = 1:n_pts | |
p = camera_matrix' * [points(1, j) points(2, j) points(3, j) 1]'; | |
hold on | |
scatter(p(1) / p(3), p(2) / p(3)); | |
end | |
daspect([1 1 1]); | |
xlim([-1 1]) | |
ylim([-1 1]) | |
drawnow | |
frame = getframe(1); | |
im = frame2im(frame); | |
[im, cm] = rgb2ind(im, 256); | |
if i == 1 | |
imwrite(im, cm, filename, 'gif', 'writemode', 'overwrite', 'LoopCount', inf, 'DelayTime', .1); | |
else | |
imwrite(im, cm, filename, 'gif', 'writemode','append', 'DelayTime', .1); | |
end | |
% pause(.1); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment