Advertisement
IcaroPeretti

AnimationBase

Jun 6th, 2021
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <GL/glut.h>
  5. #define janela_altura 400
  6. #define janela_largura 600
  7. // variaveis que armazenam a translacao no quadro
  8. float tx = 0.0;
  9. float ty = 0.0;
  10. // incremento em variaveis.
  11. float xStep = 4;
  12. float yStep = 4;
  13. void display(void);
  14. void tela(GLsizei w, GLsizei h);
  15. void keyboard(unsigned char tecla, int x, int y);
  16. void anima(int valor);
  17. int main(int argc, char** argv)
  18. {
  19.     glutInit(&argc, argv); // suporte a janelas
  20.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  21.     // PADRAO DE CORES
  22.     glutInitWindowSize(janela_largura, janela_altura); // tamanho da janela
  23.     glutInitWindowPosition(100, 100); // posicao que surge a janela
  24.     glutCreateWindow("animação"); // cria janela
  25.     //glutFullScreen();
  26.     glutReshapeFunc(tela); // configura a tela
  27.     glutDisplayFunc(display);
  28.     glutKeyboardFunc(&keyboard); // chama teclado
  29.     glutTimerFunc(150, anima, 1);
  30.     glutMainLoop(); // redesenhar
  31.     return(0);
  32. }
  33. void anima(int valor)
  34. {
  35.     if ((tx) > (300) || (tx) < (-300))
  36.     {
  37.         xStep = -xStep;
  38.     }
  39.     if ((ty) > (150) || (ty) < (-150))
  40.  
  41.  
  42.     {
  43.         yStep = -yStep;
  44.     }
  45.     tx += xStep;
  46.     ty += yStep;
  47.     printf("\n topo %.2f pe %.2f direita %.2f esquerda% .2f\n",((janela_altura)/2),
  48.         (((janela_altura) / 2) * -1),
  49.         ((janela_largura) / 2),
  50.         (((janela_largura) / 2) * -1));
  51.     printf("\n step x %.2f step y %.2f\n", xStep, yStep);
  52.     printf("\n tx %.2f ty %.2f\n", tx, ty);
  53.     glutPostRedisplay();
  54.     glutTimerFunc(150, anima, 1);
  55. }
  56. void keyboard(unsigned char tecla, int x, int y)
  57. {
  58.     printf("\ntecla %c\n", tecla);
  59.     printf("\n\nDigite 1 translacao x, 2 translacao y: ");
  60.     printf("\ntecla %c\n", tecla);
  61.     printf("\no mouse estava em %d x %d\n", x, y);
  62. }
  63.  
  64. void desenhar()
  65. {
  66.     glLoadIdentity();
  67.     glTranslatef((janela_largura) / 2, (janela_altura) / 2, 0);
  68.     glBegin(GL_TRIANGLES);
  69.     glColor3f(1.0, 0.0, 0.0); // cor
  70.     glVertex2f(-100, -100);
  71.     glVertex2f(0, 0);
  72.     glVertex2f(100, -100);
  73.     glEnd();
  74.     // Especificar o local aonde o desenho acontece: bem no centro da janela
  75.     glTranslatef(tx, ty, 0.0f);
  76.     glBegin(GL_QUADS);
  77.     glColor3f(1.0, 1.0, 0.0); // cor
  78.     glVertex2f(-30, 30);
  79.  
  80.  
  81.     glVertex2f(-10, 30);
  82.     glVertex2f(-10, 50);
  83.     glVertex2f(-30, 50);
  84.     glEnd();
  85. }
  86. void display()
  87. {
  88.     glMatrixMode(GL_MODELVIEW); //coordenadas de desenho
  89.     glLoadIdentity();
  90.     glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // cor do fundo
  91.     glClear(GL_COLOR_BUFFER_BIT); // EXECUTA LIMPESA
  92.     // Especificar o local aonde o desenho acontece: bem no centro da janela mais translacao
  93.         //glTranslatef(janela_largura / 2, janela_altura/2, 0.0f);
  94.         glViewport(0, 0, janela_largura, janela_altura);
  95.     desenhar();
  96.     glFlush(); // execute o desenho
  97. }
  98. void tela(GLsizei w, GLsizei h)
  99. {
  100.     glMatrixMode(GL_PROJECTION);
  101.     glLoadIdentity();
  102.     // cria a janela (esq, direita, embaixo, em cima)
  103.     gluOrtho2D(0, janela_largura, 0, janela_altura);
  104.     glMatrixMode(GL_MODELVIEW);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement