multimedia graphics university of wollongong 2007 - lecture 4

11
Jogl - Java bindings for OpenGL Lecture 4 : 3D http://www.uow.edu.au/~phillip/ Phillip McKerrow ITCS941 Multimedia Graphics 2007 Transformations translation x y x 1 , y 1 x 2 , y 2 x trans y trans x 2 = x 1 + x trans y 2 = y 1 + y trans Matrix Transformation matrix Matrix of xyz coordinates float matrix[][] = new double[4][4]; //or float matrix[][] ={(0f, 0f, 0f), (10f, 10f, 10f) (15f, -5f, 22f), (20f, 25f, 31f)}; matrix [3][2] = -5; //[row][col] Identity matrix I Homogeneous coordinates public void glLoadIdentity() // sets current matrix to 4*4 I Moving Objects or changing view point Draw view by hand 3DModelling

Upload: eword

Post on 17-Jul-2016

216 views

Category:

Documents


2 download

DESCRIPTION

Multimedia Graphics University of Wollongong 2007 - Lecture 4

TRANSCRIPT

Page 1: Multimedia Graphics University of Wollongong 2007 - Lecture 4

Jogl - Java bindings for OpenGL Lecture 4 : 3D

http://www.uow.edu.au/~phillip/Phillip McKerrow

ITCS941 Multimedia Graphics 2007

Transformations translation

x

y

x1, y1

x2, y2

xtrans

ytrans

x2 = x1 + xtrans

y2 = y1 + ytrans

Matrix

Transformation matrix

Matrix of xyz coordinates

float matrix[][] = new double[4][4]; //orfloat matrix[][] ={(0f, 0f, 0f), (10f, 10f, 10f) (15f, -5f, 22f), (20f, 25f, 31f)};matrix [3][2] = -5; //[row][col]

Identity matrix I

Homogeneous coordinates

public void glLoadIdentity() // sets current matrix to 4*4 I

Moving Objects or changing view point

Draw view by hand

3DModelling

Page 2: Multimedia Graphics University of Wollongong 2007 - Lecture 4

3D - 4*4 matrix - 6DOF motion

Affine transforms map parallelogramsinto parallelogramscan only warp figures in the way a grid is warped

Order may be importantMultiply matrices to combine transforms

premultiply - execute right to left with respect to reference framepostmultimpy - execute left to right wrt new frames

Page 3: Multimedia Graphics University of Wollongong 2007 - Lecture 4

public void glRotated(double angle, double x,double

y,double z)

public void glScaled(double x, double y,double z)

public void glTranslated(double x, double y, double z)

For a simple introduction to transforms, Read Chapter 3McKerrow, P. 1991. Introduction to robotics, Addison Wesley

Page 4: Multimedia Graphics University of Wollongong 2007 - Lecture 4

For a simple introduction to transforms, Read Chapter 3McKerrow, P. 1991. Introduction to robotics, Addison Wesley

Example - inclined plane

is reference frameattached to world

is new frameattached to plane

TNR

N

R

• is transform from reference frame to new frame• describes the position and orientation of new framein terms of reference frame

Initially, plane is not inclined so

When plane is inclined with a rotation around x and a rotation around ywe pre-multiply the rotation matrices to get the transform

Then we can transform a location, a force, a velocity (any vector) fromthe new frame to the reference frame

