#include #include typedef struct { int x; int y; char color; }Punto; //definicion de estructura //Variables globales Punto puntos[50]; int num_puntos = 0; int alto_ventana; float size = 4.0f; char cColor = 'a'; //Colores float rojo[3] = {1.0f, 0.0f, 0.0f}; float azul[3] = {0.0f, 0.0f, 1.0f}; float verde[3] = {0.0f, 1.0f, 0.0f}; float amarillo[3] = {1.0f, 1.0f, 0.0f}; float gris[3] = {0.5f, 0.5f, 0.5f}; float negro[3] = {0.0f, 0.0f, 0.0f}; void redimensiona (int w, int h); void dibuja (void); void raton (int button, int state, int cx, int cy); void teclado (unsigned char key, int x, int y); void tecladoEspacial (int key, int x, int y); int main (int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow("Dibujando puntos..."); //Registro de funciones de callback glutDisplayFunc(dibuja); glutMouseFunc(raton); glutKeyboardFunc(teclado); glutReshapeFunc(redimensiona); glutSpecialFunc(tecladoEspacial); glutMainLoop(); return 0; } //=================== Inicializa ========================= void redimensiona (int ancho, int alto) { glClearColor(1.0f, 1.0f, 1.0f, 0.0f); glViewport(0, 0, (GLsizei) ancho, (GLsizei) alto); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLdouble) ancho, 0.0, (GLdouble) alto); alto_ventana = alto; } //=================== Dibujo ============================= void dibuja (void) { int i; glClear(GL_COLOR_BUFFER_BIT); glPointSize(size); glBegin(GL_POINTS); for (i = 0; i < num_puntos; i++) { switch(puntos[i].color) { case 'r': glColor3fv(rojo); break; case 'a': glColor3fv(azul); break; case 'v': glColor3fv(verde); break; case 'y': glColor3fv(amarillo); break; case 'g': glColor3fv(gris); break; case 'n': glColor3fv(negro); break; default: break; } glVertex2i(puntos[i].x, puntos[i].y); } glEnd(); glFlush(); } //======================== Mouse ================ void raton (int button, int state, int cx, int cy) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { puntos[num_puntos].x = cx; puntos[num_puntos].y = alto_ventana - cy; puntos[num_puntos].color = cColor; num_puntos++; } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) exit(0); glutPostRedisplay(); } //======================= Teclado ======================== void teclado (unsigned char key, int x, int y) { switch (key) { case 'R': case 'r': cColor = 'r'; break; case 'A': case 'a': cColor = 'a'; break; case 'V': case 'v': cColor = 'v'; break; case 'Y': case 'y': cColor = 'y'; break; case 'N': case 'n': cColor = 'n'; break; case 'G': case 'g': cColor = 'g'; break; case 27: exit(0); break; default: break; } } void tecladoEspacial (int key, int x, int y) { if (key == GLUT_KEY_PAGE_UP) { size += 1.0f; } else if (key == GLUT_KEY_PAGE_DOWN) { size -= 1.0f; } if (size < 1.0f) { size = 1.0f; } glutPostRedisplay(); }