Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GL/freeglut.h>
- #include <stdlib.h>
- #include <stdio.h>
- void desenhaQuadrado(int cor, int x, int y){
- // Troca a cor
- if (cor==1) // preto
- glColor3f(0, 0, 0);
- if (cor==2) // vermelho
- glColor3f(1, 0, 0);
- if (cor==3) // verde
- glColor3f(0, 1, 0);
- if (cor==4) // azul
- glColor3f(0, 0, 1);
- if (cor==5) // amarelo
- glColor3f(1, 1, 0);
- if (cor==6) // magenta
- glColor3f(1, 0, 1);
- if (cor==7) // ciano
- glColor3f(0, 1, 1);
- if (cor==8) // cinza
- glColor3f(0.6, 0.6, 0.6);
- if (cor==9) // branco
- glColor3f(1, 1, 1);
- // Começa a desenhar o quadrado
- glBegin(GL_POLYGON);
- glVertex3f(x, y, 0);
- glVertex3f((x+40), y, 0);
- glVertex3f((x+40), (y+40), 0);
- glVertex3f(x, (y+40), 0);
- }
- void loop(void){
- int x=20, y=20, cor=1, i;
- glClear(GL_COLOR_BUFFER_BIT);
- for(i=0; i<9; i++){
- desenhaQuadrado(cor, x, y);
- cor++;
- x+=80;
- if ((i==2) || (i==5)){
- y+=80;
- x=20;
- }
- }
- glEnd();
- glFlush();
- }
- // Inicia algumas variáveis de estado
- void inicializa(void){
- // cor para limpar a tela
- glClearColor(1, 1, 1, 0); // branco
- }
- // Callback de redimensionamento
- void redimensiona(int w, int h){
- glViewport(100, 100, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 500, 0, 500, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- // Callback de evento de teclado
- void teclado(unsigned char key, int x, int y)
- {
- switch(key)
- {
- // Tecla ESC
- case 27:
- exit(0);
- break;
- default:
- break;
- }
- }
- // Rotina principal
- int main(int argc, char **argv){
- int x=20, y=20, cor=1;
- // Acordando o GLUT
- glutInit(&argc, argv);
- // Definindo a versão do OpenGL que vamos usar
- glutInitContextVersion(1, 1);
- glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
- // Configuração inicial da janela do GLUT
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
- glutInitWindowSize(400, 400);
- glutInitWindowPosition(100, 100);
- // Abre a janela
- glutCreateWindow("Quadrado");
- // Registra callbacks para alguns eventos
- glutDisplayFunc(loop);
- glutReshapeFunc(redimensiona);
- glutKeyboardFunc(teclado);
- inicializa();
- // Entra em loop e nunca sai
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement