Presentation is loading. Please wait.

Presentation is loading. Please wait.

UBI 516 Advanced Computer Graphics Introduction to OpenGL OpenGL Programming Guide.

Similar presentations


Presentation on theme: "UBI 516 Advanced Computer Graphics Introduction to OpenGL OpenGL Programming Guide."— Presentation transcript:

1 UBI 516 Advanced Computer Graphics Introduction to OpenGL OpenGL Programming Guide

2 Intro to OpenGL OpenGL operates as an infinite loop Put things in the scene (points, colored lines, textured polys)Put things in the scene (points, colored lines, textured polys) Describe the camera (location, orientation, field of view)Describe the camera (location, orientation, field of view) Listen for keyboard/mouse eventsListen for keyboard/mouse events Render – draw the sceneRender – draw the scene OpenGL operates as an infinite loop Put things in the scene (points, colored lines, textured polys)Put things in the scene (points, colored lines, textured polys) Describe the camera (location, orientation, field of view)Describe the camera (location, orientation, field of view) Listen for keyboard/mouse eventsListen for keyboard/mouse events Render – draw the sceneRender – draw the scene

3 Intro to OpenGL OpenGL has a “state” There are a lot of ways your OpenGL program can be configuredThere are a lot of ways your OpenGL program can be configured The current configuration is stored in OpenGL’s stateThe current configuration is stored in OpenGL’s state Be aware that OpenGL commands affect the program’s state rather than redirect its logical executionBe aware that OpenGL commands affect the program’s state rather than redirect its logical execution OpenGL has a “state” There are a lot of ways your OpenGL program can be configuredThere are a lot of ways your OpenGL program can be configured The current configuration is stored in OpenGL’s stateThe current configuration is stored in OpenGL’s state Be aware that OpenGL commands affect the program’s state rather than redirect its logical executionBe aware that OpenGL commands affect the program’s state rather than redirect its logical execution

4 The Graphics Pipeline and State Machines Graphic system as a state machine, a black box that contains a finite-state machine. Machine may change the state,Machine may change the state, Or cause to produce a visible output.Or cause to produce a visible output. An example state : We set a color,We set a color, That remains the present color until it is changed.That remains the present color until it is changed. Graphic system as a state machine, a black box that contains a finite-state machine. Machine may change the state,Machine may change the state, Or cause to produce a visible output.Or cause to produce a visible output. An example state : We set a color,We set a color, That remains the present color until it is changed.That remains the present color until it is changed.

5 The OpenGL Interface GLUT : Graphics utility toolkit (glut~) - interface with the window system - #include GLU : Graphics utility library (glu~) - Contain code for common objects such as spheres, teapot, etc. - #include GL : OpenGL functions (gl~) - #include GLX MS windows extension X window system MS windows system Frame buffer (video card) OpenGL application program

6 Software Hide the details User should not need to worry about how graphics are displayed on monitorUser should not need to worry about how graphics are displayed on monitor User doesn’t need to know about how a line is converted into pixels and drawn on screen (hardware dependent)User doesn’t need to know about how a line is converted into pixels and drawn on screen (hardware dependent) User doesn’t need to rebuild the basic tools of a 3D sceneUser doesn’t need to rebuild the basic tools of a 3D scene –Virtual camera, light sources, polygon drawing OpenGL does this for you… Hide the details User should not need to worry about how graphics are displayed on monitorUser should not need to worry about how graphics are displayed on monitor User doesn’t need to know about how a line is converted into pixels and drawn on screen (hardware dependent)User doesn’t need to know about how a line is converted into pixels and drawn on screen (hardware dependent) User doesn’t need to rebuild the basic tools of a 3D sceneUser doesn’t need to rebuild the basic tools of a 3D scene –Virtual camera, light sources, polygon drawing OpenGL does this for you…

7 Software Hide the details User doesn’t need to know how to read the data coming from the mouseUser doesn’t need to know how to read the data coming from the mouse User doesn’t need to know how to read the keystrokesUser doesn’t need to know how to read the keystrokes OpenGL Utility Toolkit (GLUT) does this for you… Hide the details User doesn’t need to know how to read the data coming from the mouseUser doesn’t need to know how to read the data coming from the mouse User doesn’t need to know how to read the keystrokesUser doesn’t need to know how to read the keystrokes OpenGL Utility Toolkit (GLUT) does this for you…

