Computação Gráfica II Revisão Introdução ao OpenGL Prof. Rodrigo Rocha prof.rodrigorocha@yahoo.com http://www.bolinhabolinha.com Informações Bibliografia GOMES, J. e VELHO, L. Computação Gráfica: Volume 1. Série Computação e Matemática, SBM/IMPA, 1998. VELHO, L. e GOMES, J. Sistemas Gráficos 3D. Serie Computação e Matemática, SBM/IMPA, 2001. Complementar FOLEY, J. D. et al. Computer Graphics Principles and Practice. Addison-Wesley, 1990. HEARN D., BAKER, M.P., Computer Graphics: C Version, 2nd edition, Prentice Hall WATT, A. 3D Computer Graphics. Addison-Wesley, 1993. 1
Ementa Imagens 2D Conceitos Criação Transformações Imagens 3D Fundamentos Cenas realísticas Iluminação Texturas Transformações geométricas no espaço Coordenadas Homogêneas Translação Rotação Escalonamento Projeções Geométricas Remoção de linhas e superfícies escondidas Visão computacional Filtros Compressão Informações Avaliação média de duas notas Informações (30%( atividades + 70% avaliação) Média final deverá ser superior a 6,0 (seis) Não existe arredondamento Horário 19h00 22h30 2
Questões de Reposição 1-) O que são imagens vetoriais e matriciais? 2-) O que são sistemas de cores aditivas e subtrativas? dê exemplos. 3-) Quais são as transformações no plano? Escreva suas respectivas fórmulas Dispositivos de Entrada/Saída Tipos de imagem Vetorial Matricial Sistemas de Cores Aditivas Subtrativas Transformações no Plano Translação Escala Cisalhamento Rotação Espelhamento Revisão 3
OpenGL Programa de interface para hardware gráfico API Biblioteca de rotinas gráficas (2D e 3D) GLUT Bilbioteca OpenGL Mais fácil de trabalhar Portável Baixar a biblioteca Como Instalar Glut Visual C http://www.xmission.com/~nate/glut/glut-3.7.6-src.zip -> Copiar o arquivo glut32.dll para o diretório c:\windows\system32 \ -> Copiar o arquivo glut32.lib para C:\Program Files\Microsoft Visual Studio\VC98\Lib -> Copiar o arquivo glut.h para C:\Program Files\Microsoft Visual Studio\VC98\include\GL 4
Primitivas Gráficas em OpenGL Exemplo Desenhando um triângulo com cada vértice de uma cor glbegin(gl_triangles); glcolor3f(1.0f, 0.0f, 0f 0.0f); 0f); //vermelho puro glvertex3f(0.0f, 1.0f, 0.0f); glcolor3f(0.0f, 1.0f, 0.0f); //verde puro glvertex3f(-1.0f, -1.0f, 0.0f); glcolor3f(0.0f, 0.0f, 1.0f); //azul puro glcolor3f(0.0f, 0.0f, 1.0f); //azul puro glvertex3f(1.0f, -1.0f, 0.0f); glend(); 5
Esqueleto de um programa Exemplo 6
Exemplo Exemplo 7
Fonte #include "stdafx.h" #include <GL/glut.h> void init(void) { //Apagando o fundo glclearcolor(0.0, 0.0, 0.0, 0.0); } //Inicializando as visoes glmatrixmode(gl_projection); glloadidentity(); glortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); void display(void) { //Apagar todos os pixels glclear(gl_color_buffer_bit); //Desenhando um poligono glcolor3f(1.0,1.0,1.0); glbegin(gl_polygon); glvertex3f(0.25, 0.25, 0.0); glvertex3f(0.75, 0.25, 0.0); glvertex3f(0.75, 0.75, 0.0); glvertex3f(0.25, 0.75, 0.0); glend(); // Atuzalizar glflush(); } int main (int argc,char* argv[]) { //Inicializando o Glut com os parâmetros de entrada glutinit(&argc, argv); //"Setando" o Display glutinitdisplaymode(glut_single GLUT_RGB); //Configurando o tamanho da janela glutinitwindowsize(250,250); //Configurando a posição inicial da janela glutinitwindowposition(100,100); //Criando a Janela glutcreatewindow("aula de Computacao Grafica"); //Chamando a função de inicialização init(); //Chamando a função de desenho glutdisplayfunc(display); //Entrando no loop glutmainloop(); return 0; } Transformações 8
Modifique o programa anterior: Troque as cores Desenhe figuras diferentes Implemente as transformações 2D Exercício Iluminação Para definir o seu modelo de iluminação são necessárias três etapas básicas: 1) Definir as fontes de luz (posição, cor, direção, etc.); 2) Definir a iluminação. 3) Definir o tipo de material do objeto. O modelo de iluminação do OPENGL considera que a iluminação pode ser dividida em quatro componentes independentes: emitida, ambiente, difusa e especular. - Luz Emitida: é a componente que se origina de um objeto e é inalterada pelas fontes de luz. - Luz Ambiente: é a luz proveniente de uma fonte dispersa tal que sua direção não pode ser determinada. - Luz Difusa: é a luz proveniente de uma única direção. - Luz Especular: é a luz proveniente de uma direção particular e tende a refletir em uma direção preferencial. Propriedades da fonte de luz void gllightfv(glenum luz, GLenum iluminação, GLenum param); Fonte: http://www.mat.pucrio.br/~sinesio 9
/* Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * touse, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above notice and this permission notice shall be included in all copies * or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /* File for "Lighting" lesson of the OpenGL tutorialon * www.videotutorialsrock.com */ #include <glut.h> using namespace std; //Called when a key is pressed void handlekeypress(unsigned char key, int x, int y) { switch (key) { case 27: //Escape key exit(0); } } //Initializes 3D rendering void initrendering() { glenable(gl_depth_test); glenable(gl_color_material); glenable(gl_lighting); //Enable lighting glenable(gl_light0); //Enable light #0 glenable(gl_light1); LIGHT1); //Enable light #1 glenable(gl_normalize); //Automatically normalize normals //glshademodel(gl_smooth); //Enable smooth shading } //Called when the window is resized void handleresize(int w, int h) { glviewport(0,0,w,h); glmatrixmode(gl_projection); glloadidentity(); gluperspective(45.0, (double)w / (double)h, 1.0, 200.0); } float _angle = -70.0f; //Draws the 3D scene void drawscene() { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.0f, 0.0f, -8.0f); //Add ambient light GLfloat ambientcolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2) gllightmodelfv(gl_light_model_ambient, ambientcolor); //Add positioned light GLfloat lightcolor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5) GLfloat lightpos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8) gllightfv(gl_light0, GL_DIFFUSE, lightcolor0); gllightfv(gl_light0, GL_POSITION, lightpos0); //Add directed light GLfloat lightcolor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2) //Coming from the direction (-1, 0.5, 0.5) GLfloat lightpos1[] = {-1.0f, 0.5f, 0.5f, 0.0f}; gllightfv(gl_light1, GL_DIFFUSE, lightcolor1); gllightfv(gl_light1, GL_POSITION, lightpos1); glrotatef(_angle, 0.0f, 1.0f, 0.0f); glcolor3f(1.0f, 1.0f, 0.0f); glbegin(gl_quads); //Front glnormal3f(0.0f, 0.0f, 1.0f); //glnormal3f(-1.0f, 0.0f, 1.0f); glvertex3f(-1.5f, -1.0f, 1.5f); //glnormal3f(1.0f, 0.0f, 1.0f); glvertex3f(1.5f, -1.0f, 1.5f); //glnormal3f(1.0f, 0.0f, 1.0f); glvertex3f(1.5f, 1.0f, 1.5f); //glnormal3f(-1.0f, 0.0f, 1.0f); glvertex3f(-1.5f, 1.0f, 1.5f); //Right glnormal3f(1.0f, 0.0f, 0.0f); //glnormal3f(1.0f, 0.0f, -1.0f); glvertex3f(1.5f, -1.0f, -1.5f); //glnormal3f(1.0f, 0.0f, -1.0f); glvertex3f(1.5f, 1.0f, -1.5f); //glnormal3f(1.0f, 0.0f, 1.0f); glvertex3f(1.5f, 1.0f, 1.5f); //glnormal3f(1.0f, 0.0f, 1.0f); glvertex3f(1.5f, -1.0f, 1.5f); //Back glnormal3f(0.0f, 0.0f, -1.0f); //glnormal3f(-1.0f, 0.0f, -1.0f); glvertex3f(-1.5f, -1.0f, -1.5f); //glnormal3f(-1.0f, 0.0f, -1.0f); glvertex3f(-1.5f, 1.0f, -1.5f); //glnormal3f(1.0f, 0.0f, -1.0f); glvertex3f(1.5f, 1.0f, -1.5f); //glnormal3f(1.0f, 0.0f, -1.0f); glvertex3f(1.5f, -1.0f, -1.5f); //Left glnormal3f(-1.0f, 0.0f, 0.0f); //glnormal3f(-1.0f, 0.0f, -1.0f); glvertex3f(-1.5f, -1.0f, -1.5f); //glnormal3f(-1.0f, 0.0f, 1.0f); glvertex3f(-1.5f, -1.0f, 1.5f); //glnormal3f(-1.0f, 0.0f, 1.0f); glvertex3f(-1.5f, 1.0f, 1.5f); //glnormal3f(-1.0f, 0.0f, -1.0f); glvertex3f(-1.5f, 1.0f, -1.5f); glend(); glutswapbuffers(); } void update(int value) { _angle += 1.5f; if (_angle > 360) { _angle -= 360; } glutpostredisplay(); gluttimerfunc(25, update, 0); } int main(int argc, char** argv) { //Initialize GLUT glutinit(&argc, argv); glutinitdisplaymode(glut_double GLUT_RGB GLUT_DEPTH); glutinitwindowsize(400, 400); //Create the window glutcreatewindow("lighting - videotutorialsrock.com"); initrendering(); //Set handler functions glutdisplayfunc(drawscene); glutkeyboardfunc(handlekeypress); glutreshapefunc(handleresize); gluttimerfunc(25, update, 0); //Add a timer glutmainloop(); return 0; } 03/09/2009 Com base no programa ilumina.cpp. Exercício 1-) O que ele faz? 2-) Altere o objeto desenhado para outro objeto 3D 3-) Qual a(s) linha(s) que controlam a posição da iluminação? 4-) Qual a(s) linha(s) que controlam a cor da iluminação? 5-) Modifique as cores da iluminação. 6-) Identifique e modifique o anglo de rotação 7-) Qual a função que espera o usuário apertar <ESC>(27) 8-) Que tipo de iluminação está sendo usado no programa? Ilumina.cpp 10