I just did a headotomy (removed all head-related stuff from Avatar and made it its own class)

This commit is contained in:
Jeffrey Ventrella 2013-05-10 17:05:52 -07:00
parent 2032264e7e
commit 7fc2756257
4 changed files with 568 additions and 130 deletions

View file

@ -11,6 +11,7 @@
#include <lodepng.h>
#include <SharedUtil.h>
#include "Avatar.h"
#include "Head.h"
#include "Log.h"
#include "ui/TextRenderer.h"
#include <AgentList.h>
@ -23,12 +24,12 @@ const bool BALLS_ON = false;
const bool USING_AVATAR_GRAVITY = true;
const float GRAVITY_SCALE = 10.0f;
const float BOUNCE = 0.3f;
const float DECAY = 0.1;
//const float DECAY = 0.1;
const float THRUST_MAG = 1200.0;
const float YAW_MAG = 500.0;
const float BODY_SPIN_FRICTION = 5.0;
const float BODY_UPRIGHT_FORCE = 10.0;
const float BODY_PITCH_WHILE_WALKING = 30.0;
const float BODY_PITCH_WHILE_WALKING = 40.0;
const float BODY_ROLL_WHILE_TURNING = 0.1;
const float VELOCITY_DECAY = 5.0;
const float MY_HAND_HOLDING_PULL = 0.2;
@ -66,14 +67,16 @@ float browThickness = 0.16;
bool usingBigSphereCollisionTest = true;
char iris_texture_file[] = "resources/images/green_eye.png";
//char iris_texture_file[] = "resources/images/green_eye.png";
float chatMessageScale = 0.0015;
float chatMessageHeight = 0.45;
/*
vector<unsigned char> iris_texture;
unsigned int iris_texture_width = 512;
unsigned int iris_texture_height = 256;
*/
Avatar::Avatar(bool isMine) {
@ -104,43 +107,8 @@ Avatar::Avatar(bool isMine) {
for (int i = 0; i < MAX_DRIVE_KEYS; i++) _driveKeys[i] = false;
_head.pupilSize = 0.10;
_head.interPupilDistance = 0.6;
_head.interBrowDistance = 0.75;
_head.nominalPupilSize = 0.10;
_head.pitchRate = 0.0;
_head.yawRate = 0.0;
_head.rollRate = 0.0;
_head.eyebrowPitch[0] = -30;
_head.eyebrowPitch[1] = -30;
_head.eyebrowRoll [0] = 20;
_head.eyebrowRoll [1] = -20;
_head.mouthPitch = 0;
_head.mouthYaw = 0;
_head.mouthWidth = 1.0;
_head.mouthHeight = 0.2;
_head.eyeballPitch[0] = 0;
_head.eyeballPitch[1] = 0;
_head.eyeballScaleX = 1.2;
_head.eyeballScaleY = 1.5;
_head.eyeballScaleZ = 1.0;
_head.eyeballYaw[0] = 0;
_head.eyeballYaw[1] = 0;
_head.pitchTarget = 0;
_head.yawTarget = 0;
_head.noiseEnvelope = 1.0;
_head.pupilConverge = 10.0;
_head.leanForward = 0.0;
_head.leanSideways = 0.0;
_head.eyeContact = 1;
_head.eyeContactTarget = LEFT_EYE;
_head.scale = 1.0;
_head.audioAttack = 0.0;
_head.averageLoudness = 0.0;
_head.lastLoudness = 0.0;
_head.browAudioLift = 0.0;
_head.noise = 0;
_head.returnSpringScale = 1.0;
_head.initialize();
_movedHandOffset = glm::vec3(0.0f, 0.0f, 0.0f);
_renderYaw = 0.0;
_renderPitch = 0.0;
@ -153,6 +121,7 @@ Avatar::Avatar(bool isMine) {
_avatarTouch.setReachableRadius(0.6);
/*
if (iris_texture.size() == 0) {
switchToResourcesParentIfRequired();
unsigned error = lodepng::decode(iris_texture, iris_texture_width, iris_texture_height, iris_texture_file);
@ -160,6 +129,7 @@ Avatar::Avatar(bool isMine) {
printLog("error %u: %s\n", error, lodepng_error_text(error));
}
}
*/
if (BALLS_ON) { _balls = new Balls(100); }
else { _balls = NULL; }
@ -239,7 +209,8 @@ Avatar::Avatar(const Avatar &otherAvatar) {
_distanceToNearestAvatar = otherAvatar._distanceToNearestAvatar;
initializeSkeleton();
/*
if (iris_texture.size() == 0) {
switchToResourcesParentIfRequired();
unsigned error = lodepng::decode(iris_texture, iris_texture_width, iris_texture_height, iris_texture_file);
@ -247,6 +218,7 @@ Avatar::Avatar(const Avatar &otherAvatar) {
printLog("error %u: %s\n", error, lodepng_error_text(error));
}
}
*/
}
Avatar::~Avatar() {
@ -452,8 +424,53 @@ void Avatar::simulate(float deltaTime) {
}
}
// Get head position data from network for other people
if (!_isMine) {
_head.leanSideways = getHeadLeanSideways();
_head.leanForward = getHeadLeanForward();
}
//apply the head lean values to the springy position...
if (fabs(_head.leanSideways + _head.leanForward) > 0.0f) {
glm::vec3 headLean =
_orientation.getRight() * _head.leanSideways +
_orientation.getFront() * _head.leanForward;
// this is not a long-term solution, but it works ok for initial purposes of making the avatar lean
_joint[ AVATAR_JOINT_TORSO ].springyPosition += headLean * 0.1f;
_joint[ AVATAR_JOINT_CHEST ].springyPosition += headLean * 0.4f;
_joint[ AVATAR_JOINT_NECK_BASE ].springyPosition += headLean * 0.7f;
_joint[ AVATAR_JOINT_HEAD_BASE ].springyPosition += headLean * 1.0f;
_joint[ AVATAR_JOINT_LEFT_COLLAR ].springyPosition += headLean * 0.6f;
_joint[ AVATAR_JOINT_LEFT_SHOULDER ].springyPosition += headLean * 0.6f;
_joint[ AVATAR_JOINT_LEFT_ELBOW ].springyPosition += headLean * 0.2f;
_joint[ AVATAR_JOINT_LEFT_WRIST ].springyPosition += headLean * 0.1f;
_joint[ AVATAR_JOINT_LEFT_FINGERTIPS ].springyPosition += headLean * 0.0f;
_joint[ AVATAR_JOINT_RIGHT_COLLAR ].springyPosition += headLean * 0.6f;
_joint[ AVATAR_JOINT_RIGHT_SHOULDER ].springyPosition += headLean * 0.6f;
_joint[ AVATAR_JOINT_RIGHT_ELBOW ].springyPosition += headLean * 0.2f;
_joint[ AVATAR_JOINT_RIGHT_WRIST ].springyPosition += headLean * 0.1f;
_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].springyPosition += headLean * 0.0f;
}
// update head state
updateHead(deltaTime);
//updateHead(deltaTime);
_head.setPositionRotationAndScale
(
_joint[ AVATAR_JOINT_HEAD_BASE ].springyPosition,
glm::vec3(_headYaw, _headPitch, _headRoll),
_joint[ AVATAR_JOINT_HEAD_BASE ].radius
);
_head.setAudioLoudness(_audioLoudness);
_head.setSkinColor(glm::vec3(skinColor[0], skinColor[1], skinColor[2]));
_head.simulate(deltaTime, _isMine);
// use speed and angular velocity to determine walking vs. standing
if (_speed + fabs(_bodyYawDelta) > 0.2) {
@ -463,7 +480,6 @@ void Avatar::simulate(float deltaTime) {
}
}
void Avatar::updateHandMovementAndTouching(float deltaTime) {
// reset hand and arm positions according to hand movement
@ -540,7 +556,6 @@ void Avatar::updateHandMovementAndTouching(float deltaTime) {
_avatarTouch.setHoldingHands(false);
}
//if holding hands, apply the appropriate forces
if (_avatarTouch.getHoldingHands()) {
_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].position +=
@ -549,8 +564,8 @@ void Avatar::updateHandMovementAndTouching(float deltaTime) {
- _joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].position
) * 0.5f;
if (distanceBetweenOurHands > 0.2) {
float force = 700.0f * deltaTime;
if (distanceBetweenOurHands > 0.3) {
float force = 10.0f * deltaTime;
if (force > 1.0f) {force = 1.0f;}
_velocity += vectorFromMyHandToYourHand * force;
}
@ -579,37 +594,7 @@ void Avatar::updateHandMovementAndTouching(float deltaTime) {
void Avatar::updateHead(float deltaTime) {
// Get head position data from network for other people
if (!_isMine) {
_head.leanSideways = getHeadLeanSideways();
_head.leanForward = getHeadLeanForward();
}
//apply the head lean values to the springy position...
if (fabs(_head.leanSideways + _head.leanForward) > 0.0f) {
glm::vec3 headLean =
_orientation.getRight() * _head.leanSideways +
_orientation.getFront() * _head.leanForward;
// this is not a long-term solution, but it works ok for initial purposes of making the avatar lean
_joint[ AVATAR_JOINT_TORSO ].springyPosition += headLean * 0.1f;
_joint[ AVATAR_JOINT_CHEST ].springyPosition += headLean * 0.4f;
_joint[ AVATAR_JOINT_NECK_BASE ].springyPosition += headLean * 0.7f;
_joint[ AVATAR_JOINT_HEAD_BASE ].springyPosition += headLean * 1.0f;
_joint[ AVATAR_JOINT_LEFT_COLLAR ].springyPosition += headLean * 0.6f;
_joint[ AVATAR_JOINT_LEFT_SHOULDER ].springyPosition += headLean * 0.6f;
_joint[ AVATAR_JOINT_LEFT_ELBOW ].springyPosition += headLean * 0.2f;
_joint[ AVATAR_JOINT_LEFT_WRIST ].springyPosition += headLean * 0.1f;
_joint[ AVATAR_JOINT_LEFT_FINGERTIPS ].springyPosition += headLean * 0.0f;
_joint[ AVATAR_JOINT_RIGHT_COLLAR ].springyPosition += headLean * 0.6f;
_joint[ AVATAR_JOINT_RIGHT_SHOULDER ].springyPosition += headLean * 0.6f;
_joint[ AVATAR_JOINT_RIGHT_ELBOW ].springyPosition += headLean * 0.2f;
_joint[ AVATAR_JOINT_RIGHT_WRIST ].springyPosition += headLean * 0.1f;
_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].springyPosition += headLean * 0.0f;
}
/*
// Decay head back to center if turned on
if (_isMine && _returnHeadToCenter) {
// Decay back toward center
@ -712,6 +697,7 @@ void Avatar::updateHead(float deltaTime) {
const float AUDIO_AVERAGING_SECS = 0.05;
_head.averageLoudness = (1.f - deltaTime / AUDIO_AVERAGING_SECS) * _head.averageLoudness +
(deltaTime / AUDIO_AVERAGING_SECS) * _audioLoudness;
*/
}
@ -894,7 +880,8 @@ void Avatar::render(bool lookingInMirror, glm::vec3 cameraPosition) {
// render head
if (_displayingHead) {
renderHead(lookingInMirror);
//renderHead(lookingInMirror);
_head.render(lookingInMirror, _bodyYaw);
}
// if this is my avatar, then render my interactions with the other avatar
@ -952,7 +939,10 @@ void Avatar::render(bool lookingInMirror, glm::vec3 cameraPosition) {
}
}
void Avatar::renderHead(bool lookingInMirror) {
/*
int side = 0;
glEnable(GL_DEPTH_TEST);
@ -1075,7 +1065,7 @@ void Avatar::renderHead(bool lookingInMirror) {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gluQuadricOrientation(_sphere, GLU_OUTSIDE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iris_texture_width, iris_texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &iris_texture[0]);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iris_texture_width, iris_texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &iris_texture[0]);
}
glPushMatrix();
@ -1121,8 +1111,10 @@ void Avatar::renderHead(bool lookingInMirror) {
glPopMatrix();
*/
}
void Avatar::setHandMovementValues(glm::vec3 handOffset) {
_movedHandOffset = handOffset;
}
@ -1479,10 +1471,12 @@ void Avatar::renderBody() {
}
}
/*
void Avatar::SetNewHeadTarget(float pitch, float yaw) {
_head.pitchTarget = pitch;
_head.yawTarget = yaw;
}
*/
//
// Process UDP interface data from Android transmitter or Google Glass
@ -1522,12 +1516,12 @@ void Avatar::processTransmitterData(unsigned char* packetData, int numBytes) {
// If first packet received, note time, turn head spring return OFF, get start rotation
gettimeofday(&_transmitterTimer, NULL);
if (deviceType == DEVICE_GLASS) {
setHeadReturnToCenter(true);
_head.setReturnToCenter(true);
setHeadSpringScale(10.f);
printLog("Using Google Glass to drive head, springs ON.\n");
} else {
setHeadReturnToCenter(false);
_head.setReturnToCenter(false);
printLog("Using Transmitter %s to drive head, springs OFF.\n", device);
}

View file

@ -18,8 +18,9 @@
#include "InterfaceConfig.h"
#include "SerialInterface.h"
#include "Balls.h"
#include "Head.h"
enum eyeContactTargets {LEFT_EYE, RIGHT_EYE, MOUTH};
//enum eyeContactTargets {LEFT_EYE, RIGHT_EYE, MOUTH};
enum DriveKeys
{
@ -127,7 +128,7 @@ public:
float getAverageLoudness() {return _head.averageLoudness;};
void setAverageLoudness(float al) {_head.averageLoudness = al;};
void SetNewHeadTarget(float, float);
//void SetNewHeadTarget(float, float);
// Set what driving keys are being pressed to control thrust levels
void setDriveKeys(int key, bool val) { _driveKeys[key] = val; };
@ -165,48 +166,7 @@ private:
bool isCollidable; // when false, the joint position will not register a collision
};
struct AvatarHead
{
float pitchRate;
float yawRate;
float rollRate;
float noise;
float eyeballPitch[2];
float eyeballYaw [2];
float eyebrowPitch[2];
float eyebrowRoll [2];
float eyeballScaleX;
float eyeballScaleY;
float eyeballScaleZ;
float interPupilDistance;
float interBrowDistance;
float nominalPupilSize;
float pupilSize;
float mouthPitch;
float mouthYaw;
float mouthWidth;
float mouthHeight;
float leanForward;
float leanSideways;
float pitchTarget;
float yawTarget;
float noiseEnvelope;
float pupilConverge;
float scale;
int eyeContact;
float browAudioLift;
eyeContactTargets eyeContactTarget;
// Sound loudness information
float lastLoudness;
float averageLoudness;
float audioAttack;
// Strength of return springs
float returnSpringScale;
};
AvatarHead _head;
Head _head;
bool _isMine;
glm::vec3 _TEST_bigSpherePosition;
float _TEST_bigSphereRadius;
@ -239,7 +199,7 @@ private:
Balls* _balls;
AvatarTouch _avatarTouch;
bool _displayingHead; // should be false if in first-person view
bool _returnHeadToCenter;
//bool _returnHeadToCenter;
float _distanceToNearestAvatar; // How close is the nearest avatar?
glm::vec3 _gravity;
@ -257,10 +217,6 @@ private:
void applyCollisionWithOtherAvatar( Avatar * other, float deltaTime );
void setHeadFromGyros(glm::vec3 * eulerAngles, glm::vec3 * angularVelocity, float deltaTime, float smoothingTime);
void setHeadSpringScale(float s) { _head.returnSpringScale = s; }
// Do you want head to try to return to center (depends on interface detected)
void setHeadReturnToCenter(bool r) { _returnHeadToCenter = r; };
const bool getHeadReturnToCenter() const { return _returnHeadToCenter; };
};
#endif

394
interface/src/Head.cpp Normal file
View file

@ -0,0 +1,394 @@
//
// Head.cpp
// hifi
//
// Created by Jeffrey on May, 10, 2013
//
// To avoid code bloat in Avatar.cpp, and to keep all head-related code in one place,
// I have started moving the head code over to its own class. But it's gonna require
// some work - so this is not done yet.
#include "Head.h"
#include <vector>
#include <SharedUtil.h>
#include <lodepng.h>
using namespace std;
const float HEAD_MOTION_DECAY = 0.1;
float _browColor [] = {210.0/255.0, 105.0/255.0, 30.0/255.0};
float _mouthColor[] = {1, 0, 0};
float _BrowRollAngle [5] = {0, 15, 30, -30, -15};
float _BrowPitchAngle[3] = {-70, -60, -50};
float _eyeColor [3] = {1,1,1};
float _MouthWidthChoices[3] = {0.5, 0.77, 0.3};
float _browWidth = 0.8;
float _browThickness = 0.16;
char iris_texture_file[] = "resources/images/green_eye.png";
vector<unsigned char> iris_texture;
unsigned int iris_texture_width = 512;
unsigned int iris_texture_height = 256;
Head::Head() {
if (iris_texture.size() == 0) {
switchToResourcesParentIfRequired();
unsigned error = lodepng::decode(iris_texture, iris_texture_width, iris_texture_height, iris_texture_file);
if (error != 0) {
printLog("error %u: %s\n", error, lodepng_error_text(error));
}
}
}
void Head::initialize() {
audioLoudness = 0.0;
skinColor = glm::vec3(0.0f, 0.0f, 0.0f);
position = glm::vec3(0.0f, 0.0f, 0.0f);
yaw = 0.0f;
pitch = 0.0f;
roll = 0.0f;
pupilSize = 0.10;
interPupilDistance = 0.6;
interBrowDistance = 0.75;
nominalPupilSize = 0.10;
pitchRate = 0.0;
yawRate = 0.0;
rollRate = 0.0;
eyebrowPitch[0] = -30;
eyebrowPitch[1] = -30;
eyebrowRoll [0] = 20;
eyebrowRoll [1] = -20;
mouthPitch = 0;
mouthYaw = 0;
mouthWidth = 1.0;
mouthHeight = 0.2;
eyeballPitch[0] = 0;
eyeballPitch[1] = 0;
eyeballScaleX = 1.2;
eyeballScaleY = 1.5;
eyeballScaleZ = 1.0;
eyeballYaw[0] = 0;
eyeballYaw[1] = 0;
pitchTarget = 0;
yawTarget = 0;
noiseEnvelope = 1.0;
pupilConverge = 10.0;
leanForward = 0.0;
leanSideways = 0.0;
eyeContact = 1;
eyeContactTarget = LEFT_EYE;
scale = 1.0;
audioAttack = 0.0;
averageLoudness = 0.0;
lastLoudness = 0.0;
browAudioLift = 0.0;
noise = 0;
returnSpringScale = 1.0;
sphere = NULL;
/*
if (iris_texture.size() == 0) {
switchToResourcesParentIfRequired();
unsigned error = lodepng::decode(iris_texture, iris_texture_width, iris_texture_height, iris_texture_file);
if (error != 0) {
printLog("error %u: %s\n", error, lodepng_error_text(error));
}
}
*/
}
void Head::setPositionRotationAndScale(glm::vec3 p, glm::vec3 r, float s) {
position = p;
//rotation = r;
scale = s;
yaw = r.x;
pitch = r.y;
roll = r.z;
}
void Head::setSkinColor(glm::vec3 c) {
skinColor = c;
}
void Head::setAudioLoudness(float loudness) {
audioLoudness = loudness;
}
void Head::setNewTarget(float pitch, float yaw) {
pitchTarget = pitch;
yawTarget = yaw;
}
void Head::simulate(float deltaTime, bool isMine) {
// Decay head back to center if turned on
if (isMine && returnHeadToCenter) {
// Decay back toward center
pitch *= (1.0f - HEAD_MOTION_DECAY * returnSpringScale * 2 * deltaTime);
yaw *= (1.0f - HEAD_MOTION_DECAY * returnSpringScale * 2 * deltaTime);
roll *= (1.0f - HEAD_MOTION_DECAY * returnSpringScale * 2 * deltaTime);
}
// For invensense gyro, decay only slightly when roughly centered
if (isMine) {
const float RETURN_RANGE = 15.0;
const float RETURN_STRENGTH = 2.0;
if (fabs(pitch) < RETURN_RANGE) { pitch *= (1.0f - RETURN_STRENGTH * deltaTime); }
if (fabs(yaw) < RETURN_RANGE) { yaw *= (1.0f - RETURN_STRENGTH * deltaTime); }
if (fabs(roll) < RETURN_RANGE) { roll *= (1.0f - RETURN_STRENGTH * deltaTime); }
}
if (noise) {
// Move toward new target
pitch += (pitchTarget - pitch) * 10 * deltaTime; // (1.f - DECAY*deltaTime)*Pitch + ;
yaw += (yawTarget - yaw ) * 10 * deltaTime; // (1.f - DECAY*deltaTime);
roll *= 1.f - (HEAD_MOTION_DECAY * deltaTime);
}
leanForward *= (1.f - HEAD_MOTION_DECAY * 30 * deltaTime);
leanSideways *= (1.f - HEAD_MOTION_DECAY * 30 * deltaTime);
// Update where the avatar's eyes are
//
// First, decide if we are making eye contact or not
if (randFloat() < 0.005) {
eyeContact = !eyeContact;
eyeContact = 1;
if (!eyeContact) {
// If we just stopped making eye contact,move the eyes markedly away
eyeballPitch[0] = eyeballPitch[1] = eyeballPitch[0] + 5.0 + (randFloat() - 0.5) * 10;
eyeballYaw [0] = eyeballYaw [1] = eyeballYaw [0] + 5.0 + (randFloat() - 0.5) * 5;
} else {
// If now making eye contact, turn head to look right at viewer
setNewTarget(0,0);
}
}
const float DEGREES_BETWEEN_VIEWER_EYES = 3;
const float DEGREES_TO_VIEWER_MOUTH = 7;
if (eyeContact) {
// Should we pick a new eye contact target?
if (randFloat() < 0.01) {
// Choose where to look next
if (randFloat() < 0.1) {
eyeContactTarget = MOUTH;
} else {
if (randFloat() < 0.5) eyeContactTarget = LEFT_EYE; else eyeContactTarget = RIGHT_EYE;
}
}
// Set eyeball pitch and yaw to make contact
float eye_target_yaw_adjust = 0;
float eye_target_pitch_adjust = 0;
if (eyeContactTarget == LEFT_EYE) eye_target_yaw_adjust = DEGREES_BETWEEN_VIEWER_EYES;
if (eyeContactTarget == RIGHT_EYE) eye_target_yaw_adjust = -DEGREES_BETWEEN_VIEWER_EYES;
if (eyeContactTarget == MOUTH) eye_target_pitch_adjust = DEGREES_TO_VIEWER_MOUTH;
eyeballPitch[0] = eyeballPitch[1] = -pitch + eye_target_pitch_adjust;
eyeballYaw [0] = eyeballYaw [1] = yaw + eye_target_yaw_adjust;
}
if (noise)
{
pitch += (randFloat() - 0.5) * 0.2 * noiseEnvelope;
yaw += (randFloat() - 0.5) * 0.3 *noiseEnvelope;
//PupilSize += (randFloat() - 0.5) * 0.001*NoiseEnvelope;
if (randFloat() < 0.005) mouthWidth = _MouthWidthChoices[rand()%3];
if (!eyeContact) {
if (randFloat() < 0.01) eyeballPitch[0] = eyeballPitch[1] = (randFloat() - 0.5) * 20;
if (randFloat() < 0.01) eyeballYaw[0] = eyeballYaw[1] = (randFloat()- 0.5) * 10;
}
if ((randFloat() < 0.005) && (fabs(pitchTarget - pitch) < 1.0) && (fabs(yawTarget - yaw) < 1.0)) {
setNewTarget((randFloat()-0.5) * 20.0, (randFloat()-0.5) * 45.0);
}
if (0) {
// Pick new target
pitchTarget = (randFloat() - 0.5) * 45;
yawTarget = (randFloat() - 0.5) * 22;
}
if (randFloat() < 0.01)
{
eyebrowPitch[0] = eyebrowPitch[1] = _BrowPitchAngle[rand()%3];
eyebrowRoll [0] = eyebrowRoll[1] = _BrowRollAngle[rand()%5];
eyebrowRoll [1] *=-1;
}
}
// Update audio trailing average for rendering facial animations
const float AUDIO_AVERAGING_SECS = 0.05;
averageLoudness = (1.f - deltaTime / AUDIO_AVERAGING_SECS) * averageLoudness +
(deltaTime / AUDIO_AVERAGING_SECS) * audioLoudness;
}
void Head::render(bool lookingInMirror, float bodyYaw) {
int side = 0;
glEnable(GL_DEPTH_TEST);
glEnable(GL_RESCALE_NORMAL);
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glScalef(scale, scale, scale);
if (lookingInMirror) {
glRotatef(bodyYaw - yaw, 0, 1, 0);
glRotatef(pitch, 1, 0, 0);
glRotatef(-roll, 0, 0, 1);
} else {
glRotatef(bodyYaw + yaw, 0, 1, 0);
glRotatef(pitch, 1, 0, 0);
glRotatef(roll, 0, 0, 1);
}
//glScalef(2.0, 2.0, 2.0);
glColor3f(skinColor.x, skinColor.y, skinColor.z);
glutSolidSphere(1, 30, 30);
// Ears
glPushMatrix();
glTranslatef(1.0, 0, 0);
for(side = 0; side < 2; side++) {
glPushMatrix();
glScalef(0.3, 0.65, .65);
glutSolidSphere(0.5, 30, 30);
glPopMatrix();
glTranslatef(-2.0, 0, 0);
}
glPopMatrix();
// Update audio attack data for facial animation (eyebrows and mouth)
audioAttack = 0.9 * audioAttack + 0.1 * fabs(audioLoudness - lastLoudness);
lastLoudness = audioLoudness;
const float BROW_LIFT_THRESHOLD = 100;
if (audioAttack > BROW_LIFT_THRESHOLD)
browAudioLift += sqrt(audioAttack) / 1000.0;
browAudioLift *= .90;
// Render Eyebrows
glPushMatrix();
glTranslatef(-interBrowDistance / 2.0,0.4,0.45);
for(side = 0; side < 2; side++) {
glColor3fv(_browColor);
glPushMatrix();
glTranslatef(0, 0.35 + browAudioLift, 0);
glRotatef(eyebrowPitch[side]/2.0, 1, 0, 0);
glRotatef(eyebrowRoll[side]/2.0, 0, 0, 1);
glScalef(_browWidth, _browThickness, 1);
glutSolidCube(0.5);
glPopMatrix();
glTranslatef(interBrowDistance, 0, 0);
}
glPopMatrix();
// Mouth
glPushMatrix();
glTranslatef(0,-0.35,0.75);
glColor3f(0,0,0);
glRotatef(mouthPitch, 1, 0, 0);
glRotatef(mouthYaw, 0, 0, 1);
if (averageLoudness > 1.f) {
glScalef(mouthWidth * (.7f + sqrt(averageLoudness) /60.f),
mouthHeight * (1.f + sqrt(averageLoudness) /30.f), 1);
} else {
glScalef(mouthWidth, mouthHeight, 1);
}
glutSolidCube(0.5);
glPopMatrix();
glTranslatef(0, 1.0, 0);
glTranslatef(-interPupilDistance/2.0,-0.68,0.7);
// Right Eye
glRotatef(-10, 1, 0, 0);
glColor3fv(_eyeColor);
glPushMatrix();
{
glTranslatef(interPupilDistance/10.0, 0, 0.05);
glRotatef(20, 0, 0, 1);
glScalef(eyeballScaleX, eyeballScaleY, eyeballScaleZ);
glutSolidSphere(0.25, 30, 30);
}
glPopMatrix();
// Right Pupil
if (sphere == NULL) {
sphere = gluNewQuadric();
gluQuadricTexture(sphere, GL_TRUE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gluQuadricOrientation(sphere, GLU_OUTSIDE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iris_texture_width, iris_texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &iris_texture[0]);
}
glPushMatrix();
{
glRotatef(eyeballPitch[1], 1, 0, 0);
glRotatef(eyeballYaw[1] + yaw + pupilConverge, 0, 1, 0);
glTranslatef(0,0,.35);
glRotatef(-75,1,0,0);
glScalef(1.0, 0.4, 1.0);
glEnable(GL_TEXTURE_2D);
gluSphere(sphere, pupilSize, 15, 15);
glDisable(GL_TEXTURE_2D);
}
glPopMatrix();
// Left Eye
glColor3fv(_eyeColor);
glTranslatef(interPupilDistance, 0, 0);
glPushMatrix();
{
glTranslatef(-interPupilDistance/10.0, 0, .05);
glRotatef(-20, 0, 0, 1);
glScalef(eyeballScaleX, eyeballScaleY, eyeballScaleZ);
glutSolidSphere(0.25, 30, 30);
}
glPopMatrix();
// Left Pupil
glPushMatrix();
{
glRotatef(eyeballPitch[0], 1, 0, 0);
glRotatef(eyeballYaw[0] + yaw - pupilConverge, 0, 1, 0);
glTranslatef(0, 0, .35);
glRotatef(-75, 1, 0, 0);
glScalef(1.0, 0.4, 1.0);
glEnable(GL_TEXTURE_2D);
gluSphere(sphere, pupilSize, 15, 15);
glDisable(GL_TEXTURE_2D);
}
glPopMatrix();
glPopMatrix();
}

94
interface/src/Head.h Normal file
View file

@ -0,0 +1,94 @@
//
// Head.h
// hifi
//
// Created by Jeffrey on May, 10, 2013
//
// To avoid code bloat in Avatar.cpp, and to keep all head-related code in one place,
// I have started moving the head code over to its own class. But it's gonna require
// some work - so this is not done yet.
#ifndef hifi_Head_h
#define hifi_Head_h
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <AvatarData.h>
#include "world.h"
#include "InterfaceConfig.h"
#include "SerialInterface.h"
enum eyeContactTargets {LEFT_EYE, RIGHT_EYE, MOUTH};
class Head {
public:
Head();
void initialize();
void simulate(float deltaTime, bool isMine);
void setPositionRotationAndScale(glm::vec3 position, glm::vec3 rotation, float scale);
void setSkinColor(glm::vec3 color);
void setAudioLoudness(float loudness);
void render(bool lookingInMirror, float bodyYaw);
void setNewTarget(float, float);
// Do you want head to try to return to center (depends on interface detected)
void setReturnToCenter(bool r) { returnHeadToCenter = r; }
const bool getReturnToCenter() const { return returnHeadToCenter; }
//private:
// I am making these public for now - just to get the code moved over quickly!
bool returnHeadToCenter;
float audioLoudness;
glm::vec3 skinColor;
glm::vec3 position;
glm::vec3 rotation;
float yaw;
float pitch;
float roll;
float pitchRate;
float yawRate;
float rollRate;
float noise;
float eyeballPitch[2];
float eyeballYaw [2];
float eyebrowPitch[2];
float eyebrowRoll [2];
float eyeballScaleX;
float eyeballScaleY;
float eyeballScaleZ;
float interPupilDistance;
float interBrowDistance;
float nominalPupilSize;
float pupilSize;
float mouthPitch;
float mouthYaw;
float mouthWidth;
float mouthHeight;
float leanForward;
float leanSideways;
float pitchTarget;
float yawTarget;
float noiseEnvelope;
float pupilConverge;
float scale;
int eyeContact;
float browAudioLift;
eyeContactTargets eyeContactTarget;
// Sound loudness information
float lastLoudness;
float averageLoudness;
float audioAttack;
GLUquadric* sphere;
// Strength of return springs
float returnSpringScale;
};
#endif