From 117565934c2bad882e48b8a17a16b61e2e093c1f Mon Sep 17 00:00:00 2001 From: atlante45 Date: Tue, 10 Sep 2013 17:54:59 -0700 Subject: [PATCH] added smooth normals for lighting --- interface/src/avatar/PerlinFace.cpp | 25 ++++++++++++++++++++++--- interface/src/avatar/PerlinFace.h | 2 ++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/interface/src/avatar/PerlinFace.cpp b/interface/src/avatar/PerlinFace.cpp index 80107bea8d..5ee54967e4 100644 --- a/interface/src/avatar/PerlinFace.cpp +++ b/interface/src/avatar/PerlinFace.cpp @@ -74,6 +74,8 @@ PerlinFace::PerlinFace(Head *owningHead) _vertexNumber(0), _trianglesCount(0), _vertices(NULL), + _oldNormals(NULL), + _newNormals(NULL), _triangles(NULL), _normals(NULL), _colors(NULL), @@ -101,6 +103,8 @@ PerlinFace::~PerlinFace() { glDeleteBuffers(1, &_nboID); glDeleteBuffers(1, &_cboID); delete _vertices; + delete _oldNormals; + delete _newNormals; delete _triangles; delete _normals; delete _colors; @@ -116,12 +120,15 @@ void PerlinFace::init() { glGenBuffers(1, &_cboID); _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]; @@ -355,6 +362,10 @@ void PerlinFace::updateVertices() { _normalsPos = _normals; _colorsPos = _colors; + glm::vec3* temp = _oldNormals; + _oldNormals = _newNormals; + _newNormals = temp; + // Brows addTriangles(BROW_LEFT, BROW_MID_TOP, BROW_MID_BOTTOM, 0, 0, 0); addTriangles(BROW_MID_BOTTOM, BROW_MID_TOP, BROW_RIGHT_TOP, 0, 0, 0); @@ -486,6 +497,10 @@ void PerlinFace::updateVertices() { addJunction(HAIR_7, HAIR_8, HAIR_COLOR[0], HAIR_COLOR[1], HAIR_COLOR[2]); 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]); + + for (int i = 0; i < _vertexNumber; ++i) { + _newNormals[i] = glm::normalize(_newNormals[i]); + } } void PerlinFace::addTriangles(int vertexID1, int vertexID2, int vertexID3, GLubyte r, GLubyte g, GLubyte b) { @@ -533,9 +548,13 @@ void PerlinFace::addVertex(GLubyte r, GLubyte g, GLubyte b, int vertexID, glm::v *_trianglesPos++ = _vertices[vertexID].y; *_trianglesPos++ = _vertices[vertexID].z; - *_normalsPos++ = normal.x; - *_normalsPos++ = normal.y; - *_normalsPos++ = normal.z; + *_normalsPos++ = _oldNormals[vertexID].x; + *_normalsPos++ = _oldNormals[vertexID].y; + *_normalsPos++ = _oldNormals[vertexID].z; + + _newNormals[vertexID].x += normal.x; + _newNormals[vertexID].y += normal.y; + _newNormals[vertexID].z += normal.z; *_colorsPos++ = r; *_colorsPos++ = g; diff --git a/interface/src/avatar/PerlinFace.h b/interface/src/avatar/PerlinFace.h index 575c02351b..113fe173e4 100644 --- a/interface/src/avatar/PerlinFace.h +++ b/interface/src/avatar/PerlinFace.h @@ -43,6 +43,8 @@ private: int _trianglesCount; glm::vec3* _vertices; + glm::vec3* _oldNormals; + glm::vec3* _newNormals; GLfloat* _triangles; GLfloat* _normals;