Transcript
Page 1: OpenGL L02-Transformations

Mohammad Shaker

mohammadshaker.com

@ZGTRShaker

2014

OpenGL Graphics

L02 –TRANSFORMATIONS

Page 2: OpenGL L02-Transformations

Last Class, We Talked AboutOpenGL Program Structure

Page 3: OpenGL L02-Transformations

OpenGL Program Structure

• Application Structure

– Configure and open window

– Initialize OpenGL state

– Register input callback functions

• render

• resize

• input: keyboard, mouse, etc.

– Enter event processing loop

Page 4: OpenGL L02-Transformations

OpenGL Program Structure

• Application Structure

– Configure and open window

– Initialize OpenGL state

– Register input callback functions

• render

• resize

• input: keyboard, mouse, etc.

– Enter event processing loop

void main( int argc, char** argv ){int mode = GLUT_RGB|GLUT_DOUBLE;

glutInitDisplayMode( mode );glutCreateWindow( argv[0] );

init();

glutDisplayFunc( display );glutReshapeFunc( resize ); glutKeyboardFunc( key );glutIdleFunc( idle );

glutMainLoop();}

Page 5: OpenGL L02-Transformations

OpenGL Initialization

• Set up whatever state you’re going to use

void init( void ){

glClearColor( 0.0, 0.0, 0.0, 1.0 );glClearDepth( 1.0 );

glEnable( GL_LIGHT0 );glEnable( GL_LIGHTING );glEnable( GL_DEPTH_TEST );

}

Page 6: OpenGL L02-Transformations

GLUT Callback Functions

• Routine to call when something happens

– window resize or redraw

– user input

– animation

• “Register” callbacks with GLUT

glutDisplayFunc( display );

glutIdleFunc( idle );

glutKeyboardFunc( keyboard );

Page 7: OpenGL L02-Transformations

Rendering Callback

• Do all of your drawing here

glutDisplayFunc(DrawGLScene);

void DrawGLScene( void )

{

glClear( GL_COLOR_BUFFER_BIT );

glBegin( GL_TRIANGLE_STRIP );

glVertex3fv( v[0] );

glVertex3fv( v[1] );

glVertex3fv( v[2] );

glVertex3fv( v[3] );

glEnd();

glutSwapBuffers();

}

Page 8: OpenGL L02-Transformations

Rendering Callback

• Process user input

glutKeyboardFunc( keyboard );

void keyboard( char key, int x, int y )

{

switch( key ) {

case ‘q’ : case ‘Q’ :

exit( EXIT_SUCCESS );

break;

case ‘r’ : case ‘R’ :

rotate = GL_TRUE;

break;

}

}

Page 9: OpenGL L02-Transformations

Matrices in OpenGL, 4x4

151173

141062

13951

12840

mmmm

mmmm

mmmm

mmmm

M

Page 10: OpenGL L02-Transformations

RULE

Page 11: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

Page 12: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA

Page 13: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA

PROJECTION

Page 14: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA

PROJECTION

WORLD MATRIX

Page 15: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 16: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 17: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 18: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 19: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 20: OpenGL L02-Transformations

Camera (View) Matrix

Page 21: OpenGL L02-Transformations

Camera (View) Matrix

void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ);

Page 22: OpenGL L02-Transformations

Camera (View) Matrix

void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ);

Page 23: OpenGL L02-Transformations

Camera (View) Matrix

void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ);

Page 24: OpenGL L02-Transformations

Camera (View) Matrix

void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ);

Page 25: OpenGL L02-Transformations

Camera Movement

Page 26: OpenGL L02-Transformations

Set the View (Camera) Matrix

• For example, in ReSizeGLScene method

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)

{

if (height==0) { height=1; }

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt( 0.0, 0.0, 5.0,

0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );

}

Page 27: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 28: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 29: OpenGL L02-Transformations

Projection Matrix

Page 30: OpenGL L02-Transformations

Projection

Page 31: OpenGL L02-Transformations

Projection Matrices

Page 32: OpenGL L02-Transformations

Perspective Projection

Page 33: OpenGL L02-Transformations

Projection Matrices

The Difference?!

Page 34: OpenGL L02-Transformations

Projection Matrices

The Difference?!perspective

Page 35: OpenGL L02-Transformations

Projection Matrices

The Difference?!perspective orthographic

Page 36: OpenGL L02-Transformations

Projection Matrices

Realistic?perspective orthographic

Page 37: OpenGL L02-Transformations

Projection Matrices

