1 36 37 40 41 43 class Matrix3D { 44 float xx, xy, xz, xo; 45 float yx, yy, yz, yo; 46 float zx, zy, zz, zo; 47 static final double pi = 3.14159265; 48 49 Matrix3D () { 50 xx = 1.0f; 51 yy = 1.0f; 52 zz = 1.0f; 53 } 54 55 void scale(float f) { 56 xx *= f; 57 xy *= f; 58 xz *= f; 59 xo *= f; 60 yx *= f; 61 yy *= f; 62 yz *= f; 63 yo *= f; 64 zx *= f; 65 zy *= f; 66 zz *= f; 67 zo *= f; 68 } 69 70 void scale(float xf, float yf, float zf) { 71 xx *= xf; 72 xy *= xf; 73 xz *= xf; 74 xo *= xf; 75 yx *= yf; 76 yy *= yf; 77 yz *= yf; 78 yo *= yf; 79 zx *= zf; 80 zy *= zf; 81 zz *= zf; 82 zo *= zf; 83 } 84 85 void translate(float x, float y, float z) { 86 xo += x; 87 yo += y; 88 zo += z; 89 } 90 91 void yrot(double theta) { 92 theta *= (pi / 180); 93 double ct = Math.cos(theta); 94 double st = Math.sin(theta); 95 96 float Nxx = (float) (xx * ct + zx * st); 97 float Nxy = (float) (xy * ct + zy * st); 98 float Nxz = (float) (xz * ct + zz * st); 99 float Nxo = (float) (xo * ct + zo * st); 100 101 float Nzx = (float) (zx * ct - xx * st); 102 float Nzy = (float) (zy * ct - xy * st); 103 float Nzz = (float) (zz * ct - xz * st); 104 float Nzo = (float) (zo * ct - xo * st); 105 106 xo = Nxo; 107 xx = Nxx; 108 xy = Nxy; 109 xz = Nxz; 110 zo = Nzo; 111 zx = Nzx; 112 zy = Nzy; 113 zz = Nzz; 114 } 115 116 void xrot(double theta) { 117 theta *= (pi / 180); 118 double ct = Math.cos(theta); 119 double st = Math.sin(theta); 120 121 float Nyx = (float) (yx * ct + zx * st); 122 float Nyy = (float) (yy * ct + zy * st); 123 float Nyz = (float) (yz * ct + zz * st); 124 float Nyo = (float) (yo * ct + zo * st); 125 126 float Nzx = (float) (zx * ct - yx * st); 127 float Nzy = (float) (zy * ct - yy * st); 128 float Nzz = (float) (zz * ct - yz * st); 129 float Nzo = (float) (zo * ct - yo * st); 130 131 yo = Nyo; 132 yx = Nyx; 133 yy = Nyy; 134 yz = Nyz; 135 zo = Nzo; 136 zx = Nzx; 137 zy = Nzy; 138 zz = Nzz; 139 } 140 141 void zrot(double theta) { 142 theta *= (pi / 180); 143 double ct = Math.cos(theta); 144 double st = Math.sin(theta); 145 146 float Nyx = (float) (yx * ct + xx * st); 147 float Nyy = (float) (yy * ct + xy * st); 148 float Nyz = (float) (yz * ct + xz * st); 149 float Nyo = (float) (yo * ct + xo * st); 150 151 float Nxx = (float) (xx * ct - yx * st); 152 float Nxy = (float) (xy * ct - yy * st); 153 float Nxz = (float) (xz * ct - yz * st); 154 float Nxo = (float) (xo * ct - yo * st); 155 156 yo = Nyo; 157 yx = Nyx; 158 yy = Nyy; 159 yz = Nyz; 160 xo = Nxo; 161 xx = Nxx; 162 xy = Nxy; 163 xz = Nxz; 164 } 165 166 void mult(Matrix3D rhs) { 167 float lxx = xx * rhs.xx + yx * rhs.xy + zx * rhs.xz; 168 float lxy = xy * rhs.xx + yy * rhs.xy + zy * rhs.xz; 169 float lxz = xz * rhs.xx + yz * rhs.xy + zz * rhs.xz; 170 float lxo = xo * rhs.xx + yo * rhs.xy + zo * rhs.xz + rhs.xo; 171 172 float lyx = xx * rhs.yx + yx * rhs.yy + zx * rhs.yz; 173 float lyy = xy * rhs.yx + yy * rhs.yy + zy * rhs.yz; 174 float lyz = xz * rhs.yx + yz * rhs.yy + zz * rhs.yz; 175 float lyo = xo * rhs.yx + yo * rhs.yy + zo * rhs.yz + rhs.yo; 176 177 float lzx = xx * rhs.zx + yx * rhs.zy + zx * rhs.zz; 178 float lzy = xy * rhs.zx + yy * rhs.zy + zy * rhs.zz; 179 float lzz = xz * rhs.zx + yz * rhs.zy + zz * rhs.zz; 180 float lzo = xo * rhs.zx + yo * rhs.zy + zo * rhs.zz + rhs.zo; 181 182 xx = lxx; 183 xy = lxy; 184 xz = lxz; 185 xo = lxo; 186 187 yx = lyx; 188 yy = lyy; 189 yz = lyz; 190 yo = lyo; 191 192 zx = lzx; 193 zy = lzy; 194 zz = lzz; 195 zo = lzo; 196 } 197 198 199 void unit() { 200 xo = 0; 201 xx = 1; 202 xy = 0; 203 xz = 0; 204 yo = 0; 205 yx = 0; 206 yy = 1; 207 yz = 0; 208 zo = 0; 209 zx = 0; 210 zy = 0; 211 zz = 1; 212 } 213 217 void transform(float v[], int tv[], int nvert) { 218 float lxx = xx, lxy = xy, lxz = xz, lxo = xo; 219 float lyx = yx, lyy = yy, lyz = yz, lyo = yo; 220 float lzx = zx, lzy = zy, lzz = zz, lzo = zo; 221 for (int i = nvert * 3; (i -= 3) >= 0;) { 222 float x = v[i]; 223 float y = v[i + 1]; 224 float z = v[i + 2]; 225 tv[i ] = (int) (x * lxx + y * lxy + z * lxz + lxo); 226 tv[i + 1] = (int) (x * lyx + y * lyy + z * lyz + lyo); 227 tv[i + 2] = (int) (x * lzx + y * lzy + z * lzz + lzo); 228 } 229 } 230 public String toString() { 231 return ("[" + xo + "," + xx + "," + xy + "," + xz + ";" 232 + yo + "," + yx + "," + yy + "," + yz + ";" 233 + zo + "," + zx + "," + zy + "," + zz + "]"); 234 } 235 } 236 | Popular Tags |