C: Introdução ao OpenGL

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
/*
 *Programa adaptado de
 *COHEN, Marcelo e MANSSOUR, Isabel Hard. OpenGL: uma abordagem prática e objetiva. São Paulo: Novatec, 2006.
 **/
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
 
void desenhar(void)
{
  glClearColor(1,1,1,0);
  glClear(GL_COLOR_BUFFER_BIT);
 
  glColor3f(1,0,0);
 
  glBegin(GL_TRIANGLES);
    glVertex3f(-0.5,-0.5,0);
    glVertex3f(0.0,0.5,0);
    glVertex3f(0.5,-0.5,0);
  glEnd();
 
  glFlush();
}
 
void inicializar(void)
{
  glMatrixMode(GL_PROJECTION);
  gluOrtho2D(-1.0,1.0,-1.0,1.0);
  glMatrixMode(GL_MODELVIEW);
}
 
void responder_a_acao_do_teclado(unsigned char tecla, int x, int y)
{
  printf("glutKeyboardFunc: (%d,%d,%d)\n",tecla,x,y);
 
  switch(tolower(tecla))
  {
    case 'r':
    case 'R':
      glutReshapeWindow(400,400);
      glutPositionWindow(0,0);
      break;
    case 'f':
    case 'F':
      glutFullScreen();
      break;
    case 27:
    case 'q':
    case 'Q':
      exit(0);
      break;
  }
  if (tecla == 27 || tecla == 'q' || tecla == 'Q')
    exit (0);
}
 
void responder_a_acao_especial_do_teclado(int tecla, int x, int y)
{
  printf("glutSpecialFunc: (%d,%d,%d)\n",tecla,x,y);
}
 
void responder_ao_redimensionamento(int largura, int altura)
{
  printf("glutReshapeFunc: (%d,%d)\n",largura,altura);
}
 
void responder_ao_movimento_do_mouse(int x, int y)
{
  printf("glutMotionFunc: (%d,%d)\n",x,y);
}
 
void responder_ao_movimento_passivo_do_mouse(int x, int y)
{
  printf("glutPassiveMotionFunc: (%d,%d)\n",x,y);
}
 
void executar_funcao_idle(void)
{
  printf(".");
  fflush(stdout);
}
 
int main(int argp, char *argv[])
{
  glutInit(&argp,argv);
 
  /*
   * Sugestão: 
   *  1) Pesquisar o parametro GLUT_DOUBLE e a função glutSwapBuffers
   *  2) Pesquisar o parametro GLUT_DEPTH
   *  3) Parametros GLUT_INDEX
   */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 
  /*
   * Sugestão: 
   *  1) Pesquise e tente utilizar a função glutInitWindowPosition().
   */
  glutInitWindowSize(400,400);
 
  /* Pergunta: onde aparece esta mensagem? */
  glutCreateWindow("Meu primeiro programa");
  glutDisplayFunc(desenhar);
 
  /* A chamada abaixo da funcao glutReshapeFunc esta correta?
   * glutReshapeFunc(responder_ao_redimensionamento(x,y)); */
 
  /* Quando a funcao responder_ao_movimento_do_mouse será chamada? */
  glutMotionFunc(responder_ao_movimento_do_mouse);
 
  glutPassiveMotionFunc(responder_ao_movimento_passivo_do_mouse);
 
  /* Quem são o x e o y que a função responder_a_acao_do_teclado receberá? */
  glutKeyboardFunc(responder_a_acao_do_teclado);
 
  /* Quando a função responder_a_acao_especial_do_teclado será executadá? */
  glutSpecialFunc(responder_a_acao_especial_do_teclado);
 
  /* Para as funções acima, qual o valor de x e de y quando o mouse esta fora do espaço da janela? */
 
  /* Qua o efeito no programa no caso da linha abaixo ser descomentada? */
  /* glutIdleFunc(executar_funcao_idle); */
 
  inicializar();
 
  glutMainLoop();
 
  return 0;
}