Realistic?perspective orthographic

Page 38: OpenGL L02-Transformations

Projection Matrices

Architecture?perspective orthographic

Page 39: OpenGL L02-Transformations

Projection Matrices

Architecture?perspective orthographic

Page 40: OpenGL L02-Transformations

Projection Matrices

Usability?perspective orthographic

Page 41: OpenGL L02-Transformations

Projection Matrices

Usability?perspective orthographic

Page 42: OpenGL L02-Transformations

Perspective Projection

• void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)

• void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,

GLdouble znear, GLdouble zfar);

Page 43: OpenGL L02-Transformations

Orthographic Projection

• void gluOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,

GLdouble near, GLdouble far)

Page 44: OpenGL L02-Transformations

Orthographic Projection

Page 45: OpenGL L02-Transformations

Near and Far Clipping Planes

• Wrong Near and Far clipping planes can cause problems.

Page 46: OpenGL L02-Transformations

Set the Projection Matrix

• For example, in ReSizeGLScene method

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)

{

if (height==0) { height=1; }

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt( 0.0, 0.0, 5.0,

0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );

}

Page 47: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 48: OpenGL L02-Transformations

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 49: OpenGL L02-Transformations

World MatrixEach Object in 3D Has Its Own World Matrix

Page 50: OpenGL L02-Transformations

World Matrix

Page 51: OpenGL L02-Transformations

World Matrix

• Example

• Let’s assume that the coordinates of the triangle vertices are as follows:

Page 52: OpenGL L02-Transformations

World Matrix

• Example

• To translate 40 units over the y axis’s positive direction,all you need to do is to add 40

toeach y position, and you have the new coordinates for the vertices:

Page 53: OpenGL L02-Transformations

World Matrix

Page 54: OpenGL L02-Transformations

Many Transformation?

Page 55: OpenGL L02-Transformations

Many Transformation?Always Use I.S.R.O.T

Page 56: OpenGL L02-Transformations

Identity

Scale

Rotate

Orbit

Translate

Page 57: OpenGL L02-Transformations

World Matrices

• glScalef(x, y, z);

• glTranslatef(x, y, z);

• glRotatef(angle, x, y, z);

Page 58: OpenGL L02-Transformations

World Matrices

• glScalef(x, y, z);

• glTranslatef(x, y, z);

• glRotatef(angle, x, y, z);

÷÷÷÷÷÷

ø

ö

çççççç

è

æ

=

1000

100

010

001

),,(

z

y

x

zyx

t

t

t

tttT

÷÷÷÷÷÷

ø

ö

çççççç

è

æ

=

1000

000

000

000

),,(

z

y

x

zyx

s

s

s

sssS

Page 59: OpenGL L02-Transformations

World Matrices

• glLoadIdentity();

Page 60: OpenGL L02-Transformations

N E W

RULE Remember, Remember?

OpenGL programming is state-machine based

Page 61: OpenGL L02-Transformations

N E W

RULE Remember, Remember?

OpenGL programming is state-machine based

Changing State Without you knowing you’re

changing the state is a very big deal/Problem

Page 62: OpenGL L02-Transformations

N E W

RULE Remember, Remember?

OpenGL programming is state-machine based

Changing State Without you knowing you’re

changing the state is a very big deal/Problem

Page 63: OpenGL L02-Transformations

Changing State Without you knowing you’re

changing the state is a very big deal/Problem

EXAMPLE

Page 64: OpenGL L02-Transformations

Changing State Without you knowing you’re

changing the state is a very big deal/Problem

EXAMPLE

Primitive and Transformation

Page 65: OpenGL L02-Transformations

Primitive and Transformation Example

Page 66: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 67: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 68: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 69: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 70: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 71: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 72: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 73: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 74: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 75: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 76: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 77: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 78: OpenGL L02-Transformations

Primitive and Transformation Example

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

Page 79: OpenGL L02-Transformations

What would you doif you have 100 objects?!

Page 80: OpenGL L02-Transformations

Matrix Stack

Page 81: OpenGL L02-Transformations

Matrix StackglPushMatrix();

// A Body Transformation

glPopMatrix();

Page 82: OpenGL L02-Transformations

Using Matrix Stack

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using Triangles

glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The Triangle

glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A Quad

glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad return TRUE; // Keep Going

}

We left our code like this:

Page 83: OpenGL L02-Transformations

Using Matrix Stack

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

glBegin(GL_TRIANGLES); // Drawing Using Triangles glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); // Finished Drawing The TriangleglPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); // Done Drawing The Quad glPopMatrix();

