glm::vec3 WingedUtil :: averageNormal (const WingedMesh& mesh, const VertexPtrSet& vertices) { assert (vertices.size () > 0); glm::vec3 avgNormal (0.0f); for (WingedVertex* v : vertices) { avgNormal = avgNormal + v->savedNormal (mesh); } return avgNormal / float (vertices.size ()); }
/** * render * Base render method that performs the calculations necessary before the * render equation is called. * Preconditions: * A pointer to the Pixel object that draws the pixels. * A pointer to the windows transform Matrix (WNAC). * A pointer to the Instance Matrix (TRS (I)). * A pointer to the zbuffer Matrix. * A pointer to the Light object. * A pointer to the eye point (Vertex). * A pointer to the Color of the object's material. * A pointer to the ambient light Color. * The attenuation value (double). * The shininess value (double). * * Postconditions: * Performs necessary preconditions before calling another render method * to perform the actual rendering equation. */ void Face::render( Pixel* pix, Matrix* wnd, Matrix* trs, Matrix* zbuffer, Light* light, Vertex* eye, Color* material, Color* amb, double attenuation, double shininess ) { // Obtain the average normal. Vector* tmp_np = avgNormal( vertices->get(1) ); Vector* tmp_nq = avgNormal( vertices->get(2) ); Vector* tmp_nr = avgNormal( vertices->get(3) ); Vector* np = tmp_np->multiply( trs ); Vector* nq = tmp_nq->multiply( trs ); Vector* nr = tmp_nr->multiply( trs ); np->normalize(); nq->normalize(); nr->normalize(); delete tmp_np; delete tmp_nq; delete tmp_nr; Vertex* p = vertices->get(1)->multiply(trs); Vertex* q = vertices->get(2)->multiply(trs); Vertex* r = vertices->get(3)->multiply(trs); // Lighting Computations p->setColor( material ); q->setColor( material ); r->setColor( material ); // Shading for vertex 1. Color* color = obtainShading( p, np, light, amb, eye, attenuation, shininess ); p->setRed( color->getRed() ); p->setGreen( color->getGreen() ); p->setBlue( color->getBlue() ); delete color; // Shading for vertex 2. color = obtainShading( q, nq, light, amb, eye, attenuation,shininess ); q->setRed( color->getRed() ); q->setGreen( color->getGreen() ); q->setBlue( color->getBlue() ); delete color; // Shading for vertex 3. color = obtainShading( r, nr, light, amb, eye, attenuation,shininess ); r->setRed( color->getRed() ); r->setGreen( color->getGreen() ); r->setBlue( color->getBlue() ); delete color; Vertex* tmp = p->multiply( wnd ); delete p; p = tmp; tmp = q->multiply( wnd ); delete q; q = tmp; tmp = r->multiply( wnd ); delete r; r = tmp; p->homogenize(); q->homogenize(); r->homogenize(); if ( backface(p, q, r) == 0 ) { renderBarycentric( pix, p, q, r, zbuffer ); } delete np; delete nq; delete nr; delete p; delete q; delete r; }