Ejemplo de Fractal Esponja de Sierpinski en 3d

Descargar como doc, pdf o txt
Descargar como doc, pdf o txt
Está en la página 1de 12

Ejemplo de fractal esponja de sierpinski en 3d //este es archivo trackball.h para el movimiento con la ruedita del mouse #include <math.

h> #include <stdio.h> #include <assert.h> #include <GL/glut.h> //#include "trackball.h" #define GLTB_TIME_EPSILON 10 static GLuint gltb_lasttime; static GLfloat gltb_lastposition[3]; static GLfloat gltb_angle = 0.0; static GLfloat gltb_axis[3]; static GLfloat gltb_transform[4][4]; static GLuint static GLuint gltb_width; gltb_height;

static GLint gltb_button = -1; static GLboolean gltb_tracking = GL_FALSE; static GLboolean gltb_animate = GL_TRUE; static void _gltbPointToVector(int x, int y, int width, int height, float v[3]) { float d, a; /* project x, y onto a hemi-sphere centered within width, height. */ v[0] = (2.0 * x - width) / width; v[1] = (height - 2.0 * y) / height; d = sqrt(v[0] * v[0] + v[1] * v[1]); v[2] = cos((3.14159265 / 2.0) * ((d < 1.0) ? d : 1.0)); a = 1.0 / sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); v[0] *= a; v[1] *= a; v[2] *= a; } static void _gltbAnimate(void) { glutPostRedisplay(); }

void _gltbStartMotion(int x, int y, int button, int time) { assert(gltb_button != -1); gltb_tracking = GL_TRUE; gltb_lasttime = time; _gltbPointToVector(x, y, gltb_width, gltb_height, gltb_lastposition); } void _gltbStopMotion(int button, unsigned time) { assert(gltb_button != -1); gltb_tracking = GL_FALSE; if (time - gltb_lasttime < GLTB_TIME_EPSILON && gltb_animate) { glutIdleFunc(_gltbAnimate); } else { gltb_angle = 0; if (gltb_animate) glutIdleFunc(0); } } void gltbAnimate(GLboolean animate) { gltb_animate = animate; } void gltbInit(GLuint button) { gltb_button = button; gltb_angle = 0.0; /* put the identity in the trackball transform */ glPushMatrix(); glLoadIdentity(); glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)gltb_transform); glPopMatrix(); } void gltbMatrix(void) { assert(gltb_button != -1);

glPushMatrix(); glLoadIdentity(); glRotatef(gltb_angle, gltb_axis[0], gltb_axis[1], gltb_axis[2]); glMultMatrixf((GLfloat*)gltb_transform); glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)gltb_transform); glPopMatrix(); glMultMatrixf((GLfloat*)gltb_transform); } void gltbReshape(int width, int height) { assert(gltb_button != -1); gltb_width = width; gltb_height = height; } void gltbMouse(int button, int state, int x, int y) { assert(gltb_button != -1); if (state == GLUT_DOWN && button == gltb_button) _gltbStartMotion(x, y, button, glutGet(GLUT_ELAPSED_TIME)); else if (state == GLUT_UP && button == gltb_button) _gltbStopMotion(button, glutGet(GLUT_ELAPSED_TIME)); } void gltbMotion(int x, int y) { GLfloat current_position[3], dx, dy, dz; assert(gltb_button != -1); if (gltb_tracking == GL_FALSE) return; _gltbPointToVector(x, y, gltb_width, gltb_height, current_position); /* calculate the angle to rotate by (directly proportional to the length of the mouse movement) */ dx = current_position[0] - gltb_lastposition[0]; dy = current_position[1] - gltb_lastposition[1]; dz = current_position[2] - gltb_lastposition[2]; gltb_angle = 90.0 * sqrt(dx * dx + dy * dy + dz * dz); /* calculate the axis of rotation (cross product) */

gltb_axis[0] = gltb_lastposition[1] * current_position[2] gltb_lastposition[2] * current_position[1]; gltb_axis[1] = gltb_lastposition[2] * current_position[0] gltb_lastposition[0] * current_position[2]; gltb_axis[2] = gltb_lastposition[0] * current_position[1] gltb_lastposition[1] * current_position[0]; gltb_axis[2] = 0; /* reset for next time */ gltb_lasttime = glutGet(GLUT_ELAPSED_TIME); gltb_lastposition[0] = current_position[0]; gltb_lastposition[1] = current_position[1]; gltb_lastposition[2] = current_position[2]; /* remember to draw new position */ glutPostRedisplay(); } //este es el fractal

#include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <queue> using namespace std; //------ trackball---------#include "trackball.h" //implementacion del Trackball //-------------------------//------ Trackball -----------static GLint mouse_state; static GLint mouse_button; GLdouble pan_x = 0.0; GLdouble pan_y = 0.0; GLdouble pan_z = 0.0; //----------------------------struct cuad{ float x,y,z,lado; cuad(float aa,float bb,float cc,float dd){x=aa;y=bb;z=cc;lado=dd;} };

