PuntoVector3D* Malla::CalculoVectorNormalPorNewell(Cara* c){ PuntoVector3D* n = new PuntoVector3D(0, 0, 0, 1); for (int i = 0; i < c->getNumeroVertices(); i++){ PuntoVector3D* vertActual = vertice[c->getIndiceVerticeK(i)]; PuntoVector3D* vertSiguiente = vertice[c->getIndiceVerticeK((i + 1) % c->getNumeroVertices())]; int aux = c->getIndiceVerticeK((i + 1) % c->getNumeroVertices()); n->setX((vertActual->getY() - vertSiguiente->getY()) * (vertActual->getZ() + vertSiguiente->getZ())); n->setY((vertActual->getZ() - vertSiguiente->getZ()) * (vertActual->getX() + vertSiguiente->getX())); n->setZ((vertActual->getX() - vertSiguiente->getX()) * (vertActual->getY() + vertSiguiente->getY())); } n->normalizar(); return n; }
Extrusion::Extrusion(int nP, int nQ) { type = GL_POLYGON; a = 7; b = 4; c = 2; GLfloat radio = .5f; PuntoVector3D** perfil = new PuntoVector3D*[nP]; for (int i = 0; i < nP; i++){ GLfloat theta = i * 3.14f * 2.0f / (GLfloat)nP; GLfloat c = cos(theta); GLfloat s = sin(theta); perfil[i] = new PuntoVector3D(c*radio, s*radio, 0.0f, 1); } numeroVertices = nP * nQ; numeroCaras = nP * nQ; numeroNormales = numeroCaras; //Creación de los arrays vertice = new PuntoVector3D*[numeroVertices]; normal = new PuntoVector3D*[numeroNormales]; cara = new Cara*[numeroCaras]; for (int i = 0; i < nQ; i++){ //generar el perfil i-ésimo GLfloat t = (8.0f * 3.14f * i) / nQ; PuntoVector3D* C = vectC(t); PuntoVector3D* T = vectT(t); PuntoVector3D* B = vectB(t); PuntoVector3D* N = vectN(T, B); for (int j = 0; j<nP; j++) { int indice = i*nP + j; //Transformar el punto j-ésimo del perfil original GLfloat x = N->getX() * perfil[j]->getX() + B->getX() * perfil[j]->getY() + T->getX() * perfil[j]->getZ() + C->getX(); GLfloat y = N->getY() * perfil[j]->getX() + B->getY() * perfil[j]->getY() + T->getY() * perfil[j]->getZ() + C->getY(); GLfloat z = N->getZ() * perfil[j]->getX() + B->getZ() * perfil[j]->getY() + T->getZ() * perfil[j]->getZ() + C->getZ(); PuntoVector3D* p = new PuntoVector3D(x, y, z, 1); vertice[indice] = p; } //for } int indiceCara = 0; for (int i = 0; i<nQ; i++){ //unir el perfil i-ésimo con el (i+1)%n-ésimo for (int j = 0; j<nP; j++) { //esquina inferior-izquierda de una cara // indiceCara = i*(m-1) + j; int indice = i*nP + j; VerticeNormal** vn = new VerticeNormal*[4]; vn[0] = new VerticeNormal(indice, indiceCara); vn[1] = new VerticeNormal((indice + nP) % numeroVertices, indiceCara); if (j == nP - 1){ vn[2] = new VerticeNormal((indice + 1) % numeroVertices, indiceCara); vn[3] = new VerticeNormal(indice - nP + 1, indiceCara); } else{ vn[2] = new VerticeNormal((indice + 1 + nP) % numeroVertices, indiceCara); vn[3] = new VerticeNormal(indice + 1, indiceCara); } cara[indiceCara] = new Cara(4, vn); PuntoVector3D* v = CalculoVectorNormal(cara[indiceCara]); //Newell normal[indiceCara] = v; indiceCara++; } //for } //for }