Download presentation

Presentation is loading. Please wait.

Published byKeith Gomm Modified over 2 years ago

1
OpenGL: Simple Use Open a window and attach OpenGL to it Set projection parameters (e.g., field of view) Setup lighting, if any Main rendering loop –Set camera pose with gluLookAt() Camera position specified in world coordinates –Render polygons of model Simplest case: vertices of polygons in world coordinates

2
Creating Geometry All geometry composed of vertices (points) ‘Primitive type’ defines the shape they describe Example vertices: glVertex (x, y, z, w) –glVertex2d - z coordinate is set to 0.0 –glVertex3d - w coordinate is set to 1.0 –glVertex4d - all coordinates specified (rarely done)

3
Primitive Types GL_POINTS GL_LINE –{S | _STRIP | _LOOP} GL_TRIANGLE –{S | _STRIP | _FAN} GL_QUAD –{S | _STRIP} GL_POLYGON

4
GL_POLYGON List of vertices defines polygon edges Polygon must be convex

5
Non-planar Polygons Imagine polygon with non-planar vertices Some perspectives will be rendered as concave polygons These concave polygons may not rasterize correctly

6
Generating Primitives Primitive defined within glBegin() and glEnd() Very few GL commands can be executed within these two GL calls Any amount of computation can be performed glBegin (GL_LINE_LOOP); for (j=0; j<10; j++) { angle = 2*M_PI*j/10; glVertex2f (cos(angle), sin(angle)); } glEnd();

7
OpenGL: More Examples Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); –This type of operation is called immediate-mode rendering; each command happens immediately

8
OpenGL: Front/Back Rendering Each polygon has two sides, front and back OpenGL can render the two differently The ordering of vertices in the list determines which is the front side: –When looking at the front side, the vertices go counterclockwise This is basically the right-hand rule

9
OpenGL: Drawing Triangles You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];... glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle –What do the triangles drawn above look like? –How much redundant computation is happening?

10
OpenGL: Triangle Strips An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v4); glVertex3fv(v5); glEnd(); –triangle 0 is v0, v1, v2 –triangle 1 is v2, v1, v3 (why not v1, v2, v3?) –triangle 2 is v2, v3, v4 –triangle 3 is v4, v3, v5 (again, not v3, v4, v5) v0v0 v2v2 v1v1 v3v3 v4v4 v5v5

11
Polygon Rendering Options Rendered as points, lines, or filled Front and back faces can be rendered separately using glPolygonMode( ) glPolygonStipple( ) overlays a MacPaint-style overlay on the polygon glEdgeFlag specifies polygon edges that can be drawn in line mode Normal vectors: normalized is better, but glEnable(GL_NORMALIZE) will guarantee it

12
Polygonalization Hints Keep orientations (windings) consistent Best to use triangles (guaranteed planar) Keep polygon number to minimum Put more polygons on silhouettes Avoid T-intersections to avoid cracks Use exact coordinates for closing loops AA B B CC BADOK E D

13
OpenGL: Specifying Normals Calling glNormal() sets the normal vector for the following vertices, till next glNormal() So flat-shaded lighting requires: glNormal3f(Nx, Ny, Nz); glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2); –While smooth shading requires: glNormal3f(N0x, N0y, N0z); glVertex3fv(v0); glNormal3f(N1x, N1y, N1z); glVertex3fv(v1); glNormal3f(N2x, N2y, N2z); glVertex3fv(v2); –(Of course, lighting requires additional setup…)

14
OpenGL: Specifying Color Calling glColor() sets the color for vertices following, until the next call to glColor() –To produce a single aqua-colored triangle: glColor3f(0.1, 0.5, 1.0); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); –To produce a Gouraud-shaded triangle: glColor3f(1, 0, 0); glVertex3fv(v0); glColor3f(0, 1, 0); glVertex3fv(v1); glColor3f(0, 0, 1); glVertex3fv(v2); –In OpenGL, colors can also have a fourth component (opacity) Generally want = 1.0 (opaque);