8 OpenGL Design Goals SGI’s design goals for OpenGL: High-performance (hardware-accelerated) graphics APIHigh-performance (hardware-accelerated) graphics API Some hardware independenceSome hardware independence Natural, terse API with some built-in extensibilityNatural, terse API with some built-in extensibility OpenGL has become a standard (competing with DirectX) because: It doesn’t try to do too muchIt doesn’t try to do too much –Only renders the image, doesn’t manage windows, etc. –No high-level animation, modeling, sound (!), etc. It does enoughIt does enough –Useful rendering effects + high performance Open source and promoted by SGI (& Microsoft, half-heartedly)Open source and promoted by SGI (& Microsoft, half-heartedly) SGI’s design goals for OpenGL: High-performance (hardware-accelerated) graphics APIHigh-performance (hardware-accelerated) graphics API Some hardware independenceSome hardware independence Natural, terse API with some built-in extensibilityNatural, terse API with some built-in extensibility OpenGL has become a standard (competing with DirectX) because: It doesn’t try to do too muchIt doesn’t try to do too much –Only renders the image, doesn’t manage windows, etc. –No high-level animation, modeling, sound (!), etc. It does enoughIt does enough –Useful rendering effects + high performance Open source and promoted by SGI (& Microsoft, half-heartedly)Open source and promoted by SGI (& Microsoft, half-heartedly)

9 The Big Picture Who gets control of the main control loop? GLUT – the code that controls the window and refreshGLUT – the code that controls the window and refresh –Must be responsive to windowing system and OS OpenGL – the code that controls what is drawnOpenGL – the code that controls what is drawn –Must be responsive to the program that specifies where objects are located. If something moves, I want to see it. Who gets control of the main control loop? GLUT – the code that controls the window and refreshGLUT – the code that controls the window and refresh –Must be responsive to windowing system and OS OpenGL – the code that controls what is drawnOpenGL – the code that controls what is drawn –Must be responsive to the program that specifies where objects are located. If something moves, I want to see it.

10 Intro to OpenGL OpenGL uses matrices Matrix describes camera typeMatrix describes camera type Matrix describes current configuration of the 3D spaceMatrix describes current configuration of the 3D space –Explanation… OpenGL uses matrices Matrix describes camera typeMatrix describes camera type Matrix describes current configuration of the 3D spaceMatrix describes current configuration of the 3D space –Explanation…

11 Intro to OpenGL OpenGL coordinate system right-handedright-handed –Hold out your right hand and hold your thumb, index, and middle fingers orthogonal to one another –Call your thumb the x-axis, index = y-axis, and middle = axis –This is the OpenGL coordinate system The camera defaults to look down negative z-axisThe camera defaults to look down negative z-axis OpenGL coordinate system right-handedright-handed –Hold out your right hand and hold your thumb, index, and middle fingers orthogonal to one another –Call your thumb the x-axis, index = y-axis, and middle = axis –This is the OpenGL coordinate system The camera defaults to look down negative z-axisThe camera defaults to look down negative z-axis

12 Intro to OpenGL So… X-axis = thumb = 1, 0, 0X-axis = thumb = 1, 0, 0 Y-axis = index = 0, 1, 0Y-axis = index = 0, 1, 0 Z-axis = middle = 0, 0, 1Z-axis = middle = 0, 0, 1 Camera defaults to look down negative z-axisCamera defaults to look down negative z-axis Let’s say we want it to look down x-axisLet’s say we want it to look down x-axisSo… X-axis = thumb = 1, 0, 0X-axis = thumb = 1, 0, 0 Y-axis = index = 0, 1, 0Y-axis = index = 0, 1, 0 Z-axis = middle = 0, 0, 1Z-axis = middle = 0, 0, 1 Camera defaults to look down negative z-axisCamera defaults to look down negative z-axis Let’s say we want it to look down x-axisLet’s say we want it to look down x-axis

13 Intro to OpenGL Coordinate system transformation so camera looks down x-axis If x-axis  negative z-axisIf x-axis  negative z-axis –x  -z –y  y –z  x Coordinate system transformation so camera looks down x-axis If x-axis  negative z-axisIf x-axis  negative z-axis –x  -z –y  y –z  x

14 Intro to OpenGL The a  i matrix defines the transformation Why store the transformation matrix and not the final desired matrix? The a  i matrix defines the transformation Why store the transformation matrix and not the final desired matrix? =

15 Intro to OpenGL The transformation will be applied to many points If the following transformation moves the axesIf the following transformation moves the axes The same transformation moves verticesThe same transformation moves vertices –Example: (1, 1, -1)  (-1, 1, -1) The transformation will be applied to many points If the following transformation moves the axesIf the following transformation moves the axes The same transformation moves verticesThe same transformation moves vertices –Example: (1, 1, -1)  (-1, 1, -1) = 

16 Intro to OpenGL This important matrix is stored as the MODELVIEW matrix The MODELVIEW matrix is so important OpenGL maintains a stack of these matricesThe MODELVIEW matrix is so important OpenGL maintains a stack of these matrices You have control of this stack with the glPushMatrix and glPopMatrix commandsYou have control of this stack with the glPushMatrix and glPopMatrix commands (The matrix is actually 4x4, but we will study the details to understand why in the future)(The matrix is actually 4x4, but we will study the details to understand why in the future) Note OpenGL preserves a similar matrix to describe the camera type and this is called the PROJECTION_MATRIX This important matrix is stored as the MODELVIEW matrix The MODELVIEW matrix is so important OpenGL maintains a stack of these matricesThe MODELVIEW matrix is so important OpenGL maintains a stack of these matrices You have control of this stack with the glPushMatrix and glPopMatrix commandsYou have control of this stack with the glPushMatrix and glPopMatrix commands (The matrix is actually 4x4, but we will study the details to understand why in the future)(The matrix is actually 4x4, but we will study the details to understand why in the future) Note OpenGL preserves a similar matrix to describe the camera type and this is called the PROJECTION_MATRIX

17 Introduction to OpenGL What’s hard about this assignment? Managing the MODELVIEW stackManaging the MODELVIEW stack We’ll use:We’ll use: –glLoadIdentity(); –glTranslatef(GLfloat x, y, z); –glRotatef(GLfloat degrees, x, y, z); –glScalef (GLfloat x, y, z); –glPushMatrix(), glPopMatrix(); What’s hard about this assignment? Managing the MODELVIEW stackManaging the MODELVIEW stack We’ll use:We’ll use: –glLoadIdentity(); –glTranslatef(GLfloat x, y, z); –glRotatef(GLfloat degrees, x, y, z); –glScalef (GLfloat x, y, z); –glPushMatrix(), glPopMatrix();

18 Modeling Transformations glTranslate (x, y, z) Post-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-valuesPost-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-values glRotate (theta, x, y, z) Post-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)Post-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) glTranslate (x, y, z) Post-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-valuesPost-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-values glRotate (theta, x, y, z) Post-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)Post-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)

19 Modeling Transformations glScale (x, y, z) Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axesPost-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes glScale (x, y, z) Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axesPost-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes

20 Modeling Transformations It is important that you understand the order in which OpenGL concatenates matrices glMatrixMode (MODELVIEW); glLoadIdentity();glMultMatrix(N);glMultMatrix(M);glMultMatrix(L);glBegin(POINTS);glVertex3f(v);glEnd(); 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))

21 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 stackpush all matrices in current stack down one level and copy topmost matrix of stack glPopMatrix ( ) pop the top matrix off the stackpop the top matrix off the stack 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 stackpush all matrices in current stack down one level and copy topmost matrix of stack glPopMatrix ( ) pop the top matrix off the stackpop the top matrix off the stack

22 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 ( ); }

23 Matrix Manipulation – Example Grand, fixed coordinate system 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 ( ); Global – Bottom Up Start Rot Trans R RT RTv

24 Matrix Manipulation – Example Local coordinate system 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 ( ); Local – Top Down Start Rot Trans R RT RTv

25 OpenGL: Conventions Functions in OpenGL start with gl Most functions just gl (e.g., glColor() )Most functions just gl (e.g., glColor() ) Functions starting with glu are utility functions (e.g., gluLookAt() )Functions starting with glu are utility functions (e.g., gluLookAt() ) Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c) Functions in OpenGL start with gl Most functions just gl (e.g., glColor() )Most functions just gl (e.g., glColor() ) Functions starting with glu are utility functions (e.g., gluLookAt() )Functions starting with glu are utility functions (e.g., gluLookAt() ) Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)

26 OpenGL: Conventions Function names indicate argument type and number Functions ending with f take floatsFunctions ending with f take floats Functions ending with i take intsFunctions ending with i take ints Functions ending with b take bytesFunctions ending with b take bytes Functions ending with ub take unsigned bytesFunctions ending with ub take unsigned bytes Functions that end with v take an array.Functions that end with v take an array.Examples glColor3f() takes 3 floatsglColor3f() takes 3 floats glColor4fv() takes an array of 4 floatsglColor4fv() takes an array of 4 floats Function names indicate argument type and number Functions ending with f take floatsFunctions ending with f take floats Functions ending with i take intsFunctions ending with i take ints Functions ending with b take bytesFunctions ending with b take bytes Functions ending with ub take unsigned bytesFunctions ending with ub take unsigned bytes Functions that end with v take an array.Functions that end with v take an array.Examples glColor3f() takes 3 floatsglColor3f() takes 3 floats glColor4fv() takes an array of 4 floatsglColor4fv() takes an array of 4 floats

27 OpenGL: Conventions Variables written in CAPITAL letters Example: GLUT_SINGLE, GLUT_RGBExample: GLUT_SINGLE, GLUT_RGB usually constantsusually constants use the bitwise or command (x | y) to combine constantsuse the bitwise or command (x | y) to combine constants Variables written in CAPITAL letters Example: GLUT_SINGLE, GLUT_RGBExample: GLUT_SINGLE, GLUT_RGB usually constantsusually constants use the bitwise or command (x | y) to combine constantsuse the bitwise or command (x | y) to combine constants

28 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()Set camera pose with gluLookAt() –Camera position specified in world coordinates Render polygons of modelRender polygons of model –Simplest case: vertices of polygons in world coordinates 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()Set camera pose with gluLookAt() –Camera position specified in world coordinates Render polygons of modelRender polygons of model –Simplest case: vertices of polygons in world coordinates

29 OpenGL: Simple Use Open a window and attach OpenGL to it glutCreateWindow()glutCreateWindow() Open a window and attach OpenGL to it glutCreateWindow()glutCreateWindow()

30 OpenGL: Perspective Projection Set projection parameters (e.g., field of view) Typically, we use a perspective projection Distant objects appear smaller than near objectsDistant objects appear smaller than near objects Vanishing point at center of screenVanishing point at center of screen Defined by a view frustum (draw it)Defined by a view frustum (draw it) Other projections: orthographic, isometric Set projection parameters (e.g., field of view) Typically, we use a perspective projection Distant objects appear smaller than near objectsDistant objects appear smaller than near objects Vanishing point at center of screenVanishing point at center of screen Defined by a view frustum (draw it)Defined by a view frustum (draw it) Other projections: orthographic, isometric

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

32 OpenGL: Perspective Projection In OpenGL: Projections implemented by projection matrixProjections implemented by projection matrix gluPerspective() creates a perspective projection matrix:gluPerspective() creates a perspective projection matrix:glSetMatrix(GL_PROJECTION); glLoadIdentity(); //load an identity matrix gluPerspective(vfov, aspect, near, far); Parameters to gluPerspective() : vfov : vertical field of viewvfov : vertical field of view aspect : window width/heightaspect : window width/height near, far : distance to near & far clipping planesnear, far : distance to near & far clipping planes In OpenGL: Projections implemented by projection matrixProjections implemented by projection matrix gluPerspective() creates a perspective projection matrix:gluPerspective() creates a perspective projection matrix:glSetMatrix(GL_PROJECTION); glLoadIdentity(); //load an identity matrix gluPerspective(vfov, aspect, near, far); Parameters to gluPerspective() : vfov : vertical field of viewvfov : vertical field of view aspect : window width/heightaspect : window width/height near, far : distance to near & far clipping planesnear, far : distance to near & far clipping planes

33 OpenGL: Lighting Setup lighting, if any Simplest option: change the current color between polygons or vertices glColor() sets the current colorglColor() sets the current color Or OpenGL provides a simple lighting model: Set parameters for light(s)Set parameters for light(s) –Intensity, position, direction & falloff (if applicable) Set material parameters to describe how light reflects from the surfaceSet material parameters to describe how light reflects from the surface Won’t go into details now; check the red book if interested Setup lighting, if any Simplest option: change the current color between polygons or vertices glColor() sets the current colorglColor() sets the current color Or OpenGL provides a simple lighting model: Set parameters for light(s)Set parameters for light(s) –Intensity, position, direction & falloff (if applicable) Set material parameters to describe how light reflects from the surfaceSet material parameters to describe how light reflects from the surface Won’t go into details now; check the red book if interested

34 OpenGL: Specifying Geometry Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() A simple example: telling GL to render a triangleA simple example: telling GL to render a triangleglBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glEnd(); Usage: glBegin( geomtype ) where geomtype is:Usage: glBegin( geomtype ) where geomtype is: –Points, lines, polygons, triangles, quadrilaterals, etc... Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() A simple example: telling GL to render a triangleA simple example: telling GL to render a triangleglBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glEnd(); Usage: glBegin( geomtype ) where geomtype is:Usage: glBegin( geomtype ) where geomtype is: –Points, lines, polygons, triangles, quadrilaterals, etc...

