Transformation and Viewing Sphere cylinder and cone Earth

  • Slides: 18
Download presentation
Transformation and Viewing Sphere, cylinder, and cone Earth Plane @ 2017 by Jim X.

Transformation and Viewing Sphere, cylinder, and cone Earth Plane @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 1.

Transformation and Viewing HOMOGENEOUS COORDINATES • Every column vector (x, y, z, w)T represents

Transformation and Viewing HOMOGENEOUS COORDINATES • Every column vector (x, y, z, w)T represents a homogeneous vertex if at least one of its elements is nonzero. T • If real number a is nonzero, then (x, y, z, w) and (ax, ay, az, aw)T represent the same homogeneous vertex. T • A 3 D euclidean space point (x, y, z) becomes the homogeneous vertex (x, y, z, 1. 0)T • As long as w is nonzero, the homogeneous vertex (x, y, z, w)T corresponds to the 3 D point (x/w, y/w, z/w)T. If w = 0. 0, it corresponds to a direction or a “point at infinity. ” @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 2.

Transformation and Viewing MATRIX REPRESENTATION OF 3 D TRANSFORMATIONS @ 2017 by Jim X.

Transformation and Viewing MATRIX REPRESENTATION OF 3 D TRANSFORMATIONS @ 2017 by Jim X. Chen: jchen@cs. gmu. edu Open. GL Transformations . 3.

gl. Matrix. Mode(GLenum mode); • GL_PROJECTION - Applies subsequent matrix operations to the projection

gl. Matrix. Mode(GLenum mode); • GL_PROJECTION - Applies subsequent matrix operations to the projection matrix stack. – gl. Load. Matrix, gl. Load. Identity – gl. Mult. Matrix, gl. Ortho, gl. Frustum, gl. Rotate, gl. Translate, gl. Scale • GL_MODELVIEW - Applies subsequent matrix operations to the modelview matrix stack. – gl. Load. Matrix, gl. Load. Identity – gl. Mult. Matrix, gl. Rotate, gl. Translate, gl. Scale @ 2017 by Jim X. Chen: jchen@cs. gmu. edu

