Principais blocos de um programa OpenGL (utilizando GLUT)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/glut.h>

#ifndef M_PI
#define M_PI 3.1415926
#endif

GLboolean doubleBuffer, rodar;
GLdouble tam,ang,dif;
GLint delay;

função main

int main(int argc, char **argv){

    GLenum type;

    glutInit(&argc, argv);   // Inicializa o GLUT

 // chama a função para processar os argumentos
    if (Args(argc, argv) == GL_FALSE)
        exit(1);

    type = GLUT_RGB | GLUT_ACCUM;  // define as opções GLUT_RGB e GLUT_ACCUM
  // se doubleBuffer for verdadeiro define GLUT_DOUBLE  senão define GLUT_SINGLE
    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
    glutInitDisplayMode(type);	    // Inicializa o modo do display
    glutInitWindowSize(300, 300);   // cria uma janela 300x300 pixeis
    glutCreateWindow("Teste");      // nome da janela pode passar argv[0]

    Init();	// chama a função Init para inicialização do OpenGL

    glutReshapeFunc(Reshape);   // Regista o callback Reshape
    glutKeyboardFunc(Key);      // Regista o callback Keyboard
    glutDisplayFunc(Draw);      // Regista o callback Display
    glutMainLoop();		// Inicia o ciclo de execução do GLUT
}

função Init

void Init(void){

    glClearColor(0.0, 0.0, 0.0, 0.0);   // define a cor para apagar a janela
}

callback Reshape

void Reshape(int width, int height){

    glViewport(0, 0, width, height);  // define o viewport como sendo a janela toda

    glMatrixMode(GL_PROJECTION);   // faz operações sobre a matriz GL_PROJECTION
    glLoadIdentity();
//    gluOrtho2D(-1,1,-1,1);       // define as coordenada relativas do viewport
                                   // esq, dir, baixo, cima
    glMatrixMode(GL_MODELVIEW);    // faz operações sobre a matriz GL_MODELVIEW
    glLoadIdentity();
}

callback Display

void Draw(void){

    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_QUADS);
        glColor3f(0.0, 0.0, 1.0);
        glVertex2f(tam*cos(ang),tam*sin(ang));
        glColor3f(1.0, 0.0, 0.0);
        glVertex2f(tam*cos(ang+M_PI/2), tam*sin(ang+M_PI/2));
        glColor3f(0.0, 1.0, 0.0);
        glVertex2f(tam*cos(ang+M_PI), tam*sin(ang+M_PI));
        glColor3f(1.0, 1.0, 0.0);
        glVertex2f(tam*cos(ang-M_PI/2),tam*sin(ang-M_PI/2));
    glEnd();

    if (doubleBuffer) {
        glutSwapBuffers();
    } else {
        glFlush();
    }
    printf("Tam:%f Ang:%f\n",tam, ang);
}

callback Keyboard

void Key(unsigned char key, int x, int y){

    switch (key) {
      case '<':    // diminui
            if (tam >= 0.2) {
               tam-=0.1;
               glutPostRedisplay();   // faz o redisplay da janela
            }
         break;
      case '>':    // aumenta
            if (tam <= 0.8) {
               tam+=0.1;
               glutPostRedisplay();   // faz o redisplay da janela
            }
         break;
      case 'R':    // inicia rotação
            rodar=GL_TRUE;
            glutTimerFunc(delay,Timer,0);  // define um callback de tempo (delay ms)
         break;
      case 'r':    // termina rotação
            rodar=GL_FALSE;
         break;
      case 27:   // Se é escape sai
            exit(0);
    }
}

callback Timer

void Timer(int valor) {

    ang+= dif;   // faz o incremento ao angulo de rotação

    if (ang>2*M_PI) {  // se passou de 2*M_PI subtrai 2*M_PI
       ang=ang-2*M_PI;
    }

    glutPostRedisplay();  // redesenha o ecran

    if (rodar) glutTimerFunc(delay,Timer,0);  // se ainda é para rodar redefine o callback
}

função Args

Pode ainda ser criada uma função para parse da linha de comandos, é chamada da função main

int Args(int argc, char **argv){

  GLint i;

  doubleBuffer = GL_FALSE;  // define o doubleBuffer como falso
  tam=.5;        // tamanho inicial do quadrado
  ang=0;         // angulo inicial
  dif=M_PI/16;   // incremento da rotação
  delay=250;     // intervalo de animação
  
  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-sb") == 0) {
      doubleBuffer = GL_FALSE;
    } else if (strcmp(argv[i], "-db") == 0) {
      doubleBuffer = GL_TRUE;
    } else if (strncmp(argv[i], "-dif", 4) == 0) {
      dif = atof(&argv[i][4]);
      if (dif > 2*M_PI)
        dif = M_PI/16;
    }  else if (strncmp(argv[i], "-del", 4) == 0) {
      dif = atof(&argv[i][4]);
      if (delay < 10)
        delay = 10;
    }else {
      printf("%s (Bad option).\n", argv[i]);
      return GL_FALSE;
    }
  }
  return GL_TRUE;
}

Ultima alteração: quarta-feira, 22 de Fevereiro de 2006 às 11:18