//* Módulo : main.cpp //* Includes: #include #include #include #include #include "varios.hpp" #include "objetos.hpp" #include "camara.hpp" #include "cenario.hpp" #include "luzes.hpp" #include "leitura.hpp" //* Objetos: Camara *camara; // cria um ponteiro para camara Cenario *cenario; // cria um ponteiro para cenario Raio raio; // cria um objeto raio default Imagem *matrix; // matrix de cores Imagem *fundo; /* calcula a cor do proximo raio e pinta na janela, no evento de idle */ void next_ray(void) { static i=0; static j=0; double x,y,r,g,b; Cor_rgb cor; j++; if (j>=camara->Colunas()) // fim de uma linha { j=0; i++; } if (i>= camara->Linhas()) // fim da ultima linha { glutIdleFunc( NULL ); return; } // Ray Trace raio.CorDeFundo(fundo->GetPixel(i,j)); cor = cenario->Intercepta(raio, 0 ) ; matrix->SetPixel( i, j, cor ); x=(double)j; y=(double)i; r = ((double)cor.R())/255.0; g = ((double)cor.G())/255.0; b = ((double)cor.B())/255.0; glBegin(GL_POINTS); glColor3d(r, g, b); glVertex2d(x,y); glEnd(); raio = camara->ProximoRaio(); } void display(void) { double x,y,r,g,b; Cor_rgb c; static first_time=1; if (first_time ) { glClear(GL_COLOR_BUFFER_BIT); /*clear the window */ first_time=0; } else { glBegin(GL_POINTS); for (y=0;yLinhas(); y++) { for (x=0;xColunas(); x++) { c=matrix->GetPixel((int)y,(int)x); r = ((double)c.R())/255.0; g = ((double)c.G())/255.0; b = ((double)c.B())/255.0; glColor3d(r, g, b); glVertex2d(x,y); } } glEnd(); } } void window_resize( int w, int h) { matrix->Resize(w,h); glViewport(0,0,w,h); } void main(int argc, char** argv) { char arquivo[255]; // entrada: arquivo.dat, saída: arquivo.ppm //* Leitura do Arquivo if(!LeArquivo( &cenario, &camara, arquivo)) return; matrix = new Imagem( camara->Colunas(), camara->Linhas()); /* escala a imagem do fundo */ fundo = new Imagem (camara->Colunas(), camara->Linhas(), cenario->Fundo()); fundo->SalvaPPM("fundo"); /* Pega o primeiro raio */ raio = camara->PrimeiroRaio(); /* Standard GLUT initialization */ glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */ glutInitWindowSize(camara->Colunas(),camara->Linhas()); /* w x h pixel window */ glutInitWindowPosition(100,100); /* place window top left on display */ glutCreateWindow("rt2"); /* window title */ // glutSetWindowTitle("CGI99-Ray Tracer"); /* window title */ glutDisplayFunc( display ); /* display callback invoked when window opened */ glutIdleFunc( next_ray ); /* idle callback invoked when the system is idle */ glutReshapeFunc( window_resize ); /* call back to be called when the whindow is changed */ /* set attributes */ glClearColor(0.0, 0.0, 0.0, 1.0); /* black background */ /* set up viewing w x h window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, camara->Colunas(), 0.0, camara->Linhas()); glMatrixMode(GL_MODELVIEW); /* Realiza o loop principal */ glutMainLoop(); /* enter event loop */ //* Salva o arquivo com a imagem no formato .ppm matrix->SalvaPPM( arquivo ); }