mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-30 04:42:58 +02:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
6006b8e96b
16 changed files with 560 additions and 245 deletions
interface/src
libraries
avatars/src
shared/src
voxels/src
voxel-server/src
|
@ -3570,6 +3570,7 @@ void Application::updateParticleSystem(float deltaTime) {
|
|||
|
||||
ParticleSystem::ParticleAttributes attributes;
|
||||
|
||||
attributes.radius = 0.01f;
|
||||
attributes.gravity = 0.0f + 0.05f * sinf( t * 0.52f );
|
||||
attributes.airFriction = 2.5 + 2.0f * sinf( t * 0.32f );
|
||||
attributes.jitter = 0.05f + 0.05f * sinf( t * 0.42f );
|
||||
|
@ -3586,7 +3587,7 @@ void Application::updateParticleSystem(float deltaTime) {
|
|||
attributes.gravity = 0.0f;
|
||||
}
|
||||
|
||||
_particleSystem.setParticleAttributesForEmitter(_coolDemoParticleEmitter, attributes);
|
||||
_particleSystem.setParticleAttributes(_coolDemoParticleEmitter, attributes);
|
||||
}
|
||||
|
||||
_particleSystem.setUpDirection(glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
|
|
|
@ -98,6 +98,7 @@ Avatar::Avatar(Node* owningNode) :
|
|||
_elapsedTimeMoving(0.0f),
|
||||
_elapsedTimeStopped(0.0f),
|
||||
_elapsedTimeSinceCollision(0.0f),
|
||||
_lastCollisionPosition(0, 0, 0),
|
||||
_speedBrakes(false),
|
||||
_isThrustOn(false),
|
||||
_voxels(this)
|
||||
|
@ -541,11 +542,11 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) {
|
|||
// For gravity, always move the avatar by the amount driven by gravity, so that the collision
|
||||
// routines will detect it and collide every frame when pulled by gravity to a surface
|
||||
//
|
||||
|
||||
_velocity += _scale * _gravity * (GRAVITY_EARTH * deltaTime);
|
||||
_position += _scale * _gravity * (GRAVITY_EARTH * deltaTime) * deltaTime;
|
||||
const float MIN_DISTANCE_AFTER_COLLISION_FOR_GRAVITY = 0.02f;
|
||||
if (glm::length(_position - _lastCollisionPosition) > MIN_DISTANCE_AFTER_COLLISION_FOR_GRAVITY) {
|
||||
_velocity += _scale * _gravity * (GRAVITY_EARTH * deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
updateCollisionWithEnvironment(deltaTime);
|
||||
updateCollisionWithVoxels(deltaTime);
|
||||
updateAvatarCollisions(deltaTime);
|
||||
|
@ -821,10 +822,13 @@ void Avatar::updateHandMovementAndTouching(float deltaTime, bool enableHandMovem
|
|||
}
|
||||
|
||||
// If there's a leap-interaction hand visible, use that as the endpoint
|
||||
if (getHand().getHandPositions().size() > 0) {
|
||||
_skeleton.joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].position =
|
||||
getHand().leapPositionToWorldPosition(getHand().getHandPositions()[0]);
|
||||
for (size_t i = 0; i < getHand().getPalms().size(); ++i) {
|
||||
PalmData& palm = getHand().getPalms()[i];
|
||||
if (palm.isActive()) {
|
||||
_skeleton.joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].position = palm.getPosition();
|
||||
}
|
||||
}
|
||||
|
||||
}//if (_isMine)
|
||||
|
||||
//constrain right arm length and re-adjust elbow position as it bends
|
||||
|
@ -890,11 +894,9 @@ void Avatar::updateCollisionWithEnvironment(float deltaTime) {
|
|||
if (velocityTowardCollision > VISIBLE_GROUND_COLLISION_VELOCITY) {
|
||||
Application::getInstance()->setGroundPlaneImpact(1.0f);
|
||||
}
|
||||
_lastCollisionPosition = _position;
|
||||
updateCollisionSound(penetration, deltaTime, ENVIRONMENT_COLLISION_FREQUENCY);
|
||||
|
||||
applyHardCollision(penetration, ENVIRONMENT_SURFACE_ELASTICITY, ENVIRONMENT_SURFACE_DAMPING);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -908,6 +910,7 @@ void Avatar::updateCollisionWithVoxels(float deltaTime) {
|
|||
if (Application::getInstance()->getVoxels()->findCapsulePenetration(
|
||||
_position - glm::vec3(0.0f, _pelvisFloatingHeight - radius, 0.0f),
|
||||
_position + glm::vec3(0.0f, _height - _pelvisFloatingHeight - radius, 0.0f), radius, penetration)) {
|
||||
_lastCollisionPosition = _position;
|
||||
updateCollisionSound(penetration, deltaTime, VOXEL_COLLISION_FREQUENCY);
|
||||
applyHardCollision(penetration, VOXEL_ELASTICITY, VOXEL_DAMPING);
|
||||
}
|
||||
|
|
|
@ -118,7 +118,8 @@ public:
|
|||
const glm::vec3& amplifyAngle,
|
||||
float yawFromTouch,
|
||||
float pitchFromTouch);
|
||||
void addBodyYaw(float y) {_bodyYaw += y;};
|
||||
void addBodyYaw(float bodyYaw) {_bodyYaw += bodyYaw;};
|
||||
void addBodyYawDelta(float bodyYawDelta) {_bodyYawDelta += bodyYawDelta;}
|
||||
void render(bool lookingInMirror, bool renderAvatarBalls);
|
||||
|
||||
//setters
|
||||
|
@ -155,6 +156,7 @@ public:
|
|||
float getElapsedTimeStopped () const { return _elapsedTimeStopped;}
|
||||
float getElapsedTimeMoving () const { return _elapsedTimeMoving;}
|
||||
float getElapsedTimeSinceCollision() const { return _elapsedTimeSinceCollision;}
|
||||
const glm::vec3& getLastCollisionPosition () const { return _lastCollisionPosition;}
|
||||
float getAbsoluteHeadYaw () const;
|
||||
float getAbsoluteHeadPitch () const;
|
||||
Head& getHead () {return _head; }
|
||||
|
@ -245,6 +247,7 @@ private:
|
|||
float _elapsedTimeMoving; // Timers to drive camera transitions when moving
|
||||
float _elapsedTimeStopped;
|
||||
float _elapsedTimeSinceCollision;
|
||||
glm::vec3 _lastCollisionPosition;
|
||||
bool _speedBrakes;
|
||||
bool _isThrustOn;
|
||||
|
||||
|
|
|
@ -22,8 +22,6 @@ Hand::Hand(Avatar* owningAvatar) :
|
|||
_renderAlpha(1.0),
|
||||
_lookingInMirror(false),
|
||||
_ballColor(0.0, 0.0, 0.4),
|
||||
_position(0.0, 0.4, 0.0),
|
||||
_orientation(0.0, 0.0, 0.0, 1.0),
|
||||
_particleSystemInitialized(false)
|
||||
{
|
||||
// initialize all finger particle emitters with an invalid id as default
|
||||
|
@ -45,48 +43,55 @@ void Hand::reset() {
|
|||
}
|
||||
|
||||
void Hand::simulate(float deltaTime, bool isMine) {
|
||||
updateFingerParticles(deltaTime);
|
||||
}
|
||||
|
||||
glm::vec3 Hand::leapPositionToWorldPosition(const glm::vec3& leapPosition) {
|
||||
float unitScale = 0.001; // convert mm to meters
|
||||
return _position + _orientation * (leapPosition * unitScale);
|
||||
if (_isRaveGloveActive) {
|
||||
updateFingerParticles(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
void Hand::calculateGeometry() {
|
||||
glm::vec3 offset(0.2, -0.2, -0.3); // place the hand in front of the face where we can see it
|
||||
|
||||
Head& head = _owningAvatar->getHead();
|
||||
_position = head.getPosition() + head.getOrientation() * offset;
|
||||
_orientation = head.getOrientation();
|
||||
_basePosition = head.getPosition() + head.getOrientation() * offset;
|
||||
_baseOrientation = head.getOrientation();
|
||||
|
||||
int numLeapBalls = _fingerTips.size();
|
||||
_leapBalls.resize(numLeapBalls);
|
||||
|
||||
for (int i = 0; i < _fingerTips.size(); ++i) {
|
||||
_leapBalls[i].rotation = _orientation;
|
||||
_leapBalls[i].position = leapPositionToWorldPosition(_fingerTips[i]);
|
||||
_leapBalls[i].radius = 0.01;
|
||||
_leapBalls[i].touchForce = 0.0;
|
||||
_leapBalls[i].isCollidable = true;
|
||||
_leapBalls.clear();
|
||||
for (size_t i = 0; i < getNumPalms(); ++i) {
|
||||
PalmData& palm = getPalms()[i];
|
||||
if (palm.isActive()) {
|
||||
for (size_t f = 0; f < palm.getNumFingers(); ++f) {
|
||||
FingerData& finger = palm.getFingers()[f];
|
||||
if (finger.isActive()) {
|
||||
const float standardBallRadius = 0.01f;
|
||||
_leapBalls.resize(_leapBalls.size() + 1);
|
||||
HandBall& ball = _leapBalls.back();
|
||||
ball.rotation = _baseOrientation;
|
||||
ball.position = finger.getTipPosition();
|
||||
ball.radius = standardBallRadius;
|
||||
ball.touchForce = 0.0;
|
||||
ball.isCollidable = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Hand::render(bool lookingInMirror) {
|
||||
|
||||
if (_particleSystemInitialized) {
|
||||
_particleSystem.render();
|
||||
}
|
||||
|
||||
|
||||
_renderAlpha = 1.0;
|
||||
_lookingInMirror = lookingInMirror;
|
||||
|
||||
calculateGeometry();
|
||||
|
||||
if (_isRaveGloveActive)
|
||||
if (_isRaveGloveActive) {
|
||||
renderRaveGloveStage();
|
||||
|
||||
if (_particleSystemInitialized) {
|
||||
_particleSystem.render();
|
||||
}
|
||||
}
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_RESCALE_NORMAL);
|
||||
|
||||
|
@ -138,21 +143,29 @@ void Hand::renderHandSpheres() {
|
|||
}
|
||||
|
||||
// Draw the finger root cones
|
||||
if (_fingerTips.size() == _fingerRoots.size()) {
|
||||
for (size_t i = 0; i < _fingerTips.size(); ++i) {
|
||||
glColor4f(_ballColor.r, _ballColor.g, _ballColor.b, 0.5);
|
||||
glm::vec3 tip = leapPositionToWorldPosition(_fingerTips[i]);
|
||||
glm::vec3 root = leapPositionToWorldPosition(_fingerRoots[i]);
|
||||
Avatar::renderJointConnectingCone(root, tip, 0.001, 0.003);
|
||||
for (size_t i = 0; i < getNumPalms(); ++i) {
|
||||
PalmData& palm = getPalms()[i];
|
||||
if (palm.isActive()) {
|
||||
for (size_t f = 0; f < palm.getNumFingers(); ++f) {
|
||||
FingerData& finger = palm.getFingers()[f];
|
||||
if (finger.isActive()) {
|
||||
glColor4f(_ballColor.r, _ballColor.g, _ballColor.b, 0.5);
|
||||
glm::vec3 tip = finger.getTipPosition();
|
||||
glm::vec3 root = finger.getRootPosition();
|
||||
Avatar::renderJointConnectingCone(root, tip, 0.001, 0.003);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the palms
|
||||
if (_handPositions.size() == _handNormals.size()) {
|
||||
for (size_t i = 0; i < _handPositions.size(); ++i) {
|
||||
for (size_t i = 0; i < getNumPalms(); ++i) {
|
||||
PalmData& palm = getPalms()[i];
|
||||
if (palm.isActive()) {
|
||||
const float palmThickness = 0.002f;
|
||||
glColor4f(_ballColor.r, _ballColor.g, _ballColor.b, 0.25);
|
||||
glm::vec3 tip = leapPositionToWorldPosition(_handPositions[i]);
|
||||
glm::vec3 root = leapPositionToWorldPosition(_handPositions[i] + (_handNormals[i] * 2.0f));
|
||||
glm::vec3 tip = palm.getPosition();
|
||||
glm::vec3 root = palm.getPosition() + palm.getNormal() * palmThickness;
|
||||
Avatar::renderJointConnectingCone(root, tip, 0.05, 0.03);
|
||||
}
|
||||
}
|
||||
|
@ -162,57 +175,123 @@ void Hand::renderHandSpheres() {
|
|||
|
||||
void Hand::setLeapFingers(const std::vector<glm::vec3>& fingerTips,
|
||||
const std::vector<glm::vec3>& fingerRoots) {
|
||||
_fingerTips = fingerTips;
|
||||
_fingerRoots = fingerRoots;
|
||||
// TODO: add id-checking here to increase finger stability
|
||||
|
||||
size_t fingerIndex = 0;
|
||||
for (size_t i = 0; i < getNumPalms(); ++i) {
|
||||
PalmData& palm = getPalms()[i];
|
||||
for (size_t f = 0; f < palm.getNumFingers(); ++f) {
|
||||
FingerData& finger = palm.getFingers()[f];
|
||||
if (fingerIndex < fingerTips.size()) {
|
||||
finger.setActive(true);
|
||||
finger.setRawTipPosition(fingerTips[fingerIndex]);
|
||||
finger.setRawRootPosition(fingerRoots[fingerIndex]);
|
||||
fingerIndex++;
|
||||
}
|
||||
else {
|
||||
finger.setActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Hand::setLeapHands(const std::vector<glm::vec3>& handPositions,
|
||||
const std::vector<glm::vec3>& handNormals) {
|
||||
_handPositions = handPositions;
|
||||
_handNormals = handNormals;
|
||||
for (size_t i = 0; i < getNumPalms(); ++i) {
|
||||
PalmData& palm = getPalms()[i];
|
||||
if (i < handPositions.size()) {
|
||||
palm.setActive(true);
|
||||
palm.setRawPosition(handPositions[i]);
|
||||
palm.setRawNormal(handNormals[i]);
|
||||
}
|
||||
else {
|
||||
palm.setActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Hand::updateFingerParticles(float deltaTime) {
|
||||
|
||||
if (!_particleSystemInitialized) {
|
||||
|
||||
for ( int f = 0; f< NUM_FINGERS_PER_HAND; f ++ ) {
|
||||
|
||||
_fingerParticleEmitter[f] = _particleSystem.addEmitter();
|
||||
_particleSystem.setShowingEmitter(_fingerParticleEmitter[f], true);
|
||||
|
||||
ParticleSystem::ParticleAttributes attributes;
|
||||
|
||||
// set attributes for each life stage of the particle:
|
||||
attributes.radius = 0.0f;
|
||||
attributes.color = glm::vec4( 1.0f, 1.0f, 0.5f, 0.5f);
|
||||
attributes.gravity = 0.0f;
|
||||
attributes.airFriction = 0.0f;
|
||||
attributes.jitter = 0.002f;
|
||||
attributes.emitterAttraction = 0.0f;
|
||||
attributes.tornadoForce = 0.0f;
|
||||
attributes.neighborAttraction = 0.0f;
|
||||
attributes.neighborRepulsion = 0.0f;
|
||||
attributes.bounce = 1.0f;
|
||||
attributes.usingCollisionSphere = false;
|
||||
_particleSystem.setParticleAttributes(_fingerParticleEmitter[f], 0, attributes);
|
||||
|
||||
attributes.radius = 0.01f;
|
||||
attributes.jitter = 0.0f;
|
||||
attributes.gravity = -0.005f;
|
||||
attributes.color = glm::vec4( 1.0f, 0.2f, 0.0f, 0.4f);
|
||||
_particleSystem.setParticleAttributes(_fingerParticleEmitter[f], 1, attributes);
|
||||
|
||||
attributes.radius = 0.01f;
|
||||
attributes.gravity = 0.0f;
|
||||
attributes.color = glm::vec4( 0.0f, 0.0f, 0.0f, 0.2f);
|
||||
_particleSystem.setParticleAttributes(_fingerParticleEmitter[f], 2, attributes);
|
||||
|
||||
attributes.radius = 0.02f;
|
||||
attributes.color = glm::vec4( 0.0f, 0.0f, 0.0f, 0.0f);
|
||||
_particleSystem.setParticleAttributes(_fingerParticleEmitter[f], 3, attributes);
|
||||
}
|
||||
|
||||
_particleSystemInitialized = true;
|
||||
} else {
|
||||
// update the particles
|
||||
|
||||
static float t = 0.0f;
|
||||
t += deltaTime;
|
||||
|
||||
for ( int f = 0; f< _fingerTips.size(); f ++ ) {
|
||||
|
||||
if (_fingerParticleEmitter[f] != -1) {
|
||||
|
||||
glm::vec3 particleEmitterPosition = leapPositionToWorldPosition(_fingerTips[f]);
|
||||
|
||||
// this aspect is still being designed....
|
||||
|
||||
glm::vec3 tilt = glm::vec3
|
||||
(
|
||||
30.0f * sinf( t * 0.55f ),
|
||||
0.0f,
|
||||
30.0f * cosf( t * 0.75f )
|
||||
);
|
||||
|
||||
glm::quat particleEmitterRotation = glm::quat(glm::radians(tilt));
|
||||
|
||||
_particleSystem.setEmitterPosition(_fingerParticleEmitter[0], particleEmitterPosition);
|
||||
_particleSystem.setEmitterRotation(_fingerParticleEmitter[0], particleEmitterRotation);
|
||||
|
||||
float radius = 0.005f;
|
||||
glm::vec4 color(1.0f, 0.6f, 0.0f, 0.5f);
|
||||
glm::vec3 velocity(0.0f, 0.005f, 0.0f);
|
||||
float lifespan = 0.3f;
|
||||
_particleSystem.emitParticlesNow(_fingerParticleEmitter[0], 1, radius, color, velocity, lifespan);
|
||||
}
|
||||
int fingerIndex = 0;
|
||||
for (size_t i = 0; i < getNumPalms(); ++i) {
|
||||
PalmData& palm = getPalms()[i];
|
||||
if (palm.isActive()) {
|
||||
for (size_t f = 0; f < palm.getNumFingers(); ++f) {
|
||||
FingerData& finger = palm.getFingers()[f];
|
||||
if (finger.isActive()) {
|
||||
if (_fingerParticleEmitter[fingerIndex] != -1) {
|
||||
|
||||
glm::vec3 particleEmitterPosition = finger.getTipPosition();
|
||||
|
||||
glm::vec3 fingerDirection = particleEmitterPosition - leapPositionToWorldPosition(finger.getRootPosition());
|
||||
float fingerLength = glm::length(fingerDirection);
|
||||
|
||||
if (fingerLength > 0.0f) {
|
||||
fingerDirection /= fingerLength;
|
||||
} else {
|
||||
fingerDirection = IDENTITY_UP;
|
||||
}
|
||||
|
||||
glm::quat particleEmitterRotation = rotationBetween(IDENTITY_UP, fingerDirection);
|
||||
|
||||
_particleSystem.setEmitterPosition(_fingerParticleEmitter[f], particleEmitterPosition);
|
||||
_particleSystem.setEmitterRotation(_fingerParticleEmitter[f], particleEmitterRotation);
|
||||
|
||||
float radius = 0.005f;
|
||||
const glm::vec4 color(1.0f, 0.6f, 0.0f, 0.5f);
|
||||
const glm::vec3 velocity = fingerDirection * 0.002f;
|
||||
const float lifespan = 1.0f;
|
||||
_particleSystem.emitParticlesNow(_fingerParticleEmitter[f], 1, radius, color, velocity, lifespan);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_particleSystem.setUpDirection(glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
|
|
|
@ -19,8 +19,6 @@
|
|||
#include <SharedUtil.h>
|
||||
#include <vector>
|
||||
|
||||
const int NUM_FINGERS_PER_HAND = 5;
|
||||
|
||||
class Avatar;
|
||||
class ProgramObject;
|
||||
|
||||
|
@ -56,9 +54,6 @@ public:
|
|||
const glm::vec3& getLeapBallPosition (int ball) const { return _leapBalls[ball].position;}
|
||||
bool isRaveGloveActive () const { return _isRaveGloveActive; }
|
||||
|
||||
// position conversion
|
||||
glm::vec3 leapPositionToWorldPosition(const glm::vec3& leapPosition);
|
||||
|
||||
private:
|
||||
// disallow copies of the Hand, copy of owning Avatar is disallowed too
|
||||
Hand(const Hand&);
|
||||
|
@ -71,8 +66,6 @@ private:
|
|||
bool _lookingInMirror;
|
||||
bool _isRaveGloveActive;
|
||||
glm::vec3 _ballColor;
|
||||
glm::vec3 _position;
|
||||
glm::quat _orientation;
|
||||
std::vector<HandBall> _leapBalls;
|
||||
|
||||
bool _particleSystemInitialized;
|
||||
|
|
|
@ -11,34 +11,47 @@
|
|||
#include "ParticleSystem.h"
|
||||
#include "Application.h"
|
||||
|
||||
const float DEFAULT_PARTICLE_RADIUS = 0.01f;
|
||||
const float DEFAULT_PARTICLE_BOUNCE = 1.0f;
|
||||
const float DEFAULT_PARTICLE_AIR_FRICTION = 2.0f;
|
||||
|
||||
ParticleSystem::ParticleSystem() {
|
||||
|
||||
_timer = 0.0f;
|
||||
_numEmitters = 0;
|
||||
_numParticles = 0;
|
||||
_upDirection = glm::vec3(0.0f, 1.0f, 0.0f); // default
|
||||
|
||||
|
||||
for (unsigned int e = 0; e < MAX_EMITTERS; e++) {
|
||||
_emitter[e].position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
_emitter[e].rotation = glm::quat();
|
||||
_emitter[e].right = IDENTITY_RIGHT;
|
||||
_emitter[e].up = IDENTITY_UP;
|
||||
_emitter[e].front = IDENTITY_FRONT;
|
||||
_emitter[e].showingEmitter = false;
|
||||
_emitter[e].particleAttributes.bounce = DEFAULT_PARTICLE_BOUNCE;
|
||||
_emitter[e].particleAttributes.airFriction = DEFAULT_PARTICLE_AIR_FRICTION;
|
||||
_emitter[e].particleAttributes.gravity = 0.0f;
|
||||
_emitter[e].particleAttributes.jitter = 0.0f;
|
||||
_emitter[e].particleAttributes.emitterAttraction = 0.0f;
|
||||
_emitter[e].particleAttributes.tornadoForce = 0.0f;
|
||||
_emitter[e].particleAttributes.neighborAttraction = 0.0f;
|
||||
_emitter[e].particleAttributes.neighborRepulsion = 0.0f;
|
||||
_emitter[e].particleAttributes.collisionSphereRadius = 0.0f;
|
||||
_emitter[e].particleAttributes.collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
_emitter[e].particleAttributes.usingCollisionSphere = false;
|
||||
_emitter[e].position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
_emitter[e].rotation = glm::quat();
|
||||
_emitter[e].right = IDENTITY_RIGHT;
|
||||
_emitter[e].up = IDENTITY_UP;
|
||||
_emitter[e].front = IDENTITY_FRONT;
|
||||
_emitter[e].visible = false;
|
||||
_emitter[e].baseParticle.alive = false;
|
||||
_emitter[e].baseParticle.age = 0.0f;
|
||||
_emitter[e].baseParticle.lifespan = 0.0f;
|
||||
_emitter[e].baseParticle.radius = 0.0f;
|
||||
_emitter[e].baseParticle.emitterIndex = 0;
|
||||
_emitter[e].baseParticle.position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
_emitter[e].baseParticle.velocity = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
|
||||
for (int s = 0; s<NUM_PARTICLE_LIFE_STAGES; s++) {
|
||||
_emitter[e].particleAttributes[s].radius = DEFAULT_PARTICLE_RADIUS;
|
||||
_emitter[e].particleAttributes[s].color = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
_emitter[e].particleAttributes[s].bounce = DEFAULT_PARTICLE_BOUNCE;
|
||||
_emitter[e].particleAttributes[s].airFriction = DEFAULT_PARTICLE_AIR_FRICTION;
|
||||
_emitter[e].particleAttributes[s].gravity = 0.0f;
|
||||
_emitter[e].particleAttributes[s].jitter = 0.0f;
|
||||
_emitter[e].particleAttributes[s].emitterAttraction = 0.0f;
|
||||
_emitter[e].particleAttributes[s].tornadoForce = 0.0f;
|
||||
_emitter[e].particleAttributes[s].neighborAttraction = 0.0f;
|
||||
_emitter[e].particleAttributes[s].neighborRepulsion = 0.0f;
|
||||
_emitter[e].particleAttributes[s].collisionSphereRadius = 0.0f;
|
||||
_emitter[e].particleAttributes[s].collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
_emitter[e].particleAttributes[s].usingCollisionSphere = false;
|
||||
}
|
||||
};
|
||||
|
||||
for (unsigned int p = 0; p < MAX_PARTICLES; p++) {
|
||||
|
@ -52,7 +65,6 @@ ParticleSystem::ParticleSystem() {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
int ParticleSystem::addEmitter() {
|
||||
|
||||
_numEmitters ++;
|
||||
|
@ -75,11 +87,16 @@ void ParticleSystem::simulate(float deltaTime) {
|
|||
// update particles
|
||||
for (unsigned int p = 0; p < _numParticles; p++) {
|
||||
if (_particle[p].alive) {
|
||||
updateParticle(p, deltaTime);
|
||||
if (_particle[p].age > _particle[p].lifespan) {
|
||||
killParticle(p);
|
||||
} else {
|
||||
updateParticle(p, deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ParticleSystem::updateEmitter(int e, float deltaTime) {
|
||||
|
||||
_emitter[e].front = _emitter[e].rotation * IDENTITY_FRONT;
|
||||
|
@ -106,9 +123,11 @@ void ParticleSystem::createParticle(int e, glm::vec3 position, glm::vec3 velocit
|
|||
_particle[p].age = 0.0f;
|
||||
_particle[p].position = position;
|
||||
_particle[p].velocity = velocity;
|
||||
_particle[p].radius = radius;
|
||||
_particle[p].color = color;
|
||||
|
||||
_particle[p].radius = _emitter[e].particleAttributes[0].radius;
|
||||
_particle[p].color = _emitter[e].particleAttributes[0].color;
|
||||
|
||||
_numParticles ++;
|
||||
|
||||
assert(_numParticles <= MAX_PARTICLES);
|
||||
|
@ -146,45 +165,72 @@ void ParticleSystem::setOrangeBlueColorPalette() {
|
|||
}
|
||||
|
||||
|
||||
void ParticleSystem::setParticleAttributesForEmitter(int emitterIndex, ParticleAttributes attributes) {
|
||||
void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleAttributes attributes) {
|
||||
|
||||
_emitter[emitterIndex].particleAttributes.bounce = attributes.bounce;
|
||||
_emitter[emitterIndex].particleAttributes.gravity = attributes.gravity;
|
||||
_emitter[emitterIndex].particleAttributes.airFriction = attributes.airFriction;
|
||||
_emitter[emitterIndex].particleAttributes.jitter = attributes.jitter;
|
||||
_emitter[emitterIndex].particleAttributes.emitterAttraction = attributes.emitterAttraction;
|
||||
_emitter[emitterIndex].particleAttributes.tornadoForce = attributes.tornadoForce;
|
||||
_emitter[emitterIndex].particleAttributes.neighborAttraction = attributes.neighborAttraction;
|
||||
_emitter[emitterIndex].particleAttributes.neighborRepulsion = attributes.neighborRepulsion;
|
||||
_emitter[emitterIndex].particleAttributes.usingCollisionSphere = attributes.usingCollisionSphere;
|
||||
_emitter[emitterIndex].particleAttributes.collisionSpherePosition = attributes.collisionSpherePosition;
|
||||
_emitter[emitterIndex].particleAttributes.collisionSphereRadius = attributes.collisionSphereRadius;
|
||||
for (int lifeStage = 0; lifeStage < NUM_PARTICLE_LIFE_STAGES; lifeStage ++ ) {
|
||||
setParticleAttributes(emitterIndex, lifeStage, attributes);
|
||||
}
|
||||
}
|
||||
|
||||
void ParticleSystem::setParticleAttributes(int emitterIndex, int lifeStage, ParticleAttributes attributes) {
|
||||
|
||||
ParticleAttributes * a = &_emitter[emitterIndex].particleAttributes[lifeStage];
|
||||
|
||||
a->radius = attributes.radius;
|
||||
a->color = attributes.color;
|
||||
a->bounce = attributes.bounce;
|
||||
a->gravity = attributes.gravity;
|
||||
a->airFriction = attributes.airFriction;
|
||||
a->jitter = attributes.jitter;
|
||||
a->emitterAttraction = attributes.emitterAttraction;
|
||||
a->tornadoForce = attributes.tornadoForce;
|
||||
a->neighborAttraction = attributes.neighborAttraction;
|
||||
a->neighborRepulsion = attributes.neighborRepulsion;
|
||||
a->usingCollisionSphere = attributes.usingCollisionSphere;
|
||||
a->collisionSpherePosition = attributes.collisionSpherePosition;
|
||||
a->collisionSphereRadius = attributes.collisionSphereRadius;
|
||||
}
|
||||
|
||||
|
||||
void ParticleSystem::updateParticle(int p, float deltaTime) {
|
||||
|
||||
_particle[p].age += deltaTime;
|
||||
assert(_particle[p].age <= _particle[p].lifespan);
|
||||
|
||||
if (_particle[p].age > _particle[p].lifespan) {
|
||||
killParticle(p);
|
||||
float ageFraction = _particle[p].age / _particle[p].lifespan;
|
||||
|
||||
int lifeStage = (int)( ageFraction * (NUM_PARTICLE_LIFE_STAGES-1) );
|
||||
|
||||
float lifeStageFraction = ageFraction * ( NUM_PARTICLE_LIFE_STAGES - 1 ) - lifeStage;
|
||||
|
||||
/*
|
||||
if ( p == 0 ) {
|
||||
printf( "lifespan = %f ageFraction = %f lifeStage = %d lifeStageFraction = %f\n", _particle[p].lifespan, ageFraction, lifeStage, lifeStageFraction );
|
||||
}
|
||||
*/
|
||||
|
||||
_particle[p].radius
|
||||
= _emitter[_particle[p].emitterIndex].particleAttributes[lifeStage ].radius * (1.0f - lifeStageFraction)
|
||||
+ _emitter[_particle[p].emitterIndex].particleAttributes[lifeStage+1].radius * lifeStageFraction;
|
||||
|
||||
_particle[p].color
|
||||
= _emitter[_particle[p].emitterIndex].particleAttributes[lifeStage ].color * (1.0f - lifeStageFraction)
|
||||
+ _emitter[_particle[p].emitterIndex].particleAttributes[lifeStage+1].color * lifeStageFraction;
|
||||
|
||||
Emitter myEmitter = _emitter[_particle[p].emitterIndex];
|
||||
|
||||
// apply random jitter
|
||||
float j = myEmitter.particleAttributes[lifeStage].jitter;
|
||||
_particle[p].velocity +=
|
||||
glm::vec3
|
||||
(
|
||||
-myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat(),
|
||||
-myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat(),
|
||||
-myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat()
|
||||
-j * ONE_HALF + j * randFloat(),
|
||||
-j * ONE_HALF + j * randFloat(),
|
||||
-j * ONE_HALF + j * randFloat()
|
||||
) * deltaTime;
|
||||
|
||||
// apply attraction to home position
|
||||
glm::vec3 vectorToHome = myEmitter.position - _particle[p].position;
|
||||
_particle[p].velocity += vectorToHome * myEmitter.particleAttributes.emitterAttraction * deltaTime;
|
||||
_particle[p].velocity += vectorToHome * myEmitter.particleAttributes[lifeStage].emitterAttraction * deltaTime;
|
||||
|
||||
// apply neighbor attraction
|
||||
int neighbor = p + 1;
|
||||
|
@ -195,20 +241,20 @@ void ParticleSystem::updateParticle(int p, float deltaTime) {
|
|||
if ( _particle[neighbor].emitterIndex == _particle[p].emitterIndex) {
|
||||
glm::vec3 vectorToNeighbor = _particle[p].position - _particle[neighbor].position;
|
||||
|
||||
_particle[p].velocity -= vectorToNeighbor * myEmitter.particleAttributes.neighborAttraction * deltaTime;
|
||||
_particle[p].velocity -= vectorToNeighbor * myEmitter.particleAttributes[lifeStage].neighborAttraction * deltaTime;
|
||||
|
||||
float distanceToNeighbor = glm::length(vectorToNeighbor);
|
||||
if (distanceToNeighbor > 0.0f) {
|
||||
_particle[neighbor].velocity += (vectorToNeighbor / ( 1.0f + distanceToNeighbor * distanceToNeighbor)) * myEmitter.particleAttributes.neighborRepulsion * deltaTime;
|
||||
_particle[neighbor].velocity += (vectorToNeighbor / ( 1.0f + distanceToNeighbor * distanceToNeighbor)) * myEmitter.particleAttributes[lifeStage].neighborRepulsion * deltaTime;
|
||||
}
|
||||
}
|
||||
|
||||
// apply tornado force
|
||||
glm::vec3 tornadoDirection = glm::cross(vectorToHome, myEmitter.up);
|
||||
_particle[p].velocity += tornadoDirection * myEmitter.particleAttributes.tornadoForce * deltaTime;
|
||||
_particle[p].velocity += tornadoDirection * myEmitter.particleAttributes[lifeStage].tornadoForce * deltaTime;
|
||||
|
||||
// apply air friction
|
||||
float drag = 1.0 - myEmitter.particleAttributes.airFriction * deltaTime;
|
||||
float drag = 1.0 - myEmitter.particleAttributes[lifeStage].airFriction * deltaTime;
|
||||
if (drag < 0.0f) {
|
||||
_particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
} else {
|
||||
|
@ -216,7 +262,7 @@ void ParticleSystem::updateParticle(int p, float deltaTime) {
|
|||
}
|
||||
|
||||
// apply gravity
|
||||
_particle[p].velocity -= _upDirection * myEmitter.particleAttributes.gravity * deltaTime;
|
||||
_particle[p].velocity -= _upDirection * myEmitter.particleAttributes[lifeStage].gravity * deltaTime;
|
||||
|
||||
// update position by velocity
|
||||
_particle[p].position += _particle[p].velocity;
|
||||
|
@ -226,36 +272,66 @@ void ParticleSystem::updateParticle(int p, float deltaTime) {
|
|||
_particle[p].position.y = _particle[p].radius;
|
||||
|
||||
if (_particle[p].velocity.y < 0.0f) {
|
||||
_particle[p].velocity.y *= -myEmitter.particleAttributes.bounce;
|
||||
_particle[p].velocity.y *= -myEmitter.particleAttributes[lifeStage].bounce;
|
||||
}
|
||||
}
|
||||
|
||||
// collision with sphere
|
||||
if (myEmitter.particleAttributes.usingCollisionSphere) {
|
||||
glm::vec3 vectorToSphereCenter = myEmitter.particleAttributes.collisionSpherePosition - _particle[p].position;
|
||||
if (myEmitter.particleAttributes[lifeStage].usingCollisionSphere) {
|
||||
glm::vec3 vectorToSphereCenter = myEmitter.particleAttributes[lifeStage].collisionSpherePosition - _particle[p].position;
|
||||
float distanceToSphereCenter = glm::length(vectorToSphereCenter);
|
||||
float combinedRadius = myEmitter.particleAttributes.collisionSphereRadius + _particle[p].radius;
|
||||
float combinedRadius = myEmitter.particleAttributes[lifeStage].collisionSphereRadius + _particle[p].radius;
|
||||
if (distanceToSphereCenter < combinedRadius) {
|
||||
|
||||
if (distanceToSphereCenter > 0.0f){
|
||||
glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter;
|
||||
_particle[p].position = myEmitter.particleAttributes.collisionSpherePosition - directionToSphereCenter * combinedRadius;
|
||||
_particle[p].position = myEmitter.particleAttributes[lifeStage].collisionSpherePosition - directionToSphereCenter * combinedRadius;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// do this at the end...
|
||||
_particle[p].age += deltaTime;
|
||||
}
|
||||
|
||||
void ParticleSystem::setCollisionSphere(int e, glm::vec3 position, float radius) {
|
||||
_emitter[e].particleAttributes.usingCollisionSphere = true;
|
||||
_emitter[e].particleAttributes.collisionSpherePosition = position;
|
||||
_emitter[e].particleAttributes.collisionSphereRadius = radius;
|
||||
|
||||
int lifeStage = 0;
|
||||
|
||||
_emitter[e].particleAttributes[lifeStage].usingCollisionSphere = true;
|
||||
_emitter[e].particleAttributes[lifeStage].collisionSpherePosition = position;
|
||||
_emitter[e].particleAttributes[lifeStage].collisionSphereRadius = radius;
|
||||
}
|
||||
|
||||
void ParticleSystem::setEmitterBaseParticle(int emitterIndex, bool showing ) {
|
||||
|
||||
_emitter[emitterIndex].baseParticle.alive = true;
|
||||
_emitter[emitterIndex].baseParticle.emitterIndex = emitterIndex;
|
||||
}
|
||||
|
||||
void ParticleSystem::setEmitterBaseParticle(int emitterIndex, bool showing, float radius, glm::vec4 color ) {
|
||||
|
||||
_emitter[emitterIndex].baseParticle.alive = true;
|
||||
_emitter[emitterIndex].baseParticle.emitterIndex = emitterIndex;
|
||||
_emitter[emitterIndex].baseParticle.radius = radius;
|
||||
_emitter[emitterIndex].baseParticle.color = color;
|
||||
}
|
||||
|
||||
|
||||
void ParticleSystem::render() {
|
||||
|
||||
// render the emitters
|
||||
for (unsigned int e = 0; e < _numEmitters; e++) {
|
||||
if (_emitter[e].showingEmitter) {
|
||||
for (int e = 0; e < _numEmitters; e++) {
|
||||
|
||||
if (_emitter[e].baseParticle.alive) {
|
||||
glColor4f(_emitter[e].baseParticle.color.r, _emitter[e].baseParticle.color.g, _emitter[e].baseParticle.color.b, _emitter[e].baseParticle.color.a );
|
||||
glPushMatrix();
|
||||
glTranslatef(_emitter[e].position.x, _emitter[e].position.y, _emitter[e].position.z);
|
||||
glutSolidSphere(_emitter[e].baseParticle.radius, 6, 6);
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
if (_emitter[e].visible) {
|
||||
renderEmitter(e, 0.2f);
|
||||
}
|
||||
};
|
||||
|
@ -271,19 +347,49 @@ void ParticleSystem::render() {
|
|||
void ParticleSystem::renderParticle(int p) {
|
||||
|
||||
glColor4f(_particle[p].color.r, _particle[p].color.g, _particle[p].color.b, _particle[p].color.a );
|
||||
glPushMatrix();
|
||||
glTranslatef(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z);
|
||||
glutSolidSphere(_particle[p].radius, 6, 6);
|
||||
glPopMatrix();
|
||||
|
||||
// render velocity lines
|
||||
glColor4f( _particle[p].color.x, _particle[p].color.y, _particle[p].color.z, 0.5f);
|
||||
glm::vec3 end = _particle[p].position - _particle[p].velocity * 2.0f;
|
||||
glBegin(GL_LINES);
|
||||
glVertex3f(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z);
|
||||
glVertex3f(end.x, end.y, end.z);
|
||||
|
||||
glEnd();
|
||||
if (USE_BILLBOARD_RENDERING) {
|
||||
glm::vec3 cameraPosition = Application::getInstance()->getCamera()->getPosition();
|
||||
glm::vec3 viewVector = _particle[p].position - cameraPosition;
|
||||
float distance = glm::length(viewVector);
|
||||
|
||||
if (distance >= 0.0f) {
|
||||
viewVector /= distance;
|
||||
glm::vec3 up = glm::vec3(viewVector.y, viewVector.z, viewVector.x);
|
||||
glm::vec3 right = glm::vec3(viewVector.z, viewVector.x, viewVector.y);
|
||||
|
||||
glm::vec3 p0 = _particle[p].position - right * _particle[p].radius - up * _particle[p].radius;
|
||||
glm::vec3 p1 = _particle[p].position + right * _particle[p].radius - up * _particle[p].radius;
|
||||
glm::vec3 p2 = _particle[p].position + right * _particle[p].radius + up * _particle[p].radius;
|
||||
glm::vec3 p3 = _particle[p].position - right * _particle[p].radius + up * _particle[p].radius;
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
glVertex3f(p0.x, p0.y, p0.z);
|
||||
glVertex3f(p1.x, p1.y, p1.z);
|
||||
glVertex3f(p2.x, p2.y, p2.z);
|
||||
glEnd();
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
glVertex3f(p0.x, p0.y, p0.z);
|
||||
glVertex3f(p2.x, p2.y, p2.z);
|
||||
glVertex3f(p3.x, p3.y, p3.z);
|
||||
glEnd();
|
||||
}
|
||||
} else {
|
||||
glPushMatrix();
|
||||
glTranslatef(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z);
|
||||
glutSolidSphere(_particle[p].radius, 6, 6);
|
||||
glPopMatrix();
|
||||
|
||||
if (SHOW_VELOCITY_TAILS) {
|
||||
glColor4f( _particle[p].color.x, _particle[p].color.y, _particle[p].color.z, 0.5f);
|
||||
glm::vec3 end = _particle[p].position - _particle[p].velocity * 2.0f;
|
||||
glBegin(GL_LINES);
|
||||
glVertex3f(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z);
|
||||
glVertex3f(end.x, end.y, end.z);
|
||||
glEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -11,13 +11,18 @@
|
|||
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
const int MAX_PARTICLES = 5000;
|
||||
const int MAX_EMITTERS = 20;
|
||||
const int MAX_PARTICLES = 5000;
|
||||
const int MAX_EMITTERS = 20;
|
||||
const int NUM_PARTICLE_LIFE_STAGES = 4;
|
||||
const bool USE_BILLBOARD_RENDERING = false;
|
||||
const bool SHOW_VELOCITY_TAILS = false;
|
||||
|
||||
class ParticleSystem {
|
||||
public:
|
||||
|
||||
struct ParticleAttributes {
|
||||
float radius;
|
||||
glm::vec4 color;
|
||||
float bounce;
|
||||
float gravity;
|
||||
float airFriction;
|
||||
|
@ -34,31 +39,23 @@ public:
|
|||
ParticleSystem();
|
||||
|
||||
int addEmitter(); // add (create) an emitter and get its unique id
|
||||
void emitParticlesNow(int e, int numParticles, float radius, glm::vec4 color, glm::vec3 velocity, float lifespan);
|
||||
void emitParticlesNow(int emitterIndex, int numParticles, float radius, glm::vec4 color, glm::vec3 velocity, float lifespan);
|
||||
void simulate(float deltaTime);
|
||||
void render();
|
||||
void setEmitterBaseParticle(int emitterIndex, bool showing );
|
||||
void setEmitterBaseParticle(int emitterIndex, bool showing, float radius, glm::vec4 color );
|
||||
|
||||
void setParticleAttributesForEmitter(int emitterIndex, ParticleAttributes attributes);
|
||||
void setOrangeBlueColorPalette(); // apply a nice preset color palette to the particles
|
||||
void setUpDirection(glm::vec3 upDirection) {_upDirection = upDirection;} // tell particle system which direction is up
|
||||
|
||||
void setCollisionSphere(int emitterIndex, glm::vec3 position, float radius); // specify a sphere for the particles to collide with
|
||||
void setEmitterPosition(int emitterIndex, glm::vec3 position) { _emitter[emitterIndex].position = position; } // set position of emitter
|
||||
void setEmitterRotation(int emitterIndex, glm::quat rotation) { _emitter[emitterIndex].rotation = rotation; } // set rotation of emitter
|
||||
void setShowingEmitter (int emitterIndex, bool showing ) { _emitter[emitterIndex].showingEmitter = showing; } // set its visibiity
|
||||
void setParticleAttributes(int emitterIndex, ParticleAttributes attributes);
|
||||
void setParticleAttributes(int emitterIndex, int lifeStage, ParticleAttributes attributes);
|
||||
void setCollisionSphere (int emitterIndex, glm::vec3 position, float radius); // specify a sphere for the particles to collide with
|
||||
void setEmitterPosition (int emitterIndex, glm::vec3 position) { _emitter[emitterIndex].position = position; } // set position of emitter
|
||||
void setEmitterRotation (int emitterIndex, glm::quat rotation) { _emitter[emitterIndex].rotation = rotation; } // set rotation of emitter
|
||||
void setShowingEmitter (int emitterIndex, bool showing ) { _emitter[emitterIndex].visible = showing; } // set its visibiity
|
||||
|
||||
private:
|
||||
|
||||
struct Emitter {
|
||||
glm::vec3 position;
|
||||
glm::quat rotation;
|
||||
glm::vec3 right;
|
||||
glm::vec3 up;
|
||||
glm::vec3 front;
|
||||
bool showingEmitter;
|
||||
ParticleAttributes particleAttributes;
|
||||
};
|
||||
|
||||
struct Particle {
|
||||
bool alive; // is the particle active?
|
||||
glm::vec3 position; // position
|
||||
|
@ -70,8 +67,18 @@ private:
|
|||
int emitterIndex; // which emitter created this particle?
|
||||
};
|
||||
|
||||
struct Emitter {
|
||||
glm::vec3 position;
|
||||
glm::quat rotation;
|
||||
glm::vec3 right; // derived from rotation
|
||||
glm::vec3 up; // derived from rotation
|
||||
glm::vec3 front; // derived from rotation
|
||||
bool visible;
|
||||
Particle baseParticle; // a non-physical particle at the emitter position
|
||||
ParticleAttributes particleAttributes[NUM_PARTICLE_LIFE_STAGES]; // the attributes of particles emitted from this emitter
|
||||
};
|
||||
|
||||
glm::vec3 _upDirection;
|
||||
float _timer;
|
||||
Emitter _emitter[MAX_EMITTERS];
|
||||
Particle _particle[MAX_PARTICLES];
|
||||
int _numParticles;
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
static const float fingerVectorRadix = 4; // bits of precision when converting from float<->fixed
|
||||
|
||||
AvatarData::AvatarData(Node* owningNode) :
|
||||
NodeData(owningNode),
|
||||
_handPosition(0,0,0),
|
||||
|
@ -127,36 +129,22 @@ int AvatarData::getBroadcastData(unsigned char* destinationBuffer) {
|
|||
*destinationBuffer++ = bitItems;
|
||||
|
||||
// leap hand data
|
||||
// In order to make the hand data version-robust, hand data packing is just a series of vec3's,
|
||||
// with conventions. If a client doesn't know the conventions, they can just get the vec3's
|
||||
// and render them as balls, or ignore them, without crashing or disrupting anyone.
|
||||
// Current convention:
|
||||
// Zero or more fingetTip positions, followed by the same number of fingerRoot positions
|
||||
|
||||
const std::vector<glm::vec3>& fingerTips = _handData->getFingerTips();
|
||||
const std::vector<glm::vec3>& fingerRoots = _handData->getFingerRoots();
|
||||
size_t numFingerVectors = fingerTips.size() + fingerRoots.size();
|
||||
if (numFingerVectors > 255)
|
||||
numFingerVectors = 0; // safety. We shouldn't ever get over 255, so consider that invalid.
|
||||
std::vector<glm::vec3> fingerVectors;
|
||||
_handData->encodeRemoteData(fingerVectors);
|
||||
|
||||
/////////////////////////////////
|
||||
// Temporarily disable Leap finger sending, as it's causing a crash whenever someone's got a Leap connected
|
||||
numFingerVectors = 0;
|
||||
fingerVectors.clear();
|
||||
/////////////////////////////////
|
||||
|
||||
*destinationBuffer++ = (unsigned char)numFingerVectors;
|
||||
|
||||
if (numFingerVectors > 0) {
|
||||
for (size_t i = 0; i < fingerTips.size(); ++i) {
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerTips[i].x, 4);
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerTips[i].y, 4);
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerTips[i].z, 4);
|
||||
}
|
||||
for (size_t i = 0; i < fingerRoots.size(); ++i) {
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerRoots[i].x, 4);
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerRoots[i].y, 4);
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerRoots[i].z, 4);
|
||||
}
|
||||
if (fingerVectors.size() > 255)
|
||||
fingerVectors.clear(); // safety. We shouldn't ever get over 255, so consider that invalid.
|
||||
|
||||
*destinationBuffer++ = (unsigned char)fingerVectors.size();
|
||||
|
||||
for (size_t i = 0; i < fingerVectors.size(); ++i) {
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerVectors[i].x, fingerVectorRadix);
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerVectors[i].y, fingerVectorRadix);
|
||||
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerVectors[i].z, fingerVectorRadix);
|
||||
}
|
||||
|
||||
// skeleton joints
|
||||
|
@ -263,25 +251,16 @@ int AvatarData::parseData(unsigned char* sourceBuffer, int numBytes) {
|
|||
// leap hand data
|
||||
if (sourceBuffer - startPosition < numBytes) // safety check
|
||||
{
|
||||
std::vector<glm::vec3> fingerTips;
|
||||
std::vector<glm::vec3> fingerRoots;
|
||||
unsigned int numFingerVectors = *sourceBuffer++;
|
||||
unsigned int numFingerTips = numFingerVectors / 2;
|
||||
unsigned int numFingerRoots = numFingerVectors - numFingerTips;
|
||||
fingerTips.resize(numFingerTips);
|
||||
fingerRoots.resize(numFingerRoots);
|
||||
for (size_t i = 0; i < numFingerTips; ++i) {
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerTips[i].x), 4);
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerTips[i].y), 4);
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerTips[i].z), 4);
|
||||
if (numFingerVectors > 0) {
|
||||
std::vector<glm::vec3> fingerVectors(numFingerVectors);
|
||||
for (size_t i = 0; i < numFingerVectors; ++i) {
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerVectors[i].x), fingerVectorRadix);
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerVectors[i].y), fingerVectorRadix);
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerVectors[i].z), fingerVectorRadix);
|
||||
}
|
||||
_handData->decodeRemoteData(fingerVectors);
|
||||
}
|
||||
for (size_t i = 0; i < numFingerRoots; ++i) {
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerRoots[i].x), 4);
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerRoots[i].y), 4);
|
||||
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*) sourceBuffer, &(fingerRoots[i].z), 4);
|
||||
}
|
||||
_handData->setFingerTips(fingerTips);
|
||||
_handData->setFingerRoots(fingerRoots);
|
||||
}
|
||||
|
||||
// skeleton joints
|
||||
|
|
|
@ -9,7 +9,74 @@
|
|||
#include "HandData.h"
|
||||
|
||||
HandData::HandData(AvatarData* owningAvatar) :
|
||||
_basePosition(0.0f, 0.0f, 0.0f),
|
||||
_baseOrientation(0.0f, 0.0f, 0.0f, 1.0f),
|
||||
_owningAvatarData(owningAvatar)
|
||||
{
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
_palms.push_back(PalmData(this));
|
||||
}
|
||||
}
|
||||
|
||||
PalmData::PalmData(HandData* owningHandData) :
|
||||
_rawPosition(0, 0, 0),
|
||||
_rawNormal(0, 1, 0),
|
||||
_isActive(false),
|
||||
_owningHandData(owningHandData)
|
||||
{
|
||||
for (int i = 0; i < NUM_FINGERS_PER_HAND; ++i) {
|
||||
_fingers.push_back(FingerData(this, owningHandData));
|
||||
}
|
||||
}
|
||||
|
||||
FingerData::FingerData(PalmData* owningPalmData, HandData* owningHandData) :
|
||||
_tipRawPosition(0, 0, 0),
|
||||
_rootRawPosition(0, 0, 0),
|
||||
_isActive(false),
|
||||
_owningPalmData(owningPalmData),
|
||||
_owningHandData(owningHandData)
|
||||
{
|
||||
}
|
||||
|
||||
void HandData::encodeRemoteData(std::vector<glm::vec3>& fingerVectors) {
|
||||
fingerVectors.clear();
|
||||
for (size_t i = 0; i < getNumPalms(); ++i) {
|
||||
PalmData& palm = getPalms()[i];
|
||||
fingerVectors.push_back(palm.getRawPosition());
|
||||
fingerVectors.push_back(palm.getRawNormal());
|
||||
for (size_t f = 0; f < palm.getNumFingers(); ++f) {
|
||||
FingerData& finger = palm.getFingers()[f];
|
||||
if (finger.isActive()) {
|
||||
fingerVectors.push_back(finger.getTipRawPosition());
|
||||
fingerVectors.push_back(finger.getRootRawPosition());
|
||||
}
|
||||
else {
|
||||
fingerVectors.push_back(glm::vec3(0,0,0));
|
||||
fingerVectors.push_back(glm::vec3(0,0,0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandData::decodeRemoteData(const std::vector<glm::vec3>& fingerVectors) {
|
||||
size_t vectorIndex = 0;
|
||||
for (size_t i = 0; i < getNumPalms(); ++i) {
|
||||
PalmData& palm = getPalms()[i];
|
||||
// If a palm is active, there will be
|
||||
// 1 vector for its position
|
||||
// 1 vector for normal
|
||||
// 10 vectors for fingers (5 tip/root pairs)
|
||||
bool palmActive = fingerVectors.size() >= i * 12;
|
||||
palm.setActive(palmActive);
|
||||
if (palmActive) {
|
||||
palm.setRawPosition(fingerVectors[vectorIndex++]);
|
||||
palm.setRawNormal(fingerVectors[vectorIndex++]);
|
||||
for (size_t f = 0; f < NUM_FINGERS_PER_HAND; ++f) {
|
||||
FingerData& finger = palm.getFingers()[i];
|
||||
finger.setRawTipPosition(fingerVectors[vectorIndex++]);
|
||||
finger.setRawRootPosition(fingerVectors[vectorIndex++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,8 +13,13 @@
|
|||
#include <vector>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
class AvatarData;
|
||||
class FingerData;
|
||||
class PalmData;
|
||||
|
||||
const int NUM_FINGERS_PER_HAND = 5;
|
||||
|
||||
class HandData {
|
||||
public:
|
||||
|
@ -22,26 +27,79 @@ public:
|
|||
|
||||
// These methods return the positions in Leap-relative space.
|
||||
// To convert to world coordinates, use Hand::leapPositionToWorldPosition.
|
||||
const std::vector<glm::vec3>& getFingerTips() const { return _fingerTips; }
|
||||
const std::vector<glm::vec3>& getFingerRoots() const { return _fingerRoots; }
|
||||
const std::vector<glm::vec3>& getHandPositions() const { return _handPositions; }
|
||||
const std::vector<glm::vec3>& getHandNormals() const { return _handNormals; }
|
||||
void setFingerTips(const std::vector<glm::vec3>& fingerTips) { _fingerTips = fingerTips; }
|
||||
void setFingerRoots(const std::vector<glm::vec3>& fingerRoots) { _fingerRoots = fingerRoots; }
|
||||
void setHandPositions(const std::vector<glm::vec3>& handPositons) { _handPositions = handPositons; }
|
||||
void setHandNormals(const std::vector<glm::vec3>& handNormals) { _handNormals = handNormals; }
|
||||
|
||||
// position conversion
|
||||
glm::vec3 leapPositionToWorldPosition(const glm::vec3& leapPosition) {
|
||||
const float unitScale = 0.001; // convert mm to meters
|
||||
return _basePosition + _baseOrientation * (leapPosition * unitScale);
|
||||
}
|
||||
glm::vec3 leapDirectionToWorldDirection(const glm::vec3& leapDirection) {
|
||||
return glm::normalize(_baseOrientation * leapDirection);
|
||||
}
|
||||
|
||||
std::vector<PalmData>& getPalms() { return _palms; }
|
||||
size_t getNumPalms() { return _palms.size(); }
|
||||
|
||||
// Use these for sending and receiving hand data
|
||||
void encodeRemoteData(std::vector<glm::vec3>& fingerVectors);
|
||||
void decodeRemoteData(const std::vector<glm::vec3>& fingerVectors);
|
||||
|
||||
friend class AvatarData;
|
||||
protected:
|
||||
std::vector<glm::vec3> _fingerTips;
|
||||
std::vector<glm::vec3> _fingerRoots;
|
||||
std::vector<glm::vec3> _handPositions;
|
||||
std::vector<glm::vec3> _handNormals;
|
||||
glm::vec3 _basePosition; // Hands are placed relative to this
|
||||
glm::quat _baseOrientation; // Hands are placed relative to this
|
||||
AvatarData* _owningAvatarData;
|
||||
std::vector<PalmData> _palms;
|
||||
private:
|
||||
// privatize copy ctor and assignment operator so copies of this object cannot be made
|
||||
HandData(const HandData&);
|
||||
HandData& operator= (const HandData&);
|
||||
};
|
||||
|
||||
class FingerData {
|
||||
public:
|
||||
FingerData(PalmData* owningPalmData, HandData* owningHandData);
|
||||
|
||||
glm::vec3 getTipPosition() const { return _owningHandData->leapPositionToWorldPosition(_tipRawPosition); }
|
||||
glm::vec3 getRootPosition() const { return _owningHandData->leapPositionToWorldPosition(_rootRawPosition); }
|
||||
const glm::vec3& getTipRawPosition() const { return _tipRawPosition; }
|
||||
const glm::vec3& getRootRawPosition() const { return _rootRawPosition; }
|
||||
bool isActive() const { return _isActive; }
|
||||
|
||||
void setActive(bool active) { _isActive = active; }
|
||||
void setRawTipPosition(const glm::vec3& pos) { _tipRawPosition = pos; }
|
||||
void setRawRootPosition(const glm::vec3& pos) { _rootRawPosition = pos; }
|
||||
|
||||
private:
|
||||
glm::vec3 _tipRawPosition;
|
||||
glm::vec3 _rootRawPosition;
|
||||
bool _isActive; // This has current valid data
|
||||
PalmData* _owningPalmData;
|
||||
HandData* _owningHandData;
|
||||
};
|
||||
|
||||
class PalmData {
|
||||
public:
|
||||
PalmData(HandData* owningHandData);
|
||||
glm::vec3 getPosition() const { return _owningHandData->leapPositionToWorldPosition(_rawPosition); }
|
||||
glm::vec3 getNormal() const { return _owningHandData->leapDirectionToWorldDirection(_rawNormal); }
|
||||
const glm::vec3& getRawPosition() const { return _rawPosition; }
|
||||
const glm::vec3& getRawNormal() const { return _rawNormal; }
|
||||
bool isActive() const { return _isActive; }
|
||||
|
||||
std::vector<FingerData>& getFingers() { return _fingers; }
|
||||
size_t getNumFingers() { return _fingers.size(); }
|
||||
|
||||
void setActive(bool active) { _isActive = active; }
|
||||
void setRawPosition(const glm::vec3& pos) { _rawPosition = pos; }
|
||||
void setRawNormal(const glm::vec3& normal) { _rawNormal = normal; }
|
||||
|
||||
private:
|
||||
std::vector<FingerData> _fingers;
|
||||
glm::vec3 _rawPosition;
|
||||
glm::vec3 _rawNormal;
|
||||
bool _isActive; // This has current valid data
|
||||
HandData* _owningHandData;
|
||||
};
|
||||
|
||||
#endif /* defined(__hifi__HandData__) */
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
PACKET_VERSION versionForPacketType(PACKET_TYPE type) {
|
||||
switch (type) {
|
||||
case PACKET_TYPE_HEAD_DATA:
|
||||
return 1;
|
||||
return 2;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
|
|
|
@ -1114,14 +1114,12 @@ int VoxelTree::encodeTreeBitstreamRecursion(VoxelNode* node, unsigned char* outp
|
|||
return bytesAtThisLevel;
|
||||
}
|
||||
|
||||
/** Not ready for production - coming soon.
|
||||
// If we're not in delta sending mode, but the voxel hasn't changed, then we can also bail early...
|
||||
if (!params.deltaViewFrustum && !node->hasChangedSince(params.lastViewFrustumSent - CHANGE_FUDGE)) {
|
||||
printf("not delta sending, and the node hasn't changed, bail early... lastSent=%lld getLastChanged=%lld\n",
|
||||
params.lastViewFrustumSent, node->getLastChanged());
|
||||
// If we're not in delta sending mode, and we weren't asked to do a force send, and the voxel hasn't changed,
|
||||
// then we can also bail early and save bits
|
||||
if (!params.forceSendScene && !params.deltaViewFrustum &&
|
||||
!node->hasChangedSince(params.lastViewFrustumSent - CHANGE_FUDGE)) {
|
||||
return bytesAtThisLevel;
|
||||
}
|
||||
**/
|
||||
|
||||
// If the user also asked for occlusion culling, check if this node is occluded, but only if it's not a leaf.
|
||||
// leaf occlusion is handled down below when we check child nodes
|
||||
|
|
|
@ -51,7 +51,7 @@ public:
|
|||
long childWasInViewDiscarded;
|
||||
int boundaryLevelAdjust;
|
||||
uint64_t lastViewFrustumSent;
|
||||
|
||||
bool forceSendScene;
|
||||
CoverageMap* map;
|
||||
|
||||
EncodeBitstreamParams(
|
||||
|
@ -65,7 +65,8 @@ public:
|
|||
bool wantOcclusionCulling= NO_OCCLUSION_CULLING,
|
||||
CoverageMap* map = IGNORE_COVERAGE_MAP,
|
||||
int boundaryLevelAdjust = NO_BOUNDARY_ADJUST,
|
||||
uint64_t lastViewFrustumSent = IGNORE_LAST_SENT) :
|
||||
uint64_t lastViewFrustumSent = IGNORE_LAST_SENT,
|
||||
bool forceSendScene = true) :
|
||||
maxEncodeLevel (maxEncodeLevel),
|
||||
maxLevelReached (0),
|
||||
viewFrustum (viewFrustum),
|
||||
|
@ -78,6 +79,7 @@ public:
|
|||
childWasInViewDiscarded (0),
|
||||
boundaryLevelAdjust (boundaryLevelAdjust),
|
||||
lastViewFrustumSent (lastViewFrustumSent),
|
||||
forceSendScene (forceSendScene),
|
||||
map (map)
|
||||
{}
|
||||
};
|
||||
|
|
|
@ -20,6 +20,7 @@ VoxelNodeData::VoxelNodeData(Node* owningNode) :
|
|||
_maxLevelReachedInLastSearch(1),
|
||||
_lastTimeBagEmpty(0),
|
||||
_viewFrustumChanging(false),
|
||||
_viewFrustumJustStoppedChanging(true),
|
||||
_currentPacketIsColor(true)
|
||||
{
|
||||
_voxelPacket = new unsigned char[MAX_VOXEL_PACKET_SIZE];
|
||||
|
@ -69,10 +70,25 @@ bool VoxelNodeData::updateCurrentViewFrustum() {
|
|||
_currentViewFrustum.calculate();
|
||||
currentViewFrustumChanged = true;
|
||||
}
|
||||
|
||||
// When we first detect that the view stopped changing, we record this.
|
||||
// but we don't change it back to false until we've completely sent this
|
||||
// scene.
|
||||
if (_viewFrustumChanging && !currentViewFrustumChanged) {
|
||||
_viewFrustumJustStoppedChanging = true;
|
||||
}
|
||||
_viewFrustumChanging = currentViewFrustumChanged;
|
||||
return currentViewFrustumChanged;
|
||||
}
|
||||
|
||||
void VoxelNodeData::setViewSent(bool viewSent) {
|
||||
_viewSent = viewSent;
|
||||
if (viewSent) {
|
||||
_viewFrustumJustStoppedChanging = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void VoxelNodeData::updateLastKnownViewFrustum() {
|
||||
bool frustumChanges = !_lastKnownViewFrustum.matches(_currentViewFrustum);
|
||||
|
||||
|
|
|
@ -48,7 +48,11 @@ public:
|
|||
void updateLastKnownViewFrustum();
|
||||
|
||||
bool getViewSent() const { return _viewSent; };
|
||||
void setViewSent(bool viewSent) { _viewSent = viewSent; }
|
||||
void setViewSent(bool viewSent);
|
||||
|
||||
bool getViewFrustumChanging() const { return _viewFrustumChanging; };
|
||||
bool getViewFrustumJustStoppedChanging() const { return _viewFrustumJustStoppedChanging; };
|
||||
|
||||
|
||||
uint64_t getLastTimeBagEmpty() const { return _lastTimeBagEmpty; };
|
||||
void setLastTimeBagEmpty(uint64_t lastTimeBagEmpty) { _lastTimeBagEmpty = lastTimeBagEmpty; };
|
||||
|
@ -69,6 +73,7 @@ private:
|
|||
ViewFrustum _lastKnownViewFrustum;
|
||||
uint64_t _lastTimeBagEmpty;
|
||||
bool _viewFrustumChanging;
|
||||
bool _viewFrustumJustStoppedChanging;
|
||||
bool _currentPacketIsColor;
|
||||
};
|
||||
|
||||
|
|
|
@ -205,6 +205,7 @@ void deepestLevelVoxelDistributor(NodeList* nodeList,
|
|||
}
|
||||
}
|
||||
|
||||
// This is the start of "resending" the scene.
|
||||
nodeData->nodeBag.insert(serverTree.rootNode);
|
||||
}
|
||||
|
||||
|
@ -242,15 +243,12 @@ void deepestLevelVoxelDistributor(NodeList* nodeList,
|
|||
EncodeBitstreamParams params(INT_MAX, &nodeData->getCurrentViewFrustum(), wantColor,
|
||||
WANT_EXISTS_BITS, DONT_CHOP, wantDelta, lastViewFrustum,
|
||||
wantOcclusionCulling, coverageMap, boundaryLevelAdjust,
|
||||
nodeData->getLastTimeBagEmpty());
|
||||
|
||||
nodeData->getLastTimeBagEmpty(),
|
||||
nodeData->getViewFrustumJustStoppedChanging());
|
||||
|
||||
bytesWritten = serverTree.encodeTreeBitstream(subTree, &tempOutputBuffer[0], MAX_VOXEL_PACKET_SIZE - 1,
|
||||
nodeData->nodeBag, params);
|
||||
|
||||
if (::debugVoxelSending && wantDelta) {
|
||||
printf("encodeTreeBitstream() childWasInViewDiscarded=%ld\n", params.childWasInViewDiscarded);
|
||||
}
|
||||
|
||||
if (nodeData->getAvailable() >= bytesWritten) {
|
||||
nodeData->writeToPacket(&tempOutputBuffer[0], bytesWritten);
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue