martes, 17 de abril de 2007

Reporte - Práctica 6

A continuación se presenta el código de un programa el cual dibuja líneas aleatoriamente; con un clic izquierdo dibuja triángulos y con uno más cuadriláteros. Un clic izquierdo regresa a la función anterior y la pantalla se limpia cada 30 segundos.

#include
#include
#include

static int x = 0;
static float r = 1.0;
static float b = 1.0;
static float g = 1.0;
static int alea = 0;

void randomize()
{
srand((unsigned)time(NULL)); // tiempo del sistema como semilla.
}

void init(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

void linea (void)
{
glBegin(GL_LINE_LOOP);
glVertex3f (rand()%5, rand()%5, 1);
glVertex3f (rand()%5, rand()%5, 1);
glEnd();
glFlush ();
}

void triangulo (void)
{
glBegin(GL_TRIANGLES);
glVertex3f (rand()%5, rand()%5, 1);
glVertex3f (rand()%5, rand()%5, 1);
glVertex3f (rand()%5, rand()%5, 1);
glEnd();
glFlush ();
}

void cuadrilatero (void)
{
glBegin(GL_QUADS);
glVertex3f (rand()%5, rand()%5, 1);
glVertex3f (rand()%5, rand()%5, 1);
glVertex3f (rand()%5, rand()%5, 1);
glVertex3f (rand()%5, rand()%5, 1);
glEnd();
glFlush ();
}

void display(void)
{
randomize();
r = rand()%10;
r = r/10;
b = rand()%10;
b = b/10;
g = rand()%10;
g = g/10;
glColor3f (r, g, b);
glLoadIdentity ();
gluLookAt (0.0, 0.0, 5.0, 2.5, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glTranslatef(0.0,-2.5,0.0);
if (alea <= 0) { linea(); } else if (alea == 1) { triangulo(); } else { cuadrilatero(); } glPopMatrix(); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode (GL_MODELVIEW); } void processMouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) { if (button == GLUT_LEFT_BUTTON) { alea = alea + 1; if (alea > 2)
{
alea = 2;
}
}
else if (button == GLUT_RIGHT_BUTTON) {
alea = alea - 1;
if (alea <= 0){
alea = 0;
}
}
}
}


void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case 'q':
glutPostRedisplay();
break;
}
}

void idle()
{
for(x = 0; x < 100000000; x ++)
{
}
glutPostRedisplay();
}

void tiempo()
{
glClear (GL_COLOR_BUFFER_BIT);
glutPostRedisplay();
glutTimerFunc(30000, tiempo, 0);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(processMouse);
glutIdleFunc(idle);
glutTimerFunc(30000,tiempo,0);
glutMainLoop();
return 0;
}

No hay comentarios.: