fprintf( ‘————————————-\n’ );
fprintf( ‘Q 1\n’ );
fprintf( ‘————————————-\n’ );
nx = 640;
ny = 480;
T_cann_from_screen = [ 2/nx 0 -(1-1/nx);
0 -2/ny (1-1/ny);
0 0 1 ]
ptA = [ 10 35 1 ]’;
ptB = [ 50 340 1 ]’;
ptA_cann = T_cann_from_screen * ptA;
ptB_cann = T_cann_from_screen * ptB;
fprintf( ‘Click A = \n’)
fprintf( ‘[ %5.2f %5.2f ]^T\n’, ptA_cann(1:2) );
fprintf( ‘Click B = \n’)
fprintf( ‘[ %5.2f %5.2f ]^T\n’, ptB_cann(1:2) );
% Try this to convince yourself that its is correct:
% inv(T_cann_from_screen)
clear all;
fprintf( ‘————————————-\n’ );
fprintf( ‘Q 2\n’ );
fprintf( ‘————————————-\n’ );
eyeVec = [ 5 1 3 ]’;
atVec = [ -1 -1 -6 ]’;
upVec = [ 0 0 1]’;
gazeVec = atVec-eyeVec;
w = -gazeVec/norm(gazeVec,2);
u = cross( upVec, w )/norm(upVec,2);
v = cross( w, u );
fprintf( ‘u = \n’)
fprintf( ‘[ %5.2f %5.2f %5.2f ]^T\n’, u );
fprintf( ‘v = \n’)
fprintf( ‘[ %5.2f %5.2f %5.2f ]^T\n’, v );
fprintf( ‘w = \n’)
fprintf( ‘[ %5.2f %5.2f %5.2f ]^T\n’, w );
fprintf( ‘\n’)
M_v = [ u’ -dot(u,eyeVec);
v’ -dot(v,eyeVec);
w’ -dot(w,eyeVec);
0 0 0 1 ];
fprintf( ‘Therefore M_v = \n’)
fprintf( ‘%5.2f %5.2f %5.2f %5.2f\n’, (M_v)’ );
fprintf( ‘\n\n’)
clear all;
fprintf( ‘————————————-\n’ );
fprintf( ‘Q 3\n’ );
fprintf( ‘————————————-\n’ );
% Viewing frustum from question
n_p = -5;
l_p = -3;
b_p = -1;
r_p = l_p + 5;
t_p = b_p + 5;
f_p = n_p – 10;
% Open GL projection matrix set with glFrustum
MOpenGLProj = [ 2*abs(n_p)/(r_p-l_p) 0 (r_p+l_p)/(r_p-l_p) 0;
0 2*abs(n_p)/(r_p-l_p) (t_p+b_p)/(t_p-b_p) 0;
0 0 (abs(n_p)+abs(f_p))/(abs(n_p)-abs(f_p)) 2*abs(n_p)*abs(f_p)/(abs(n_p)-abs(f_p));
0 0 -1 0 ];
% Scene points to be projected
a1 = transpose([ 2 -1 -5 1 ]);
a2 = transpose([ -9 12 -15 1 ]);
% Points in homogenous canonical volume coordinates
a1Proj_homogeneous = MOpenGLProj * a1;
a2Proj_homogeneous = MOpenGLProj * a2;
% Points in cartesian canonical volume coordinates
a1Proj = a1Proj_homogeneous(1:3)/a1Proj_homogeneous(4);
a2Proj = a2Proj_homogeneous(1:3)/a2Proj_homogeneous(4);
fprintf( ‘Points in cartesian canonical viewing volume: \n’)
fprintf( ‘[ %5.2f %5.2f %5.2f ]^T\n’, a1Proj );
fprintf( ‘[ %5.2f %5.2f %5.2f ]^T\n’, a2Proj );
fprintf( ‘\n\n’)
clear all;