Módulo 7 Sistemas Gráficos e Interação Instituto Superior de Engenharia do Porto Filipe Pacheco ffp@isep.ipp.pt Transformações geométricas SGRAI 2017/18 DEI-ISEP 97
Conteúdo Tipos de transformações Model/View Viewport Operações de transformação Rotação Translação Escalamento Posicionamento da câmara Composição de transformações SGRAI 2017/18 DEI-ISEP 98
Analogia da câmara fotográfica 1. Apontar a câmara à cena (viewing transformation). 2. Compor a cena (modeling transformation). 3. Escolher o tipo de lente e acertar o zoom (projection transformation). 4. Determinar o tamanho físico da cena (viewport transformation). SGRAI 2017/18 99
Tipos de transformações View e Model glmatrixmode(gl_modelview) Posicionamento da câmara Rotações, translações, escalamentos Projection glmatrixmode(gl_projection) Definição do volume de projeção Viewport glviewport (x, y, width, height) SGRAI 2017/18 DEI-ISEP 100
Esqueleto de código void reshape(int w, int h) { // viewport transformation glviewport(0, 0, w, h); // projection transformation glmatrixmode(gl_projection); glloadidentity(); projecao();... } void display() { // modelview transformation glmatrixmode(gl_modelview); // costuma estar na reshape glloadidentity(); // posicionamento da câmara camara(); // transformações do modelo... } SGRAI 2017/18 DEI-ISEP 101
Evitar transformações desnecessárias Deve-se evitar cálculos acumulados nas matrizes Exemplo: não ir aplicando rotações na mesma matriz, mas sim ter uma variável com o ângulo de rotação SGRAI 2017/18 DEI-ISEP 102
Inicializar as matrizes Antes de definir transformações deve-se inicializar a matriz de transformação usando glloadidentity() SGRAI 2017/18 DEI-ISEP 103
Rotações void glrotate{f d} (angle, ex, ey, ez) R SGRAI 2017/18 DEI-ISEP 104
Rotações glloadidentity(); glcolor3f(1, 0, 0); glrectf(-0.5,-0.5,0.5,0.5); glrotatef(10, 0, 0, 1); glcolor3ub(0, 255, 0); glrectf(-0.25,-0.25,0.25,0.25); R SGRAI 2017/18 DEI-ISEP 105
Translações void gltranslate{f d} (dx, dy, dz); T SGRAI 2017/18 DEI-ISEP 106
Translações glloadidentity(); glcolor3f(1, 0, 0); glrectf(-0.5,-0.5,0.5,0.5); gltranslatef(0.5, 0.0, 0.0); glcolor3ub(0, 255, 0); glrectf(-0.25,-0.25,0.25,0.25); T SGRAI 2017/18 DEI-ISEP 107
Translações glloadidentity(); glcolor3f(1, 0, 0); glrectf(-0.5,-0.5,0.5,0.5); gltranslatef(0.5, 0.0, 0.0); glcolor3ub(0, 255, 0); glrectf(-0.25,-0.25,0.25,0.25); 0.5, 0.5 0.5, 0.5 0.25, 0.25 0.25, 0.25 T 0.0, 0.0 0.5, 0.0-0.5, -0.5-0.25, -0.25-0.5, -0.5-0.25, -0.25 SGRAI 2017/18 DEI-ISEP 108
Escalamento void glscale{f d} sx, sy, sz) evitar escalas não proporcionais (sx!= sy!= sz) pois alteram vectores normal, rotações, etc S SGRAI 2017/18 DEI-ISEP 109
Escalamento glloadidentity(); glcolor3f(1, 0, 0); glrectf(-0.5,-0.5,0.5,0.5); glscalef(0.5, 0.5, 0.5); glcolor3ub(0, 255, 0); glrectf(-0.25,-0.25,0.25,0.25); S SGRAI 2017/18 DEI-ISEP 110
Escalamento glloadidentity(); glcolor3f(1, 0, 0); glrectf(-0.5,-0.5,0.5,0.5); glscalef(0.5, 0.5, 0.5); glcolor3ub(0, 255, 0); glrectf(-0.25,-0.25,0.25,0.25); 0.5, 0.5 0.5, 0.5 0.25, 0.25 1.0, 1.0 S -1.0, -1.0 0.25, 0.25-0.5, -0.5-0.25, -0.25-0.5, -0.5-0.25, -0.25 SGRAI 2017/18 DEI-ISEP 111
Demo SGRAI 2017/18 DEI-ISEP 112
Posição da câmara Por omissão a câmara está colocada na origem (0, 0, 0) dirigida para o eixo negativo dos zz SGRAI 2017/18 DEI-ISEP 114
Posicionamento da câmara void glulookat( eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz) center eye SGRAI 2017/18 DEI-ISEP 115
Posicionamento da câmara void glulookat(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz) SGRAI 2017/18 DEI-ISEP 116
Posição da câmara Mover a câmara ou mover a cena têm o mesmo resultado glulookat(0,0,+5, 0,0,0, 0,1,0) gltranslatef(0, 0, -5) y (0, 1,0) z (0, 0,5) (0, 0,0) SGRAI 2017/18 DEI-ISEP 117
Posição da câmara Mover a câmara ou mover a cena têm o mesmo resultado glulookat(0,0,+5, 0,0,0, 0,1,0) gltranslatef(0, 0, -5) y y z z (0, 0, -5) SGRAI 2017/18 DEI-ISEP 118
Posição da câmara Podem usar glulookat ou construir a vossa própria rotina de visualização, por exemplo, câmara com movimento polar usando as operações básicas de transformação SGRAI 2017/18 DEI-ISEP 119
O sistema de coordenadas local Ao aplicar uma transformação estão na realidade a mover um sistema de coordenadas agarrado ao modelo glutsolidcube(2) gltranslatef(3, 3, 0) glrotate(45, 0, 0, 1) glutsolidcube(2) SGRAI 2017/18 DEI-ISEP 122
O sistema de coordenadas local glutsolidcube(2) glrect(3, 2, 0, 5, 4, 0) gltranslatef(3, 3, 0) glrotate(45, 0, 0, 1) glutsolidcube(2) glrect(3, 2, 0, 5, 4, 0) SGRAI 2017/18 DEI-ISEP 123
Efeito cumulativo de transformações Translação + Rotação Rotação + Translação T R R T SGRAI 2017/18 DEI-ISEP 124
PushMatrix + PopMatrix Cena final desejada glloadidentity() glpushmatrix() gltranslatef(3, 3, 0) glrotate(45, 0, 0, 1) glutsolidcube(2) glpopmatrix() glrect(3, 2, 0, 5, 4, 0) SGRAI 2017/18 DEI-ISEP 126
Transformações locais Guardar a matriz de transformação usando glpushmatrix() Recuperar a matriz anterior usando glpopmatrix() glpushmatrix e glpopmatrix podem ser usados para a matriz de projeção ou para a matriz de modelo/vista Devem ter em atenção, qual a matriz ativa no momento SGRAI 2017/18 DEI-ISEP 127
Transformações (in)dependentes SGRAI 2017/18 DEI-ISEP 128
Demo SGRAI 2017/18 DEI-ISEP 129
Texto em GLUT Desenhar um carácter glutstrokecharacter(fonte, caracter) glutbitmapcharacter(fonte, caracter) Fontes GLUT_STROKE_ROMAN GLUT_STROKE_MONO_ROMAN GLUT_BITMAP_ 9_BY_15 GLUT_BITMAP_ 8_BY_13 GLUT_BITMAP_TIMES_ROMAN_ 10 GLUT_BITMAP_TIMES_ROMAN_ 24 GLUT_BITMAP_HELVETICA_ 10 GLUT_BITMAP_HELVETICA_ 12 GLUT_BITMAP_HELVETICA_ 18 SGRAI 2017/18 DEI-ISEP 130
Texto em GLUT Desenhado usando o sistema de coordenadas locais e todas as transformações em vigor O tamanho de cada carácter é tipicamente 120 unidades Necessário escalar Proteger a matriz de transformação usando push e pop matrix Ciclo para imprimir uma string completa SGRAI 2017/18 DEI-ISEP 131
Função escreve() void drawstring(glfloat x, GLfloat y, GLfloat z, GLfloat scale, char* msg) { glpushmatrix(); gltranslatef(x, y, z); glscalef(scale, scale, scale); for (int i = 0; i < strlen(msg); i++) glutstrokecharacter(glut_stroke_roman, msg[i]); glpopmatrix(); } SGRAI 2017/18 DEI-ISEP 132
Texto sem usar transformações do modelo // exemplo glpushmatrix(); glloadidentity(); glulookat(0, 0, 5, 0, 0, 0, 0, 1, 0); drawstring(-2, -2, 0, 0.002, msg); glpopmatrix(); SGRAI 2017/18 DEI-ISEP 133
Demo SGRAI 2017/18 DEI-ISEP 134