PuntoVector3D* Extrusion::vectT(GLfloat t) { PuntoVector3D* aux = new PuntoVector3D( -(a - b) * sin(t) - c * sin(((a - b) / b) * t) * ((a - b) / b), 0.0f, (a - b) * cos(t) - c * cos(((a - b) / b) * t) * ((a - b) / b), 0 ); aux->normalizar(); return aux; }
PuntoVector3D* Extrusion::vectB(GLfloat t) { PuntoVector3D* c1 = new PuntoVector3D( -(a - b) * sin(t) - c * sin(((a - b) / b) * t) * ((a - b) / b), 0.0f, (a - b) * cos(t) - c * cos(((a - b) / b) * t) * ((a - b) / b), 0 ); PuntoVector3D* c2 = new PuntoVector3D( -(a - b) * cos(t) - c * cos(((a - b) / b) * t) * ((a - b) / b) * ((a - b) / b), 0.0f, -(a - b) * sin(t) + c * sin(((a - b) / b) * t) * ((a - b) / b) * ((a - b) / b), 0 ); PuntoVector3D* aux = c2->productoVectorial(c1); aux->normalizar(); return aux; }
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; }
void drawScene(){ glMatrixMode(GL_MODELVIEW); glPushMatrix(); // Rotating the scene glRotatef(angX, 1.0f, 0.0f, 0.0f); glRotatef(angY, 0.0f, 1.0f, 0.0f); glRotatef(angZ, 0.0f, 0.0f, 1.0f); glLineWidth(1.5f); // Drawing axes glBegin(GL_LINES); glColor3f(1.0, 0.0, 0.0); glVertex3f(0, 0, 0); glVertex3f(20, 0, 0); glColor3f(0.0, 1.0, 0.0); glVertex3f(0, 0, 0); glVertex3f(0, 20, 0); glColor3f(0.0, 0.0, 1.0); glVertex3f(0, 0, 0); glVertex3f(0, 0, 20); glEnd(); // Drawing the scene glColor3f(1.0, 1.0, 1.0); //........................................... GLfloat xA, xB, xC, xD; GLfloat zA, zB, zC, zD; PuntoVector3D* EYE = new PuntoVector3D(eyeX, 0.0f, eyeZ, 1.0f); //PuntoVector3D* LOOK = new PuntoVector3D(lookX, 0.0f, lookZ, 1.0f); PuntoVector3D* dir = new PuntoVector3D(lookX - eyeX, 0.0f, lookZ - eyeZ, 0.0f); dir->normalizar(); PuntoVector3D* perpen = new PuntoVector3D(-1.0*dir->getZ(), 0.0f, dir->getX(), 0.0f); perpen->escalar((xRight - xLeft) / 2.0f); //aux centro del plano cercano PuntoVector3D* aux = dir->clonar(); aux->escalar(N); aux->sumar(EYE); xA = aux->getX() + perpen->getX(); zA = aux->getZ() + perpen->getZ(); xD = aux->getX() - perpen->getX(); zD = aux->getZ() - perpen->getZ(); delete aux; //aux centro del plano lejano aux = dir->clonar(); aux->escalar(F); aux->sumar(EYE); xB = aux->getX() + perpen->getX(); zB = aux->getZ() + perpen->getZ(); xC = aux->getX() - perpen->getX(); zC = aux->getZ() - perpen->getZ(); delete aux; delete EYE; delete dir; delete perpen; b->dibuja(min(xA, min(xB, min(xC, xD))), min(zA, min(zB, min(zC, zD))), max(xA, max(xB, max(xC, xD))), max(zA, max(zB, max(zC, zD))) ); //........................................... glPopMatrix(); }
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 }