rotate.cpp 8.24 KB
 Kirill Terekhov committed Mar 19, 2014 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ``````/********************************* Implementation of quaternion-based object rotation Functions clickmotion - call when holded mouse moved click - call when user clicks motion - call when mouse just moves quatinit - flush rotation value rotate - multiply GL matrix Dependency: rotate.h, Standard: math.h Specific: glut.h **********************************/ #include "my_glut.h" #include "rotate.h" #include "math.h" `````` Kirill Terekhov committed Nov 06, 2014 20 ``````#include `````` Kirill Terekhov committed Mar 19, 2014 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 `````` struct quaternion { double x,y,z,w; }; struct vector { double x,y,z; }; // Rotation struct quaternion q; struct vector drag, onclick; double mx,my; extern int width, height; extern int interactive; // `````` Kirill Terekhov committed Nov 06, 2014 39 ``````std::vector storage; `````` Kirill Terekhov committed Mar 19, 2014 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 `````` void clickmotion(int nmx, int nmy) // Mouse { struct vector n; double norm,length,t; mx = 2.*(nmx/(double)width - 0.5); my = 2.*(0.5 - nmy/(double)height); norm = mx*mx + my*my; if( norm > 1.0 ) { length = sqrt(norm); drag.x = mx/length; drag.y = my/length; drag.z = 0.0; } else { drag.x = mx; drag.y = my; drag.z = sqrt(1.0-norm); } n.x = drag.y*onclick.z - drag.z*onclick.y; n.y = drag.z*onclick.x - drag.x*onclick.z; n.z = drag.x*onclick.y - drag.y*onclick.x; if ( n.x*n.x + n.y*n.y + n.z*n.z > 10e-7 ) { t = drag.x*onclick.x + drag.y*onclick.y + drag.z*onclick.z; q.x = + q.x*t + q.y*n.z - q.z*n.y + q.w*n.x; q.y = - q.x*n.z + q.y*t + q.z*n.x + q.w*n.y; q.z = + q.x*n.y - q.y*n.x + q.z*t + q.w*n.z; q.w = - q.x*n.x - q.y*n.y - q.z*n.z + q.w*t; onclick.x = drag.x; onclick.y = drag.y; onclick.z = drag.z; } glutPostRedisplay(); } void motion(int nmx, int nmy) // Mouse { mx = 2.*(nmx/(double)width - 0.5); my = 2.*(0.5 - nmy/(double)height); } void click(int b, int s, int nmx, int nmy) // Mouse { double norm,length; switch(b) { case GLUT_LEFT_BUTTON: if( s == GLUT_DOWN ) interactive = 1; else interactive = 0; mx = 2.*(nmx/(double)width - 0.5); my = 2.*(0.5 - nmy/(double)height); norm = mx*mx + my*my; if( norm > 1.0 ) { length = sqrt(norm); drag.x = mx/length; drag.y = my/length; drag.z = 0.0; } else { drag.x = mx; drag.y = my; drag.z = sqrt(1.0-norm); } onclick.x = drag.x; onclick.y = drag.y; onclick.z = drag.z; break; } glutPostRedisplay(); } void quatinit() { q.x = 0.0; q.y = 0.0; q.z = 0.0; q.w = 1.0; } `````` Kirill Terekhov committed Nov 06, 2014 123 124 125 126 127 128 129 130 131 132 133 134 135 ``````void quatpush() { storage.push_back(q); } void quatpop() { if( !storage.empty() ) { q = storage.back(); storage.pop_back(); } } `````` Kirill Terekhov committed Oct 23, 2014 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 `````` void rotatevector(double * vec) { int i; double rot[16]; double temp[4] = {vec[0],vec[1],vec[2],1.0}; double ret[4]; rot[ 0] = (q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z); rot[ 1] = 2.*(q.x*q.y - q.w*q.z); rot[ 2] = 2.*(q.x*q.z + q.w*q.y); rot[ 3] = 0.0; rot[ 4] = 2.*(q.x*q.y + q.w*q.z); rot[ 5] = (q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z); rot[ 6] = 2.*(q.y*q.z - q.w*q.x); rot[ 7] = 0.0; rot[ 8] = 2.*(q.x*q.z - q.w*q.y); rot[ 9] = 2.*(q.y*q.z + q.w*q.x); rot[10] = (q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z); rot[11] = 0.0; rot[12] = 0.0; rot[13] = 0.0; rot[14] = 0.0; rot[15] = (q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z); for(i=0; i < 4; i++) { ret[i] = temp[0] * rot[i*4]; ret[i] += temp[1] * rot[i*4+1]; ret[i] += temp[2] * rot[i*4+2]; ret[i] += temp[3] * rot[i*4+3]; } vec[0] = ret[0]/ret[3]; vec[1] = ret[1]/ret[3]; vec[2] = ret[2]/ret[3]; } `````` Kirill Terekhov committed Jun 09, 2015 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 ``````void reverse_rotatevector(double * vec) { int i; double rot[16]; double temp[4] = {vec[0],vec[1],vec[2],1.0}; double ret[4]; q.x = -q.x; q.y = -q.y; q.z = -q.z; rot[ 0] = (q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z); rot[ 1] = 2.*(q.x*q.y - q.w*q.z); rot[ 2] = 2.*(q.x*q.z + q.w*q.y); rot[ 3] = 0.0; rot[ 4] = 2.*(q.x*q.y + q.w*q.z); rot[ 5] = (q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z); rot[ 6] = 2.*(q.y*q.z - q.w*q.x); rot[ 7] = 0.0; rot[ 8] = 2.*(q.x*q.z - q.w*q.y); rot[ 9] = 2.*(q.y*q.z + q.w*q.x); rot[10] = (q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z); rot[11] = 0.0; rot[12] = 0.0; rot[13] = 0.0; rot[14] = 0.0; rot[15] = (q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z); for(i=0; i < 4; i++) { ret[i] = temp[0] * rot[i*4]; ret[i] += temp[1] * rot[i*4+1]; ret[i] += temp[2] * rot[i*4+2]; ret[i] += temp[3] * rot[i*4+3]; } vec[0] = ret[0]/ret[3]; vec[1] = ret[1]/ret[3]; vec[2] = ret[2]/ret[3]; q.x = -q.x; q.y = -q.y; q.z = -q.z; } `````` Kirill Terekhov committed Nov 06, 2014 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 ``````void revrotatevector(double * vec) { int i; double rot[16]; double temp[4] = {vec[0],vec[1],vec[2],1.0}; double ret[4]; q.w = -q.w; rot[ 0] = (q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z); rot[ 1] = 2.*(q.x*q.y - q.w*q.z); rot[ 2] = 2.*(q.x*q.z + q.w*q.y); rot[ 3] = 0.0; rot[ 4] = 2.*(q.x*q.y + q.w*q.z); rot[ 5] = (q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z); rot[ 6] = 2.*(q.y*q.z - q.w*q.x); rot[ 7] = 0.0; rot[ 8] = 2.*(q.x*q.z - q.w*q.y); rot[ 9] = 2.*(q.y*q.z + q.w*q.x); rot[10] = (q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z); rot[11] = 0.0; rot[12] = 0.0; rot[13] = 0.0; rot[14] = 0.0; rot[15] = (q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z); for(i=0; i < 4; i++) { ret[i] = temp[0] * rot[i*4]; ret[i] += temp[1] * rot[i*4+1]; ret[i] += temp[2] * rot[i*4+2]; ret[i] += temp[3] * rot[i*4+3]; } vec[0] = ret[0]/ret[3]; vec[1] = ret[1]/ret[3]; vec[2] = ret[2]/ret[3]; q.w = -q.w; } void rotatevector_from_stack(double * vec) { int i; struct quaternion q = storage.back(); double rot[16]; double temp[4] = {vec[0],vec[1],vec[2],1.0}; double ret[4]; rot[ 0] = (q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z); rot[ 1] = 2.*(q.x*q.y - q.w*q.z); rot[ 2] = 2.*(q.x*q.z + q.w*q.y); rot[ 3] = 0.0; rot[ 4] = 2.*(q.x*q.y + q.w*q.z); rot[ 5] = (q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z); rot[ 6] = 2.*(q.y*q.z - q.w*q.x); rot[ 7] = 0.0; rot[ 8] = 2.*(q.x*q.z - q.w*q.y); rot[ 9] = 2.*(q.y*q.z + q.w*q.x); rot[10] = (q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z); rot[11] = 0.0; rot[12] = 0.0; rot[13] = 0.0; rot[14] = 0.0; rot[15] = (q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z); for(i=0; i < 4; i++) { ret[i] = temp[0] * rot[i*4]; ret[i] += temp[1] * rot[i*4+1]; ret[i] += temp[2] * rot[i*4+2]; ret[i] += temp[3] * rot[i*4+3]; } vec[0] = ret[0]/ret[3]; vec[1] = ret[1]/ret[3]; vec[2] = ret[2]/ret[3]; } `````` Kirill Terekhov committed Jun 09, 2015 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 ``````void quatget(double *vec) { vec[0] = q.x / q.w; vec[1] = q.y / q.w; vec[2] = q.z / q.w; } void reverse_rotatevector_from_stack(double * vec) { int i; struct quaternion q = storage.back(); q.x = -q.x; q.y = -q.y; q.z = -q.z; double rot[16]; double temp[4] = {vec[0],vec[1],vec[2],1.0}; double ret[4]; rot[ 0] = (q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z); rot[ 1] = 2.*(q.x*q.y - q.w*q.z); rot[ 2] = 2.*(q.x*q.z + q.w*q.y); rot[ 3] = 0.0; rot[ 4] = 2.*(q.x*q.y + q.w*q.z); rot[ 5] = (q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z); rot[ 6] = 2.*(q.y*q.z - q.w*q.x); rot[ 7] = 0.0; rot[ 8] = 2.*(q.x*q.z - q.w*q.y); rot[ 9] = 2.*(q.y*q.z + q.w*q.x); rot[10] = (q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z); rot[11] = 0.0; rot[12] = 0.0; rot[13] = 0.0; rot[14] = 0.0; rot[15] = (q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z); for(i=0; i < 4; i++) { ret[i] = temp[0] * rot[i*4]; ret[i] += temp[1] * rot[i*4+1]; ret[i] += temp[2] * rot[i*4+2]; ret[i] += temp[3] * rot[i*4+3]; } vec[0] = ret[0]/ret[3]; vec[1] = ret[1]/ret[3]; vec[2] = ret[2]/ret[3]; } `````` Kirill Terekhov committed Mar 19, 2014 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 ``````void rotate() { double rot[16]; rot[ 0] = (q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z); rot[ 1] = 2.*(q.x*q.y - q.w*q.z); rot[ 2] = 2.*(q.x*q.z + q.w*q.y); rot[ 3] = 0.0; rot[ 4] = 2.*(q.x*q.y + q.w*q.z); rot[ 5] = (q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z); rot[ 6] = 2.*(q.y*q.z - q.w*q.x); rot[ 7] = 0.0; rot[ 8] = 2.*(q.x*q.z - q.w*q.y); rot[ 9] = 2.*(q.y*q.z + q.w*q.x); rot[10] = (q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z); rot[11] = 0.0; rot[12] = 0.0; rot[13] = 0.0; rot[14] = 0.0; rot[15] = (q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z); glMultMatrixd(rot); } `````` Kirill Terekhov committed Nov 06, 2014 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 `````` void rotate_from_stack() { double rot[16]; struct quaternion q = storage.back(); rot[ 0] = (q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z); rot[ 1] = 2.*(q.x*q.y - q.w*q.z); rot[ 2] = 2.*(q.x*q.z + q.w*q.y); rot[ 3] = 0.0; rot[ 4] = 2.*(q.x*q.y + q.w*q.z); rot[ 5] = (q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z); rot[ 6] = 2.*(q.y*q.z - q.w*q.x); rot[ 7] = 0.0; rot[ 8] = 2.*(q.x*q.z - q.w*q.y); rot[ 9] = 2.*(q.y*q.z + q.w*q.x); rot[10] = (q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z); rot[11] = 0.0; rot[12] = 0.0; rot[13] = 0.0; rot[14] = 0.0; rot[15] = (q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z); glMultMatrixd(rot); } ``````