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
|
/*
* Modification History
*
* 2001-August-29 Jason Rohrer
* Created.
*
* 2001-August-30 Jason Rohrer
* Fixed some comments.
*
* 2001-October-29 Jason Rohrer
* Changed to move and rotate view at a fixed
* time rate, regardless of framerate.
*
* 2003-June-14 Jason Rohrer
* Fixed namespace for exit call.
* Added M_PI backup definition.
*/
#ifndef SCENE_NAVIGATOR_DISPLAY_GL_INCLUDED
#define SCENE_NAVIGATOR_DISPLAY_GL_INCLUDED
#include "ScreenGL.h"
#include "MouseHandlerGL.h"
#include "KeyboardHandlerGL.h"
#include "SceneHandlerGL.h"
#include "RedrawListenerGL.h"
#include "minorGems/system/Time.h"
#include <stdlib.h>
#include <math.h>
// make sure M_PI is defined
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/**
* Subclass of ScreenGL that handles general-purpose 3D scene navigation
* rendered through OpenGL.
*
* Motion is unlimited, though forward-backward motion is confinded
* to the x-z plane (the user can move up and down explicitly, however).
*
* To constrict motion, subclass SceneNavigatorDisplayGL and override
* the moveView() function (a member of ScreenGL).
*
* Compile note: For Linux, add these library flags:
* -lGL -lglut -lGLU -L/usr/X11R6/lib
* Be sure to include ScreenGL.cpp in the file list passed to the compiler.
*
* @author Jason Rohrer
*/
class SceneNavigatorDisplayGL :
public ScreenGL, public MouseHandlerGL,
public KeyboardHandlerGL, public RedrawListenerGL {
public:
/**
* Constructs a navigator.
*
* @param inWide the width of the screen in pixels.
* @param inHigh the height of the screen in pixels.
* @param inFullScreen true iff the display should
* fill the screen.
* @param inWindowName the name of the display window.
* @param inSceenHandler the handler responsible for
* drawing the scene.
* Must be destroyed by caller.
* @param inMoveUnitsPerSecond the number of world
* units to move per second when the user makes a move.
* Defaults to 1.0 units per second.
* @param inRotateRadiansPerSecond the number of
* radians to rotate per second when the user makes a rotation.
* Defaults to 1.0 radians per second.
*/
SceneNavigatorDisplayGL( int inWide, int inHigh, char inFullScreen,
char *inWindowName,
SceneHandlerGL *inSceneHandler,
double inMoveUnitsPerSecond = 1.0,
double inRotateRadiansPerSecond = 1.0 );
~SceneNavigatorDisplayGL();
// implements the MouseHandlerGL interface
virtual void mouseMoved( int inX, int inY );
virtual void mouseDragged( int inX, int inY );
virtual void mousePressed( int inX, int inY );
virtual void mouseReleased( int inX, int inY );
// implements the KeyboardHandlerGL interface
virtual void keyPressed( unsigned char inKey, int inX, int inY );
virtual void specialKeyPressed( int inKey, int inX, int inY );
virtual void keyReleased( unsigned char inKey, int inX, int inY );
virtual void specialKeyReleased( int inKey, int inX, int inY );
// implements the RedrawListener interface
virtual void fireRedraw();
private :
int mLastMouseX, mLastMouseY;
// amount to move and rotate view during each redraw
Vector3D *mMoveDirection;
Angle3D *mRotationOffset;
// keep y movement separate from view direction.
double mYMovement;
// true if a moving key is currently depressed
char mMoving;
// true if a rotation key is currently depressed
char mRotating;
unsigned long mTimeLastRedrawS;
unsigned long mTimeLastRedrawMS;
double mMoveUnitsPerSecond;
double mRotateRadiansPerSecond;
};
inline SceneNavigatorDisplayGL::SceneNavigatorDisplayGL(
int inWide, int inHigh, char inFullScreen, char *inWindowName,
SceneHandlerGL *inSceneHandler,
double inMoveUnitsPerSecond,
double inRotateRadiansPerSecond )
: ScreenGL( inWide, inHigh, inFullScreen, inWindowName, NULL, NULL,
inSceneHandler ),
mMoveDirection( new Vector3D( 0, 0, 0 ) ),
mRotationOffset( new Angle3D( 0, 0, 0 ) ),
mMoving( false ), mRotating( false ),
mYMovement( 0 ),
mMoveUnitsPerSecond( inMoveUnitsPerSecond ),
mRotateRadiansPerSecond( inRotateRadiansPerSecond ) {
// add ourself to the superclass' listener lists
addMouseHandler( this );
addKeyboardHandler( this );
addRedrawListener( this );
Time::getCurrentTime( &mTimeLastRedrawS, &mTimeLastRedrawMS );
}
inline void SceneNavigatorDisplayGL::mouseMoved( int inX, int inY ) {
//printf( "mouse moved to (%d, %d)\n", inX, inY );
}
inline void SceneNavigatorDisplayGL::mouseDragged( int inX, int inY ) {
int delX = inX - mLastMouseX;
int delY = inY - mLastMouseY;
Angle3D *rotAngle = new Angle3D( delY / 50, delX/50, 0 );
//mScreen->rotateView( rotAngle );
delete rotAngle;
mLastMouseX = inX;
mLastMouseY = inY;
}
inline void SceneNavigatorDisplayGL::mousePressed( int inX, int inY ) {
}
inline void SceneNavigatorDisplayGL::mouseReleased( int inX, int inY ) {
}
inline void SceneNavigatorDisplayGL::keyPressed(
unsigned char inKey, int inX, int inY ) {
if( inKey == 'y' || inKey == 'Y' ) {
// turn view downwards
mRotationOffset->mX = M_PI/20;
}
else if( inKey == 'h' || inKey == 'H' ) {
// turn view upwards
mRotationOffset->mX = -M_PI/20;
}
if( inKey == 's' || inKey == 'S' ) {
// turn to the left
mRotationOffset->mY = M_PI/20;
}
else if( inKey == 'f' || inKey == 'F' ) {
// turn to the right
mRotationOffset->mY = -M_PI/20;
}
if( inKey == 'i' || inKey == 'I' ) {
// move upwards
mYMovement = 1;
}
else if( inKey == 'k' || inKey == 'K' ) {
// move downwards
mYMovement = -1;
}
if( inKey == 'j' || inKey == 'J' ) {
// strafe to the left
mMoveDirection->mX = 1;
}
else if( inKey == 'l' || inKey == 'L' ) {
// strafe to the right
mMoveDirection->mX = -1;
}
if( inKey == 'e' || inKey == 'E' ) {
// move forward
mMoveDirection->mZ = 1;
}
else if( inKey == 'd' || inKey == 'D' ) {
// move backward
mMoveDirection->mZ = -1;
}
else if( inKey == 'q' || inKey == 'Q' ) {
// quit
::exit( 0 );
}
}
inline void SceneNavigatorDisplayGL::keyReleased(
unsigned char inKey, int inX, int inY ) {
if( inKey == 'e' || inKey == 'E' ) {
mMoveDirection->mZ = 0;
}
else if( inKey == 'd' || inKey == 'D' ) {
mMoveDirection->mZ = 0;
}
if( inKey == 'j' || inKey == 'J' ) {
mMoveDirection->mX = 0;
}
else if( inKey == 'l' || inKey == 'L' ) {
mMoveDirection->mX = 0;
}
if( inKey == 'i' || inKey == 'I' ) {
mYMovement = 0;
}
else if( inKey == 'k' || inKey == 'K' ) {
mYMovement = 0;
}
if( inKey == 's' || inKey == 'S' ) {
mRotationOffset->mY = 0;
}
else if( inKey == 'f' || inKey == 'F' ) {
mRotationOffset->mY = 0;
}
if( inKey == 'y' || inKey == 'Y' ) {
mRotationOffset->mX = 0;
}
else if( inKey == 'h' || inKey == 'H' ) {
mRotationOffset->mX = 0;
}
}
inline void SceneNavigatorDisplayGL::specialKeyPressed(
int inKey, int inX, int inY ) {
}
inline void SceneNavigatorDisplayGL::specialKeyReleased(
int inKey, int inX, int inY ) {
}
inline void SceneNavigatorDisplayGL::fireRedraw() {
unsigned long currentTimeS;
unsigned long currentTimeMS;
Time::getCurrentTime( ¤tTimeS, ¤tTimeMS );
unsigned long deltaMS = Time::getMillisecondsSince(
mTimeLastRedrawS, mTimeLastRedrawMS );
mTimeLastRedrawS = currentTimeS;
mTimeLastRedrawMS = currentTimeMS;
// compute the number of units to move to maintain
// a constant speed of mUnitsPerSecond
double unitsToMove = mMoveUnitsPerSecond * ( deltaMS / 1000.0 );
double radiansToRotate = mRotateRadiansPerSecond * ( deltaMS / 1000.0 );
// scale our rotation offset
Angle3D *scaledRotationOffset = new Angle3D( mRotationOffset );
scaledRotationOffset->scale( radiansToRotate );
// rotate the view in the super class
rotateView( scaledRotationOffset );
delete scaledRotationOffset;
Vector3D *move = new Vector3D( mMoveDirection );
// scale our move direction
move->scale( unitsToMove );
Angle3D *rotation = new Angle3D( getViewOrientation() );
// only rotate movement direction about y axis
// we don't want view y direction to affect the direction of movement
rotation->mX = 0;
rotation->mZ = 0;
move->rotate( rotation );
delete rotation;
// now add in y movement, scaled
move->mY = mYMovement * unitsToMove;
// move the view in the superclass
moveView( move );
delete move;
}
#endif
|