vector<cuad*>p; void cor(float x1, float y1,float z1,float lado ,int iteracion) { if(iteracion>0) { cor(x1,y1,z1,lado/3.0,iteracion-1);//ok cor(x1+lado/3.0,y1,z1,lado/3.0,iteracion-1);//ok cor(x1+2*lado/3.0,y1,z1,lado/3.0,iteracion-1);//ok cor(x1,y1,z1-lado/3.0,lado/3.0,iteracion-1);//ok cor(x1+2*lado/3.0,y1,z1-lado/3.0,lado/3.0,iteracion-1);//ok cor(x1,y1,z1-2*lado/3.0,lado/3.0,iteracion-1);//ok cor(x1+lado/3.0,y1,z1-2*lado/3.0,lado/3.0,iteracion-1);//ok cor(x1+2*lado/3.0,y1,z1-2*lado/3.0,lado/3.0,iteracion-1);//ok cor(x1,y1+lado/3.0,z1,lado/3.0,iteracion-1);//ok cor(x1+2*lado/3.0,y1+lado/3.0,z1,lado/3.0,iteracion-1);//ok cor(x1,y1+lado/3.0,z1-2*lado/3.0,lado/3.0,iteracion-1); cor(x1+2*lado/3.0,y1+lado/3.0,z1-2*lado/3.0,lado/3.0,iteracion-1); cor(x1,y1+2*lado/3.0,z1,lado/3.0,iteracion-1); cor(x1+lado/3.0,y1+2*lado/3.0,z1,lado/3.0,iteracion-1); cor(x1+2*lado/3.0,y1+2*lado/3.0,z1,lado/3.0,iteracion-1); cor(x1,y1+2*lado/3.0,z1-lado/3.0,lado/3.0,iteracion-1); cor(x1+2*lado/3.0,y1+2*lado/3.0,z1-lado/3.0,lado/3.0,iteracion-1); cor(x1,y1+2*lado/3.0,z1-2*lado/3.0,lado/3.0,iteracion-1); cor(x1+lado/3.0,y1+2*lado/3.0,z1-2*lado/3.0,lado/3.0,iteracion-1); cor(x1+2*lado/3.0,y1+2*lado/3.0,z1-2*lado/3.0,lado/3.0,iteracion-1);

} else { p.push_back(new cuad(x1,y1,z1,lado)); }

} int iter=0;

void limpiar() { while(!p.empty()){ p.pop_back();} } void inline drawString (char *s) { unsigned int i; for (i=0; i<strlen(s); i++) glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, s[i]); } void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH); glCullFace(GL_BACK); gltbInit(GLUT_MIDDLE_BUTTON); //---------------------------} int iterm=0; void dibujasier() { //------ Trackball ----gltbMatrix(); //---------------------limpiar(); cor(-1.0f,-1.0f,-1.0f,2.0f,iterm); vector<cuad*>::iterator iterr=p.begin(); for(;iterr!=p.end();iterr++){ cuad a= **iterr; glPushMatrix(); glColor3f(1.0f, 0.0f, 0.0f); //glTranslatef(0.0,-2.5,5.0); glBegin(GL_QUADS); //cara frontal glVertex3f(a.x,a.y,a.z);//1 glVertex3f(a.x+a.lado,a.y,a.z);//2

glVertex3f(a.x+a.lado,a.y+a.lado,a.z);//3 glVertex3f(a.x,a.y+a.lado,a.z);//4 glEnd(); glPopMatrix(); glPushMatrix(); glColor3f(0.0f, 1.0f, 0.0f); glBegin(GL_QUADS); //cara trasera glVertex3f(a.x+a.lado,a.y,a.z);//2 glVertex3f(a.x+a.lado,a.y+a.lado,a.z);//3 glVertex3f(a.x+a.lado,a.y+a.lado,a.z-a.lado);//3 glVertex3f(a.x+a.lado,a.y,a.z-a.lado);//2

glEnd(); glPopMatrix(); glPushMatrix(); glColor3f(0.0f, 0.0f, 1.0f); glBegin(GL_QUADS); //cara lateral izq glVertex3f(a.x+a.lado,a.y+a.lado,a.z);//3 glVertex3f(a.x,a.y+a.lado,a.z);//4 glVertex3f(a.x,a.y+a.lado,a.z-a.lado);//4 glVertex3f(a.x+a.lado,a.y+a.lado,a.z-a.lado);//3 glPopMatrix(); glPushMatrix(); // glTranslatef(0.0,5.0,0.0); glColor3f(1.0f, 1.0f, 0.0f); glBegin(GL_QUADS); //cara arriba glVertex3f(a.x,a.y,a.z);//1 glVertex3f(a.x+a.lado,a.y,a.z);//2 glVertex3f(a.x+a.lado,a.y,a.z-a.lado);//2 glVertex3f(a.x,a.y,a.z-a.lado);//1 glEnd(); glPopMatrix();