15
OpenGL: Specifying Viewpoint glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); eye[XYZ ]: camera position in world coordinates look[XYZ] : a point centered in camera’s view up[XYZ] : a vector defining the camera’s vertical Creates a matrix that transforms points in world coordinates to camera coordinates –Camera at origin –Looking down -Z axis –Up vector aligned with Y axis

16
Translations For convenience we usually describe objects in relation to their own coordinate system We can translate or move points to a new position by adding offsets to their coordinates: –Note that this translates all points uniformly

17
Scaling Scaling a coordinate means multiplying each of its components by a scalar Uniform scaling means this scalar is the same for all components: 2 2

18
Scaling Non-uniform scaling: different scalars per component: How can we represent this in matrix form? X 2, Y 0.5

19
Scaling Scaling operation: Or, in matrix form: scaling matrix

20
2-D Rotation (x, y) (x’, y’) x’ = x cos( ) - y sin( ) y’ = x sin( ) + y cos( )

21
2-D Rotation (x, y) (x’, y’) x = r cos ( ) y = r sin ( ) x’ = r cos ( + ) y’ = r sin ( + ) Trig Identity… x’ = r cos( ) cos( ) – r sin( ) sin( ) y’ = r sin( ) sin( ) – r cos( ) cos( ) Substitute… x’ = x cos( ) - y sin( ) y’ = x sin( ) + y cos( )

22
2-D Rotation This is easy to capture in matrix form: 3-D is more complicated –Need to specify an axis of rotation –Simple cases: rotation about X, Y, Z axes

23
3-D Rotation What does the 3-D rotation matrix look like for a rotation about the Z-axis? –Build it coordinate-by-coordinate

24
3-D Rotation What does the 3-D rotation matrix look like for a rotation about the Y-axis? –Build it coordinate-by-coordinate

25
3-D Rotation What does the 3-D rotation matrix look like for a rotation about the X-axis? –Build it coordinate-by-coordinate

26
3-D Rotation General rotations in 3-D require rotating about an arbitrary axis of rotation Deriving the rotation matrix for such a rotation directly is a good exercise in linear algebra Standard approach: express general rotation as composition of canonical rotations –Rotations about X, Y, Z

27
Composing Canonical Rotations Goal: rotate about arbitrary vector A by –Idea: we know how to rotate about X,Y,Z So, rotate about Y by until A lies in the YZ plane Then rotate about X by until A coincides with +Z Then rotate about Z by Then reverse the rotation about X (by - ) Then reverse the rotation about Y (by - )

28
Composing Canonical Rotations First: rotating about Y by until A lies in YZ How exactly do we calculate ? –Project A onto XZ plane (Throw away y-coordinate) –Find angle to X: = -(90° - ) = - 90 ° Second: rotating about X by until A lies on Z How do we calculate ?

29
Composing Canonical Rotations Why are we slogging through all this tedium? A: Because you’ll have to do it on the test

30
3-D Rotation Matrices So an arbitrary rotation about A composites several canonical rotations together We can express each rotation as a matrix Compositing transforms == multiplying matrices Thus we can express the final rotation as the product of canonical rotation matrices Thus we can express the final rotation with a single matrix!

31
Compositing Matrices So we have the following matrices: p: The point to be rotated about A by R y : Rotate about Y by R x : Rotate about X by R z : Rotate about Z by R x -1 : Undo rotation about X by R y -1 : Undo rotation about Y by In what order should we multiply them?

32
Compositing Matrices Short answer: the transformations, in order, are written from right to left –In other words, the first matrix to affect the vector goes next to the vector, the second next to the first, etc. So in our case: p’ = R y -1 R x -1 R z R x R y p

33
Rotation Matrices Notice these two matrices: R x : Rotate about X by R x -1 : Undo rotation about X by How can we calculate R x -1 ?

