2 Transformations
R. Mukundan Department of Computer Science and Software Engineering University of Canterbury, .
Model-View Transformation
Copyright By PowCoder代写 加微信 powcoder
Objects are created in their own local coordinate space and then transformed into the world coordinate space.
They are transformed again into the coordinate space of the camera to generate the view as seen by the camera.
Model Transformation. Eg. Scale, Rotation, Translation
View Transformation
gluLookAt(..)
Camera Space
Modelling Space
World Space
glutSolidCube(..)
glutSolidTeapot(..)
Models are created at the origin
COSC363 R. Mukundan, CSSE, University of in OpenGL
OpenGLsupportsthefollowingtypesof three-dimensional model transformations:
Translations:
Rotations:
Scale Transformations:
glTranslatef(a, b, c); glRotatef(angle, l, m, n); glScalef(sx, sy, sz);
Generalized transformation: glMultMatrixf(mat); All transformations are stored as 4×4 matrices (discussed
later in the course).
Model transformations form part of the model-view matrix.
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
R. Mukundan, CSSE, University of Canterbury
Translation
OpenGL function: glTranslatef(a, b, c); Y
void display()
… glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(…)
glLightfv(…) glTranslatef(3, 5, −1); glutSolidTeapot(1.0); glFlush();
T(a, b, c)
R. Mukundan, CSSE, University of Canterbury
OpenGL function: glScalef(a, b, c)
A negative scale factor corresponds to a reflection. A zero scale factor corresponds to a projection
glScalef(2.0, 0.5, 1); glutSolidTeapot(1);
glScalef(1, -3, 2);
glutSolidTeapot(1);
glScalef(1, 1, 0); glutSolidTeapot(1);
R. Mukundan, CSSE, University of (a, b, c)
(xa , yb, zc)
Axis of rotation
OpenGLfunction: glRotatef(theta,l,m,n) A positive angle corresponds to a rotation in the anti-
clockwise sense about the axis of rotation
glRotatef(45, 1, 0, 0);
glutSolidTeapot(1);
glRotatef(45, 0, 1, 0);
glutSolidTeapot(1);
glRotatef(45, 0, 0, 1);
glutSolidTeapot(1);
NOTE: OpenGL rotations are always performed about an axis through the origin
R. Mukundan, CSSE, University of Canterbury
Model Creation
A model’s vertex coordinates are usually defined with the model’s centre at the origin.
The display of a model therefore appears at the origin when it is created.
This allows proper “in-place” scaling and rotation of models.
void display()
… //camera glColor3f(0.,1.,1.); glutSolidTeapot(1); glColor3f(0., 1., 0.); glutSolidCube(1.3); glColor3f(1, 0, 0); glutSolidIcosahedron(); glFlush();
R. Mukundan, CSSE, University of Canterbury
Rotation and Scaling
Rotations and scale transformations are always performed about the origin.
glScalef(0.5, 0.5, 1);
glBegin(GL_TRIANGLES); glVertex3f(4, 2, 0); glVertex3f(8, 2, 0); glVertex3f(6, 8, 0);
glRotatef(40, 0,0,1);
glBegin(GL_TRIANGLES);
glVertex3f(4, 2, 0);
glVertex3f(8, 2, 0);
glVertex3f(6, 8, 0);
glBegin(GL_TRIANGLES);
glVertex3f(4, 2, 0);
glVertex3f(8, 2, 0);
glVertex3f(6, 8, 0);
R. Mukundan, CSSE, University of Canterbury
Composite Transformations
Suppose a model of a teapot is generated, rotated by a certain angle, and then translated to a certain position.
Teapot model → Rotation → Translation → Camera view
Conceptual Model of Transformations
gluLookAt(…) glTranslatef(…) glRotatef(…) glutSolidTeapot(…)
R. Mukundan, CSSE, University of Canterbury
Camera Translation Rotation Teapot
Implementation Code
Order of Transformations
OpenGL Transformations
OpenGL post-multiplies the current transformation matrix with the new transformation matrix
Current Matrix
[V] [ T ][ R ] [V] [ T ][ R ] P
gluLookAt(…) glTranslatef(tx, ty, tz); glRotatef(theta, 0, 0, 1); glutSolidTeapot(1);
R. Mukundan, CSSE, University of Canterbury
Teapot’s vertices
Rotation followed by translation !!
Composite Transformations
Example: A rotation followed by a translation
glRotatef(30,0,0,1);
glTranslatef(2,0,0);
Order of Transformations
glTranslatef(2., 0., 0.);
glRotatef(30., 0., 0., 1.);
glutSolidTeapot(1);
R. Mukundan, CSSE, University of Canterbury
Composite Transformations
Example: A translation followed by a rotation
glTranslatef(2,0,0);
glRotatef(30,0,0,1);
Order of Transformations
glRotatef(30., 0., 0., 1.); glTranslatef(2., 0., 0.); glutSolidTeapot(1);
R. Mukundan, CSSE, University of Canterbury
Pivot Point Rotation
glTranslatef(6, 2, 0);
glRotatef(40, 0, 0, 1);
glTranslatef(-6, -2, 0);
glBegin(GL_TRIANGLES);
glVertex3f(4, 2, 0);
glVertex3f(8, 2, 0);
glVertex3f(6, 8, 0);
Rotations of models about points other than the origin are called pivot point rotations.
In the above example, we require a rotation of the triangle by 40 degs about the point (6, 2, 0)
Transformation sequence: Move the triangle to the origin with the pivot point coinciding with the origin, then perform the rotation, and finally move the triangle back to
the original position of the pivot point.
R. Mukundan, CSSE, University of Canterbury
Model Creation
Transformations are applied to primitives, and must be specified before defining primitives.
Do not place transformation functions within glBegin-glEnd blocks
Include transformation functions here
Valid functions:
glVertex3f glNormal3f glColor3f glTexCoord2f
glRotatef(…);
glTranslatef(…);
glBegin(GL_TRIANGLES);
glVertex3f(…);
glVertex3f(…);
glVertex3f(…);
glVertex3f(…);
… … …
R. Mukundan, CSSE, University of Canterbury
Independent Transformations
Each object in a scene normally requires its own set of transformations
Example: A scaled (reduced size) teapot at location (-2, 0.3, 0), and a cube with size=1 at location (4, 0.5, 3).
(The highlighted y-offsets are included only for moving the objects above the floor plane)
A teapot here
A cube here
R. Mukundan, CSSE, University of Canterbury
Required output
Incorrect transformation
glTranslatef(-2.0, 0.3, 0.0); glScalef(0.5, 0.5, 0.5); glutSolidTeapot(1);
glTranslatef(4, 0.5, 3);
glutSolidCube(1);
The cube is in the wrong position, and has a reduced size!
In this case, the transformations applied to the teapot are also applied to the cube.
We require a method for transforming objects independently of each other.
R. Mukundan, CSSE, University of Canterbury
Independent Transformations
Resetting the transformation matrix using the identity matrix will not give the desired output (why?).
Where did the cube go?
glTranslatef(-2.0, 0.3, 0.0); glScalef(0.5, 0.5, 0.5); glutSolidTeapot(1);
glLoadIdentity();
glTranslatef(4, 0.5, 3);
glutSolidCube(1);
R. Mukundan, CSSE, University of Canterbury
Independent Transformations
Correct approach to specifying independent transformations:
glPushMatrix();
glTranslatef(-2.0, 0.3, 0.0); glScalef(0.5, 0.5, 0.5); glutSolidTeapot(1);
glPopMatrix();
glPushMatrix();
glTranslatef(4, 0.5, 3);
glutSolidCube(1);
glPopMatrix();
R. Mukundan, CSSE, University of Canterbury
Matrix Stack
Transformations in OpenGL use a matrix stack. The top of stack represents the current transformation matrix
The matrix stack is useful for applying independent sets of transformations to different objects
For each object,
Create a temporary copy of the current transformation
matrix on the matrix stack
Create a composite transformation using the current matrix and the object’s transformations (slide 10)
Apply this transformation to the object
Discard the composite transformation, by deleting the
matrix and restoring the previous state of the stack
The current matrix is now available for the next object.
R. Mukundan, CSSE, University of Canterbury
Matrix Stack
glPushMatrix(): Create a copy of the current
transformation matrix (top of stack) and push into the stack
glPopMatrix(): Remove the matrix at the top of stack
R. Mukundan, CSSE, University of Canterbury
Independent Transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(…);
glPushMatrix();
glTranslatef(-2.0, 0.3, 0.0); glScalef(0.5, 0.5, 0.5); glutSolidTeapot(1);
glPopMatrix();
glPushMatrix();
glTranslatef(4, 0.5, 3);
glutSolidCube(1);
glPopMatrix();
View matrix
Applied to the teapot
R. Mukundan, CSSE, University of Canterbury
Applied to the cube
Independent Transformations
A modified example:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(…);
glRotatef(30, 0, 1, 0);
glPushMatrix();
glTranslatef(-2.0, 0.3, 0.0);
glScalef(0.5, 0.5, 0.5);
glutSolidTeapot(1);
glPopMatrix();
glPushMatrix();
glTranslatef(4, 0.5, 3);
glutSolidCube(1);
glPopMatrix();
This transformation will be applied to both the teapot and the cube.
R. Mukundan, CSSE, University of Canterbury
Transformation of Light Sources
void display()
float lgt_pos[4]={0., 10., 10., 1.}; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(5., 3., 2., 0., 0., 0., 0., 1., 0.);
glLightfv(GL_LIGHT0, GL_POSITION, lgt_pos);
glTranslatef(0.0, 1.2, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glutSolidTeapot(1.0);
glFlush(); }
R. Mukundan, CSSE, University of Canterbury
Light source moves with the object Light source’s position fixed in the scene Light source fixed relative to the camera.
A point light source is transformed like any other point
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com