To calculate the inverse (move a vector from reference to new e.g. gravitywe invert the transform (swap rows and columns) - page 152 of McKerrow

Page 5: Multimedia Graphics University of Wollongong 2007 - Lecture 4

The components of force in the new frame due to gravity are

Animate1. from equations calculate the 3 force components2. from the force components calculate the direction of roll - rot z3. from the force components calculate the acceleration components fxt -> axt and fyt -> ayt4. Calculate the velocity components - finite difference maths vxt = vx(t-1) + axt * δt and vyt = vy(t-1) + ayt * δt5. Calculate the position components - finite difference maths δxt = vxt * δt and δyt = vyt * δt xt = x(t-1) + δxt and yt = y(t-1) + δyt6. Find the distance moved - pythagoras δd = √(δxt *δxt + δyt+ δyt)7. Calculate the arc rolled in each direction arcx = δd cos Rotz and arcy = δd sin Rotz8. Calculate the angles rolled rollx = δψt = arcx / radius and rolly = δθt = arcy/radius9. Calculate the orientation ψt = ψt (t-1) + δψt and rolly = θt = θt (t-1) + δθt

Page 6: Multimedia Graphics University of Wollongong 2007 - Lecture 4

Images rendered from JOGL = the result of multiplying the original objects by a set of matrices representing transforms.

GL_MODELVIEW matrix - everything else mode

GL_PROJECTION matrix - camera mode

public void glMatrixMode(int mode)

Specifies which matrix will be affected by transformation commands: modelview, projection, or texture matrix - modelview is default

public void gluOrtho2D(double left, double right, double bottom, double top)

public void glLoadMatrixd(double[] m)

public void glMultMatrixd(double[] m)

gl.glMatrixMode(GL.GL_MODELVIEW);gl.glLoadIdentity(); //Igl.glMultMatrixf(N); // I*N = Ngl.glMultMatrixf(M); //N*Mgl.glMultMatrixf(L);//N*M*Lgl.glBegin(GL.GL_POINT);gl.glVertex3f(v); //N*M*L*vgl.glEnd();

Transformation is N(M(Lv))

gluPerspective, glFrustrum, glOrtho, glOrtho2Dshould only be called in PROJECTION mode

Projecting 3D world as a 2D image on display

Viewing volume is a rectangular parallelepiped

Projections

Post multiplication - left to right so actions relative to the current matrix

Page 7: Multimedia Graphics University of Wollongong 2007 - Lecture 4

public void glFrustum(double left, double right, double bottom, double top, double near_val, double far_val)

public void gluPerspective(double fovy, double aspect, double zNear, double zFar)

public void gluLookAt(double eyeX, double eyeY, double eyeZ, double centerX, double centerY, double centerZ, double upX, double upY, double upZ)

public void glMultMatrixd(double[] m)

Perspective projection - how we view the world- defines our camera

Frustum - theregion we wantthe viewer to seeComputer onlyrenders pointsin the frustum

gluPerspectivesets up thecamera

Parametersare relativeto the camera

eye up

at

No coordinates

gluLookAt specifies the location of the camera and the direction it is pointinggluPerspective specifies the camera characteristics relative to that location

Are the squares different sizes or different distancesfrom the camera?

zx

yWindow

Points our camera

Planes

Page 8: Multimedia Graphics University of Wollongong 2007 - Lecture 4

Geometric Primitives

GL place the right number of vertices between glBegin(polygon type) and glEnd polygons must be convex divide concave polygons into convex

GLU has methods for cylinders, shperes and disks using quadrics

public void gluQuadricDrawStyle(GLUquadric quad, int draw)

public void gluSphere(GLUquadric quad,double radius, int slices, int stacks)

public void gluDeleteQuadric(GLUquadric quad)

GLUquadric quad = glu.gluNewQuadric (); //create a quadric objectglu.gluQuadricDrawStyle(quad, GLU.GLU_Line); //set a draw styleglu.gluSphere(quad, 1.0, 15, 15); //create a sphereglu.gluDeleteQuadric(quad); //destroy quadric

Draw styles - POINT, LINE, SILHOUETTE, FILL

Objects - sphere, cylinder and disk

To draw the cylinder place the camera looking at the origin

GLUT methods for cubes, cones, bitmaps, dodecahedrons, spheres, etc.

public void glutSolidTorus( double innerRadius, double outerRadius,int nsides, int rings)

Cylinder drawn around z axis - normal to window- rotate to get around other axes

GlutObjects

Page 9: Multimedia Graphics University of Wollongong 2007 - Lecture 4

Moving objectsMotion - using the transforms - scaled, translated and rotated public void display(GLDrawable drawable) { GL gl = drawable.getGL(); GLU glu = new GLU(); GLUT glut = new GLUT(); U gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

//Define points for eye, at (the origin) and up - your camera. // It ALWAYS goes in the GL_MODELVIEW matrix. glu.gluLookAt( xPosition, 0, zPosition, xPosition, 0, (zPosition+20), 0, 1, 0 );

gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glColor3f(0.0f, 0.0f, 0.9f); //transforming the place the next shape will be drawn. gl.glTranslated(2, 0, 2); glut.glutWireIcosahedron(); //draw wire frames gl.glTranslated(-4, 0, 0); //more shapes to navigate through glut.glutWireIcosahedron();

Use keys 2, 4, 6, 82 is back, 4 is left

Movement changes the position of the camera

After placing the camera, create shapes and translate coordinate system

Keys 2, 4, 6, 8 for camera movement2 is back, 4 is left

Keys S, A, E, W to move robot jointsS is shoulder, E is elbow

Matrix Stacks

FirstPersonMovement

RobotArmMovement

Page 10: Multimedia Graphics University of Wollongong 2007 - Lecture 4

Matrix Stacks

hierarchical model - build a complicated object from simpler onese.g. a wheel with 6 nuts on it

- a method draws a wheel- another draws a nut

Draw wheel with nuts at originWhen want to draw a car - call the wheel drawing method 4 times with different transformations

drawWheel() {FOR i=0 TO 6 DO {

glPushMatrixglRotate glTranslatedraw nut

glPopMatrix}

}

drawCar() {draw car bodyglPushMatrix

glTranslate to 1st wheel positiondraw wheel

glPopMatrixglPushMatrixglTranslate to 2nd wheel positiondraw wheel

ModelView matrix stack contains up to 32 matrices

1. transforms work on the current matrix (post multiply)2. you draw an object by

1. setting the current matrix to the identity matrix 2. multiplying it by transforms and then by the object description

(each transform changes the current matrix) 3. multiply the final matrix by the object to calculate its size, location, etc 3. you see the object by pointing the camera at it. 4. push matrix

1. makes a copy of the current matrix2. pushes copy onto the stack 3. you now have the current matrix and a copy

5. transforms following push matrix work on the current matrix and keep changing it 6. pop matrix

1. pops the top matrix off the stack2. overwrites the current matrix with it

7. So you can:1. define transforms on an object in the current matrix 2. save a copy of the transforms on the stack (push matrix) 3. translate the object to another place and draw it 4. pop the matrix to get the original location back again

Push and Pop

Stacks describe complexs objectPush the object down the stackReuse with different transformations

Page 11: Multimedia Graphics University of Wollongong 2007 - Lecture 4

Exercises

2. modify Planes to change the frustum and move the squares away from the camera

1. change GlutObjects to draw a sphere (un-comment code)

3. write a JOGL program to draw a simple shape (e.g. a square). The make a circular motif by drawing multiple squares on a circle4. The following code draws a sun and a planet. Using a copy of FirstPersonMovement as a template, draw them with key presses to change the day and the year, as well as the camera position.

gl.glPushMatrix();glut.glutWireSphere( 1.0f, 20.0f, 16.0f); //sungl.glRotatef(year, 0.0f, 1.0f, 0.0f);gl.glTranslatef(2.0f, 0.0f, 0.0f);gl.glRotatef(day, 0.0f, 1.0f, 0.0f);glut.glutWireSphere( 0.2f, 10.0f, 8.0f); //planetgl.glPopMatrix();

1. transforms work on the current matrix (post multiply)2. you draw an object by

1. setting the current matrix to the identity matrix 2. multiplying it by transforms and then by the object description

(each transform changes the current matrix) 3. multiply the final matrix by the object to calculate its size, location, etc 3. you see the object by pointing the camera at it. 4. push matrix

1. makes a copy of the current matrix2. pushes copy onto the stack 3. you now have the current matrix and a copy

5. transforms following push matrix work on the current matrix and keep changing it 6. pop matrix

1. pops the top matrix off the stack2. overwrites the current matrix with it

7. So you can:1. define transforms on an object in the current matrix 2. save a copy of the transforms on the stack (push matrix) 3. translate the object to another place and draw it 4. pop the matrix to get the original location back again

Exam questions1. What is the purpose of the current GL Modelview matrix?2. What operation does the following JOGL method perform?

glu.gluLookAt(xPos, 0, zPos,xPos, 0, (zPos+20),

0, 1, 0);

3. Explain what happens to the current matrix in each step of the followingJOGL code. What does each 3D transformation do to the object? Whatobject is drawn, what are its dimensions and where is it drawn?

float base = 30.0f;gl.glTranslatef(-1.0f, 0.0f, 0.0f);gl.glRotatef(base, 0.0f, 0.0f, 1.0f);gl.glTranslatef(1.0f, 0.0f, 0.0f); gl.glPushMatrix();gl.glScalef(2.0f, 0.4f, 1.0f);glut.glutWireCube(gl, 1.0f);gl.glPopMatrix();

4. What is the purpose of the depth buffer?5. Why would you push and pop the current matrix when drawing a complex object, such as a car with 4 wheels?6. When setting up the camera you use two functions: gluPerspective(…) and gluLookat(...). What is their purpose?