Sunteți pe pagina 1din 27

COMPUTACION GRFICA Y VISUAL

Ing. Jos Luis Peralta Lujn

Traslacin

Ing. Jos Luis Peralta Lujn

Para construir una matriz de traslacin:


void glTranslatef (GLfloat x, GLfloat y, GLfloat z)
y

x
z

x
z

// Trasladar 10 unidades hacia arriba


glTranslatef (0.0f, 10.0f, 0.0f);
// Dibuja el cubo
glutSolidCube (10.0f);
Ing. Jos Luis Peralta Lujn

Rotacin

Ing. Jos Luis Peralta Lujn

Para construir una matriz de rotacin:


void glRotatef (GLfloat ang, GLfloat x, GLfloat y, GLfloat z)
y

x
z

x
z

// Rotar 45 grados en el eje y


glRotatef (45.0f, 0.0f, 1.0f, 0.0f);
// Dibuja el cubo
glutSolidCube (10.0f);
Ing. Jos Luis Peralta Lujn

Escalado

Ing. Jos Luis Peralta Lujn

Para construir una matriz de escalado:


void glScalef (GLfloat x, GLfloat y, GLfloat z)
y

x
z

x
z

// Escalar el doble en vertical


glScalef (1.0f, 2.0f, 1.0f);
// Dibuja el cubo
glutSolidCube (10.0f);
Ing. Jos Luis Peralta Lujn

Orden de las Transformaciones

Ing. Jos Luis Peralta Lujn

Ing. Jos Luis Peralta Lujn

Ejemplo

Ing. Jos Luis Peralta Lujn

Ing. Jos Luis Peralta Lujn

Ing. Jos Luis Peralta Lujn

Ing. Jos Luis Peralta Lujn

Ejemplo con OpenGL

Ing. Jos Luis Peralta Lujn

Ing. Jos Luis Peralta Lujn

//Segunda Transformacin que se aplica

y'

glRotatef(45,0,0,1);

//Primera Transformacin que se aplica

x'
x

glTranslatef(10,0,0);

1/ 2

1/ 2
M=
0
0

1/ 2 0 0

1/ 2 0 0

0
1 0
0
0 1

glRectf(-5,-5,5,5);

y'
x'
y

0
M = M
0

0 0 10 1/ 2

1 0 0 1/ 2
=
0 1 0 0

0 0 1 0

1/ 2 0 10/ 2

1/ 2 0 10/ 2

0
1
0
0
0
1

y'
x'
y

P
x

Ing. Jos Luis Peralta Lujn

1/ 2

1/ 2
P = MP =
0
0

1/ 2 0 10/ 2 5 10 2

1/ 2 0 10/ 2 5 2
=

0
1
0 0 0

0
0
1 1 1

Otro Ejemplo con OpenGL

Ing. Jos Luis Peralta Lujn

Ing. Jos Luis Peralta Lujn

Funciones de Matrices

Ing. Jos Luis Peralta Lujn

glLoadIdentity(void)
glLoadMatrix{fd}(cont TYPE *m)
glMultMatrix{fd}(cont TYPE *m)

m1
m
M = 2
m3

m4

Ing. Jos Luis Peralta Lujn

m5
m6
m7
m8

m9
m10
m11
m12

m13
m14
m15

m16

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrixf(M1);
glMultMatrixf(M2);
glMultMatrixf(M3);

Se aplica al vector v:
M1M2M3v = [M1[M2[M3v]]]

Nota: en realidad se aplica: [M1M2M3]v

Ing. Jos Luis Peralta Lujn

Reiniciar la Matriz de Modelado

Ing. Jos Luis Peralta Lujn

y
10

Si quisiramos obtener esta escena,


no podemos hacer esto
10

// Sube 10 unidades en y
glTranslatef (0.0f, 10.0f, 0.0f);

// Dibuja el primer cubo


glutSolidCube (1.0f);

// Mueve 10 unidades en x
glTranslatef (10.0f, 0.0f, 0.0f);
10

// Dibuja el segundo cubo


glutSolidCube (1.0f);
10

porque obtendramos esto:


Ing. Jos Luis Peralta Lujn

Necesitamos una forma de resetear la matriz de modelado


void glLoadIdentity()

El cdigo correcto sera:


// Inicializa la matriz del modelador
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();

y
10

// Sube 10 unidades en y
glTranslatef (0.0f, 10.0f, 0.0f);
// Dibuja el primer cubo
glutSolidCube (1.0f);
// Reinicia de nuevo la matriz
glLoadIdentity();
// Mueve 10 unidades en x
glTranslatef (10.0f, 0.0f, 0.0f);
// Dibuja el segundo cubo
glutSolidCube (1.0f);
Ing. Jos Luis Peralta Lujn

10

Pila de Matrices

Ing. Jos Luis Peralta Lujn

No siempre es deseable reiniciar por completo la matriz


de modelado
A veces es preferible querer almacenar la matriz actual, y
volverla a recuperar ms adelante
OpenGL mantiene una pila de matrices

Para conocer la profundidad mxima de la pila:


glGet(GL_MAX_MODELVIEW_STACK_DEPTH)

Ing. Jos Luis Peralta Lujn

glPushMatrix(void)
glPopMatrix(void)

Ing. Jos Luis Peralta Lujn

S-ar putea să vă placă și