Advertisement
Guest User

Untitled

a guest
Aug 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GL/freeglut.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. void desenhaQuadrado(int cor, int x, int y){
  7.  
  8.     // Troca a cor
  9.     if (cor==1) // preto
  10.         glColor3f(0, 0, 0);
  11.     if (cor==2) // vermelho
  12.         glColor3f(1, 0, 0);
  13.     if (cor==3) // verde
  14.         glColor3f(0, 1, 0);
  15.     if (cor==4) // azul
  16.         glColor3f(0, 0, 1);
  17.     if (cor==5) // amarelo
  18.         glColor3f(1, 1, 0);
  19.     if (cor==6) // magenta
  20.         glColor3f(1, 0, 1);
  21.     if (cor==7) // ciano
  22.         glColor3f(0, 1, 1);
  23.     if (cor==8) // cinza
  24.         glColor3f(0.6, 0.6, 0.6);
  25.     if (cor==9) // branco
  26.         glColor3f(1, 1, 1);
  27.  
  28.     // Começa a desenhar o quadrado
  29.     glBegin(GL_POLYGON);
  30.         glVertex3f(x, y, 0);
  31.         glVertex3f((x+40), y, 0);
  32.         glVertex3f((x+40), (y+40), 0);
  33.         glVertex3f(x, (y+40), 0);
  34.    
  35.  
  36. }
  37.  
  38. void loop(void){
  39.     int x=20, y=20, cor=1, i;
  40.  
  41.     glClear(GL_COLOR_BUFFER_BIT);
  42.    
  43.     for(i=0; i<9; i++){
  44.             desenhaQuadrado(cor, x, y);
  45.             cor++;
  46.             x+=80;
  47.  
  48.             if ((i==2) || (i==5)){
  49.                 y+=80;
  50.                 x=20;
  51.             }
  52.         }
  53.  
  54.     glEnd();
  55.     glFlush();
  56.  
  57. }
  58.  
  59. // Inicia algumas variáveis de estado
  60. void inicializa(void){
  61.     // cor para limpar a tela
  62.     glClearColor(1, 1, 1, 0);      // branco
  63. }
  64.  
  65. // Callback de redimensionamento
  66. void redimensiona(int w, int h){
  67.    glViewport(100, 100, w, h);
  68.    glMatrixMode(GL_PROJECTION);
  69.    glLoadIdentity();
  70.    glOrtho(0, 500, 0, 500, -1, 1);
  71.    glMatrixMode(GL_MODELVIEW);
  72.    glLoadIdentity();
  73. }
  74.  
  75. // Callback de evento de teclado
  76. void teclado(unsigned char key, int x, int y)
  77. {
  78.    switch(key)
  79.    {
  80.       // Tecla ESC
  81.       case 27:
  82.          exit(0);
  83.          break;
  84.       default:
  85.          break;
  86.    }
  87. }
  88.  
  89. // Rotina principal
  90. int main(int argc, char **argv){
  91.  
  92.     int x=20, y=20, cor=1;
  93.  
  94.     // Acordando o GLUT
  95.     glutInit(&argc, argv);
  96.  
  97.     // Definindo a versão do OpenGL que vamos usar
  98.     glutInitContextVersion(1, 1);
  99.     glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
  100.  
  101.     // Configuração inicial da janela do GLUT
  102.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
  103.     glutInitWindowSize(400, 400);
  104.     glutInitWindowPosition(100, 100);
  105.  
  106.     // Abre a janela
  107.     glutCreateWindow("Quadrado");
  108.  
  109.     // Registra callbacks para alguns eventos
  110.     glutDisplayFunc(loop);
  111.     glutReshapeFunc(redimensiona);
  112.     glutKeyboardFunc(teclado);
  113.     inicializa();
  114.  
  115.     // Entra em loop e nunca sai
  116.     glutMainLoop();
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement