1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 123 124 125 126 127 128 129 130 131 132 133 134 135 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 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 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 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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
/*
* walkviewer.c [from agviewer.c (version 1.0)]
*
* AGV: a glut viewer. Routines for viewing a 3d scene w/ glut
*
* See agv_example.c and agviewer.h comments within for more info.
*
* I welcome any feedback or improved versions!
*
* Philip Winston - 4/11/95
* pwinston@hmc.edu
* http://www.cs.hmc.edu/people/pwinston
*/
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "walkviewer.h"
/***************************************************************/
/************************** SETTINGS ***************************/
/***************************************************************/
/* Initial polar movement settings */
#define INIT_POLAR_AZ 0.0
#define INIT_POLAR_EL 30.0
#define INIT_DIST 3.0
#define INIT_AZ_SPIN 0.5
#define INIT_EL_SPIN 0.0
/* Initial flying movement settings */
#define INIT_EX 0.0
#define INIT_EY -2.0
#define INIT_EZ -2.0
#define INIT_MOVE 0.01
#define MINMOVE 0.001
/* Start in this mode */
#define INIT_MODE POLAR
/* Controls: */
/* map 0-9 to an EyeMove value when number key is hit in FLYING mode */
#define SPEEDFUNCTION(x) ((x)*(x)*0.001)
/* Multiply EyeMove by (1+-MOVEFRACTION) when +/- hit in FLYING mode */
#define MOVEFRACTION 0.25
/* What to multiply number of pixels mouse moved by to get rotation amount */
#define EL_SENS 0.5
#define AZ_SENS 0.5
/* What to multiply number of pixels mouse moved by for movement amounts */
#define DIST_SENS 0.01
#define E_SENS 0.01
/* Minimum spin to allow in polar (lower forced to zero) */
#define MIN_AZSPIN 0.1
#define MIN_ELSPIN 0.1
/* Factors used in computing dAz and dEl (which determine AzSpin, ElSpin) */
#define SLOW_DAZ 0.90
#define SLOW_DEL 0.90
#define PREV_DAZ 0.80
#define PREV_DEL 0.80
#define CUR_DAZ 0.20
#define CUR_DEL 0.20
/***************************************************************/
/************************** GLOBALS ****************************/
/***************************************************************/
int MoveMode = INIT_MODE; /* FLYING or POLAR mode? */
GLfloat Ex = INIT_EX, /* flying parameters */
Ey = INIT_EY,
Ez = INIT_EZ,
EyeMove = INIT_MOVE,
EyeDist = INIT_DIST, /* polar params */
AzSpin = INIT_AZ_SPIN,
ElSpin = INIT_EL_SPIN,
EyeAz = INIT_POLAR_AZ, /* used by both */
EyeEl = INIT_POLAR_EL;
int agvMoving; /* Currently moving? */
int downx, downy, /* for tracking mouse position */
lastx, lasty,
downb = -1; /* and button status */
GLfloat downDist, downEl, downAz, /* for saving state of things */
downEx, downEy, downEz, /* when button is pressed */
downEyeMove;
GLfloat dAz, dEl, lastAz, lastEl; /* to calculate spinning w/ polar motion */
int AdjustingAzEl = 0;
int AllowIdle, RedisplayWindow;
/* If AllowIdle is 1 it means AGV will install its own idle which
* will update the viewpoint as needed and send glutPostRedisplay() to the
* window RedisplayWindow which was set in agvInit(). AllowIdle of 0
* means AGV won't install an idle funciton, and something like
* "if (agvMoving) agvMove()" should exist at the end of the running
* idle function.
*/
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
#define TORAD(x) ((M_PI/180.0)*(x))
#define TODEG(x) ((180.0/M_PI)*(x))
/***************************************************************/
/************************ PROTOTYPES ***************************/
/***************************************************************/
/*
* these are functions meant for internal use only
* the other prototypes are in agviewer.h
*/
void PolarLookFrom(GLfloat dist, GLfloat elevation, GLfloat azimuth);
void FlyLookFrom(GLfloat x, GLfloat y, GLfloat z, GLfloat az, GLfloat el);
int ConstrainEl(void);
void MoveOn(int v);
void SetMove(float newmove);
void normalize(GLfloat v[3]);
void ncrossprod(float v1[3], float v2[3], float cp[3]);
/***************************************************************/
/************************ agvInit ******************************/
/***************************************************************/
void agvInit(int window)
{
glutMouseFunc(agvHandleButton);
glutMotionFunc(agvHandleMotion);
glutKeyboardFunc(agvHandleKeys);
RedisplayWindow = glutGetWindow();
agvSetAllowIdle(window);
}
/***************************************************************/
/************************ VIEWPOINT STUFF **********************/
/***************************************************************/
/*
* viewing transformation modified from page 90 of red book
*/
void PolarLookFrom(GLfloat dist, GLfloat elevation, GLfloat azimuth)
{
glTranslatef(0, 0, -dist);
glRotatef(elevation, 1, 0, 0);
glRotatef(azimuth, 0, 1, 0);
}
/*
* I took the idea of tracking eye position in absolute
* coords and direction looking in Polar form from denis
*/
void FlyLookFrom(GLfloat x, GLfloat y, GLfloat z, GLfloat az, GLfloat el)
{
float lookat[3], perp[3], up[3];
lookat[0] = sin(TORAD(az))*cos(TORAD(el));
lookat[1] = sin(TORAD(el));
lookat[2] = -cos(TORAD(az))*cos(TORAD(el));
normalize(lookat);
perp[0] = lookat[2];
perp[1] = 0;
perp[2] = -lookat[0];
normalize(perp);
ncrossprod(lookat, perp, up);
gluLookAt(x, y, z,
x+lookat[0], y+lookat[1], z+lookat[2],
up[0], up[1], up[2]);
}
/*
* Call viewing transformation based on movement mode
*/
void agvViewTransform(void)
{
switch (MoveMode) {
case FLYING:
FlyLookFrom(Ex, Ey, Ez, EyeAz, EyeEl);
break;
case POLAR:
PolarLookFrom(EyeDist, EyeEl, EyeAz);
break;
}
}
/*
* keep them vertical; I think this makes a lot of things easier,
* but maybe it wouldn't be too hard to adapt things to let you go
* upside down
*/
int ConstrainEl(void)
{
if (EyeEl <= -90) {
EyeEl = -89.99;
return 1;
} else if (EyeEl >= 90) {
EyeEl = 89.99;
return 1;
}
return 0;
}
/*
* Idle Function - moves eyeposition
*/
void agvMove(void)
{
switch (MoveMode) {
case FLYING:
Ex += EyeMove*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
Ey += EyeMove*sin(TORAD(EyeEl));
Ez -= EyeMove*cos(TORAD(EyeAz))*cos(TORAD(EyeEl));
break;
case POLAR:
EyeEl += ElSpin;
EyeAz += AzSpin;
if (ConstrainEl()) { /* weird spin thing to make things look */
ElSpin = -ElSpin; /* look better when you are kept from going */
/* upside down while spinning - Isn't great */
if (fabs(ElSpin) > fabs(AzSpin))
AzSpin = fabs(ElSpin) * ((AzSpin > 0) ? 1 : -1);
}
break;
}
if (AdjustingAzEl) {
dAz *= SLOW_DAZ;
dEl *= SLOW_DEL;
}
if (AllowIdle) {
glutPostWindowRedisplay(RedisplayWindow);
}
}
/*
* Don't install agvMove as idle unless we will be updating the view
* and we've been given a RedisplayWindow
*/
void MoveOn(int v)
{
if (v && ((MoveMode == FLYING && EyeMove != 0) ||
(MoveMode == POLAR &&
(AzSpin != 0 || ElSpin != 0 || AdjustingAzEl)))) {
agvMoving = 1;
if (AllowIdle)
glutIdleFunc(agvMove);
} else {
agvMoving = 0;
if (AllowIdle)
glutIdleFunc(NULL);
}
}
/*
* set new redisplay window. If <= 0 it means we are not to install
* an idle function and will rely on whoever does install one to
* put statement like "if (agvMoving) agvMove();" at end of it
*/
void agvSetAllowIdle(int allowidle)
{
if ((AllowIdle = allowidle))
MoveOn(1);
}
/*
* when moving to flying we stay in the same spot, moving to polar we
* reset since we have to be looking at the origin (though a pivot from
* current position to look at origin might be cooler)
*/
void agvSwitchMoveMode(int move)
{
switch (move) {
case FLYING:
Ex = -EyeDist*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
Ey = EyeDist*sin(TORAD(EyeEl));
Ez = EyeDist*(cos(TORAD(EyeAz))*cos(TORAD(EyeEl)));
EyeAz = EyeAz;
EyeEl = -EyeEl;
EyeMove = INIT_MOVE;
break;
case POLAR:
EyeDist = INIT_DIST;
EyeAz = INIT_POLAR_AZ;
EyeEl = INIT_POLAR_EL;
AzSpin = INIT_AZ_SPIN;
ElSpin = INIT_EL_SPIN;
break;
}
MoveMode = move;
MoveOn(1);
glutPostRedisplay();
}
/***************************************************************/
/******************* MOUSE HANDLING ***********************/
/***************************************************************/
void agvHandleButton(int button, int state, int x, int y)
{
if (state == GLUT_DOWN && downb == -1) {
lastx = downx = x;
lasty = downy = y;
downb = button;
switch (button) {
case GLUT_LEFT_BUTTON:
lastEl = downEl = EyeEl;
lastAz = downAz = EyeAz;
AzSpin = ElSpin = dAz = dEl = 0;
AdjustingAzEl = 1;
MoveOn(1);
break;
case GLUT_MIDDLE_BUTTON:
downDist = EyeDist;
downEx = Ex;
downEy = Ey;
downEz = Ez;
downEyeMove = EyeMove;
EyeMove = 0;
}
} else if (state == GLUT_UP && button == downb) {
downb = -1;
switch (button) {
case GLUT_LEFT_BUTTON:
if (MoveMode != FLYING) {
AzSpin = -dAz;
if (AzSpin < MIN_AZSPIN && AzSpin > -MIN_AZSPIN)
AzSpin = 0;
ElSpin = -dEl;
if (ElSpin < MIN_ELSPIN && ElSpin > -MIN_ELSPIN)
ElSpin = 0;
}
AdjustingAzEl = 0;
MoveOn(1);
break;
case GLUT_MIDDLE_BUTTON:
EyeMove = downEyeMove;
}
}
}
/*
* change EyeEl and EyeAz and position when mouse is moved w/ button down
*/
void agvHandleMotion(int x, int y)
{
int deltax = x - downx, deltay = y - downy;
switch (downb) {
case GLUT_LEFT_BUTTON:
EyeEl = downEl + EL_SENS * ((MoveMode == FLYING) ? -deltay : deltay);
ConstrainEl();
EyeAz = downAz + AZ_SENS * deltax;
dAz = PREV_DAZ*dAz + CUR_DAZ*(lastAz - EyeAz);
dEl = PREV_DEL*dEl + CUR_DEL*(lastEl - EyeEl);
lastAz = EyeAz;
lastEl = EyeEl;
break;
case GLUT_MIDDLE_BUTTON:
EyeDist = downDist + DIST_SENS*deltay;
Ex = downEx - E_SENS*deltay*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
Ey = downEy - E_SENS*deltay*sin(TORAD(EyeEl));
Ez = downEz + E_SENS*deltay*cos(TORAD(EyeAz))*cos(TORAD(EyeEl));
break;
}
glutPostRedisplay();
}
/***************************************************************/
/********************* KEYBOARD HANDLING ***********************/
/***************************************************************/
/*
* set EyeMove (current speed) for FLYING mode
*/
void SetMove(float newmove)
{
if (newmove > MINMOVE) {
EyeMove = newmove;
MoveOn(1);
} else {
EyeMove = 0;
MoveOn(0);
}
}
/*
* 0->9 set speed, +/- adjust current speed -- in FLYING mode
*/
/* ARGSUSED1 */
void agvHandleKeys(unsigned char key, int x, int y)
{
if (MoveMode != FLYING)
return;
if (key >= '0' && key <= '9')
SetMove(SPEEDFUNCTION((key-'0')));
else
switch(key) {
case '+':
if (EyeMove == 0)
SetMove(MINMOVE);
else
SetMove(EyeMove *= (1 + MOVEFRACTION));
break;
case '-':
SetMove(EyeMove *= (1 - MOVEFRACTION));
break;
}
}
/***************************************************************/
/*********************** VECTOR STUFF **************************/
/***************************************************************/
/* normalizes v */
void normalize(GLfloat v[3])
{
GLfloat d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
if (d == 0)
fprintf(stderr, "Zero length vector in normalize\n");
else
v[0] /= d; v[1] /= d; v[2] /= d;
}
/* calculates a normalized crossproduct to v1, v2 */
void ncrossprod(float v1[3], float v2[3], float cp[3])
{
cp[0] = v1[1]*v2[2] - v1[2]*v2[1];
cp[1] = v1[2]*v2[0] - v1[0]*v2[2];
cp[2] = v1[0]*v2[1] - v1[1]*v2[0];
normalize(cp);
}
/***************************************************************/
/**************************** AXES *****************************/
/***************************************************************/
/* draw axes -- was helpful to debug/design things */
void agvMakeAxesList(int displaylistnum)
{
int i,j;
GLfloat axes_ambuse[] = { 0.5, 0.0, 0.0, 1.0 };
glNewList(displaylistnum, GL_COMPILE);
glPushAttrib(GL_LIGHTING_BIT);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, axes_ambuse);
glBegin(GL_LINES);
glVertex3f(15, 0, 0); glVertex3f(-15, 0, 0);
glVertex3f(0, 15, 0); glVertex3f(0, -15, 0);
glVertex3f(0, 0, 15); glVertex3f(0, 0, -15);
glEnd();
for (i = 0; i < 3; i++) {
glPushMatrix();
glTranslatef(-10*(i==0), -10*(i==1), -10*(i==2));
for (j = 0; j < 21; j++) {
glutSolidCube(0.1);
glTranslatef(i==0, i==1, i==2);
}
glPopMatrix();
}
glPopAttrib();
glEndList();
}
|