35 Primitive Types GL_POINTSGL_LINE {S | _STRIP | _LOOP}{S | _STRIP | _LOOP}GL_TRIANGLE {S | _STRIP | _FAN}{S | _STRIP | _FAN}GL_QUAD {S | _STRIP}{S | _STRIP}GL_POLYGONGL_POINTSGL_LINE {S | _STRIP | _LOOP}{S | _STRIP | _LOOP}GL_TRIANGLE {S | _STRIP | _FAN}{S | _STRIP | _FAN}GL_QUAD {S | _STRIP}{S | _STRIP}GL_POLYGON

36 Points in OpenGL glBegin(GL_POINTS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); glBegin(GL_POINTS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

37 Lines in OpenGL (1/3) Line Segments glBegin(GL_LINES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); glBegin(GL_LINES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

38 Lines in OpenGL (2/3) Polylines – Line Strip glBegin(GL_LINE_STRIP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); glBegin(GL_LINE_STRIP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

39 Lines in OpenGL (3/3) Polylines – Line Loop glBegin(GL_LINE_LOOP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); glBegin(GL_LINE_LOOP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

40 Polygon Basics (1/2) Definition Object that is closed as a line loop, but that has an interiorObject that is closed as a line loop, but that has an interior Simple Polygon No pair of edges of a polygon cross each otherNo pair of edges of a polygon cross each otherDefinition Object that is closed as a line loop, but that has an interiorObject that is closed as a line loop, but that has an interior Simple Polygon No pair of edges of a polygon cross each otherNo pair of edges of a polygon cross each other Simple Nonsimple

41 Polygon Basics (2/2) Convexity If all points on the line segment between any two points inside the object, or on its boundary, are inside the objectIf all points on the line segment between any two points inside the object, or on its boundary, are inside the objectConvexity Convex Objects p1 p2 concave convex

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

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

44 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 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

45 Polygons in OpenGL (1/6) PolygonPolygon glBegin(GL_POLYGON); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); glBegin(GL_POLYGON); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

46 Polygons in OpenGL (2/6) QuadrilateralsQuadrilaterals glBegin(GL_QUADS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); glBegin(GL_QUADS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

47 Polygons in OpenGL (3/6) QuadstripQuadstrip glBegin(GL_QUAD_STRIP); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p0); glVertex2fv(p4); glVertex2fv(p7); glVertex2fv(p5); glVertex2fv(p6); glEnd(); glBegin(GL_QUAD_STRIP); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p0); glVertex2fv(p4); glVertex2fv(p7); glVertex2fv(p5); glVertex2fv(p6); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

48 Polygons in OpenGL (4/6) TrianglesTriangles glBegin(GL_TRIANGLES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); glBegin(GL_TRIANGLES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

49 Polygons in OpenGL (5/6) Triangle Strip glBegin(GL_TRIANGLE_STRIP); glVertex2fv(p0); glVertex2fv(p7); glVertex2fv(p1); glVertex2fv(p6); glVertex2fv(p2); glVertex2fv(p5); glVertex2fv(p3); glVertex2fv(p4); glEnd(); glBegin(GL_TRIANGLE_STRIP); glVertex2fv(p0); glVertex2fv(p7); glVertex2fv(p1); glVertex2fv(p6); glVertex2fv(p2); glVertex2fv(p5); glVertex2fv(p3); glVertex2fv(p4); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

50 Polygons in OpenGL (6/6) Triangle Fan glBegin(GL_TRIANGLE_FAN); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); glBegin(GL_TRIANGLE_FAN); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

51 Double Buffering Avoids displaying partially rendered frame buffer OpenGL generates one raster image while another raster image is displayed on monitor glutSwapBuffers (void) Avoids displaying partially rendered frame buffer OpenGL generates one raster image while another raster image is displayed on monitor glutSwapBuffers (void)

52 Learn OpenGL by example robot.c from the OpenGL Programming Guide

53 Learn OpenGL by example Two bodies Upper armUpper arm Lower armLower arm Major tasks PositionPosition OrientationOrientation Both bodies originally at origin Two bodies Upper armUpper arm Lower armLower arm Major tasks PositionPosition OrientationOrientation Both bodies originally at origin

54 Learn OpenGL by example Headers #include #include Headers

55 Learn OpenGL by example int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMainLoop(); return 0; } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMainLoop(); return 0; }

56 Learn OpenGL by example void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); }

57 Learn OpenGL by example void display(void){ glClear (GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslatef (-1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();Continued… void display(void){ glClear (GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslatef (-1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();Continued…

58 Learn OpenGL by example glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();glPopMatrix();glutSwapBuffers();} glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();glPopMatrix();glutSwapBuffers();}


Download ppt "UBI 516 Advanced Computer Graphics Introduction to OpenGL OpenGL Programming Guide."

Similar presentations


Ads by Google