1 | /* |
---|
2 | |
---|
3 | Brian Curless |
---|
4 | |
---|
5 | Computer Graphics Laboratory |
---|
6 | Stanford University |
---|
7 | |
---|
8 | --------------------------------------------------------------------- |
---|
9 | |
---|
10 | Copyright (1997) The Board of Trustees of the Leland Stanford Junior |
---|
11 | University. Except for commercial resale, lease, license or other |
---|
12 | commercial transactions, permission is hereby given to use, copy, |
---|
13 | modify this software for academic purposes only. No part of this |
---|
14 | software or any derivatives thereof may be used in the production of |
---|
15 | computer models for resale or for use in a commercial |
---|
16 | product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND |
---|
17 | CONCERNING THIS SOFTWARE. No support is implied or provided. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | #include "perspective.h" |
---|
23 | #include "vrip.h" |
---|
24 | #include "configure.h" |
---|
25 | #include "vripGlobals.h" |
---|
26 | |
---|
27 | |
---|
28 | Vec3f thePerspViewRay; |
---|
29 | Vec3f theCOP; |
---|
30 | OrthoShear *thePerspShear; |
---|
31 | float theFocalLength; |
---|
32 | |
---|
33 | OrthoShear * |
---|
34 | initPersp(Quaternion &quat, Vec3f &trans, Vec3f ¢er, |
---|
35 | float resolution) |
---|
36 | { |
---|
37 | Vec3f vec, dir, ctr; |
---|
38 | Matrix4f mrot, mtrans, mpermute, mflipz; |
---|
39 | Matrix4f mrotz, mtransyz, mshear, mprod; |
---|
40 | Matrix4f mrotzinv, mtransyzinv, mshearinv; |
---|
41 | float dot; |
---|
42 | Matrix4f mrotx, mrotxinv; |
---|
43 | |
---|
44 | ctr = center; |
---|
45 | thePerspViewRay.setValue(PerspectiveDir); |
---|
46 | theCOP.setValue(PerspectiveCOP); |
---|
47 | |
---|
48 | // Set up the pose matrices |
---|
49 | quat.toMatrix(mrot); |
---|
50 | mtrans.makeIdentity(); |
---|
51 | mtrans.translate(trans); |
---|
52 | |
---|
53 | // Transform the COP and center ray |
---|
54 | mrot.multVec(thePerspViewRay, vec); |
---|
55 | thePerspViewRay.setValue(vec); |
---|
56 | mrot.multVec(theCOP, vec); |
---|
57 | theCOP.setValue(vec); |
---|
58 | theCOP += trans; |
---|
59 | |
---|
60 | if (Verbose) { |
---|
61 | printf("\nCOP: (%f, %f, %f)\n", theCOP.x, theCOP.y, theCOP.z); |
---|
62 | printf("View direction: (%f, %f, %f)\n", thePerspViewRay.x, thePerspViewRay.y, thePerspViewRay.z); |
---|
63 | } |
---|
64 | |
---|
65 | vec3f rayDir; |
---|
66 | rayDir[0] = thePerspViewRay.x; |
---|
67 | rayDir[1] = thePerspViewRay.y; |
---|
68 | rayDir[2] = thePerspViewRay.z; |
---|
69 | OrthoShear *shear = computeShear(rayDir); |
---|
70 | thePerspShear = shear; |
---|
71 | |
---|
72 | // Set up permutation matrix |
---|
73 | |
---|
74 | mpermute.makeIdentity(); |
---|
75 | if (shear->axis == X_AXIS) { |
---|
76 | mpermute.m[0][0] = 0; |
---|
77 | mpermute.m[0][2] = 1; |
---|
78 | mpermute.m[2][2] = 0; |
---|
79 | mpermute.m[2][0] = 1; |
---|
80 | } |
---|
81 | else if (shear->axis == Y_AXIS) { |
---|
82 | mpermute.m[1][1] = 0; |
---|
83 | mpermute.m[1][2] = 1; |
---|
84 | mpermute.m[2][2] = 0; |
---|
85 | mpermute.m[2][1] = 1; |
---|
86 | } |
---|
87 | |
---|
88 | // Update the COP and center of image plane in the new coordinate system |
---|
89 | mpermute.multVec(theCOP, vec); |
---|
90 | theCOP.setValue(vec); |
---|
91 | mpermute.multVec(ctr, vec); |
---|
92 | ctr.setValue(vec); |
---|
93 | |
---|
94 | // Set up flip matrix |
---|
95 | mflipz.makeIdentity(); |
---|
96 | if (shear->flip) |
---|
97 | mflipz.m[2][2] = -1; |
---|
98 | |
---|
99 | |
---|
100 | // Update the COP and image center in the new flipped coordinate system |
---|
101 | mflipz.multVec(theCOP, vec); |
---|
102 | theCOP.setValue(vec); |
---|
103 | mflipz.multVec(ctr, vec); |
---|
104 | ctr.setValue(vec); |
---|
105 | |
---|
106 | Matrix4f msh; |
---|
107 | mshear.makeIdentity(); |
---|
108 | mshear.m[0][2] = shear->sx; |
---|
109 | mshear.m[1][2] = shear->sy; |
---|
110 | |
---|
111 | // Update the COP and image center |
---|
112 | mshear.multVec(theCOP, vec); |
---|
113 | theCOP.setValue(vec); |
---|
114 | mshear.multVec(ctr, vec); |
---|
115 | ctr.setValue(vec); |
---|
116 | |
---|
117 | theFocalLength = ctr.z - theCOP.z; |
---|
118 | |
---|
119 | if (Verbose) { |
---|
120 | printf("Axis = %d (x=%d, y=%d, z=%d), flip = %d, sx = %f, sy = %f\n", |
---|
121 | shear->axis, X_AXIS, Y_AXIS, Z_AXIS, shear->flip, shear->sx, shear->sy); |
---|
122 | printf("\nTransformed COP: (%f, %f, %f)\n", theCOP.x, theCOP.y, theCOP.z); |
---|
123 | printf("\nFocal length: %f\n", theFocalLength); |
---|
124 | } |
---|
125 | |
---|
126 | return shear; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | void |
---|
131 | applyPersp(Vec3f &vin, Vec3f &vout) { |
---|
132 | // apply shear |
---|
133 | vout.x = vin.x + vin.z*thePerspShear->sx; |
---|
134 | vout.y = vin.y + vin.z*thePerspShear->sy; |
---|
135 | |
---|
136 | // translate eye to origin |
---|
137 | vout.x = vout.x - theCOP.x; |
---|
138 | vout.y = vout.y - theCOP.y; |
---|
139 | |
---|
140 | // apply projection |
---|
141 | vout.x = vout.x*theFocalLength/(vin.z - theCOP.z); |
---|
142 | vout.y = vout.y*theFocalLength/(vin.z - theCOP.z); |
---|
143 | |
---|
144 | vout.z = vin.z; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | void |
---|
149 | applyInvPersp(Vec3f &vin, Vec3f &vout) |
---|
150 | { |
---|
151 | // un-project |
---|
152 | vout.x = vin.x*(vin.z - theCOP.z)/theFocalLength; |
---|
153 | vout.y = vin.y*(vin.z - theCOP.z)/theFocalLength; |
---|
154 | |
---|
155 | // undo translation of eye to origin |
---|
156 | vout.x = vout.x + theCOP.x; |
---|
157 | vout.y = vout.y + theCOP.y; |
---|
158 | |
---|
159 | // un-apply shear |
---|
160 | vout.x = vout.x - vin.z*thePerspShear->sx; |
---|
161 | vout.y = vout.y - vin.z*thePerspShear->sy; |
---|
162 | |
---|
163 | vout.z = vin.z; |
---|
164 | } |
---|
165 | |
---|