FCG2006 Prova sobre OpenGL e Rastreamento de Raios Aluno(a): matrícula: 1. O programa SimpleLight.c mostrado no Quadro 1 produz a janela mostrada na Figura 1. A partir do código, calcule coordenadas na imagem e as componentes RGB de cada um dos 4 vértices das 3 faces visíveis. Confira os valores com os da imagem obtida a partir da implementação do programa (capture a imagem diretamente da tela de seu computador). #include <glut.h> Figura 1 Imagem produzida pelo programa SimpleLight.c. typedef struct float ambiente[4]; float diffuse[4]; float specular[4]; float shininess[1]; Material; Material brass=0.33f, 0.22f, 0.03f, 1, 0.78f, 0.57f, 0.11f, 1, 0.99f, 0.91f, 0.81f, 1, 27.8f; void setmaterial(material* p) glmaterialfv(gl_front_and_back, GL_AMBIENT, p->ambiente); glmaterialfv(gl_front_and_back, GL_DIFFUSE, p->diffuse); glmaterialfv(gl_front_and_back, GL_SPECULAR, p->specular); glmaterialfv(gl_front_and_back, GL_SHININESS, p->shininess); void displaycall(void) glclearcolor(0.4f,0.4f,0.4f,1); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); setmaterial(&brass);
glutsolidcube(1); glflush(); /* Callback de REDISPLAY */ void reshapecall(int width, int height) glviewport(0, 0, (GLsizei)(width), (GLsizei)(height)); glmatrixmode(gl_projection); gluperspective(45,4./3,0.5,10); glulookat(1.5,1.5,1.2, 0,0,0, 0,0,1); /* Callback de RESHAPE */ void keyboardcall(unsigned char c, int x, int y) switch (c) case 27: exit(1); /* Callback de KEYBOARD */ void initlight( ) float position[]= 0.5f,2.f,0.f,1.f; float low[]=0.2f,0.2f,0.2f,1; float white[]=1,1,1,1; gllightfv(gl_light0, GL_AMBIENT, low); gllightfv(gl_light0, GL_DIFFUSE, white); gllightfv(gl_light0, GL_SPECULAR, white); gllightfv(gl_light0, GL_POSITION, position); glenable(gl_lighting); glenable(gl_light0); int main(int argc, char **argv) glutinit(&argc, argv); /* Inicializando a GLUT */ glutinitdisplaymode(glut_rgb GLUT_DEPTH); glutinitwindowsize(640, 480); glutcreatewindow("fcg:simple Light"); glutdisplayfunc(displaycall); glutreshapefunc(reshapecall); glutkeyboardfunc(keyboardcall); glenable(gl_depth_test); glenable(gl_normalize); initlight(); /* Inicializando a luz e o material */ glutmainloop(); /* GLUT main loop */ return 0; Quadro 1 Programa SimpleLight.c
2. O programa SimpleTexture.c mostrado no Quadro 2 produz a imagem mostrada na Figura 2 numa janela GLUT. A partir do código, calcule todas as transformações que sofrem os vértices do retângulo. Ou seja, calcule as matrizes Model-View e Projection e as coordenadas de cada um dos vértices nos sistemas do olho, de recorte, normalizado e da janela. Calcule também a interpolação da textura e confira as coordenadas dos 9 vértices que estão na linha vertical tracejada vermelha do centro da imagem (implemente o programa e capture a imagem diretamente da tela de seu computador). Procure calcular as posições dos 7 vértices internos da linha vermelha por interpolação dos 2 da extremidade. Figura 2 Imagem produzida pelo programa SimpleTexture.c. #include <glut.h> unsigned char checker[64][64][3]; static void checkerboard( ) int i,j; for(i=0;i<64;i++) for(j=0; j<64; j++) unsigned char c = (((i&0x8)==0)^((j&0x8)==0))? 255: 0; checker[i][j][0]=c; checker[i][j][1]=c; checker[i][j][2]=c; void displaycall(void) glclearcolor(0.6f,0.6f,0.6f,1); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glbegin(gl_polygon); gltexcoord2d(0.,0.); glvertex3d(0,0,0); gltexcoord2d(1.,0.); glvertex3d(1,0,0); gltexcoord2d(1.,1.); glvertex3d(1,1,0); gltexcoord2d(0.,1.); glvertex3d(0,1,0); glend(); glflush(); /* Callback de REDISPLAY */
void reshapecall(int width, int height) glviewport(0, 0, (GLsizei)(width), (GLsizei)(height)); glmatrixmode(gl_projection); gluperspective(45,4./3,0.5,2); glulookat(1.5,0.5,0.5, 0.5,0.5,0, 0,0,1); /* Callback de RESHAPE */ void keyboardcall(unsigned char c, int x, int y) switch (c) case 27: exit(1); /* Callback de KEYBOARD */ int main(int argc, char **argv) /* Inicializando a GLUT */ glutinit(&argc, argv); glutinitdisplaymode(glut_rgb GLUT_DEPTH); glutinitwindowsize(640, 480); glutcreatewindow("fcg:simple Texture"); /* Registrando as callbacks */ glutdisplayfunc(displaycall); glutreshapefunc(reshapecall); glutkeyboardfunc(keyboardcall); /* Inicializa as texturas do cubo */ checkerboard( ); glenable(gl_depth_test); glenable(gl_texture_2d); glteximage2d(gl_texture_2d,0,3,64,64,0,gl_rgb,gl_unsigned_byte,checker); gltexparameterf(gl_texture_2d,gl_texture_wrap_s,gl_clamp); gltexparameterf(gl_texture_2d,gl_texture_wrap_t,gl_clamp); gltexparameterf(gl_texture_2d,gl_texture_mag_filter,gl_nearest); gltexparameterf(gl_texture_2d,gl_texture_min_filter,gl_nearest); /* GLUT main loop */ glutmainloop(); return 0; Quadro 2 Programa SimpleTexture.c
3. A partir da cena descrita no Quadro 3, um programa de Rasteamento de Raios produziu a imagem ilustrada na Figura 3. A Tabela 1 mostra os valores de RGB de dois pixels diferentes. Re-faça os cálculos de forma a conferir os valores da tabela. Camera: eye = (100, 40, 40), center = (0, 0, 0), up=(0,1,0), fov=90 o, near = 30, far=230, w=500, h=500. Material: amarelo k d = (0.71, 0.71, 0), k s = (1, 1, 1), n = 40, opacidade = 1, não é espelhada. azul k d = (0, 0, 1), k s = (1, 1, 1), n = 50, opacidade = 1, não é espelhada. Esfera: c = (0,20,0), r = 30, material azul Caixa alinhada com os eixos: p 0 = (-80, -50, -50), p 1 = (50, -45, 50) e material amarelo Luzes: Ambiente: (0.35, 0.35, 0.51) Pontual posição=(40,120,0) e intensidade RGB l = (1,1,1) Quadro 3 Descrição da cena Figura 3 Imagem produzida por um programa de Rastreamento de Raios. Pixel x y R G B 1 278 294 0.00 0.00 0.78 2 200 100 0.94 0.94 0.00 Tabela 1 Valores de RGB para dois pixels diferentes.