Title: CS297 Graphics with Java and OpenGL
1CS297 Graphics with Java and OpenGL
- Viewing, the model view matrix
2coordinates to pixel positions
- A series computer operations convert an object's
three-dimensional coordinates to pixel positions
on the screen. - Transformations, which are represented by matrix
multiplication, include - modeling,
- viewing,
- projection operations.
- Such operations include
- rotation,
- translation,
- scaling,
- reflecting,
- orthographic projection,
- and perspective projection.
3coordinates to pixel positions
- Generally, you use a combination of several
transformations to draw a scene. - Since the scene is rendered on a rectangular
window, objects (or parts of objects) that lie
outside the window must be clipped. - In three-dimensional computer graphics, clipping
occurs by throwing out objects on one side of a
clipping plane. - Finally, a correspondence must be established
between the transformed coordinates and screen
pixels. - This is known as a viewport transformation.
4Overview The Camera Analogy
- Set up your tripod and pointing the camera at the
scene (viewing transformation). - Arrange the scene to be photographed into the
desired composition (modeling transformation). - Choose a camera lens or adjust the zoom
(projection transformation). - Determine how large you want the final photograph
to be - for example, you might want it enlarged
(viewport transformation). - After these steps are performed, the picture can
be snapped or the scene can be drawn.
5Overview The Camera Analogy
Set up your tripod to look at where the model
will be created
6Overview The Camera Analogy
Position viewing model of world which will
contain final model
7Overview The Camera Analogy
Create geometric objects for adding to viewing
model
8Overview The Camera Analogy
Insert geometric model in viewing model, and
decide on perspective
9Perspective choices
Difference of perspectivegiven by choice of
cameraangle, and no change in any other
parameters
glu.gluPerspective(100.0, (float) w / (float) h,
1.0, 20.0)
glu.gluPerspective(10.0, (float) w / (float) h,
1.0, 20.0)
10Perspective choices
Difference of perspectivegiven by choice
aspect,and no change in any other parameters
glu.gluPerspective(50.0, 0.5f, 1.0, 20.0)
glu.gluPerspective(50.0, 10.0f, 1.0, 20.0)
11Viewport
Choose which part of scene is shown on screen
gl.glViewport(40, 50, w 500, h 400)
gl.glViewport(0, 0, w, h)
12Computer Vertex Transformations Pipeline
- Note that these steps correspond to the order in
which you specify the desired transformations in
your program, not necessarily the order in which
the relevant mathematical operations are
performed on an object's vertices. - The viewing transformations must precede the
modeling transformations in your code. - You can specify the projection and viewport
transformations at any point before drawing
occurs.
13Vertex Transformations
normalised device coordinates
clip coordinates
eye coordinates
modelview matrix
projection matrix
perspective division
viewport transformation
window coordinates
object coordinate
14modelview matrix
- The viewing and modeling transformations you
specify are combined to form the modelview
matrix, which is applied to the incoming object
coordinates to yield eye coordinates. - Next, if you've specified additional clipping
planes to remove certain objects from the scene
or to provide cutaway views of objects, these
clipping planes are applied.
15clip coordinates
- After that, OpenGL applies the projection matrix
to yield clip coordinates. - This transformation defines a viewing volume
objects outside this volume are clipped so that
they're not drawn in the final scene. - After this point, the perspective division is
performed by dividing coordinate values by w (the
forth coordinate in a vertex), to produce
normalized device coordinates. (See Appendix F of
the Redbook for more information.) - Finally, the transformed coordinates are
converted to window coordinates by applying the
viewport transformation. - You can manipulate the dimensions of the viewport
to cause the final image to be enlarged, shrunk,
or stretched.
16Matrix transformations
- To specify viewing, modeling, and projection
transformations, you construct a 4 4 matrix A,
which is then multiplied by the coordinates of
each vertex v in the scene to accomplish the
transformation. - Note that viewing and modeling transformations
are automatically applied to surface normal
vectors, in addition to vertices.
17Matrix transformations, the formulae
Where ui a(i,1)v1 a(i,2)v2 a(i,3)v3
a(i,4)v4
18Matrix transformations, the formulae
Where ui a(i,1)v1 a(i,2)v2 a(i,3)v3
a(i,4)v4
19Matrix transformations, the formulae
Where ui a(i,1)v1 a(i,2)v2 a(i,3)v3
a(i,4)v4
20Matrix transformations, the formulae
Where ui a(i,1)v1 a(i,2)v2 a(i,3)v3
a(i,4)v4
21Matrix transformations, the formulae
Where ui a(i,1)v1 a(i,2)v2 a(i,3)v3
a(i,4)v4
22Rotation by matrix multiplication
Rotation about the x-axis by ? degrees in a
counter-clockwise direction
23Rotation by matrix multiplication
Rotation about the y-axis by ? degrees in a
counter-clockwise direction
-sin(?)
0,
cos(?),
0,
x0
x1
0,
0
1,
y0
y1
0,
0
cos(?),
sin(?),
0,
z0
z1
w0
w1
0,
1
0,
0,
24Rotation by matrix multiplication
Rotation about the z-axis by ? degrees in a
counter-clockwise direction
25Translation by matrix multiplication
Translation along the X, Y and Z axes by x, y,
and z respectively
ASSUMES that final value in vertex is 1, if not
then translation is scaled.
26Matrix transformations, the formulae
c(i,j) a(i,1)b(1,j) a(i,2)b(2,j)
a(i,3)b(3,j) a(i,4)b(4,j)
27Matrix transformations, the formulae
c(i,j) a(i,1)b(1,j) a(i,2)b(2,j)
a(i,3)b(3,j) a(i,4)b(4,j)
28Matrix transformations, the formulae
- To first apply a rotation (matrix R) and then a
translation (matrix T) to a vertex v the matrix
multiplication formula is - new_v T.R.v (T.R) v T (R.v)
- This is not the same as R.T.v. The order of
matrix multiplication is important.
29A Simple Example Drawing a Cube
30The code
- public void display(GLAutoDrawable drawable)
-
- GL gl drawable.getGL( )
- GLU glu new GLU()
- GLUT glut new GLUT()
-
- gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f)
- gl.glClear(GL.GL_COLOR_BUFFER_BIT)
- gl.glColor3f(1.0f, 1.0f, 1.0f)
- gl.glLineWidth(3.0f)
-
- gl.glLoadIdentity ()
- gl.glTranslatef
- (0.0f, 0.0f, -3.0f)
- gl.glRotatef
- (30.0f,0.0f,1.0f,0.0f)
- glut.glutWireCube(0.9f)
-
public void reshape( GLAutoDrawable
drawable, int x, int y, int width, int
height) GL gl drawable.getGL(
) GLU glu new GLU()
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(GL.GL_PROJECTION)
gl.glLoadIdentity ()
glu.gluPerspective(50.0, 1.0, 1.0, -5.0)
gl.glMatrixMode (GL.GL_MODELVIEW)
31The matrix transformations
-
- gl.glLoadIdentity ()
- gl.glTranslatef(0.0f, 0.0f, -3.0f)
- gl.glRotatef(30.0f,0.0f,1.0f,0.0f)
-
-
32glLoadIdentity
- In the particular context of the display method,
this command will set the current model view
matrix to the identity matrix Id
0,
0
0,
1,
0,
0
1,
0,
0
1,
0,
0,
0,
1
0,
0,
33glTranslatef(x,y,z)
- this command multiplies the current model view
matrix M Id, by this
0,
x
0,
1,
0,
y
1,
0,
T
z
1,
0,
0,
0,
1
0,
0,
So that the model matrix is then M1 M.T Id.T
34glRotatef(?, 0,1,0)
- this command multiplies the current model view
matrix M, by this
-sin(?)
0,
cos(?),
0,
0,
0
1,
0,
R
0
cos(?),
sin(?),
0,
0,
1
0,
0,
So that the model view matrix is then M1.R
M.T.R Id.T.R
35glut.glutWireCube(0.9f)
- When this line is now executed
- each vertex v in the cube is created
- then each vertex is multiplied by the current
model view matrix M1 - the new vertices M1.v are then used to create the
geometric model.