28
Tan-Chi Ho, CGGM Lab., CSIE of NCTU Viewing and Viewing and Transformation Transformation

Viewing and Transformation

  • Upload
    zaria

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Viewing and Transformation. The Camera Analogy 1/2. Set up tripod and pointing the camera at the scene (viewing transformation). Arrange the scene to be photographed into the desired composition (modeling transformation). The Camera Analogy 2/2. Choose a camera lens or adjust the zoom - PowerPoint PPT Presentation

Citation preview

Page 1: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Viewing and Viewing and TransformationTransformation

Page 2: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

The Camera AnalogyThe Camera Analogy1/21/2

– Set up tripod and pointing the

camera at the scene(viewing transformation).

– Arrange the scene to be photographed into the desired composition

(modeling transformation).

Page 3: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

The Camera AnalogyThe Camera Analogy2/22/2

– Choose a camera lens or adjust

the zoom

(projection transformation).

– Determine how large you

want the final photograph

to be (viewport transformation).

Page 4: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

OverviewOverview

ModelviewMatrix

ModelviewMatrix

ViewportTransformation

ViewportTransformation

ProjectionMatrix

ProjectionMatrix

PerspectiveDivision

eye coordinat

es

clip coordinat

es

normalized device

coordinates

window coordinat

es

XYZW

Object coordinat

es

VertexVertex

TODO:

1. Switch matrix mode to GL_MODELVIEW and call glLoadIdentity().

2. Call gluLookAt().

3. Your own modeling transformations.

TODO:

1. Switch matrix mode to GL_PROJECTION and call glLoadIdentity().

2. Call gluPerspective() if you want perspective projection.

3. Call gluOrtho2D() if you want orthogonal projection.

TODO:

1. Call glViewport().

XY

Page 5: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Matrices in OpenGLMatrices in OpenGL

• Consider a transformation: (T1T2…Tn) 。 (multiply object coordinate by Tn first, then Tn-1 …until T1 ), To build the transformation matrix: (T1T2…Tn) we shall multiply identity matrix by T1 then T2…until Tn, as: I 。 T1 。 T2 。…。 T3.

• Thus, the order of issuing commands shall be inversed.

//Rotate then translate:

glTranslatef( 1,0,0 );

glRotatef(45.0, 0,0,1 );

drawObject();

XYZW

Page 6: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Viewing Viewing TransformationsTransformations1/21/2

• To positioning and aiming the camera.

• Use gluLookAt() to indicate where the camera is placed and aimed.

• If gluLookAt() was not called, the camera has a default position at the origin, points down the negative Z-axis, and an up-vector of positive Y-axis.

Page 7: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Viewing Viewing TransformationsTransformations2/22/2

• gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdoubpe upz );– eyex, eyey, eyez is where the camera is

positioned.– centerx, centery, centerz is where the camera

looks at.– Upx, upy, upz is the up-vector of the camera.

Page 8: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Modeling Modeling TransformationsTransformations1/51/5

• To position and orient the models.– Perform rotate, translate, scale and

combinations of these transformations.– In OpenGL, modeling and viewing

transformation are combined into the modelview matrix before the transformation are applied.

Page 9: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Modeling Modeling TransformationsTransformations2/52/5

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

Multiplies current matrix by a matrix that moves an object by x,y,z

glTranslatef( 0, 0, -1 );

Page 10: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Modeling Modeling TransformationsTransformations3/53/5

– glRotate{fd}( TYPR angle,TYPE x,TYPR y,TYPE z );

Multiplies current matrix by a matrix that rotates an object in a counterclockwise direction about the ray from origin to (x,y,z) with angle as the degrees.

glRotatef( 45.0, 0, 0, 1);

Page 11: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Modeling Modeling TransformationsTransformations4/54/5

– glScale{fd}( TYPE x,TYPE y,TYPE z ); Multiplies current matrix by a matrix that scales

an object along axes.

glScalef( 2.0, -0.5, 1.0 );

Page 12: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Modeling Modeling TransformationsTransformations5/55/5

– The order of transformations is critical.

Page 13: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Projection Projection TransformationsTransformations1/51/5

• Determine what the field of view(or viewing volume) is and how objects are projected onto the screen.

• Two types of projections are provided:– Perspective projection– Orthographic projection

Page 14: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Projection Projection TransformationsTransformations2/52/5

• Perspective Projection– glFrustum( GLdouble left, GLdouble

right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far );

Page 15: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Projection Projection TransformationsTransformations3/53/5

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

Page 16: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Projection Projection TransformationsTransformations4/54/5

• Orthographic Projection– glOrtho( GLdouble left, GLdouble right,

GLdouble bottom, GLdouble top, GLdouble near, GLdouble far );

– gluOrtho2D( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);

For 2D projection matrix The Z coordinates for objects are assumed to

lie between –1.0 and 1.0.

Page 17: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Projection Projection TransformationsTransformations5/55/5

Page 18: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Viewport Viewport TransformationsTransformations• Transform the final image into some

region of the window.

• The viewport is measured in window coordinates.– glViewport( GLint x, GLint y, GLsizei

width, GLsizei height ); Initial values are (0, 0, winWidth, winHeight).

Page 19: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Matrix ManipulationMatrix Manipulation1/31/3

• glMatrixMode( GLenum mode ); Switch between modelview, projection, texture

matrix mode.(GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE)

Each matrix mode has its own matrix stack.

Page 20: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Matrix ManipulationMatrix Manipulation2/32/3

• glLoadIdentity();– Set current matrix to the 4x4 identity matrix.

• glLoadMatrix{f,d}( const TYPE* m );– Replaces current matrix by a user defined matrix.– The user defined matrix m is a 4x4 array.

• glMultMatrix{f,d}( const TYPE* m );– Multiplies current matrix by a user defined matrix.– The user defined matrix m is a 4x4 array.

Page 21: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Matrix ManipulationMatrix Manipulation3/33/3

• glPushMatrix();– Push current matrix into matrix stack.

• glPopMatrix();– Pop matrix from matrix stack.

• These stack operations of matrix is very useful for constructing a hierarchical structure.

Page 22: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Transformation Transformation ProgramProgram1/61/6

#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_display(){

// clear the bufferglClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);glPushMatrix();

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

Page 23: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Transformation Transformation ProgramProgram2/62/6

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 buffersglutSwapBuffers();

}void GL_reshape(GLsizei w, GLsizei h){

// viewport transformationglViewport(0, 0, w, h);

// projection transformationglMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0);

Page 24: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Transformation Transformation ProgramProgram3/63/6

// viewing and modeling transformationglMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0.0, 3.0, 5.0, // eye

0.0, 0.0, 0.0, // center 0.0, 1.0, 0.0); // up

}

// GLUT idle functionvoid GL_idle(){

day += 10.0;if(day > 360.0)

day -= 360.0;year += 1.0;if(year > 360.0)

year -= 360.0;// recall GL_display() functionglutPostRedisplay();

}

Page 25: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Transformation Transformation ProgramProgram4/64/6

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

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': // assign idle functionglutIdleFunc(GL_idle);break;

case 'A': glutIdleFunc(0);break;

case 27: exit(0);}

}

Page 26: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Transformation Transformation ProgramProgram5/65/6

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();

}

Page 27: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Transformation Transformation ProgramProgram6/66/6

Page 28: Viewing and Transformation

Tan-Chi Ho, CGGM Lab., CSIE of NCTU

Any Question?Any Question?

?