From 88e8ef7c7c79194ba3544ca86ec678b06abf1a85 Mon Sep 17 00:00:00 2001 From: atlante45 Date: Wed, 11 Sep 2013 12:57:58 -0700 Subject: [PATCH] eye opening and some better memory handling --- interface/src/avatar/PerlinFace.cpp | 107 +++++++++++++++------------- interface/src/avatar/PerlinFace.h | 18 +++-- interface/src/devices/Faceshift.cpp | 18 ++++- interface/src/devices/Faceshift.h | 8 ++- 4 files changed, 95 insertions(+), 56 deletions(-) diff --git a/interface/src/avatar/PerlinFace.cpp b/interface/src/avatar/PerlinFace.cpp index 2fec2b4713..80df8c63cf 100644 --- a/interface/src/avatar/PerlinFace.cpp +++ b/interface/src/avatar/PerlinFace.cpp @@ -68,17 +68,20 @@ const int TRIANGLES_NUMBER = 202; const int VERTEX_PER_TRIANGLE = 3; const int FLOAT_PER_VERTEX = 3; +const GLfloat NO_SPECULAR_COLOR[] = { 0.0f, 0.0f, 0.0f, 1.0f }; + +glm::vec3* PerlinFace::_vertices = NULL; +GLfloat* PerlinFace::_triangles = NULL; +GLfloat* PerlinFace::_normals = NULL; +GLubyte* PerlinFace::_colors = NULL; + PerlinFace::PerlinFace(Head *owningHead) : _owningHead(owningHead), _initialized(false), _vertexNumber(0), _trianglesCount(0), - _vertices(NULL), _oldNormals(NULL), _newNormals(NULL), - _triangles(NULL), - _normals(NULL), - _colors(NULL), _trianglesPos(NULL), _normalsPos(NULL), _colorsPos(NULL), @@ -93,8 +96,10 @@ PerlinFace::PerlinFace(Head *owningHead) _mouthSize(0), _mouthSmileLeft(0), _mouthSmileRight(0), - _leftBlink(0), - _rightBlink(0) { + _eyeBlink_L(0), + _eyeBlink_R(0), + _eyeOpen_L(0), + _eyeOpen_R(0) { } PerlinFace::~PerlinFace() { @@ -102,15 +107,18 @@ PerlinFace::~PerlinFace() { glDeleteBuffers(1, &_vboID); glDeleteBuffers(1, &_nboID); glDeleteBuffers(1, &_cboID); - delete _vertices; - delete _oldNormals; - delete _newNormals; - delete _triangles; - delete _normals; - delete _colors; + delete[] _oldNormals; + delete[] _newNormals; } } +void staticCleanup() { + delete[] PerlinFace::_vertices; + delete[] PerlinFace::_triangles; + delete[] PerlinFace::_normals; + delete[] PerlinFace::_colors; +} + void PerlinFace::init() { if (_initialized) { return; @@ -121,18 +129,21 @@ void PerlinFace::init() { _vertexNumber = sizeof(VERTICES) / (FLOAT_PER_VERTEX * sizeof(float)); - _vertices = new glm::vec3[_vertexNumber]; - for (int i = 0; i < _vertexNumber; ++i) { - _vertices[i] = glm::vec3(VERTICES[VERTEX_PER_TRIANGLE * i], - VERTICES[VERTEX_PER_TRIANGLE * i + 1], - -VERTICES[VERTEX_PER_TRIANGLE * i + 2]); // Inverse Z axis to face the right direction - } _oldNormals = new glm::vec3[_vertexNumber]; _newNormals = new glm::vec3[_vertexNumber]; - _trianglesPos = _triangles = new GLfloat[FLOAT_PER_VERTEX * VERTEX_PER_TRIANGLE * TRIANGLES_NUMBER]; - _normalsPos = _normals = new GLfloat[FLOAT_PER_VERTEX * VERTEX_PER_TRIANGLE * TRIANGLES_NUMBER]; - _colorsPos = _colors = new GLubyte[FLOAT_PER_VERTEX * VERTEX_PER_TRIANGLE * TRIANGLES_NUMBER]; + if (_vertices == NULL) { + _vertices = new glm::vec3[_vertexNumber]; + for (int i = 0; i < _vertexNumber; ++i) { + _vertices[i] = getVec3(i); + } + + _trianglesPos = _triangles = new GLfloat[FLOAT_PER_VERTEX * VERTEX_PER_TRIANGLE * TRIANGLES_NUMBER]; + _normalsPos = _normals = new GLfloat[FLOAT_PER_VERTEX * VERTEX_PER_TRIANGLE * TRIANGLES_NUMBER]; + _colorsPos = _colors = new GLubyte[FLOAT_PER_VERTEX * VERTEX_PER_TRIANGLE * TRIANGLES_NUMBER]; + + atexit(staticCleanup); + } _initialized = true; } @@ -164,8 +175,6 @@ void PerlinFace::update() { GL_DYNAMIC_DRAW); } -const GLfloat NO_SPECULAR_COLOR[] = { 0.0f, 0.0f, 0.0f, 1.0f }; - void PerlinFace::render() { glPushMatrix(); @@ -174,6 +183,7 @@ void PerlinFace::render() { update(); + // Jump to head position, orientation and scale glm::quat orientation = _owningHead->getOrientation(); glm::vec3 axis = glm::axis(orientation); glTranslatef(_owningHead->getPosition().x, _owningHead->getPosition().y, _owningHead->getPosition().z); @@ -269,8 +279,10 @@ void PerlinFace::updatePositions() { _mouthSize = faceshift->getMouthSize(); _mouthSmileLeft = faceshift->getMouthSmileLeft(); _mouthSmileRight = faceshift->getMouthSmileRight(); - _leftBlink = faceshift->getLeftBlink(); - _rightBlink = faceshift->getRightBlink(); + _eyeBlink_L = faceshift->getLeftBlink(); + _eyeBlink_R = faceshift->getRightBlink(); + _eyeOpen_L = faceshift->getLeftEyeOpen(); + _eyeOpen_R = faceshift->getRightEyeOpen(); } @@ -342,28 +354,20 @@ void PerlinFace::updatePositions() { // Eyelids - glm::vec3 topLeftEyelid = glm::vec3(VERTICES[FLOAT_PER_VERTEX * EYE_MID_TOP], - VERTICES[FLOAT_PER_VERTEX * EYE_MID_TOP + 1], - -VERTICES[FLOAT_PER_VERTEX * EYE_MID_TOP + 2]); - glm::vec3 bottomLeftEyelid = glm::vec3(VERTICES[FLOAT_PER_VERTEX * EYE_MID_BOTTOM], - VERTICES[FLOAT_PER_VERTEX * EYE_MID_BOTTOM + 1], - -VERTICES[FLOAT_PER_VERTEX * EYE_MID_BOTTOM + 2]); - glm::vec3 topRightEyelid = glm::vec3(VERTICES[FLOAT_PER_VERTEX * (NUM_VERTICES + EYE_MID_TOP)], - VERTICES[FLOAT_PER_VERTEX * (NUM_VERTICES + EYE_MID_TOP) + 1], - -VERTICES[FLOAT_PER_VERTEX * (NUM_VERTICES + EYE_MID_TOP) + 2]); - glm::vec3 bottomRightEyelid = glm::vec3(VERTICES[FLOAT_PER_VERTEX * (NUM_VERTICES + EYE_MID_BOTTOM)], - VERTICES[FLOAT_PER_VERTEX * (NUM_VERTICES + EYE_MID_BOTTOM) + 1], - -VERTICES[FLOAT_PER_VERTEX * (NUM_VERTICES + EYE_MID_BOTTOM) + 2]); + glm::vec3 topLeftEyelid = getVec3(EYE_MID_TOP); + glm::vec3 bottomLeftEyelid = getVec3(EYE_MID_BOTTOM); + glm::vec3 topRightEyelid = getVec3(NUM_VERTICES + EYE_MID_TOP); + glm::vec3 bottomRightEyelid = getVec3(NUM_VERTICES + EYE_MID_BOTTOM); - _vertices[EYE_MID_TOP] = (1.0f - _leftBlink) * topLeftEyelid - + _leftBlink * (topLeftEyelid + bottomLeftEyelid) / 2.0f; - _vertices[EYE_MID_BOTTOM] = (1.0f - _leftBlink) * bottomLeftEyelid - + _leftBlink * (topLeftEyelid + bottomLeftEyelid) / 2.0f; + _vertices[EYE_MID_TOP] = (1.0f - (_eyeBlink_L + _eyeOpen_L / 2.0f)) * topLeftEyelid + + (_eyeBlink_L + _eyeOpen_L / 2.0f) * (topLeftEyelid + bottomLeftEyelid) / 2.0f; + _vertices[EYE_MID_BOTTOM] = (1.0f - (_eyeBlink_L + _eyeOpen_L / 2.0f)) * bottomLeftEyelid + + (_eyeBlink_L + _eyeOpen_L / 2.0f) * (topLeftEyelid + bottomLeftEyelid) / 2.0f; - _vertices[NUM_VERTICES + EYE_MID_TOP] = (1.0f - _rightBlink) * topRightEyelid - + _rightBlink * (topRightEyelid + bottomRightEyelid) / 2.0f; - _vertices[NUM_VERTICES + EYE_MID_BOTTOM] = (1.0f - _rightBlink) * bottomRightEyelid - + _rightBlink * (topRightEyelid + bottomRightEyelid) / 2.0f; + _vertices[NUM_VERTICES + EYE_MID_TOP] = (1.0f - (_eyeBlink_R + _eyeOpen_R / 2.0f)) * topRightEyelid + + (_eyeBlink_R + _eyeOpen_R / 2.0f) * (topRightEyelid + bottomRightEyelid) / 2.0f; + _vertices[NUM_VERTICES + EYE_MID_BOTTOM] = (1.0f - (_eyeBlink_R + _eyeOpen_R / 2.0f)) * bottomRightEyelid + + (_eyeBlink_R + _eyeOpen_R / 2.0f) * (topRightEyelid + bottomRightEyelid) / 2.0f; } void PerlinFace::updateVertices() { @@ -376,9 +380,7 @@ void PerlinFace::updateVertices() { _normalsPos = _normals; _colorsPos = _colors; - glm::vec3* temp = _oldNormals; - _oldNormals = _newNormals; - _newNormals = temp; + std::swap(_oldNormals, _newNormals); // Brows addTriangles(BROW_LEFT, BROW_MID_TOP, BROW_MID_BOTTOM, 0, 0, 0); @@ -512,6 +514,7 @@ void PerlinFace::updateVertices() { addJunction(HAIR_8, HAIR_9, HAIR_COLOR[0], HAIR_COLOR[1], HAIR_COLOR[2]); addJunction(HAIR_9, JAW_BOTTOM, HAIR_COLOR[0], HAIR_COLOR[1], HAIR_COLOR[2]); + // Now that we have summed up the normals for each point, let's normalize them. for (int i = 0; i < _vertexNumber; ++i) { _newNormals[i] = glm::normalize(_newNormals[i]); } @@ -562,10 +565,12 @@ void PerlinFace::addVertex(GLubyte r, GLubyte g, GLubyte b, int vertexID, glm::v *_trianglesPos++ = _vertices[vertexID].y; *_trianglesPos++ = _vertices[vertexID].z; + // use the normals of the previous frame for lighting *_normalsPos++ = _oldNormals[vertexID].x; *_normalsPos++ = _oldNormals[vertexID].y; *_normalsPos++ = _oldNormals[vertexID].z; + // store all the normals associated to each point for next frame _newNormals[vertexID].x += normal.x; _newNormals[vertexID].y += normal.y; _newNormals[vertexID].z += normal.z; @@ -574,3 +579,9 @@ void PerlinFace::addVertex(GLubyte r, GLubyte g, GLubyte b, int vertexID, glm::v *_colorsPos++ = g; *_colorsPos++ = b; } + +glm::vec3 PerlinFace::getVec3(int vertexID) { + return glm::vec3(VERTICES[FLOAT_PER_VERTEX * vertexID], + VERTICES[FLOAT_PER_VERTEX * vertexID + 1], + -VERTICES[FLOAT_PER_VERTEX * vertexID + 2]); // Inverse Z axis to face the right direction +} diff --git a/interface/src/avatar/PerlinFace.h b/interface/src/avatar/PerlinFace.h index 113fe173e4..32492c98e2 100644 --- a/interface/src/avatar/PerlinFace.h +++ b/interface/src/avatar/PerlinFace.h @@ -35,6 +35,8 @@ private: void addJunction(int vertexID1, int vertexID2, GLubyte r, GLubyte g, GLubyte b); void addVertex(GLubyte r, GLubyte g, GLubyte b, int vertexID, glm::vec3 normal); + glm::vec3 getVec3(int vertexID); + Head* _owningHead; bool _initialized; @@ -42,13 +44,13 @@ private: int _vertexNumber; int _trianglesCount; - glm::vec3* _vertices; + static glm::vec3* _vertices; glm::vec3* _oldNormals; glm::vec3* _newNormals; - GLfloat* _triangles; - GLfloat* _normals; - GLubyte* _colors; + static GLfloat* _triangles; + static GLfloat* _normals; + static GLubyte* _colors; GLfloat* _trianglesPos; GLfloat* _normalsPos; @@ -68,8 +70,12 @@ private: float _mouthSmileLeft; float _mouthSmileRight; - float _leftBlink; - float _rightBlink; + float _eyeBlink_L; + float _eyeBlink_R; + float _eyeOpen_L; + float _eyeOpen_R; + + friend void staticCleanup(); }; #endif /* defined(__interface__Face__) */ diff --git a/interface/src/devices/Faceshift.cpp b/interface/src/devices/Faceshift.cpp index 7ab14039fe..9a20c16b51 100644 --- a/interface/src/devices/Faceshift.cpp +++ b/interface/src/devices/Faceshift.cpp @@ -21,8 +21,12 @@ Faceshift::Faceshift() : _eyeGazeRightYaw(0.0f), _leftBlink(0.0f), _rightBlink(0.0f), + _leftEyeOpen(0.0f), + _rightEyeOpen(0.0f), _leftBlinkIndex(-1), _rightBlinkIndex(-1), + _leftEyeOpenIndex(-1), + _rightEyeOpenIndex(-1), _browDownLeft(0.0f), _browDownRight(0.0f), _browUpCenter(0.0f), @@ -132,6 +136,12 @@ void Faceshift::readFromSocket() { if (_rightBlinkIndex != -1) { _rightBlink = data.m_coeffs[_rightBlinkIndex]; } + if (_leftEyeOpenIndex != -1) { + _leftEyeOpen = data.m_coeffs[_leftEyeOpenIndex]; + } + if (_rightEyeOpenIndex != -1) { + _rightEyeOpen = data.m_coeffs[_rightEyeOpenIndex]; + } if (_browDownLeftIndex != -1) { _browDownLeft = data.m_coeffs[_browDownLeftIndex]; } @@ -167,7 +177,13 @@ void Faceshift::readFromSocket() { } else if (names[i] == "EyeBlink_R") { _rightBlinkIndex = i; - + + }else if (names[i] == "EyeOpen_L") { + _leftEyeOpenIndex = i; + + }else if (names[i] == "EyeOpen_R") { + _rightEyeOpenIndex = i; + } else if (names[i] == "BrowsD_L") { _browDownLeftIndex = i; diff --git a/interface/src/devices/Faceshift.h b/interface/src/devices/Faceshift.h index ddd352b46d..32829fbc2d 100644 --- a/interface/src/devices/Faceshift.h +++ b/interface/src/devices/Faceshift.h @@ -40,6 +40,8 @@ public: float getLeftBlink() const { return _leftBlink; } float getRightBlink() const { return _rightBlink; } + float getLeftEyeOpen() const { return _leftEyeOpen; } + float getRightEyeOpen() const { return _rightEyeOpen; } float getBrowDownLeft() const { return _browDownLeft; } float getBrowDownRight() const { return _browDownRight; } @@ -85,9 +87,13 @@ private: float _leftBlink; float _rightBlink; - + float _leftEyeOpen; + float _rightEyeOpen; + int _leftBlinkIndex; int _rightBlinkIndex; + int _leftEyeOpenIndex; + int _rightEyeOpenIndex; // Brows