Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Philip Rosedale 2013-05-09 15:06:36 -07:00
commit e631208850
12 changed files with 288 additions and 335 deletions

View file

@ -22,7 +22,6 @@
#include <AgentTypes.h> #include <AgentTypes.h>
#include <SharedUtil.h> #include <SharedUtil.h>
#include <StdDev.h> #include <StdDev.h>
#include <Stacktrace.h>
#include "AudioRingBuffer.h" #include "AudioRingBuffer.h"
#include "PacketHeaders.h" #include "PacketHeaders.h"
@ -72,23 +71,42 @@ void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) {
mixSample = normalizedSample; mixSample = normalizedSample;
} }
void *sendBuffer(void *args) { void attachNewBufferToAgent(Agent *newAgent) {
int sentBytes; if (!newAgent->getLinkedData()) {
newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL));
}
}
int main(int argc, const char* argv[]) {
setvbuf(stdout, NULL, _IOLBF, 0);
AgentList* agentList = AgentList::createInstance(AGENT_TYPE_AUDIO_MIXER, MIXER_LISTEN_PORT);
ssize_t receivedBytes = 0;
agentList->linkedDataCreateCallback = attachNewBufferToAgent;
agentList->startSilentAgentRemovalThread();
agentList->startDomainServerCheckInThread();
unsigned char* packetData = new unsigned char[MAX_PACKET_SIZE];
sockaddr* agentAddress = new sockaddr;
// make sure our agent socket is non-blocking
agentList->getAgentSocket().setBlocking(false);
int nextFrame = 0; int nextFrame = 0;
timeval startTime; timeval startTime;
AgentList* agentList = AgentList::getInstance();
gettimeofday(&startTime, NULL); gettimeofday(&startTime, NULL);
while (true) { while (true) {
sentBytes = 0; // enumerate the agents, check if we can add audio from the agent to current mix
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData();
if (agentBuffer != NULL && agentBuffer->getEndOfLastWrite() != NULL) { if (agentBuffer->getEndOfLastWrite()) {
if (!agentBuffer->isStarted() if (!agentBuffer->isStarted()
&& agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) { && agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) {
printf("Held back buffer for agent with ID %d.\n", agent->getAgentId()); printf("Held back buffer for agent with ID %d.\n", agent->getAgentId());
@ -108,10 +126,10 @@ void *sendBuffer(void *args) {
int numAgents = agentList->size(); int numAgents = agentList->size();
float distanceCoefficients[numAgents][numAgents]; float distanceCoefficients[numAgents][numAgents];
memset(distanceCoefficients, 0, sizeof(distanceCoefficients)); memset(distanceCoefficients, 0, sizeof(distanceCoefficients));
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData(); AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData();
int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {}; int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {};
for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) { for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) {
@ -119,15 +137,15 @@ void *sendBuffer(void *args) {
AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData(); AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData();
if (otherAgentBuffer->shouldBeAddedToMix()) { if (otherAgentBuffer->shouldBeAddedToMix()) {
float bearingRelativeAngleToSource = 0.f; float bearingRelativeAngleToSource = 0.f;
float attenuationCoefficient = 1.f; float attenuationCoefficient = 1.f;
int numSamplesDelay = 0; int numSamplesDelay = 0;
float weakChannelAmplitudeRatio = 1.f; float weakChannelAmplitudeRatio = 1.f;
if (otherAgent != agent) { if (otherAgent != agent) {
float *agentPosition = agentRingBuffer->getPosition(); Position agentPosition = agentRingBuffer->getPosition();
float *otherAgentPosition = otherAgentBuffer->getPosition(); Position otherAgentPosition = otherAgentBuffer->getPosition();
// calculate the distance to the other agent // calculate the distance to the other agent
@ -136,31 +154,32 @@ void *sendBuffer(void *args) {
int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex()); int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex());
if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) { if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) {
float distanceToAgent = sqrtf(powf(agentPosition[0] - otherAgentPosition[0], 2) + float distanceToAgent = sqrtf(powf(agentPosition.x - otherAgentPosition.x, 2) +
powf(agentPosition[1] - otherAgentPosition[1], 2) + powf(agentPosition.y - otherAgentPosition.y, 2) +
powf(agentPosition[2] - otherAgentPosition[2], 2)); powf(agentPosition.z - otherAgentPosition.z, 2));
float minCoefficient = std::min(1.0f, float minCoefficient = std::min(1.0f,
powf(0.5, (logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1)); powf(0.5,
(logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1));
distanceCoefficients[lowAgentIndex][highAgentIndex] = minCoefficient; distanceCoefficients[lowAgentIndex][highAgentIndex] = minCoefficient;
} }
// get the angle from the right-angle triangle // get the angle from the right-angle triangle
float triangleAngle = atan2f(fabsf(agentPosition[2] - otherAgentPosition[2]), float triangleAngle = atan2f(fabsf(agentPosition.z - otherAgentPosition.z),
fabsf(agentPosition[0] - otherAgentPosition[0])) * (180 / M_PI); fabsf(agentPosition.x - otherAgentPosition.x)) * (180 / M_PI);
float absoluteAngleToSource = 0; float absoluteAngleToSource = 0;
bearingRelativeAngleToSource = 0; bearingRelativeAngleToSource = 0;
// find the angle we need for calculation based on the orientation of the triangle // find the angle we need for calculation based on the orientation of the triangle
if (otherAgentPosition[0] > agentPosition[0]) { if (otherAgentPosition.x > agentPosition.x) {
if (otherAgentPosition[2] > agentPosition[2]) { if (otherAgentPosition.z > agentPosition.z) {
absoluteAngleToSource = -90 + triangleAngle; absoluteAngleToSource = -90 + triangleAngle;
} else { } else {
absoluteAngleToSource = -90 - triangleAngle; absoluteAngleToSource = -90 - triangleAngle;
} }
} else { } else {
if (otherAgentPosition[2] > agentPosition[2]) { if (otherAgentPosition.z > agentPosition.z) {
absoluteAngleToSource = 90 - triangleAngle; absoluteAngleToSource = 90 - triangleAngle;
} else { } else {
absoluteAngleToSource = 90 + triangleAngle; absoluteAngleToSource = 90 + triangleAngle;
@ -184,11 +203,11 @@ void *sendBuffer(void *args) {
} }
float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION + float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION +
(OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f)); (OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f));
attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex] attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex]
* otherAgentBuffer->getAttenuationRatio() * otherAgentBuffer->getAttenuationRatio()
* offAxisCoefficient; * offAxisCoefficient;
bearingRelativeAngleToSource *= (M_PI / 180); bearingRelativeAngleToSource *= (M_PI / 180);
@ -197,13 +216,17 @@ void *sendBuffer(void *args) {
weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio); weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio);
} }
int16_t* goodChannel = bearingRelativeAngleToSource > 0.0f ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL : clientMix; int16_t* goodChannel = bearingRelativeAngleToSource > 0.0f
int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f ? clientMix : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL
: clientMix;
int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f
? clientMix
: clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer() int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer()
? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay ? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay
: otherAgentBuffer->getNextOutput() - numSamplesDelay; : otherAgentBuffer->getNextOutput() - numSamplesDelay;
for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) { for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) {
if (s < numSamplesDelay) { if (s < numSamplesDelay) {
@ -227,9 +250,10 @@ void *sendBuffer(void *args) {
agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES); agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES);
} }
// push forward the next output pointers for any audio buffers we used
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData();
if (agentBuffer->shouldBeAddedToMix()) { if (agentBuffer && agentBuffer->shouldBeAddedToMix()) {
agentBuffer->setNextOutput(agentBuffer->getNextOutput() + BUFFER_LENGTH_SAMPLES_PER_CHANNEL); agentBuffer->setNextOutput(agentBuffer->getNextOutput() + BUFFER_LENGTH_SAMPLES_PER_CHANNEL);
if (agentBuffer->getNextOutput() >= agentBuffer->getBuffer() + RING_BUFFER_SAMPLES) { if (agentBuffer->getNextOutput() >= agentBuffer->getBuffer() + RING_BUFFER_SAMPLES) {
@ -240,46 +264,8 @@ void *sendBuffer(void *args) {
} }
} }
double usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow(); // pull any new audio data from agents off of the network stack
while (agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
if (usecToSleep > 0) {
usleep(usecToSleep);
} else {
std::cout << "Took too much time, not sleeping!\n";
}
}
pthread_exit(0);
}
void attachNewBufferToAgent(Agent *newAgent) {
if (newAgent->getLinkedData() == NULL) {
newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL));
}
}
int main(int argc, const char* argv[]) {
signal(SIGSEGV, printStacktrace);
setvbuf(stdout, NULL, _IOLBF, 0);
AgentList* agentList = AgentList::createInstance(AGENT_TYPE_AUDIO_MIXER, MIXER_LISTEN_PORT);
ssize_t receivedBytes = 0;
agentList->linkedDataCreateCallback = attachNewBufferToAgent;
agentList->startSilentAgentRemovalThread();
agentList->startDomainServerCheckInThread();
unsigned char *packetData = new unsigned char[MAX_PACKET_SIZE];
pthread_t sendBufferThread;
pthread_create(&sendBufferThread, NULL, sendBuffer, NULL);
sockaddr *agentAddress = new sockaddr;
while (true) {
if(agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
if (packetData[0] == PACKET_HEADER_INJECT_AUDIO) { if (packetData[0] == PACKET_HEADER_INJECT_AUDIO) {
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentID())) { if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentID())) {
@ -289,9 +275,15 @@ int main(int argc, const char* argv[]) {
agentList->updateAgentWithData(agentAddress, packetData, receivedBytes); agentList->updateAgentWithData(agentAddress, packetData, receivedBytes);
} }
} }
double usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
if (usecToSleep > 0) {
usleep(usecToSleep);
} else {
std::cout << "Took too much time, not sleeping!\n";
}
} }
pthread_join(sendBufferThread, NULL);
return 0; return 0;
} }

View file

@ -30,7 +30,7 @@ const float BODY_SPIN_FRICTION = 5.0;
const float BODY_UPRIGHT_FORCE = 10.0; const float BODY_UPRIGHT_FORCE = 10.0;
const float BODY_PITCH_WHILE_WALKING = 30.0; const float BODY_PITCH_WHILE_WALKING = 30.0;
const float BODY_ROLL_WHILE_TURNING = 0.1; const float BODY_ROLL_WHILE_TURNING = 0.1;
const float LIN_VEL_DECAY = 2.0; const float VELOCITY_DECAY = 5.0;
const float MY_HAND_HOLDING_PULL = 0.2; const float MY_HAND_HOLDING_PULL = 0.2;
const float YOUR_HAND_HOLDING_PULL = 1.0; const float YOUR_HAND_HOLDING_PULL = 1.0;
const float BODY_SPRING_DEFAULT_TIGHTNESS = 1500.0f; const float BODY_SPRING_DEFAULT_TIGHTNESS = 1500.0f;
@ -47,6 +47,8 @@ const float HEAD_MAX_PITCH = 45;
const float HEAD_MIN_PITCH = -45; const float HEAD_MIN_PITCH = -45;
const float HEAD_MAX_YAW = 85; const float HEAD_MAX_YAW = 85;
const float HEAD_MIN_YAW = -85; const float HEAD_MIN_YAW = -85;
const float AVATAR_BRAKING_RANGE = 1.6f;
const float AVATAR_BRAKING_STRENGTH = 30.0f;
float skinColor [] = {1.0, 0.84, 0.66}; float skinColor [] = {1.0, 0.84, 0.66};
float lightBlue [] = {0.7, 0.8, 1.0}; float lightBlue [] = {0.7, 0.8, 1.0};
@ -403,23 +405,33 @@ void Avatar::simulate(float deltaTime) {
if (tiltDecay < 0.0f) {tiltDecay = 0.0f;} if (tiltDecay < 0.0f) {tiltDecay = 0.0f;}
_bodyPitch *= tiltDecay; _bodyPitch *= tiltDecay;
_bodyRoll *= tiltDecay; _bodyRoll *= tiltDecay;
//the following will be used to make the avatar upright no matter what gravity is
//float f = angleBetween(_orientation.getUp(), _gravity);
// update position by velocity // update position by velocity
_position += _velocity * deltaTime; _position += _velocity * deltaTime;
// decay velocity // decay velocity
_velocity *= (1.0 - LIN_VEL_DECAY * deltaTime); float decay = 1.0 - VELOCITY_DECAY * deltaTime;
if ( decay < 0.0 ) {
// If someone is near, damp velocity as a function of closeness _velocity = glm::vec3( 0.0f, 0.0f, 0.0f );
const float AVATAR_BRAKING_RANGE = 1.6f; } else {
const float AVATAR_BRAKING_STRENGTH = 35.f; _velocity *= decay;
if (_isMine && (_distanceToNearestAvatar < AVATAR_BRAKING_RANGE)) {
_velocity *=
(1.f - deltaTime * AVATAR_BRAKING_STRENGTH *
(AVATAR_BRAKING_RANGE - _distanceToNearestAvatar));
} }
// update head information // If another avatar is near, dampen velocity as a function of closeness
if (_isMine && (_distanceToNearestAvatar < AVATAR_BRAKING_RANGE)) {
float closeness = 1.0f - (_distanceToNearestAvatar / AVATAR_BRAKING_RANGE);
float drag = 1.0f - closeness * AVATAR_BRAKING_STRENGTH * deltaTime;
if ( drag > 0.0f ) {
_velocity *= drag;
} else {
_velocity = glm::vec3( 0.0f, 0.0f, 0.0f );
}
}
// update head state
updateHead(deltaTime); updateHead(deltaTime);
// use speed and angular velocity to determine walking vs. standing // use speed and angular velocity to determine walking vs. standing
@ -458,8 +470,7 @@ void Avatar::updateHandMovementAndTouching(float deltaTime) {
// Test: Show angle between your fwd vector and nearest avatar // Test: Show angle between your fwd vector and nearest avatar
glm::vec3 vectorBetweenUs = otherAvatar->getJointPosition(AVATAR_JOINT_PELVIS) - glm::vec3 vectorBetweenUs = otherAvatar->getJointPosition(AVATAR_JOINT_PELVIS) -
getJointPosition(AVATAR_JOINT_PELVIS); getJointPosition(AVATAR_JOINT_PELVIS);
glm::vec3 myForwardVector = _orientation.getFront(); printLog("Angle between: %f\n", angleBetween(vectorBetweenUs, _orientation.getFront()));
printLog("Angle between: %f\n", angleBetween(&vectorBetweenUs, &myForwardVector));
*/ */
// test whether shoulders are close enough to allow for reaching to touch hands // test whether shoulders are close enough to allow for reaching to touch hands
@ -474,8 +485,13 @@ void Avatar::updateHandMovementAndTouching(float deltaTime) {
if (_interactingOther) { if (_interactingOther) {
_avatarTouch.setYourBodyPosition(_interactingOther->_position); _avatarTouch.setYourBodyPosition(_interactingOther->_position);
_avatarTouch.setYourHandPosition(_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].springyPosition); _avatarTouch.setYourHandPosition(_interactingOther->_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].springyPosition);
_avatarTouch.setYourHandState (_interactingOther->_handState); _avatarTouch.setYourHandState (_interactingOther->_handState);
if ( _avatarTouch.getAbleToReachOtherAvatar()) {
_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].position =
_interactingOther->_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].springyPosition;
}
} }
}//if (_isMine) }//if (_isMine)
@ -495,23 +511,45 @@ void Avatar::updateHandMovementAndTouching(float deltaTime) {
} }
_avatarTouch.setMyHandState(_handState); _avatarTouch.setMyHandState(_handState);
_avatarTouch.setMyHandPosition(_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].springyPosition);
if (_handState == 1) {
_avatarTouch.setMyHandPosition(_joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].springyPosition);
}
} }
} }
void Avatar::updateHead(float deltaTime) { void Avatar::updateHead(float deltaTime) {
// hold on to this - used for testing....
/*
static float test = 0.0f;
test += deltaTime;
_head.leanForward = 0.02 * sin( test * 0.2f );
_head.leanSideways = 0.02 * sin( test * 0.3f );
*/
//apply the head lean values to the springy position... //apply the head lean values to the springy position...
if (fabs(_head.leanSideways + _head.leanForward) > 0.0f) { if (fabs(_head.leanSideways + _head.leanForward) > 0.0f) {
glm::vec3 headLean = glm::vec3 headLean =
_orientation.getRight() * _head.leanSideways + _orientation.getRight() * _head.leanSideways +
_orientation.getFront() * _head.leanForward; _orientation.getFront() * _head.leanForward;
_joint[ AVATAR_JOINT_HEAD_BASE ].springyPosition += headLean;
// this is not a long-term solution, but it works ok for initial purposes...
_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 // Decay head back to center if turned on
if (_returnHeadToCenter) { if (_returnHeadToCenter) {
// Decay back toward center // Decay back toward center

View file

@ -16,13 +16,14 @@ const float THREAD_RADIUS = 0.012;
AvatarTouch::AvatarTouch() { AvatarTouch::AvatarTouch() {
_myHandPosition = glm::vec3(0.0f, 0.0f, 0.0f); _myHandPosition = glm::vec3(0.0f, 0.0f, 0.0f);
_yourHandPosition = glm::vec3(0.0f, 0.0f, 0.0f); _yourHandPosition = glm::vec3(0.0f, 0.0f, 0.0f);
_myBodyPosition = glm::vec3(0.0f, 0.0f, 0.0f); _myBodyPosition = glm::vec3(0.0f, 0.0f, 0.0f);
_yourBodyPosition = glm::vec3(0.0f, 0.0f, 0.0f); _yourBodyPosition = glm::vec3(0.0f, 0.0f, 0.0f);
_myHandState = 0; _vectorBetweenHands = glm::vec3(0.0f, 0.0f, 0.0f);
_yourHandState = 0; _myHandState = 0;
_reachableRadius = 0.0f; _yourHandState = 0;
_reachableRadius = 0.0f;
_canReachToOtherAvatar = false; _canReachToOtherAvatar = false;
_handsCloseEnoughToGrasp = false; _handsCloseEnoughToGrasp = false;
@ -60,16 +61,17 @@ void AvatarTouch::setReachableRadius(float r) {
_reachableRadius = r; _reachableRadius = r;
} }
void AvatarTouch::render(glm::vec3 cameraPosition) { void AvatarTouch::render(glm::vec3 cameraPosition) {
if (_canReachToOtherAvatar) { if (_canReachToOtherAvatar) {
glColor4f(0.3, 0.4, 0.5, 0.5); glColor4f(0.3, 0.4, 0.5, 0.5);
glm::vec3 p(_yourBodyPosition); glm::vec3 p(_yourBodyPosition);
p.y = 0.0005f; p.y = 0.0005f;
renderCircle(p, _reachableRadius, glm::vec3(0.0f, 1.0f, 0.0f), 30); renderCircle(p, _reachableRadius, glm::vec3(0.0f, 1.0f, 0.0f), 30);
renderBeamBetweenHands();
// if your hand is grasping, show it... // if your hand is grasping, show it...
if (_yourHandState == 1) { if (_yourHandState == 1) {
glPushMatrix(); glPushMatrix();
@ -79,28 +81,7 @@ void AvatarTouch::render(glm::vec3 cameraPosition) {
glColor4f(1.0, 1.0, 0.2, 0.1); glutSolidSphere(0.030f, 10.0f, 10.0f); glColor4f(1.0, 1.0, 0.2, 0.1); glutSolidSphere(0.030f, 10.0f, 10.0f);
glPopMatrix(); glPopMatrix();
} }
}
//show beam
glm::vec3 v1(_myHandPosition);
glm::vec3 v2(_yourHandPosition);
if (_handsCloseEnoughToGrasp) {
glLineWidth(2.0);
glColor4f(0.7f, 0.4f, 0.1f, 0.3);
glBegin(GL_LINE_STRIP);
glVertex3f(v1.x, v1.y, v1.z);
glVertex3f(v2.x, v2.y, v2.z);
glEnd();
glColor4f(1.0f, 1.0f, 0.0f, 0.8);
for (int p=0; p<NUM_POINTS; p++) {
glBegin(GL_POINTS);
glVertex3f(_point[p].x, _point[p].y, _point[p].z);
glEnd();
}
}
}
// if my hand is grasping, show it... // if my hand is grasping, show it...
if (_myHandState == 1) { if (_myHandState == 1) {
@ -113,27 +94,56 @@ void AvatarTouch::render(glm::vec3 cameraPosition) {
} }
} }
void AvatarTouch::simulate (float deltaTime) { void AvatarTouch::simulate (float deltaTime) {
glm::vec3 v = _yourBodyPosition - _myBodyPosition; _vectorBetweenHands = _yourBodyPosition - _myBodyPosition;
float distance = glm::length(_vectorBetweenHands);
float distance = glm::length(v);
if (distance < _reachableRadius) { if (distance < _reachableRadius) {
_canReachToOtherAvatar = true; _canReachToOtherAvatar = true;
generateBeamBetweenHands();
} else { } else {
_canReachToOtherAvatar = false; _canReachToOtherAvatar = false;
} }
/*
}
void AvatarTouch::generateBeamBetweenHands() {
for (int p=0; p<NUM_POINTS; p++) { for (int p=0; p<NUM_POINTS; p++) {
_point[p] = _myHandPosition + v * ((float)p / (float)NUM_POINTS); _point[p] = _myHandPosition + _vectorBetweenHands * ((float)p / (float)NUM_POINTS);
_point[p].x += randFloatInRange(-THREAD_RADIUS, THREAD_RADIUS); _point[p].x += randFloatInRange(-THREAD_RADIUS, THREAD_RADIUS);
_point[p].y += randFloatInRange(-THREAD_RADIUS, THREAD_RADIUS); _point[p].y += randFloatInRange(-THREAD_RADIUS, THREAD_RADIUS);
_point[p].z += randFloatInRange(-THREAD_RADIUS, THREAD_RADIUS); _point[p].z += randFloatInRange(-THREAD_RADIUS, THREAD_RADIUS);
} }
*/ }
}
void AvatarTouch::renderBeamBetweenHands() {
glm::vec3 v1(_myHandPosition);
glm::vec3 v2(_yourHandPosition);
glLineWidth(2.0);
glColor4f(0.7f, 0.4f, 0.1f, 0.3);
glBegin(GL_LINE_STRIP);
glVertex3f(v1.x, v1.y, v1.z);
glVertex3f(v2.x, v2.y, v2.z);
glEnd();
glColor4f(1.0f, 1.0f, 0.0f, 0.8);
for (int p=0; p<NUM_POINTS; p++) {
glBegin(GL_POINTS);
glVertex3f(_point[p].x, _point[p].y, _point[p].z);
glEnd();
}
}

View file

@ -43,11 +43,15 @@ private:
glm::vec3 _yourBodyPosition; glm::vec3 _yourBodyPosition;
glm::vec3 _myHandPosition; glm::vec3 _myHandPosition;
glm::vec3 _yourHandPosition; glm::vec3 _yourHandPosition;
glm::vec3 _vectorBetweenHands;
int _myHandState; int _myHandState;
int _yourHandState; int _yourHandState;
bool _canReachToOtherAvatar; bool _canReachToOtherAvatar;
bool _handsCloseEnoughToGrasp; bool _handsCloseEnoughToGrasp;
float _reachableRadius; float _reachableRadius;
void generateBeamBetweenHands();
void renderBeamBetweenHands();
}; };
#endif #endif

View file

@ -13,23 +13,23 @@
Camera::Camera() { Camera::Camera() {
_frustumNeedsReshape = false; _frustumNeedsReshape = false;
_mode = CAMERA_MODE_THIRD_PERSON; _mode = CAMERA_MODE_THIRD_PERSON;
_tightness = 10.0; // default _tightness = 10.0; // default
_fieldOfView = 60.0; // default _fieldOfView = 60.0; // default
_nearClip = 0.08; // default _nearClip = 0.08; // default
_farClip = 50.0 * TREE_SCALE; // default _farClip = 50.0 * TREE_SCALE; // default
_modeShift = 0.0; _modeShift = 0.0;
_yaw = 0.0; _yaw = 0.0;
_pitch = 0.0; _pitch = 0.0;
_roll = 0.0; _roll = 0.0;
_upShift = 0.0; _upShift = 0.0;
_rightShift = 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);
_position = glm::vec3(0.0, 0.0, 0.0); _position = glm::vec3(0.0, 0.0, 0.0);
_idealPosition = glm::vec3(0.0, 0.0, 0.0); _idealPosition = glm::vec3(0.0, 0.0, 0.0);
_orientation.setToIdentity(); _orientation.setToIdentity();
} }
@ -38,7 +38,7 @@ void Camera::update(float deltaTime) {
if (_mode == CAMERA_MODE_NULL) { if (_mode == CAMERA_MODE_NULL) {
_modeShift = 0.0; _modeShift = 0.0;
} else { } else {
// use iterative forces to keep the camera at the desired position and angle // use iterative forces to push the camera towards the desired position and angle
updateFollowMode(deltaTime); updateFollowMode(deltaTime);
if (_modeShift < 1.0f) { if (_modeShift < 1.0f) {

View file

@ -67,8 +67,8 @@ float angle_to(glm::vec3 head_pos, glm::vec3 source_pos, float render_yaw, float
} }
// Helper function returns the positive angle in degrees between two 3D vectors // Helper function returns the positive angle in degrees between two 3D vectors
float angleBetween(glm::vec3 * v1, glm::vec3 * v2) { float angleBetween(const glm::vec3& v1, const glm::vec3& v2) {
return acos((glm::dot(*v1, *v2)) / (glm::length(*v1) * glm::length(*v2))) * 180.f / PI; return acos((glm::dot(v1, v2)) / (glm::length(v1) * glm::length(v2))) * 180.f / PI;
} }
// Draw a 3D vector floating in space // Draw a 3D vector floating in space

View file

@ -44,7 +44,7 @@ void noiseTest(int w, int h);
void drawVector(glm::vec3* vector); void drawVector(glm::vec3* vector);
float angleBetween(glm::vec3 * v1, glm::vec3 * v2); float angleBetween(const glm::vec3& v1, const glm::vec3& v2);
double diffclock(timeval *clock1,timeval *clock2); double diffclock(timeval *clock1,timeval *clock2);

View file

@ -1767,9 +1767,7 @@ void idle(void) {
myAvatar.setHandMovementValues(handControl.getValues()); myAvatar.setHandMovementValues(handControl.getValues());
// tell my avatar if the mouse is being pressed... // tell my avatar if the mouse is being pressed...
if (mousePressed) { myAvatar.setMousePressed(mousePressed);
myAvatar.setMousePressed(mousePressed);
}
// walking triggers the handControl to stop // walking triggers the handControl to stop
if (myAvatar.getMode() == AVATAR_MODE_WALKING) { if (myAvatar.getMode() == AVATAR_MODE_WALKING) {
@ -1889,19 +1887,24 @@ glm::vec3 getGravity(glm::vec3 pos) {
} }
void mouseFunc(int button, int state, int x, int y) { void mouseFunc(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
if (state == GLUT_DOWN && !menu.mouseClick(x, y)) { //catch mouse actiond on the menu
bool menuClickedOrUnclicked = menu.mouseClick(x, y);
if (!menuClickedOrUnclicked) {
if ( button == GLUT_LEFT_BUTTON ) {
mouseX = x; mouseX = x;
mouseY = y; mouseY = y;
mousePressed = 1; if (state == GLUT_DOWN ) {
} else if (state == GLUT_UP) { mousePressed = 1;
mouseX = x; } else if (state == GLUT_UP ) {
mouseY = y; mousePressed = 0;
mousePressed = 0; }
} }
} }
} }
void motionFunc(int x, int y) { void motionFunc(int x, int y) {
mouseX = x; mouseX = x;
mouseY = y; mouseY = y;

View file

@ -9,120 +9,64 @@
#include <cstring> #include <cstring>
#include "AudioRingBuffer.h" #include "AudioRingBuffer.h"
AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) { AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) :
ringBufferLengthSamples = ringSamples; _ringBufferLengthSamples(ringSamples),
bufferLengthSamples = bufferSamples; _bufferLengthSamples(bufferSamples),
_endOfLastWrite(NULL),
started = false; _started(false),
_shouldBeAddedToMix = false; _shouldBeAddedToMix(false),
_shouldLoopbackForAgent(false) {
endOfLastWrite = NULL;
_buffer = new int16_t[_ringBufferLengthSamples];
buffer = new int16_t[ringBufferLengthSamples]; _nextOutput = _buffer;
nextOutput = buffer;
}; };
AudioRingBuffer::AudioRingBuffer(const AudioRingBuffer &otherRingBuffer) { AudioRingBuffer::AudioRingBuffer(const AudioRingBuffer &otherRingBuffer) {
ringBufferLengthSamples = otherRingBuffer.ringBufferLengthSamples; _ringBufferLengthSamples = otherRingBuffer._ringBufferLengthSamples;
bufferLengthSamples = otherRingBuffer.bufferLengthSamples; _bufferLengthSamples = otherRingBuffer._bufferLengthSamples;
started = otherRingBuffer.started; _started = otherRingBuffer._started;
_shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix; _shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix;
_shouldLoopbackForAgent = otherRingBuffer._shouldLoopbackForAgent;
buffer = new int16_t[ringBufferLengthSamples]; _buffer = new int16_t[_ringBufferLengthSamples];
memcpy(buffer, otherRingBuffer.buffer, sizeof(int16_t) * ringBufferLengthSamples); memcpy(_buffer, otherRingBuffer._buffer, sizeof(int16_t) * _ringBufferLengthSamples);
nextOutput = buffer + (otherRingBuffer.nextOutput - otherRingBuffer.buffer); _nextOutput = _buffer + (otherRingBuffer._nextOutput - otherRingBuffer._buffer);
endOfLastWrite = buffer + (otherRingBuffer.endOfLastWrite - otherRingBuffer.buffer); _endOfLastWrite = _buffer + (otherRingBuffer._endOfLastWrite - otherRingBuffer._buffer);
} }
AudioRingBuffer::~AudioRingBuffer() { AudioRingBuffer::~AudioRingBuffer() {
delete[] buffer; delete[] _buffer;
}; };
AudioRingBuffer* AudioRingBuffer::clone() const { AudioRingBuffer* AudioRingBuffer::clone() const {
return new AudioRingBuffer(*this); return new AudioRingBuffer(*this);
} }
int16_t* AudioRingBuffer::getNextOutput() {
return nextOutput;
}
void AudioRingBuffer::setNextOutput(int16_t *newPointer) {
nextOutput = newPointer;
}
int16_t* AudioRingBuffer::getEndOfLastWrite() {
return endOfLastWrite;
}
void AudioRingBuffer::setEndOfLastWrite(int16_t *newPointer) {
endOfLastWrite = newPointer;
}
int16_t* AudioRingBuffer::getBuffer() {
return buffer;
}
bool AudioRingBuffer::isStarted() {
return started;
}
void AudioRingBuffer::setStarted(bool status) {
started = status;
}
float* AudioRingBuffer::getPosition() {
return position;
}
void AudioRingBuffer::setPosition(float *newPosition) {
position[0] = newPosition[0];
position[1] = newPosition[1];
position[2] = newPosition[2];
}
float AudioRingBuffer::getAttenuationRatio() {
return attenuationRatio;
}
void AudioRingBuffer::setAttenuationRatio(float newAttenuation) {
attenuationRatio = newAttenuation;
}
float AudioRingBuffer::getBearing() {
return bearing;
}
void AudioRingBuffer::setBearing(float newBearing) {
bearing = newBearing;
}
const int AGENT_LOOPBACK_MODIFIER = 307; const int AGENT_LOOPBACK_MODIFIER = 307;
int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
if (numBytes > (bufferLengthSamples * sizeof(int16_t))) { if (numBytes > (_bufferLengthSamples * sizeof(int16_t))) {
unsigned char *dataPtr = sourceBuffer + 1; unsigned char *dataPtr = sourceBuffer + 1;
for (int p = 0; p < 3; p ++) { memcpy(&_position, dataPtr, sizeof(_position));
memcpy(&position[p], dataPtr, sizeof(float)); dataPtr += (sizeof(_position));
dataPtr += sizeof(float);
}
unsigned int attenuationByte = *(dataPtr++); unsigned int attenuationByte = *(dataPtr++);
attenuationRatio = attenuationByte / 255.0f; _attenuationRatio = attenuationByte / 255.0f;
memcpy(&bearing, dataPtr, sizeof(float)); memcpy(&_bearing, dataPtr, sizeof(float));
dataPtr += sizeof(bearing); dataPtr += sizeof(_bearing);
if (bearing > 180 || bearing < -180) { if (_bearing > 180 || _bearing < -180) {
// we were passed an invalid bearing because this agent wants loopback (pressed the H key) // we were passed an invalid bearing because this agent wants loopback (pressed the H key)
_shouldLoopbackForAgent = true; _shouldLoopbackForAgent = true;
// correct the bearing // correct the bearing
bearing = bearing > 0 _bearing = _bearing > 0
? bearing - AGENT_LOOPBACK_MODIFIER ? _bearing - AGENT_LOOPBACK_MODIFIER
: bearing + AGENT_LOOPBACK_MODIFIER; : _bearing + AGENT_LOOPBACK_MODIFIER;
} else { } else {
_shouldLoopbackForAgent = false; _shouldLoopbackForAgent = false;
} }
@ -130,34 +74,33 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
sourceBuffer = dataPtr; sourceBuffer = dataPtr;
} }
if (endOfLastWrite == NULL) { if (!_endOfLastWrite) {
endOfLastWrite = buffer; _endOfLastWrite = _buffer;
} else if (diffLastWriteNextOutput() > ringBufferLengthSamples - bufferLengthSamples) { } else if (diffLastWriteNextOutput() > _ringBufferLengthSamples - _bufferLengthSamples) {
endOfLastWrite = buffer; _endOfLastWrite = _buffer;
nextOutput = buffer; _nextOutput = _buffer;
started = false; _started = false;
} }
memcpy(endOfLastWrite, sourceBuffer, bufferLengthSamples * sizeof(int16_t)); memcpy(_endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t));
endOfLastWrite += bufferLengthSamples; _endOfLastWrite += _bufferLengthSamples;
if (endOfLastWrite >= buffer + ringBufferLengthSamples) { if (_endOfLastWrite >= _buffer + _ringBufferLengthSamples) {
endOfLastWrite = buffer; _endOfLastWrite = _buffer;
} }
return numBytes; return numBytes;
} }
short AudioRingBuffer::diffLastWriteNextOutput() short AudioRingBuffer::diffLastWriteNextOutput() {
{ if (!_endOfLastWrite) {
if (endOfLastWrite == NULL) {
return 0; return 0;
} else { } else {
short sampleDifference = endOfLastWrite - nextOutput; short sampleDifference = _endOfLastWrite - _nextOutput;
if (sampleDifference < 0) { if (sampleDifference < 0) {
sampleDifference += ringBufferLengthSamples; sampleDifference += _ringBufferLengthSamples;
} }
return sampleDifference; return sampleDifference;

View file

@ -12,6 +12,12 @@
#include <stdint.h> #include <stdint.h>
#include "AgentData.h" #include "AgentData.h"
struct Position {
float x;
float y;
float z;
};
class AudioRingBuffer : public AgentData { class AudioRingBuffer : public AgentData {
public: public:
AudioRingBuffer(int ringSamples, int bufferSamples); AudioRingBuffer(int ringSamples, int bufferSamples);
@ -21,35 +27,36 @@ public:
int parseData(unsigned char* sourceBuffer, int numBytes); int parseData(unsigned char* sourceBuffer, int numBytes);
AudioRingBuffer* clone() const; AudioRingBuffer* clone() const;
int16_t* getNextOutput(); int16_t* getNextOutput() const { return _nextOutput; }
void setNextOutput(int16_t *newPointer); void setNextOutput(int16_t* nextOutput) { _nextOutput = nextOutput; }
int16_t* getEndOfLastWrite();
void setEndOfLastWrite(int16_t *newPointer); int16_t* getEndOfLastWrite() const { return _endOfLastWrite; }
int16_t* getBuffer(); void setEndOfLastWrite(int16_t* endOfLastWrite) { _endOfLastWrite = endOfLastWrite; }
bool isStarted();
void setStarted(bool status); int16_t* getBuffer() const { return _buffer; }
bool isStarted() const { return _started; }
void setStarted(bool started) { _started = started; }
bool shouldBeAddedToMix() const { return _shouldBeAddedToMix; } bool shouldBeAddedToMix() const { return _shouldBeAddedToMix; }
void setShouldBeAddedToMix(bool shouldBeAddedToMix) { _shouldBeAddedToMix = shouldBeAddedToMix; } void setShouldBeAddedToMix(bool shouldBeAddedToMix) { _shouldBeAddedToMix = shouldBeAddedToMix; }
float* getPosition();
void setPosition(float newPosition[]);
float getAttenuationRatio();
void setAttenuationRatio(float newAttenuation);
float getBearing();
void setBearing(float newBearing);
const Position& getPosition() const { return _position; }
float getAttenuationRatio() const { return _attenuationRatio; }
float getBearing() const { return _bearing; }
bool shouldLoopbackForAgent() const { return _shouldLoopbackForAgent; } bool shouldLoopbackForAgent() const { return _shouldLoopbackForAgent; }
short diffLastWriteNextOutput(); short diffLastWriteNextOutput();
private: private:
int ringBufferLengthSamples; int _ringBufferLengthSamples;
int bufferLengthSamples; int _bufferLengthSamples;
float position[3]; Position _position;
float attenuationRatio; float _attenuationRatio;
float bearing; float _bearing;
int16_t *nextOutput; int16_t* _nextOutput;
int16_t *endOfLastWrite; int16_t* _endOfLastWrite;
int16_t *buffer; int16_t* _buffer;
bool started; bool _started;
bool _shouldBeAddedToMix; bool _shouldBeAddedToMix;
bool _shouldLoopbackForAgent; bool _shouldLoopbackForAgent;
}; };

View file

@ -1,28 +0,0 @@
//
// Stacktrace.cpp
// hifi
//
// Created by Stephen Birarda on 5/6/13.
//
//
#include <signal.h>
#include <stdio.h>
#include <execinfo.h>
#include <cstdlib>
#include "Stacktrace.h"
const int NUMBER_OF_STACK_ENTRIES = 20;
void printStacktrace(int signal) {
void* array[NUMBER_OF_STACK_ENTRIES];
// get void*'s for all entries on the stack
size_t size = backtrace(array, NUMBER_OF_STACK_ENTRIES);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", signal);
backtrace_symbols_fd(array, size, 2);
exit(1);
}

View file

@ -1,16 +0,0 @@
//
// Stacktrace.h
// hifi
//
// Created by Stephen Birarda on 5/6/13.
//
//
#ifndef __hifi__Stacktrace__
#define __hifi__Stacktrace__
#include <iostream>
void printStacktrace(int signal);
#endif /* defined(__hifi__Stacktrace__) */