Push. Matrix and Pop. Matrix void draw. Robot() { // the robot arm is

Push. Matrix and Pop. Matrix void draw. Robot() { // the robot arm is rotating around y axis gl. Rotatef(5. 0 f, 0. 0 f, 1. 0 f, 0. 0 f); gl. Push. Matrix(); gl. Rotatef(alpha, 0. 0 f, 1. 0 f); // Rz(alpha) is on top of the matrix stack draw. Arm(O, A); gl. Translatef(A[0], A[1], 0. 0 f); gl. Rotatef(beta, 0. 0 f, 1. 0 f); gl. Translatef(-A[0], -A[1], 0. 0 f); // Rz(alpha)T(A)Rz(beta)T(-A) is on top draw. Arm(A, B); gl. Translatef(B[0], B[1], 0. 0 f); gl. Rotatef(gama, 0. 0 f, 1. 0 f); gl. Translatef(-B[0], -B[1], 0. 0 f); // Rz(alpha)T(A)Rz(beta)T(-A) is on top draw. Arm(B, C); gl. Pop. Matrix(); } Example: J 2_8_Robot 3 d @ 2017 by Jim X. Chen: jchen@cs. gmu. edu

Transformation and Viewing @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 6.

Transformation and Viewing @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 6.

void draw. Arm(float End 1, float End 2) { float scale = End 2

void draw. Arm(float End 1, float End 2) { float scale = End 2 -End 1; gl. Push. Matrix(); // the cylinder lies in the z axis; // rotate it to lie in the x axis gl. Rotatef(90. 0 f, 1. 0 f, 0. 0 f); gl. Scalef(scale/5. 0 f, scale); if (cnt%1500<500) { draw. Cylinder(); } else if (cnt%1500<1000) { draw. Cone(); } else { gl. Scalef(0. 5 f, 0. 5 f); gl. Translatef(0, 0, 1); draw. Sphere(); } gl. Pop. Matrix(); } @ 2017 by Jim X. Chen: jchen@cs. gmu. edu

Transformation and Viewing COMPOSITION OF 3 D TRANS Solar System Solar system: my. Load.

Transformation and Viewing COMPOSITION OF 3 D TRANS Solar System Solar system: my. Load. Identity(); trans. Draw(Sun); my. Rotatef(e); trans. Draw(Earth); my. Translatev(E); my. Rotatef(m, 0, 1, 0); my. Translatev(-E); trans. Draw(Moon); • The earth rotate around the Sun; the moon around the earth • Given the earth & moon centers (E & M) and rotation angles (e & m) find the new locations of the earth and moon @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 8.

Transformation and Viewing Float matrix[4][4]; //for a sphere is at the center of the

Transformation and Viewing Float matrix[4][4]; //for a sphere is at the center of the coordinates my. Load. Identity(); trans. Draw. Sphere(); //sun my. Rotatef(e, 0, 1, 0); my. Translatef(E, 0, 0); trans. Draw. Sphere(); //earth my. Rotatef(m, 0, 1, 0); my. Translatef(M, 0, 0); trans. Draw. Sphere(); //moon @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 9.

Transformation and Viewing My Implementation with Push&Pop. Matrix Float matrix[DEPTH][4][4]; my. Push. Matrix(); my.

Transformation and Viewing My Implementation with Push&Pop. Matrix Float matrix[DEPTH][4][4]; my. Push. Matrix(); my. Scalef(4, 4, 4); trans. Draw. Sphere(); //sun my. Pop. Matrix(); my. Rotatef(e, 0, 1, 0); my. Translatef(E, 0, 0); my. Push. Matrix(); my. Scalef(2, 2, 2); trans. Draw. Sphere(); //earth my. Pop. Matrix(); my. Rotatef(m, 0, 1, 0); my. Translatef(M, 0, 0); trans. Draw. Sphere(); //moon @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 10.

Transformation and Viewing An Open. GL Implementation Example: J 2_9_Solar gl. Matrix. Mode (GL_MODELVIEW);

Transformation and Viewing An Open. GL Implementation Example: J 2_9_Solar gl. Matrix. Mode (GL_MODELVIEW); …… gl. Load. Identity (); gl. Push. Matrix(); gl. Scalef(4, 4, 4); Sphere(); //sun, matrix multi. automatic in Open. GL gl. Pop. Matrix(); gl. Rotatef(e, 0, 1, 0); gl. Translatef(E, 0, 0); gl. Push. Matrix(); gl. Scalef(2, 2, 2); Sphere(); // earth, transformation implied gl. Pop. Matrix(); gl. Rotatef(m, 0, 1, 0); gl. Translatef(M, 0, 0); Sphere(); //moon, again … @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 11.

Transformation and Viewing The Motion of a Top: Generalized Solar System Example: J 2_10_Gen.

Transformation and Viewing The Motion of a Top: Generalized Solar System Example: J 2_10_Gen. Solar 1. Rotate around y (b degree) so E is in xy plane; 2. Rotate around z (a degree) so E will be in y axis; 3. Rotate around y (m degree) so the moon will proceed; 4. Rotate around z (-a degree) so E will be in xy plane; 5. Rotate around y (-b degree) so E returns to its original location. Mnew = Ry(e)Ry(-b)Rz(-a)Ry(m)Rz(a)Ry(b)M @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 12.

Jogl implementation: J 2_10_Gen. Solar gl. Push. Matrix(); gl. Rotatef(earth. Angle, 0. 0 f,

Jogl implementation: J 2_10_Gen. Solar gl. Push. Matrix(); gl. Rotatef(earth. Angle, 0. 0 f, 1. 0 f, 0. 0 f); // around the "sun"; proceed angle gl. Rotatef(tilt. Angle, 0. 0 f, 1. 0 f); // tilt between the center and y axis gl. Translatef(0. 0 f, earth. Distance, 0. 0 f); gl. Push. Matrix(); gl. Scalef(WIDTH/20, WIDTH/20); draw. Sphere(); gl. Pop. Matrix(); gl. Rotatef(moon. Angle, 0. 0 f, 1. 0 f, 0. 0 f); // rotating around the "earth" gl. Translatef(moon. Distance, 0. 0 f); gl. Scalef(WIDTH/40, WIDTH/40); draw. Sphere(); gl. Pop. Matrix(); @ 2017 by Jim X. Chen: jchen@cs. gmu. edu

Transformation and Viewing The Motion of a Top: Example: J 2_11_Cone. Solar Generalized Solar

Transformation and Viewing The Motion of a Top: Example: J 2_11_Cone. Solar Generalized Solar System Cone Lab Cone and Sphere @ 2017 by Jim X. Chen: jchen@cs. gmu. edu Earth Lighting . 14.

Transformation and Viewing Collision Detection: J 2_11_Cone. Solar. Collision Draw a cone, a cylinder,

Transformation and Viewing Collision Detection: J 2_11_Cone. Solar. Collision Draw a cone, a cylinder, and a sphere that move and collide in the moon’s trajectory in the generalized solar system. The center of a moon from (0, 0, 0) to (moonx, moony, moonz): gl. Get. Floatv(GL. GL_MODELVIEW_MATRIX, curr. M); moonx = curr. M[12]; moony = curr. M[13]; moonz = curr. M[14]; @ 2017 by Jim X. Chen: jchen@cs. gmu. edu . 15.

Put a solar system on the Robot Arm J 2_12_Robot. Solar void draw. Robot

Put a solar system on the Robot Arm J 2_12_Robot. Solar void draw. Robot () { gl. Push. Matrix(); gl. Rotatef(cnt, 0, 1, 0); gl. Rotatef(alpha, 0, 0, 1); draw. Arm(O, A); gl. Translatef(A, 0, 0); gl. Rotatef(beta, 0, 0, 1); draw. Arm(A, B); gl. Translatef(B-A, 0, 0); gl. Rotatef(gama, 0, 0, 1); draw. Arm(B, C); // put the solar system at the end of the robot arm gl. Translatef(C-B, 0, 0); draw. Solar(); gl. Pop. Matrix(); } @ 2017 by Jim X. Chen: jchen@cs. gmu. edu

GLSL • Matrix multiplication with point is provided • It is uniform across an

GLSL • Matrix multiplication with point is provided • It is uniform across an object (all vertices), so it is better to send the corresponding combined transformation to the vertex shader. @ 2017 by Jim X. Chen: jchen@cs. gmu. edu

How to reason or think about transformation • From where you draw at the

How to reason or think about transformation • From where you draw at the origin to where you load identity in reverse order of the Open. GL code • From where you draw to the origin of a local coordinate system – The local coordinate system may be further transformed, which you don’t need to reason or think about again @ 2017 by Jim X. Chen: jchen@cs. gmu. edu