• No se han encontrado resultados

Transformaciones con OpenGL

N/A
N/A
Protected

Academic year: 2021

Share "Transformaciones con OpenGL"

Copied!
36
0
0

Texto completo

(1)

Transformaciones con

OpenGL

(2)

Pipeline:Transformaciones

(3)

Transformaciones

!   Las operaciones con matrices en OpenGL

–  glMatrixMode( GLenum mode );

•  mode: GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE

–  Es utilizada para definir las coordenadas de vision, se asigna una matriz de 4x4

–  De igual manera, se define la proyección, como la imagen es proyectada sobre la pantalla, una

matriz de 4X4

(4)

Transformaciones

! Una alternativa, podemos asignar otros valores a los elementos de una matriz

–  glLoadIdentity();

•  La matriz inicial, una matriz identidad de 4x4

–   glLoadMatrix{f,d}( const TYPE* m );

–  glMultMatrix{f,d}( const TYPE* m );

–   glPushMatrix();

–   glPopMatrix();

•  Las operaciones de stack son muy utiles para construir estructuras jerarquicas.

•  Ejemplo Hacer el render de un carro con cuatro ruedas

(5)

! glLoadMatrix*( element16);

glMatrixMode(GL_MODELVIEW);

glfloat elems[16];

GLint k;

for( k=0; k<16; k++) elems [k] = float (k);

glLoadMatrixf(elems);

(6)

M=

0.0 4.0 8.0 12.0

1.0 5.0 9.0 13.0

2.0 6.0 10.0 14.0

3.0 7.0 11.0 15.0

(7)

Composicion de Matrices

! glMultMatrix*(otroelements16);

M= MxM1

glMatrixMode(GL_MODELVIEW);

glLoadIdentity( );

glMultMatrixf( elemsM2);

glMultMatrixf( elemsM1);

M=M2xM1

(8)

Transformaciones

!   OpenGL ofrece transformaciones preconstruidas :

–  glTranslate{f,d}( TYPE x, TYPE, y, TYPE z );

Efecto de glTranslate()

(9)

Transformationes

–   glRotate{f,d}( TYPE angle, TYPE x, TYPE y, TYPE z );

Efecto de glRotatef(45.0, 0.0, 0.0, 1.0)

(10)

Transformaciones

–   glScale{f,d}( TYPE x, TYPE y, TYPE z);

El efecto de glScalef(2.0, -0.5, 1.0)

(11)

Transformaciones

! Rotando primero o Translación primero :

(12)

Transformaciones

! Transformaciones de Visión

–  Hay que seleccionar un sistema de Visión o de Camara

•  Center-orientation-up system

–  Aplicando gluLookAt.

»   gluLookAt( cx, cy, cz, atx, aty, atz, upx, upy, upz );

»  ( cx, cy, cz ) es la ubicación de la cámara

»  ( atx, aty, atz ) hacia donde ve la cámara

»  ( upx, upy, upz ) es el vector de orientación de la cámara

•  Coordenadas Polares

(13)

Transformaciones

! Transformaciones de Proyección: Proyección en Perspectiva

–  glFrustum( GLdouble left, GLdouble right,

GLdouble bottom, GLdouble top, GLdouble near,

GLdouble far );

(14)

Transformaciones

–  gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far );

(15)

Transformaciones

! Transformación de Proyección: Proyección Ortogonal (Orthogonal projection)

–   glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far );

–   gluOrtho2D( GLdouble left, GLdouble right, GLdouble

bottom, GLdouble top );

(16)

Transformaciones

! Transformación de Viewport

–  glViewport( GLint x, GLint y, GLsizei w, GLsizei

h );

(17)

Transformaciones

!   Nota:

–  Por default, el observador esta situado en

el origen, y mira hacia el eje negativo del

eje-z

(18)

Ejemplo

(19)

Rotando un cubo con Interpolacion de Color

#include <stdlib.h>

#include <GL/glut.h>

GLfloat vertices[][3] = {{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},

{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},

{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};

(20)

void polygon(int a, int b, int c, int d) {

/* draw a polygon via list of vertices */

glBegin(GL_POLYGON);

glColor3fv(colors[a]);

glVertex3fv(vertices[a]);

glColor3fv(colors[b]);

glVertex3fv(vertices[b]);

glColor3fv(colors[c]);

glVertex3fv(vertices[c]);

glColor3fv(colors[d]);

glVertex3fv(vertices[d]);

glEnd();

}

(21)

void colorcube() {

/* map vertices to faces */

polygon(0,3,2,1);

polygon(2,3,7,6);

polygon(0,4,7,3);

polygon(1,2,6,5);

polygon(4,5,6,7);

polygon(0,1,5,4);

}

(22)

static GLfloat theta[] = {0.0,0.0,0.0};

static GLint axis = 2;

void display() {

/* display callback, clear frame buffer and z buffer, rotate cube and draw, swap buffers */

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glRotatef(theta[0], 1.0, 0.0, 0.0);

glRotatef(theta[1], 0.0, 1.0, 0.0);

glRotatef(theta[2], 0.0, 0.0, 1.0);

colorcube();

glFlush();

glutSwapBuffers();

}

(23)

void spinCube() {

/* idle callback, spin cube 2 degrees about selected axis */

theta[axis] += 2.0;

if( theta[axis] > 360.0 ) theta[axis] -= 360.0;

/* display(); */

glutPostRedisplay();

}

void mouse(int btn, int state, int x, int y) {

/* mouse callback, selects an axis about which to rotate */

if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;

if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;

if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;

}

(24)

void myReshape(int w, int h) {

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

if (w <= h)

glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);

else

glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,

2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);