glPushMatrix(); glColor3f(1.0f, 0.0f, 1.0f); glBegin(GL_QUADS); //cara abajo54554 glVertex3f(a.x,a.y,a.z);//1

glVertex3f(a.x,a.y+a.lado,a.z);//4 glVertex3f(a.x,a.y+a.lado,a.z-a.lado);//4 glVertex3f(a.x,a.y,a.z-a.lado);//1 glEnd(); glPopMatrix(); glPushMatrix(); glColor3f(0.0f, 1.0f, 1.0f); glBegin(GL_QUADS); //cara abajo54554 glVertex3f(a.x,a.y+a.lado,a.z-a.lado);//4 glVertex3f(a.x+a.lado,a.y+a.lado,a.z-a.lado);//3 glVertex3f(a.x+a.lado,a.y,a.z-a.lado);//2 glVertex3f(a.x,a.y,a.z-a.lado);//1 glEnd(); glPopMatrix(); } } void display(void) { glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); drawString("hola mundo"); glRasterPos3f (3,3,0); glPushMatrix(); dibujasier(); glPopMatrix(); glPopMatrix(); glFlush(); glutSwapBuffers(); } float leftt=0.0f,rightt=0.f; float ax=20; void reshape(int w, int h) { if (!h) return; //------ Trackball -------gltbReshape(w, h); //-------------------------

glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(ax>=80.0 ) ax=79.0; if(ax==0) ax=1; if(ax<80){ glTranslatef(leftt, rightt, 0.0f);} gluPerspective(ax, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //------ Trackball ----------glTranslatef(0.0, 0.0, -18.0); //---------------------------} void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; case 'i':iterm++; //display(); dibujasier(); //reshape(400,400); glutPostRedisplay(); break; case '+': ax=ax-0.5; reshape(400,400); glutPostRedisplay(); break; case '-': ax=ax+0.5; reshape(400,400); glutPostRedisplay(); break; case 'a': leftt-=0.08f; reshape(400,400); glutPostRedisplay(); break; case 's':

leftt +=0.08f; reshape(400,400); glutPostRedisplay(); break; case 'w': rightt+=0.08f; reshape(400,400); glutPostRedisplay(); break; case 'z': rightt-=0.08f; reshape(400,400); glutPostRedisplay(); break; } } //----- Tracknall ----------void mouse(int button, int state, int x, int y) { GLdouble model[4*4]; GLdouble proj[4*4]; GLint view[4]; //fix for two-button mice -- left mouse + shift = middle mouse if (button == GLUT_LEFT_BUTTON && glutGetModifiers() & GLUT_ACTIVE_SHIFT) button = GLUT_MIDDLE_BUTTON; gltbMouse(button, state, x, y); mouse_state = state; mouse_button = button; if (state == GLUT_DOWN && button == GLUT_MIDDLE_BUTTON) { glGetDoublev(GL_MODELVIEW_MATRIX, model); glGetDoublev(GL_PROJECTION_MATRIX, proj); glGetIntegerv(GL_VIEWPORT, view); gluProject((GLdouble)x, (GLdouble)y, 0.0, model, proj, view, &pan_x, &pan_y, &pan_z); gluUnProject((GLdouble)x, (GLdouble)y, pan_z, model, proj, view, &pan_x, &pan_y, &pan_z); pan_y = -pan_y; } glutPostRedisplay(); }

//------ Trackball-------------------void motion(int x, int y) { GLdouble model[4*4]; GLdouble proj[4*4]; GLint view[4]; gltbMotion(x, y); if (mouse_state == GLUT_DOWN && mouse_button == GLUT_MIDDLE_BUTTON) { glGetDoublev(GL_MODELVIEW_MATRIX, model); glGetDoublev(GL_PROJECTION_MATRIX, proj); glGetIntegerv(GL_VIEWPORT, view); gluProject((GLdouble)x, (GLdouble)y, 0.0, model, proj, view, &pan_x, &pan_y, &pan_z); gluUnProject((GLdouble)x, (GLdouble)y, pan_z, model, proj, view, &pan_x, &pan_y, &pan_z); pan_y = -pan_y; } glutPostRedisplay(); } void TimerFunction(int value) { display(); glutMainLoop(); glutTimerFunc(1000, TimerFunction, 1); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(380, 380); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH); glutCreateWindow("Trackball en OpenGL"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); //----- Trackball -------// glutTimerFunc(1000, TimerFunction, 1);

glutMouseFunc(mouse); // glutDisplayFunc(display); glutMotionFunc(motion); //-----------------------glutMainLoop(); return 0; }

También podría gustarte