34
Rotation Matrices Notice these two matrices: R x : Rotate about X by R x -1 : Undo rotation about X by How can we calculate R x -1 ? –Obvious answer: calculate R x (- ) –Clever answer: exploit fact that rotation matrices are orthonormal

35
Rotation Matrices Notice these two matrices: R x : Rotate about X by R x -1 : Undo rotation about X by How can we calculate R x -1 ? –Obvious answer: calculate R x (- ) –Clever answer: exploit fact that rotation matrices are orthonormal What is an orthonormal matrix? What property are we talking about?

36
Rotation Matrices Orthonormal matrix: –orthogonal (columns/rows linearly independent) –normalized (columns/rows length of 1) The inverse of an orthogonal matrix is just its transpose:

37
Modeling Transformations glTranslate (x, y, z) –Multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z- values glRotate (theta, x, y, z) –Multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z)

38
Modeling Transformations glScale (x, y, z) –Multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes.

39
Matrix Multiplcations Certain commands affect the current matrix in OpenGL –glMatrixMode() sets the current matrix –glLoadIdentity() replaces the current matrix with an identity matrix –glTranslate() postmultiplies the current matrix with a translation matrix –gluPerspective() postmultiplies the current matrix with a perspective projection matrix It is important that you understand the order in which OpenGL concatenates matrices

40
Matrix Operations In OpenGL In OpenGL: –Vertices are multiplied by the MODELVIEW matrix –The resulting vertices are multiplied by the projection matrix Example: –Suppose you want to scale an object, translate it, apply a lookat transformation, and view it under perspective projection. What order should you make calls?

41
Matrix Operations in OpenGL Problem: scale an object, translate it, apply a lookat transformation, and view it under perspective A correct code fragment: glMatrixMode(GL_PERSPECTIVE); glLoadIdentity(); gluPerspective(…); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(…); glTranslate(…); glScale(…); /* Draw the object...*/

42
Matrix Operations in OpenGL Problem: scale an object, translate it, apply a lookat transformation, and view it under perspective An incorrect code fragment: glMatrixMode(GL_PERSPECTIVE); glLoadIdentity(); glTranslate(…); glScale(…); gluPerspective(…); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(…); /* Draw the object...*/

43
Multiplication Order glMatrixMode (MODELVIEW); glLoadIdentity(); glMultMatrix(N); glMultMatrix(M); glMultMatrix(L); glBegin(POINTS); glVertex3f(v); glEnd(); Modelview matrix successively contains: I(dentity), N, NM, NML The transformed vertex is: NMLv = N(M(Lv))

44
Multiplication Order Rotate line segment by 45 degrees about endpoint WrongRight R(45) T(-3), R(45), T(3) OpenGL T(3) R(45) T(-3)

45
Manipulating Matrix Stacks Observation: Certain model transformations are shared among many models We want to avoid continuously reloading the same sequence of transformations glPushMatrix ( ) –push all matrices in current stack down one level and copy topmost matrix of stack glPopMatrix ( ) –pop the top matrix off the stack

46
Matrix Manipulation - Example Drawing a car with wheels and lugnuts draw_wheel( ); for (j=0; j<5; j++) { glPushMatrix (); glRotatef(72.0*j, 0.0, 0.0, 1.0); glTranslatef (3.0, 0.0, 0.0); draw_bolt ( ); glPopMatrix ( );

Similar presentations

OK

Computer Graphics Basic 3D Geometry CO2409 Computer Graphics Week 5-1.

Computer Graphics Basic 3D Geometry CO2409 Computer Graphics Week 5-1.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Viewer ppt online shopping Ppt on natural disasters free download Persistence of vision display ppt online Ppt on water scarcity in the middle east Ppt on two stroke petrol engine Ppt on automatic car parking system Ppt on relations and functions for class 11th chemistry Ppt on smart power grid Convert a pdf to ppt online Ppt on mapping space around us