cleaned up some camera code and added some functionality for first-person view

This commit is contained in:
Jeffrey Ventrella 2013-04-25 12:19:23 -07:00
parent 68b3f54f5c
commit 9a4d27c0b5
4 changed files with 76 additions and 81 deletions

View file

@ -316,8 +316,8 @@ void Avatar::simulate(float deltaTime) {
updateAvatarCollisionDetectionAndResponse updateAvatarCollisionDetectionAndResponse
( (
otherAvatar->getBonePosition( AVATAR_BONE_PELVIS_SPINE ), otherAvatar->getBonePosition( AVATAR_BONE_PELVIS_SPINE ),
0.2, 0.1,
0.2, 0.1,
otherAvatar->getBodyUpDirection(), otherAvatar->getBodyUpDirection(),
deltaTime deltaTime
); );

View file

@ -12,14 +12,15 @@
Camera::Camera() { Camera::Camera() {
_mode = CAMERA_MODE_THIRD_PERSON; _mode = CAMERA_MODE_THIRD_PERSON;
_tightness = DEFAULT_CAMERA_TIGHTNESS; _tightness = 10.0; // default
_fieldOfView = 60.0; // default _fieldOfView = 60.0; // default
_nearClip = 0.08; // default _nearClip = 0.08; // default
_farClip = 50.0; // default _farClip = 50.0; // default
_yaw = 0.0; _yaw = 0.0;
_pitch = 0.0; _pitch = 0.0;
_roll = 0.0; _roll = 0.0;
_up = 0.0; _upShift = 0.0;
_rightShift = 0.0;
_distance = 0.0; _distance = 0.0;
_idealYaw = 0.0; _idealYaw = 0.0;
_targetPosition = glm::vec3( 0.0, 0.0, 0.0 ); _targetPosition = glm::vec3( 0.0, 0.0, 0.0 );
@ -28,46 +29,36 @@ Camera::Camera() {
_orientation.setToIdentity(); _orientation.setToIdentity();
} }
void Camera::update( float deltaTime ) {
void Camera::update( float deltaTime )
{
//----------------------------------------
// derive t from tightness // derive t from tightness
//---------------------------------------- float t = _tightness * deltaTime;
float t = _tightness * deltaTime; if ( t > 1.0 ) {
if ( t > 1.0 ){
t = 1.0; t = 1.0;
} }
//----------------------------------------
// update _yaw (before position!) // update _yaw (before position!)
//----------------------------------------
_yaw += ( _idealYaw - _yaw ) * t; _yaw += ( _idealYaw - _yaw ) * t;
// generate the ortho-normals for the orientation based on the Euler angles
_orientation.setToIdentity();
_orientation.yaw ( _yaw );
_orientation.pitch( _pitch );
_orientation.roll ( _roll );
float radian = ( _yaw / 180.0 ) * PIE; float radian = ( _yaw / 180.0 ) * PIE;
//----------------------------------------
// update _position // update _position
//---------------------------------------- //these need to be checked to make sure they correspond to the correct coordinate system.
//these need to be checked to make sure they correspond to the coordinate system.
double x = _distance * -sin( radian ); double x = _distance * -sin( radian );
double z = _distance * cos( radian ); double z = _distance * cos( radian );
double y = _up; double y = _upShift;
_idealPosition = _targetPosition + glm::vec3( x, y, z ); _idealPosition = _targetPosition + glm::vec3( x, y, z );
//_idealPosition += _orientation.getRight() * _rightShift;
//_idealPosition += _orientation.getUp () * _upShift;
// pull position towards ideal position
_position += ( _idealPosition - _position ) * t; _position += ( _idealPosition - _position ) * t;
//------------------------------------------------------------------------------
// generate the ortho-normals for the orientation based on the Euler angles
//------------------------------------------------------------------------------
_orientation.setToIdentity();
_orientation.yaw ( _yaw );
_orientation.pitch ( _pitch );
_orientation.roll ( _roll );
//printLog( "orientation.front = %f, %f, %f\n", _orientation.front.x, _orientation.front.y, _orientation.front.z );
} }

View file

@ -20,8 +20,6 @@ enum CameraMode
NUM_CAMERA_MODES NUM_CAMERA_MODES
}; };
static const float DEFAULT_CAMERA_TIGHTNESS = 10.0f;
class Camera class Camera
{ {
public: public:
@ -29,31 +27,33 @@ public:
void update( float deltaTime ); void update( float deltaTime );
void setMode ( CameraMode m ) { _mode = m; } void setMode ( CameraMode m ) { _mode = m; }
void setYaw ( float y ) { _idealYaw = y; } void setYaw ( float y ) { _yaw = y; }
void setPitch ( float p ) { _pitch = p; } void setPitch ( float p ) { _pitch = p; }
void setRoll ( float r ) { _roll = r; } void setRoll ( float r ) { _roll = r; }
void setUp ( float u ) { _up = u; } void setUpShift ( float u ) { _upShift = u; }
void setDistance ( float d ) { _distance = d; } void setRightShift ( float r ) { _rightShift = r; }
void setTargetPosition ( glm::vec3 t ) { _targetPosition = t; }; void setDistance ( float d ) { _distance = d; }
void setPosition ( glm::vec3 p ) { _position = p; }; void setTargetPosition( glm::vec3 t ) { _targetPosition = t; }
void setOrientation ( Orientation o ) { _orientation.set(o); } void setTargetYaw ( float y ) { _idealYaw = y; }
void setTightness ( float t ) { _tightness = t; } void setPosition ( glm::vec3 p ) { _position = p; }
void setFieldOfView ( float f ) { _fieldOfView = f; } void setOrientation ( Orientation o ) { _orientation.set(o); }
void setAspectRatio ( float a ) { _aspectRatio = a; } void setTightness ( float t ) { _tightness = t; }
void setNearClip ( float n ) { _nearClip = n; } void setFieldOfView ( float f ) { _fieldOfView = f; }
void setFarClip ( float f ) { _farClip = f; } void setAspectRatio ( float a ) { _aspectRatio = a; }
void setNearClip ( float n ) { _nearClip = n; }
void setFarClip ( float f ) { _farClip = f; }
float getYaw () { return _yaw; } float getYaw () { return _yaw; }
float getPitch () { return _pitch; } float getPitch () { return _pitch; }
float getRoll () { return _roll; } float getRoll () { return _roll; }
glm::vec3 getPosition () { return _position; } glm::vec3 getPosition () { return _position; }
Orientation getOrientation () { return _orientation; } Orientation getOrientation() { return _orientation; }
CameraMode getMode () { return _mode; } CameraMode getMode () { return _mode; }
float getFieldOfView () { return _fieldOfView; } float getFieldOfView() { return _fieldOfView; }
float getAspectRatio () { return _aspectRatio; } float getAspectRatio() { return _aspectRatio; }
float getNearClip () { return _nearClip; } float getNearClip () { return _nearClip; }
float getFarClip () { return _farClip; } float getFarClip () { return _farClip; }
private: private:
@ -68,7 +68,8 @@ private:
float _yaw; float _yaw;
float _pitch; float _pitch;
float _roll; float _roll;
float _up; float _upShift;
float _rightShift;
float _idealYaw; float _idealYaw;
float _distance; float _distance;
float _tightness; float _tightness;

View file

@ -800,33 +800,38 @@ void display(void)
glMaterialfv(GL_FRONT, GL_SPECULAR, specular_color); glMaterialfv(GL_FRONT, GL_SPECULAR, specular_color);
glMateriali(GL_FRONT, GL_SHININESS, 96); glMateriali(GL_FRONT, GL_SHININESS, 96);
//--------------------------------------------------------
// camera settings // camera settings
//--------------------------------------------------------
if ( ::lookingInMirror ) { if ( ::lookingInMirror ) {
//-----------------------------------------------
// set the camera to looking at my own face // set the camera to looking at my own face
//-----------------------------------------------
myCamera.setTargetPosition ( myAvatar.getHeadPosition() ); myCamera.setTargetPosition ( myAvatar.getHeadPosition() );
myCamera.setYaw ( - myAvatar.getBodyYaw() ); myCamera.setTargetYaw ( - myAvatar.getBodyYaw() );
myCamera.setPitch ( 0.0 ); myCamera.setPitch ( 0.0 );
myCamera.setRoll ( 0.0 ); myCamera.setRoll ( 0.0 );
myCamera.setUp ( 0.0 ); myCamera.setUpShift ( 0.0 );
myCamera.setDistance ( 0.2 ); myCamera.setDistance ( 0.2 );
myCamera.setTightness ( 100.0f ); myCamera.setTightness ( 100.0f );
myCamera.update ( 1.f/FPS ); myCamera.update ( 1.f/FPS );
} else { } else {
//----------------------------------------------------
// set the camera to third-person view behind my av //this is in the prototyping stages..keep firstPerson false for now.
//---------------------------------------------------- bool firstPerson = false;
myCamera.setTargetPosition ( myAvatar.getPosition() );
myCamera.setYaw ( 180.0 - myAvatar.getBodyYaw() ); if ( firstPerson ) {
myCamera.setPitch ( 0.0 ); // temporarily, this must be 0.0 or else bad juju myCamera.setPitch (15.0f ); // temporarily, this must be 0.0 or else bad juju
myCamera.setRoll ( 0.0 ); myCamera.setUpShift (0.0f );
myCamera.setUp ( 0.45 ); myCamera.setDistance (0.0f );
myCamera.setDistance ( 1.0 ); myCamera.setTightness (100.0f);
myCamera.setTightness ( 8.0f ); } else {
myCamera.update ( 1.f/FPS); myCamera.setPitch (0.0f ); // temporarily, this must be 0.0 or else bad juju
myCamera.setUpShift (-0.1f);
myCamera.setDistance (1.0f );
myCamera.setTightness (8.0f );
}
myCamera.setTargetPosition( myAvatar.getHeadPosition() );
myCamera.setTargetYaw ( 180.0 - myAvatar.getBodyYaw() );
myCamera.setRoll ( 0.0 );
myCamera.update ( 1.f/FPS);
} }
// Note: whichCamera is used to pick between the normal camera myCamera for our // Note: whichCamera is used to pick between the normal camera myCamera for our
@ -843,10 +848,10 @@ void display(void)
if (::viewFrustumFromOffset && ::frustumOn) { if (::viewFrustumFromOffset && ::frustumOn) {
// set the camera to third-person view but offset so we can see the frustum // set the camera to third-person view but offset so we can see the frustum
viewFrustumOffsetCamera.setYaw ( 180.0 - myAvatar.getBodyYaw() + ::viewFrustumOffsetYaw ); viewFrustumOffsetCamera.setTargetYaw( 180.0 - myAvatar.getBodyYaw() + ::viewFrustumOffsetYaw );
viewFrustumOffsetCamera.setPitch ( ::viewFrustumOffsetPitch ); viewFrustumOffsetCamera.setPitch ( ::viewFrustumOffsetPitch );
viewFrustumOffsetCamera.setRoll ( ::viewFrustumOffsetRoll ); viewFrustumOffsetCamera.setRoll ( ::viewFrustumOffsetRoll );
viewFrustumOffsetCamera.setUp ( ::viewFrustumOffsetUp ); viewFrustumOffsetCamera.setUpShift ( ::viewFrustumOffsetUp );
viewFrustumOffsetCamera.setDistance ( ::viewFrustumOffsetDistance ); viewFrustumOffsetCamera.setDistance ( ::viewFrustumOffsetDistance );
viewFrustumOffsetCamera.update(1.f/FPS); viewFrustumOffsetCamera.update(1.f/FPS);
whichCamera = viewFrustumOffsetCamera; whichCamera = viewFrustumOffsetCamera;
@ -864,10 +869,8 @@ void display(void)
if (::starsOn) { if (::starsOn) {
// should be the first rendering pass - w/o depth buffer / lighting // should be the first rendering pass - w/o depth buffer / lighting
// finally render the starfield // finally render the starfield
stars.render(whichCamera.getFieldOfView(), aspectRatio, whichCamera.getNearClip()); stars.render(whichCamera.getFieldOfView(), aspectRatio, whichCamera.getNearClip());
} }
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);