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
(
otherAvatar->getBonePosition( AVATAR_BONE_PELVIS_SPINE ),
0.2,
0.2,
0.1,
0.1,
otherAvatar->getBodyUpDirection(),
deltaTime
);

View file

@ -12,14 +12,15 @@
Camera::Camera() {
_mode = CAMERA_MODE_THIRD_PERSON;
_tightness = DEFAULT_CAMERA_TIGHTNESS;
_fieldOfView = 60.0; // default
_nearClip = 0.08; // default
_farClip = 50.0; // default
_tightness = 10.0; // default
_fieldOfView = 60.0; // default
_nearClip = 0.08; // default
_farClip = 50.0; // default
_yaw = 0.0;
_pitch = 0.0;
_roll = 0.0;
_up = 0.0;
_upShift = 0.0;
_rightShift = 0.0;
_distance = 0.0;
_idealYaw = 0.0;
_targetPosition = glm::vec3( 0.0, 0.0, 0.0 );
@ -28,46 +29,36 @@ Camera::Camera() {
_orientation.setToIdentity();
}
void Camera::update( float deltaTime ) {
void Camera::update( float deltaTime )
{
//----------------------------------------
// derive t from tightness
//----------------------------------------
float t = _tightness * deltaTime;
if ( t > 1.0 ){
float t = _tightness * deltaTime;
if ( t > 1.0 ) {
t = 1.0;
}
//----------------------------------------
// update _yaw (before position!)
//----------------------------------------
_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;
//----------------------------------------
// update _position
//----------------------------------------
//these need to be checked to make sure they correspond to the coordinate system.
//these need to be checked to make sure they correspond to the correct coordinate system.
double x = _distance * -sin( 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;
//------------------------------------------------------------------------------
// 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
};
static const float DEFAULT_CAMERA_TIGHTNESS = 10.0f;
class Camera
{
public:
@ -29,31 +27,33 @@ public:
void update( float deltaTime );
void setMode ( CameraMode m ) { _mode = m; }
void setYaw ( float y ) { _idealYaw = y; }
void setPitch ( float p ) { _pitch = p; }
void setRoll ( float r ) { _roll = r; }
void setUp ( float u ) { _up = u; }
void setDistance ( float d ) { _distance = d; }
void setTargetPosition ( glm::vec3 t ) { _targetPosition = t; };
void setPosition ( glm::vec3 p ) { _position = p; };
void setOrientation ( Orientation o ) { _orientation.set(o); }
void setTightness ( float t ) { _tightness = t; }
void setFieldOfView ( float f ) { _fieldOfView = f; }
void setAspectRatio ( float a ) { _aspectRatio = a; }
void setNearClip ( float n ) { _nearClip = n; }
void setFarClip ( float f ) { _farClip = f; }
void setMode ( CameraMode m ) { _mode = m; }
void setYaw ( float y ) { _yaw = y; }
void setPitch ( float p ) { _pitch = p; }
void setRoll ( float r ) { _roll = r; }
void setUpShift ( float u ) { _upShift = u; }
void setRightShift ( float r ) { _rightShift = r; }
void setDistance ( float d ) { _distance = d; }
void setTargetPosition( glm::vec3 t ) { _targetPosition = t; }
void setTargetYaw ( float y ) { _idealYaw = y; }
void setPosition ( glm::vec3 p ) { _position = p; }
void setOrientation ( Orientation o ) { _orientation.set(o); }
void setTightness ( float t ) { _tightness = t; }
void setFieldOfView ( float f ) { _fieldOfView = f; }
void setAspectRatio ( float a ) { _aspectRatio = a; }
void setNearClip ( float n ) { _nearClip = n; }
void setFarClip ( float f ) { _farClip = f; }
float getYaw () { return _yaw; }
float getPitch () { return _pitch; }
float getRoll () { return _roll; }
glm::vec3 getPosition () { return _position; }
Orientation getOrientation () { return _orientation; }
CameraMode getMode () { return _mode; }
float getFieldOfView () { return _fieldOfView; }
float getAspectRatio () { return _aspectRatio; }
float getNearClip () { return _nearClip; }
float getFarClip () { return _farClip; }
float getYaw () { return _yaw; }
float getPitch () { return _pitch; }
float getRoll () { return _roll; }
glm::vec3 getPosition () { return _position; }
Orientation getOrientation() { return _orientation; }
CameraMode getMode () { return _mode; }
float getFieldOfView() { return _fieldOfView; }
float getAspectRatio() { return _aspectRatio; }
float getNearClip () { return _nearClip; }
float getFarClip () { return _farClip; }
private:
@ -68,7 +68,8 @@ private:
float _yaw;
float _pitch;
float _roll;
float _up;
float _upShift;
float _rightShift;
float _idealYaw;
float _distance;
float _tightness;

View file

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