glMatrixMode(GL_MODELVIEW);

}

(25)

int main(int argc, char **argv) {

glutInit(&argc, argv);

/* need both double buffering and z buffer */

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(500, 500);

glutCreateWindow("colorcube");

glutReshapeFunc(myReshape);

glutDisplayFunc(display);

glutIdleFunc(spinCube);

glutMouseFunc(mouse);

glEnable(GL_DEPTH_TEST); /* Enable hidden-surface removal */

glutMainLoop();

}

(26)
(27)

Ejemplo

!  planet.c

– Control:

•  ‘d’

•  ‘y’

•  ‘a’

•  ‘A’

•  ESC

(28)

#include <GL/glut.h>

static GLfloat year=0.0f, day=0.0f;

void init()

{ glClearColor(0.0, 0.0, 0.0, 0.0); }

void GL_reshape(GLsizei w, GLsizei h) // GLUT reshape function {

glViewport(0, 0, w, h); // viewport transformation glMatrixMode(GL_PROJECTION); // projection transformation glLoadIdentity();

gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0);

glMatrixMode(GL_MODELVIEW); // viewing and modeling transformation glLoadIdentity();

gluLookAt(0.0, 3.0, 5.0, // eye 0.0, 0.0, 0.0, // center 0.0, 1.0, 0.0); // up }

(29)

void GL_display() // GLUT display function {

// clear the buffer

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);

glPushMatrix();

glutWireSphere(1.0, 20, 16); // the Sun glRotatef(year, 0.0, 1.0, 0.0);

glTranslatef(3.0, 0.0, 0.0);

glRotatef(day, 0.0, 1.0, 0.0);

glutWireSphere(0.5, 10, 8); // the Planet glPopMatrix();

// swap the front and back buffers glutSwapBuffers();

}

(30)

void GL_idle() // GLUT idle function {

day += 10.0;

if(day > 360.0) day -= 360.0;

year += 1.0;

if(year > 360.0) year -= 360.0;

// recall GL_display() function glutPostRedisplay();

}

(31)

void GL_keyboard(unsigned char key, int x, int y) // GLUT keyboard function {

switch(key) {

case 'd': day += 10.0;

if(day > 360.0) day -= 360.0;

glutPostRedisplay();

break;

case 'y': year += 1.0;

if(year > 360.0) year -= 360.0;

glutPostRedisplay();

break;

case 'a': glutIdleFunc(GL_idle); // assign idle function break;

case 'A': glutIdleFunc(0);

break;

case 27: exit(0);

} }

(32)

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitWindowSize(500, 500);

glutInitWindowPosition(0, 0);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutCreateWindow("Planet");

init();

glutDisplayFunc(GL_display);

glutReshapeFunc(GL_reshape);

glutKeyboardFunc(GL_keyboard);

glutMainLoop();

return 0;

}

(33)
(34)

Tarea

(35)

!   Para poder ejecutar el código que simula un perro

corriendo, el código esta en la carpeta C-Dog ubicada en el sitio del curso. Enfocarse en las

transformaciones en el código proporcionado.

!   La tarea consiste en modificar el movimiento del perro para que pueda correr de manera mas

adecuada

!   Fecha de entrega 30 de marzo

(36)

Preguntas

?

Referencias

Documento similar

As introduced in Section 3.1, another way to exploit item aspect sentiment information for recommendation is to consider the topic distribution vectors extracted from the

Intent space, aspect extraction, information retrieval, diversity, probabilistic latent semantic

SECCION EN

In the dipole matrix elements examined so far, at least one of the wave functions is localized near the origin. To be able to represent multiphoton transitions and

2004, Space physics : an introduction to plasmas and particles in the he- liosphere and magnetospheres (Space physics : an introduction to plasmas and particles in the heliosphere

This code implements the interactions of electrons in PMMA based on the realistic initial distribution of secondary electrons generated along the proton’s path, according to the

Estos resultados fueran estendidos para la familia de Bonnet, que es una clase de superficies m´ıninas de R 3 que contiene la superficie de Enneper y o catenoide. (Lemes,

Aunque se anunció que no se realizarían juicios contra los miembros de la vencida Guardia Nacional, con la llegada de las nuevas autoridades nicaragüenses se