Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de...
Transcript of Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de...
![Page 1: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/1.jpg)
OpenGL
Conceitos Básicos
1
Waldemar Celes
Tecgraf/PUC-Rio
![Page 2: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/2.jpg)
OpenGL: o que é?
• API
– Interface para programador de aplicação
Aplicação
API abstrataAplicação
2
API abstrata
OpenGL API
Software & Hardware
Dispositivo
de saída
Dispositivo
de entrada
![Page 3: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/3.jpg)
Por que OpenGL?
• primitivas geométricas e imagens
• arquitetura bem definida
• relativamente simples
• boa performance (sw & hw)
3
• boa performance (sw & hw)
• bem documentado
• independente de sistemas de janelas
• padrão
– disponível em diversas plataformas
![Page 4: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/4.jpg)
Primitivas geométricas básicas
4
Ponto Linha
Polígono (convexo)
Triângulo
Quadrado
![Page 5: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/5.jpg)
Objetos 3D
5From SIGGRAPH’97 course
![Page 6: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/6.jpg)
Imagem e Textura
6
![Page 7: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/7.jpg)
OpenGL rendering pipeline
VérticesOperações
geométricas RasterizaçãoOperações
sobre
fragmentos
7
Operações
sobre imagensImagens Textura
Frame
Buffer
![Page 8: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/8.jpg)
Aplicação típica
Aplicação
Sistema de GLU
8
Sistema de
Janelas/Interface
(GLUT/IUP)OpenGL
GLU
Hardware
![Page 9: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/9.jpg)
Programa simples (usando GLUT)
int main (int argc, char* argv[]){/* openg GLUT */glutInit(&argc, argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize (250, 250);
#ifdef _WIN32#include <windows.h>#endif#include "GL/gl.h"#include "GL/glu.h”#include “GL/glut.h”
9
/* create window */glutCreateWindow (”simple");glutDisplayFunc(display);
/* interact … */glutMainLoop();return 0;}
![Page 10: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/10.jpg)
Programa simples (usando GLUT) - cont.
void display (void){ /* clear window */glClearColor(1,1,1,1); glClear(GL_COLOR_BUFFER_BIT);
/* draw red triangle */glColor3d(1,0,0);glBegin(GL_TRIANGLES);
10
glBegin(GL_TRIANGLES); glVertex2d(-1,-1); glVertex2d(1,-1); glVertex2d(0,1); glEnd();
/* update screen */glFlush();}
![Page 11: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/11.jpg)
Programa simples (usando IUP)int main (void){ Ihandle *dg, *cv;
/* open GUI */IupOpen(); IupGLCanvasOpen();
/* create canvas and dialog */
#include "iup.h"#include "iupgl.h"
#ifdef _WIN32#include <windows.h>#endif#include "GL/gl.h" /* create canvas and dialog */
cv = IupGLCanvas( "redraw" );dg = IupDialog(cv);IupSetAttribute(dg,"SIZE","200x200");IupSetFunction("redraw",(Icallback)redraw);IupShow(dg);
/* interact... */IupMainLoop(); IupClose(); return 0;}
"GL/gl.h"#include "GL/glu.h"
![Page 12: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/12.jpg)
Programa simples (cont.)int redraw (Ihandle *cv, double x, double y){ IupGLMakeCurrent(cv);
/* clear window */glClearColor(1,1,1,1); glClear(GL_COLOR_BUFFER_BIT);
/* draw red triangle *//* draw red triangle */glColor3d(1,0,0);glBegin(GL_TRIANGLES); glVertex2d(-1,-1); glVertex2d(1,-1); glVertex2d(0,1); glEnd();
/* update screen */glFlush();return IUP_DEFAULT;}
![Page 13: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/13.jpg)
OpenGL & IUP
#include “iup.h”#include “iupgl.h”
Inicialização
IupOpen(); IupGLCanvasOpen();
Bibliotecas:• IUP
iup.lib & iupgl.lib• OpenGL
opengl.lib & glu.lib (SGI)opengl32.lib & glu32.lib (MS)
IupGLCanvasOpen();
Criação em C
cv = IupGLCanvas( "redraw" );
Criação em LED
cv = GLCanvas(redraw)
Atributos
BUFFER = SINGLE ou DOUBLECOLOR = RGBA ou INDEX...
![Page 14: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/14.jpg)
OpenGL: máquina de estado
• Trabalha com o conceito de valor corrente
– Iluminação
– Shading
14
– Shading
– Textura
– etc.
glEnable/glDisable
![Page 15: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/15.jpg)
Inicialização da área de desenho
glClearColor(red,green,blue,alpha);
glClear(GL_COLOR_BUFFER_BIT);
15
Atualização da área de desenho
glFlush( );glFinish( ); // modal
![Page 16: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/16.jpg)
Primitivas geométricas
glBegin(tipo_de_prim);
16
…define attributo de vértice
…define vértice
glEnd();
![Page 17: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/17.jpg)
Tipos de primitivasGL_POINTS
GL_LINE_LOOPGL_LINE_STRIPGL_LINES
01
2
0
1
2
3 5
4 0
1
2
3
0 1
234
2
1 21 65 3
1 5
17
GL_QUAD_STRIPGL_QUADSGL_POLYGON
GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
04
31
03
2
4 7 0
3
2 4
0
1
2
34
5
1
02
3
4
5
0
1
2 3
4
![Page 18: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/18.jpg)
Especificação de vértice
glVertex{tam}{tipo}{vetor} (…);
exemplo:
GLdouble pos[ ] = {0.4,9.0,2.0};
18
GLdouble pos[ ] = {0.4,9.0,2.0};
glVertex3dv(pos);ou
glVertex3d(0.4,9.0,2.0);
• OpenGL trabalha com coordenadas homegêneas
![Page 19: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/19.jpg)
Especificação de atributos: Cor
• Modelo de cor
–RGB
glColor3d(red,green,blue);
19
–Color index
• Paleta previamente definida
IupGLPalette (handle, index, red, green, blue);
...
glIndexi(index);
![Page 20: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/20.jpg)
Modelo de Shading
• Flat
glShadeModel(GL_FLAT);
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0); // red
20
glVertex2f(-1.0,-1.0);
glColor3f(0.0,1.0,0.0); // green
glVertex2f(1.0,-1.0);
glColor3f(0.0,0.0,1.0); // blue
glVertex2f(0.0,1.0);
glEnd( );
![Page 21: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/21.jpg)
Modelo de Shading• Smooth (Gouraud)
glShadeModel(GL_SMOOTH); // default
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0); // red
21
glVertex2f(-1.0,-1.0);
glColor3f(0.0,1.0,0.0); // green
glVertex2f(1.0,-1.0);
glColor3f(0.0,0.0,1.0); // blue
glVertex2f(0.0,1.0);
glEnd( );
![Page 22: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/22.jpg)
Transformações 3D e
Sistemas de Coordenadas
22From SIGGRAPH’97 course
![Page 23: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/23.jpg)
Visualização 3D
• Camera
– Posição fixa: (0.0,0.0,0.0)
– Direção: -z
y
23
– Direção: -z
• Composição da cena
– move camera ou
– move objetos
x
z
![Page 24: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/24.jpg)
Transformação de
Modelagem x Visualização
• Transformação de modelagem
– Sistema global fixo
– Ordem inversa para especificação
24
– Ordem inversa para especificação
X
Y
X
Y
X
Y
...glMatrixMode(GL_MODELVIEW); glLoadIdentity();glRotatef(30,0,0,1);glTranslatef(10,0,0);...
![Page 25: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/25.jpg)
• Transformação de visualização
– Sistema local móvel
– Ordem natural para especificação
Transformação de
Modelagem x Visualização (cont.)
25
– Ordem natural para especificação
…glMatrixMode(GL_PROJECTION); glLoadIdentity();glRotatef(30,0,0,1);glTranslatef(10,0,0);...
X
Y
X
Y
X
Y
![Page 26: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/26.jpg)
Manipulação da pilha de
matrizes
…glMatrixMode(GL_MODELVIEW); glLoadIdentity( ); I IglLoadIdentity( );glPushMatrix( );glRotate(30,0,0,1);glTranslate(10,0,0);draw_object_1( );glPopMatrix( );...
I
I
I
I
RT
R
I
![Page 27: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/27.jpg)
Posicionamento da camera
• Função auxiliar
…glMatrixMode(GL_MODELVIEW);
27
glMatrixMode(GL_MODELVIEW); glLoadIdentity();gluLookAt(eye_x, eye_y, eye_z,
center_x, center_y, center_z,up_x, up_y, up_z);
...
![Page 28: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/28.jpg)
Projeção: tipo de camera
• Perspectivaw
h
near
teta
28
near
far
…glMatrixMode(GL_PROJECTION); glLoadIdentity( );gluPerspective (teta_y,aspect,znear,zfar);...
aspect = w / h
![Page 29: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/29.jpg)
Projeção: tipo de camera (cont.)
• Ortográfica
near
far
29
far
…glMatrixMode(GL_PROJECTION); glLoadIdentity( );glOrtho (xleft,xright,ybottom,ytop,znear,zfar);...
2D:
gluOrtho2D (xleft,xright,ybottom,ytop);
![Page 30: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/30.jpg)
Transformação de viewport
…glViewport (x, y, width, height); ...
30
...
IUP & GLUT:
• A função default de “resize” define a viewport
como sendo a área total do canvas.
![Page 31: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/31.jpg)
Exemplo: sistema solar
• Sol e um planetaRaio
Rotaçãodiária
Rotaçãoanual
31
anual
Sol: desenhado na origemPlaneta:
Pensando em sistema local• Rotação anual• Translação em x• Rotação diária
![Page 32: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/32.jpg)
Remoção de superfícies ocultas
• Z-BUFFER
– Inicializa window (default)
– Habilita teste em Z
32
glEnable (GL_DEPTH_TEST);
– Define teste
glDepthFunc (GL_LESS);
– Limpa buffer
glClear (GL_DEPTH_BUFFER_BIT);
![Page 33: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/33.jpg)
Animação• Double color buffer: BACK & FRONT
– Inicialização
• GLUT
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
• IUP/C
33
cv = IupGLCanvas (“redraw”);
IupSetAttribute (cv, IUP_BUFFER, IUP_DOUBLE);
• IUP/LED
cv = GLCanvas [BUFFER=DOUBLE] (redraw)
– Atualização da tela
IupGLSwapBuffers (cv);
glutSwapBuffers( );
![Page 34: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/34.jpg)
Rendering
• Cor do objeto depende de:
– fonte de luz
– orientação da superfície
– posição do observador
34
– reflexividade do material
• ambiente
• difusa
• especular
Modelo de iluminação: Phong
![Page 35: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/35.jpg)
Especificação da orientação
• Vetor normal em cada vértice
glNormal3d(nx,ny,nz);
• Se não for normalizado
35
• Se não for normalizado
glEnable (GL_NORMALIZE);
Obs: cálculo de normal é caro!
![Page 36: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/36.jpg)
Fontes de luz
• Tipos
Glfloat pos[ ] = {x,y,z,w};
glLightf (GL_LIGHT0, GL_POSITION, pos);
• Cor e intensidade: ambiente, difusa, especular
Direcional Pontual Spot
36
• Cor e intensidade: ambiente, difusa, especular
Glfloat dif[ ] = {red,green,blue,alpha};
glLightf (GL_LIGHT0, GL_DIFFUSE, dif);
• Habilitação
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
![Page 37: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/37.jpg)
Parâmetros adicionais de iluminação
• Luz ambiente globalGLfloat amb[ ] = {0.2,0.2,0.2,1.0};glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
• Posição do observador: local ou infinito
37
• Posição do observador: local ou infinitoglLightModeli (GL_LIGHT_MODEL_VIEWER, GL_TRUE);
• Iluminação de faces: back e frontglLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
![Page 38: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/38.jpg)
Material• Cor (reflexividade)
– Ambiente• não depende de orientação
– Difusa• depende da orientação da superfície e da posição da fonte de luz
– Especular• depende da orientação da superfície, da posição da fonte de luz e
posição do observador
– Brilho (shininess)
38
– Brilho (shininess)• fator de brilho da reflexão especular
– Emissão• para representação de fontes de luz na cena
Glfloat color [ ] = { red, green, blue, alpha };glMaterialf (GL_BACK_AND_FRONT,
GL_AMBIENT_AND_DIFFUSE, color);
![Page 39: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/39.jpg)
Cor como material
• Usando cor para definição de material
glColorMaterial (GL_BACK_AND_FRONT,
39
glColorMaterial (GL_BACK_AND_FRONT,GL_AMBIENT_AND_DIFFUSE);
glEnable (GL_COLOR_MATERIAL);
…glColor3f (red, green, blue);...
![Page 40: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/40.jpg)
OpenGL rendering pipeline
VérticesOperações
geométricas RasterizaçãoOperações
sobre
fragmentos
40
Operações
sobre imagensImagens Textura
Frame
Buffer
![Page 41: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/41.jpg)
Blending• Combinação da cor do fragmento sendo
processado com a cor do pixel
– depois da rasterização e antes do fragmento
ser desenhado no framebuffer.
41
• Aplicações
– transparência
– composição digital
– pintura
obs: blending não funciona com color index.
![Page 42: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/42.jpg)
Blending: exemplos de uso
• Desenho temporário sobre imagemglEnable (GL_BLEND);
glBlendFunc (GL_ONE_MINUS_DST_COLOR,
GL_ZERO);
glColor3d (1.0, 1.0, 1.0);
42
• Objetos transparentes
– Cor dada por: (red, green, blue, opacity)
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_APHA );
![Page 43: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/43.jpg)
Transparência
em cena 3D
43
• habilita-se z-buffer
• desenha-se objetos opacos
• define-se z-buffer como read-only
glDepthMask (GL_FALSE);
• desenha-se objetos com transparência em ordem
![Page 44: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/44.jpg)
Imagens
• Representa uma área retangular de valores
associados aos pixels
• Fatores complicantes
– existem diferentes dados associados aos
pixels
44
pixels
– existem diferentes formas de armazenar uma
imagem
– existem diferentes conversões de dados
quando operamos sobre pixels
![Page 45: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/45.jpg)
Operações sobre imagens
• Read
• Draw
Frame buffer Memória
45
• Draw
• Copy
Memória Frame buffer
Frame buffer Frame buffer
![Page 46: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/46.jpg)
Formato de cada pixel
GL_RGB
GL_RGBA
GL_RED
GL_GREEN
• De 1 a 4 elementos representam um pixel
46
GL_GREEN
GL_BLUE
GL_ALPHA
GL_LUMINANCE
GL_LUMINANCE_ALPHA
GL_DEPTH_COMPONENT
GL_STENCIL_INDEX
GL_COLOR_INDEX
![Page 47: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/47.jpg)
Tipo de cada elemento
UNSIGNED_BYTE …………………………………… 8 bitsBYTE ..…………………………………………………. 7 bitsUNSIGNED_SHORT …………………………………. 16 bitsSHORT …………………………………………………. 15 bits
47
SHORT …………………………………………………. 15 bitsUNSIGNED_INT ………………………………………. 32 bitsINT ………………………………………………………. 31 bitsFLOAT ………………………………………………….. [0.0,1.0]
BITMAP…………………………………………………. 1 bit
![Page 48: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/48.jpg)
Desenhando imagens
• Posição da imagem
glRasterPos* (x, y, z, w);
• Especificação da imagem
glDrawPixels (width, height, format, type, pixels)
48
glDrawPixels (width, height, format, type, pixels)
• Especificação de bitmap
– Projetado para suportar desenho de fontes raster
glBitmap (width, height, x0, y0, xinc, yinc, bitmap);
![Page 49: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/49.jpg)
Textura• Mapeamento de imagens sobre primitivas
• Imagem composta por texels
• Largura e altura: 2n
gluScaleImage (format,
width_in, height_in, type_in, data_in,
width_out, height_out, type_out, data_out);
49
width_out, height_out, type_out, data_out);
• Especificação: 1D e 2DglEnable (GL_TEXTURE_2D or GL_TEXTURE_1D);
glTexImage2D (GL_TEXTURE_2D, level, components,
width, height, border, format, type, pixels);
glTexImage1D (GL_TEXTURE_1D, level, components,
width, border, format, type, pixels);
![Page 50: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/50.jpg)
Coordenada de textura
• Para cada vértice
– coordenada: identifica qual o pixel no buffer
– coordenada de textura: identifica qual o texel
• Coordenadas de textura: s, t, r, q
50
• Coordenadas de textura: s, t, r, q
• Coordenadas de textura são linearmente
interpoladas entre vértices
glTexCoord* (s, t, r, q);
• Intervalo [0,1], senão wrap?
• Transformados pela matriz de textura
![Page 51: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/51.jpg)
Mapeamento de textura
(0,0) (1,0)
(1,1)(0,1)
51
Textura
(0,0) (1,0)
(0,0) (8,0)
(8,4)(0,4)
![Page 52: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/52.jpg)
Combinação de pixel com texel• Decal
– Cor definida pelo texel
• Modulate– Cor do pixel é modulada pela cor do texel
• Blend
52
• Blend– Cor combinada com uma cor adicional de
ambiente
• Exemplo– Modular com a cor branca para dar iluminação
glTexEnvi (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
![Page 53: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/53.jpg)
Exemplo de modulação
53
DECAL MODULATE
![Page 54: Waldemar Celes - PUC-Rio€¦ · Material • Cor (reflexividade) – Ambiente • não depende de orientação – Difusa • depende da orientação da superfície e da posição](https://reader035.fdocumentos.tips/reader035/viewer/2022071004/5fc1258797ca174aca4422a6/html5/thumbnails/54.jpg)
Referências• “The Red Book”
OpenGL: Programming Guide
Release 1.2
M. Woo, J. Neider, T. Davis
• Web sites
54
• Web sites
The official OpenGL web page
http://www.opengl.org
SGI’s OpenGL WWW Center
http://www.sgi.com/Technology/openGL
Gateway to OpenGL
http://reality.sgi.com/mjk_asd/opengl-links.html