I am attempting to model a simple graphics pipeline (i.e. Local->Word->View->Screen->2D spaces).
Although I have been using U,V,N,C notation rather than Xc,Yc,Zc,e.
And my matrix transformation at first glance, appears correct: When I plot the vectors onto world space, I get a vector from the camera position to the point we're looking at, and two correctly placed new X and Y vectors (see the left hand image below).
By looking along the line of sight vector (such that it disappears,and the focus & camera points overlap), as in the middle figure, should (I believe?) give an accurate example of what the resultant viewspace transformation should look like, with the green vertical and right-pointing vectors showing the new X & Y vectors.
However - when I actually do the view space transform I don't get the middle figure, instead, the resultant transform seems to be looking at the object from a different position possibly aligned with one of the axes? This is shown in the right hand figure - which is actually looking at the object from a slightly lower perspective
My question is essentially what have I done wrong, or am I misunderstanding both the transform and thus my results?
Thanks very much,
After stepping through the code - I discovered that my source book was having me convert the line-of-sight vector to spherical coords and then they were being converted straight back again to create R (viewspace coordination matrix). I've eliminated this redundancy (and potential source of errors) but it hasn't solved the problem...
clear; clc; close all; %======Create World Space (hard-coded values for demo) =========== ws_vtx = [0,2,0,2,1,0.5,1.5,0.5,1.5,0.5,1.5,0.5,1.5; 0,0,0,0,-2,0,0,2,2,0,0,2,2; 0,0,2,2,1,0.5,0.5,0.5,0.5,1.5,1.5,1.5,1.5]; ws_fcs = [1,2,4,3,3,1,6,6,6,6,7,7,9,8,8,8,10,10; 2,4,3,1,4,4,9,8,7,11,9,13,8,12,10,6,11,13; 5,5,5,5,1,2,7,9,11,10,13,11,13,13,12,10,13,12]; %==================Create view matrix=================== focus = [1.5,0,1.5]; %The point we're looking at Cx = 3; Cy = -3; Cz = 3; %Position of camera Vspec = [0;0;1]; %Specified up direction for view space (i.e. camera orientation) vector = focus - [Cx,Cy,Cz]; %Vector camera to focus point p = sqrt(vector(1)^2 + vector(2)^2 + vector(3)^2); %Total magnitude of vector N = vector'/p; V = Vspec - cross(cross(Vspec,N),N); %Create new "up" direction U = cross(N,V); %Create new x-axis %Create rotational matrix to view space R= [U(1),U(2),U(3),0; % U is direction of camera space X axis V(1),V(2),V(3),0; % V is direction of camera space Y axis N(1),N(2),N(3),0; 0 , 0, 0,1]; Tr = [1,0,0,-Cx; 0,1,0,-Cy; 0,0,1,-Cz; 0,0,0, 1]; T = R*Tr; %Total view transform = rotation * translation %============Plot the camera vectors & World Space================= grid on; hold on; xlabel('x'); ylabel('y'); zlabel('z'); scatter3(Cx,Cy,Cz,'s'); %Plot the camera position plot3([Cx, Cx+p*N(1)],[Cy, Cy+p*N(2)],[Cz, Cz+p*N(3)],'g'); %Plot camera vectors plot3([Cx, Cx+V(1)],[Cy, Cy+V(2)],[Cz, Cz+V(3)],'g'); plot3([Cx, Cx+U(1)],[Cy, Cy+U(2)],[Cz, Cz+U(3)],'g'); scatter3(ws_vtx(1,:),ws_vtx(2,:),ws_vtx(3,:)) %Plot all the points patch('Faces',ws_fcs','Vertices',ws_vtx','Facecolor', 'none'); for i = 1:length(ws_vtx) str = sprintf('%d',i); text(ws_vtx(1,i),ws_vtx(2,i),ws_vtx(3,i), str,'FontSize',14, 'Color','r'); end %====================Viewspace Transform=========================== ws_vtx = T*vertcat(ws_vtx,(ones(1,length(ws_vtx)))); %Transform worldspace ws_vtx = ws_vtx(1:3,:); %Remove homogenous column position = T*[Cx;Cy;Cz;1]; focus = T*horzcat(focus,1)'; %Transform set points position = position(1:3,:); focus = focus(1:3,:); %Remove homogenous columns %================Plot new viewspace=============================== figure(); grid on; hold on; xlabel('x'); ylabel('y'); zlabel('z'); scatter3(position(1),position(2),position(3),'s'); %Plot new camera vectors scatter3([position(1); position(1)+focus(1)],... [position(2); position(2)+focus(2)],[position(3); position(3)+focus(3)],'s'); plot3([position(1), focus(1)],[position(2), focus(2)],[position(3), focus(3)],'g'); patch('Faces',ws_fcs','Vertices',ws_vtx','Facecolor', 'none');