Now, it will look like this:

Page 84: OpenGL L02-Transformations

Using Matrix Stack

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

glBegin(GL_TRIANGLES); // Drawing Using Triangles glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); // Finished Drawing The TriangleglPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); // Done Drawing The Quad glPopMatrix();

Now, it will look like this:

Page 85: OpenGL L02-Transformations

Using Matrix Stack

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

glBegin(GL_TRIANGLES); // Drawing Using Triangles glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); // Finished Drawing The TriangleglPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); // Done Drawing The Quad glPopMatrix();

Now, it will look like this:

Applying I.S.R.O.T

Applying I.S.R.O.T

Page 86: OpenGL L02-Transformations

Using Matrix Stack

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

glBegin(GL_TRIANGLES); // Drawing Using Triangles glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); // Finished Drawing The TriangleglPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); // Done Drawing The Quad glPopMatrix();

The result is exactly the same

Page 87: OpenGL L02-Transformations

Adding Some Colors

Page 88: OpenGL L02-Transformations

Using Matrix Stack

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

glBegin(GL_TRIANGLES); // Drawing Using Triangles glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); // Finished Drawing The TriangleglPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); // Done Drawing The Quad glPopMatrix();

Our code is like this:

Page 89: OpenGL L02-Transformations

Using Matrix Stack

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

glBegin(GL_TRIANGLES); // Drawing Using TrianglesglColor3f(1.0f,0.0f,0.0f); // Set The Color To RedglVertex3f( 0.0f, 1.0f, 0.0f); // TopglColor3f(0.0f,1.0f,0.0f); // Set The Color To GreenglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglColor3f(0.0f,0.0f,1.0f); // Set The Color To BlueglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglEnd(); // Finished Drawing The TriangleglPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); // Done Drawing The Quad glPopMatrix();

Page 90: OpenGL L02-Transformations

Applying Some Animation(Transformation)

Page 91: OpenGL L02-Transformations

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

glBegin(GL_TRIANGLES); // Drawing Using TrianglesglColor3f(1.0f,0.0f,0.0f); // Set The Color To RedglVertex3f( 0.0f, 1.0f, 0.0f); // TopglColor3f(0.0f,1.0f,0.0f); // Set The Color To GreenglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglColor3f(0.0f,0.0f,1.0f); // Set The Color To BlueglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The TriangleglPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad glPopMatrix();

We have this code:

Page 92: OpenGL L02-Transformations

// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0

glBegin(GL_TRIANGLES); // Drawing Using TrianglesglColor3f(1.0f,0.0f,0.0f); // Set The Color To RedglVertex3f( 0.0f, 1.0f, 0.0f); // TopglColor3f(0.0f,1.0f,0.0f); // Set The Color To GreenglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglColor3f(0.0f,0.0f,1.0f); // Set The Color To BlueglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right

glEnd(); // Finished Drawing The TriangleglPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad glPopMatrix();

We have this code:

Page 93: OpenGL L02-Transformations

rtri+=0.2f;rquad-=0.15f;// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glRotatef(rtri, 0, 1, 0); // Rotate around the Y axis with the rtri parameter

glBegin(GL_TRIANGLES); // Drawing Using TrianglesglColor3f(1.0f,0.0f,0.0f); // Set The Color To RedglVertex3f( 0.0f, 1.0f, 0.0f); // TopglColor3f(0.0f,1.0f,0.0f); // Set The Color To GreenglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglColor3f(0.0f,0.0f,1.0f); // Set The Color To BlueglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglEnd(); // Finished Drawing The Triangle

glPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 UnitsglRotatef(rquad, 1,0, 0);

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad glPopMatrix();

Change it to this:

Applying I.S.R.O.T

Page 94: OpenGL L02-Transformations

rtri+=0.2f;rquad-=0.15f;// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glRotatef(rtri, 0, 1, 0); // Rotate around the Y axis with the rtri parameter

glBegin(GL_TRIANGLES); // Drawing Using TrianglesglColor3f(1.0f,0.0f,0.0f); // Set The Color To RedglVertex3f( 0.0f, 1.0f, 0.0f); // TopglColor3f(0.0f,1.0f,0.0f); // Set The Color To GreenglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglColor3f(0.0f,0.0f,1.0f); // Set The Color To BlueglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglEnd(); // Finished Drawing The Triangle

glPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 UnitsglRotatef(rquad, 1,0, 0); // Rotate around the X axis with the rquad parameter

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad glPopMatrix();

Change it to this:

Applying I.S.R.O.T

Applying I.S.R.O.T

Page 95: OpenGL L02-Transformations

rtri+=0.2f;rquad-=0.15f;// Clear The Screen And The Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glRotatef(rtri, 0, 1, 0); // Rotate around the Y axis with the rtri parameter

glBegin(GL_TRIANGLES); // Drawing Using TrianglesglColor3f(1.0f,0.0f,0.0f); // Set The Color To RedglVertex3f( 0.0f, 1.0f, 0.0f); // TopglColor3f(0.0f,1.0f,0.0f); // Set The Color To GreenglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglColor3f(0.0f,0.0f,1.0f); // Set The Color To BlueglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglEnd(); // Finished Drawing The Triangle

glPopMatrix();

glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 UnitsglRotatef(rquad, 1,0, 0); // Rotate around the X axis with the rquad parameter

glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left

glEnd(); // Done Drawing The Quad glPopMatrix();

Change it to this:

Page 96: OpenGL L02-Transformations

Let’s Clean the Code a Bit..(You can find it in L02-Sol01)

Page 97: OpenGL L02-Transformations

Transformation Example

void DrawTriangle(){

rtri+=0.2f;glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); glRotatef(rtri, 0, 1, 0);

glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f, 0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

void DrawQuad(){

rquad-=0.15f;glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f)glRotatef(rquad, 1,0, 0);

glBegin(GL_QUADS);glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

int DrawGLScene(GLvoid){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

DrawTriangle();DrawQuad();

return TRUE;}

Page 98: OpenGL L02-Transformations

Transformation Example

void DrawTriangle(){

rtri+=0.2f;glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); glRotatef(rtri, 0, 1, 0);

glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f, 0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

void DrawQuad(){

rquad-=0.15f;glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f)glRotatef(rquad, 1,0, 0);

glBegin(GL_QUADS);glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

int DrawGLScene(GLvoid){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

DrawTriangle();DrawQuad();

return TRUE;}

Page 99: OpenGL L02-Transformations

Transformation Example

void DrawTriangle(){

rtri+=0.2f;glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); glRotatef(rtri, 0, 1, 0);

glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f, 0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

void DrawQuad(){

rquad-=0.15f;glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f)glRotatef(rquad, 1,0, 0);

glBegin(GL_QUADS);glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

int DrawGLScene(GLvoid){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

DrawTriangle();DrawQuad();

return TRUE;}

Page 100: OpenGL L02-Transformations

Viewports

Page 101: OpenGL L02-Transformations

Viewports

• glViewport(x, y, width, height)

• glViewport(0, 0, window_width, window_height);– Will use all the window for drawing

Page 102: OpenGL L02-Transformations

Viewports

• Here is it, in ReSizeGLScene method.

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)

{

if (height==0) { height=1; }

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

}

Page 103: OpenGL L02-Transformations

So let’s wrap up!Transformation Pipeline

Page 104: OpenGL L02-Transformations

OpenGL Architecture

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

OperationsFrameBuffer

TextureMemory

CPU

PixelOperations

Page 105: OpenGL L02-Transformations

OpenGL Architecture

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

OperationsFrameBuffer

TextureMemory

CPU

PixelOperations

We are here, Let’s zoom in!

Page 106: OpenGL L02-Transformations

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

lll

object eye clip normalizeddevice

window

• other calculations here– material and color– shade model (flat)– polygon rendering mode– polygon culling– clipping

Transformation Pipeline

Page 107: OpenGL L02-Transformations

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

lll

object eye clip normalizeddevice

window

• other calculations here– material and color– shade model (flat)– polygon rendering mode– polygon culling– clipping

Transformation Pipeline

Page 108: OpenGL L02-Transformations

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

lll

object eye clip normalizeddevice

window

• other calculations here– material and color– shade model (flat)– polygon rendering mode– polygon culling– clipping

Transformation Pipeline

Page 109: OpenGL L02-Transformations

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

lll

object eye clip normalizeddevice

window

• other calculations here– material and color– shade model (flat)– polygon rendering mode– polygon culling– clipping

Transformation Pipeline

Page 110: OpenGL L02-Transformations

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

lll

object eye clip normalizeddevice

window

• other calculations here– material and color– shade model (flat)– polygon rendering mode– polygon culling– clipping

Transformation Pipeline


Recommended