OpenGL I - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

OpenGL I

Description:

OpenGL I by Rasmus Stenholt rs_at_cvmt.dk Course Style Few students = study circle What does study circle mean? Fewer lectures More independent work How will it be ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 52
Provided by: cvmtDkedu
Category:

less

Transcript and Presenter's Notes

Title: OpenGL I


1
OpenGL I
  • by Rasmus Stenholt
  • rs_at_cvmt.dk

2
Course Style
  • Few students study circle
  • What does study circle mean?
  • Fewer lectures
  • More independent work
  • How will it be handled in this course?
  • 3 mini modules of ordinary lectures
  • 1 mini module of micro project work
  • 1 mini module of micro project presentation and
    discussion

3
Agenda
  • What is OpenGL?
  • Installing OpenGL
  • OpenGL and your OS
  • OpenGL functions
  • OpenGL primitives
  • Colours
  • Transformations
  • Matrices and matrix stacks
  • Homogenous coordinates
  • Translations, rotations
  • Perspective projection, orthographic projection

4
What is OpenGL?
  • OpenGL is a standard
  • A library of functions and constants for making
    real time rendering of 3-D graphics
  • A specification of the input and output to these
    functions
  • An OpenGL implementation is a state machine
  • The mode of operation depends on the set state
  • OpenGL is maintained by the architecture review
    board (ARB)

5
What is OpenGL?
  • OpenGL in itself is platform independent
  • Works with many operating systems and programming
    languages
  • The current version of OpenGL is 2.0
  • Standard OpenGL has two parts
  • The core of OpenGL GL
  • The GL utility library GLU

6
What is OpenGL?
  • OpenGL is well-documented
  • The red book
  • OpenGL(R) Programming Guide The Official Guide
    to Learning OpenGL(R), Version 2 (5th Edition)
  • The blue book
  • OpenGL(R) Reference Manual The Official
    Reference Document to OpenGL, Version 1.4 (4th
    Edition)
  • The orange book
  • OpenGL Shading Language, 2nd Edition
  • The official OpenGL website
  • http//www.opengl.org
  • The MSDN OpenGL website
  • Just google for msdn opengl
  • NeHes tutorials
  • http//nehe.gamedev.net
  • Lighthouse3Ds tutorials
  • http//www.lighthouse3d.com/opengl/

7
Installing OpenGL
  • Windows
  • Install the latest drivers for your graphics card
  • Install Visual Studio
  • Install GLUT
  • http//www.xmission.com/nate/glut.html
  • Put glut32.dll in your windows/system32 folder
  • Let Visual Studio know where glut.h and
    glut32.lib are located or place them in the
    standard include and lib paths

8
Installing OpenGL
  • Linux
  • Install the latest drivers for your graphics card
  • Download and install freeglut or Mesa3D glut
  • Make sure that your compiler knows the paths to
    the GL and GLUT header files and libraries

9
OpenGL and your OS
  • OpenGL being platform independent does not mean
    that OpenGL applications are platform
    independent!
  • Why?
  • Because the price of platform independence is
    that OpenGL knows nothing about
  • Opening a window
  • Getting mouse input
  • Reading from the keyboard
  • Handling OS events
  • Consequently any OpenGL application must add this
  • Result Platform dependence!

10
OpenGL and your OS
  • If you use OS specific commands in your OpenGL
    application, you will not have platform
    independence!
  • A solution to this is GLUT
  • A small library of standard functions
  • Handles windows
  • Handles I/O
  • The GLUT functions are the same on all systems
  • Only the implementation varies

11
OpenGL and your OS
  • GLUT serves several purposes
  • Platform independence
  • No knowledge of OS
  • Easier programming
  • Sequences of OS calls are abstracted
  • Compact code
  • Easier-to-understand code
  • GLUT can/should not be used when you want full
    control of OS related issues
  • Example An application with several windows
    and/or non-GL windows

12
OpenGL and your OS
  • How does GLUT work?
  • Callback functions
  • A callback function is a function which is passed
    to another function as a parameter
  • The other function can then call its parameter
    function back

13
OpenGL and your OS
  • GLUTs callback functions include
  • A function for updating the display
  • This is always needed
  • A function for resizing the window
  • A function for reading keyboard input
  • A function for getting mouse input
  • A function for creating menus

14
Creating a Window
  • Example
  • include ltGL/glut.hgt
  • void main(int argc, char argv)
  • glutInit(argc, argv) glutInitDisplayMode(GLUT_D
    EPTHGLUT_SINGLEGLUT_RGBA)
  • glutInitWindowPosition(100,100)
    glutInitWindowSize(320,320)
  • glutCreateWindow(My first window")
    glutDisplayFunc(renderScene)
  • glutMainLoop()

15
OpenGL Functions
  • To gain access to OpenGL functions do one of the
    following
  • Using GLUT
  • include ltGL/glut.hgt
  • Not using GLUT
  • include ltGL/GL.hgt
  • include ltGL/GLU.hgt

16
OpenGL Functions
  • OpenGL functions all follow the same naming
    conventions
  • Function names have gl, glu, or glut as prefix
    depending on their package of origin
  • The name of the function follows the prefix
  • The parameter type of the function is placed as a
    postfix

17
OpenGL Functions
glVertex3fv( v )
Number of components
Data Type
Vector
b - byte ub - unsigned byte s - short us -
unsigned short i - int ui - unsigned int f -
float d - double
omit v for scalar form glVertex2f( x, y )
2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w)
18
OpenGL Primitives
  • In OpenGL, geometry is specified by vertices
  • A vertex represents the junction between the
    edges of a 3-D model
  • Vertices are specified by the glVertex() family
    of functions
  • Vertices must be specified between
    glBegin(primitive type) and glEnd() function
    calls
  • The primitive type represents how vertices are to
    be connected

19
OpenGL Primitives
  • Triangles
  • There are 3 ways of making triangles with OpenGL
  • Individual triangles
  • Type is GL_TRIANGLES
  • Each triangle requires 3 explicit vertices
  • Sets of unconnected triangles are often called
    polygon soups
  • Strips of connected triangles
  • Type is GL_TRIANGLE_STRIP
  • The first triangle requires 3 vertices, the rest
    use 1 new vertex and the 2 most recently defined
    vertices
  • Complex objects are often built from
  • Fans of connected triangles
  • Type is GL_TRIANGLE_FAN
  • Every triangle use the first, the previous, and a
    new vertex
  • Useful for creating polygons or approximating
    circles/ellipses

20
OpenGL Primitives
  • Triangles are the most common way of building 3-D
    geometry
  • Triangle vertices are always coplanar
  • Triangles are always convex
  • Triangles are the simplest shapes with non-zero
    areas
  • All other surface primitives are translated into
    triangles by the graphics card
  • The vertex order determines the normal direction
  • Normal direction front face
  • Inverse normal direction back face

21
OpenGL Primitives
  • Quadrilaterals (quads)
  • Individual quads
  • Type is GL_QUADS
  • A quad is defined by 4 vertices
  • Quads can be decomposed into two triangles
  • Quads are not necessarily plane or convex
  • Be careful with the vertex sequence
  • Strips of connected quads
  • Type is GL_QUAD_STRIP
  • Uses the most recent 2 vertices and 2 new
    vertices

22
OpenGL Primitives
  • Polygons
  • Type is GL_POLYGON
  • Polygons need 3 or more vertices
  • I.e. can be used for any polygon
  • Polygons are divided into triangles by the
    graphics card

23
OpenGL Primitives
  • Points
  • Type is GL_POINTS
  • Points are 0-D
  • Points represent the simplest drawable primitive
  • 1 vertex is used per point
  • Points are rendered as small, unconnected dots on
    the screen
  • Theoretically points have no area

24
OpenGL Primitives
  • Lines
  • Type is GL_LINES
  • Lines are 1-D
  • Each line needs 2 vertices
  • Lines have no area
  • Open series of lines
  • Type is GL_LINE_STRIP
  • Closed series of lines
  • Type is GL_LINE_LOOP

25
OpenGL Primitives
  • There are other, more efficient and/or stable
    ways of defining vertices
  • Display lists
  • Vertex arrays
  • A display list is a precompiled list of vertices
    which can be reused multiple times
  • A vertex array is an indexed list of vertices,
    which can be individually accessed

26
Tutorial
  • Lets have a look at Nate Robins shapes tutorial

27
Break
  • Well meet again in 10 minutes

28
Colours in OpenGL
  • In OpenGL colours are assigned to vertices
  • Every vertex has a colour
  • Colours can be
  • Interpolated between vertices
  • Smooth (Gouraud) shading
  • Constant for each primitive
  • Flat shading
  • The shading model is controlled using the
    glShadeModel(mode) function
  • The mode can be GL_SMOOTH or GL_FLAT

29
Colours in OpenGL
  • The current colour is an OpenGL state
  • All new vertices get the colour of the current
    state
  • The current colour remains set until you change it

30
Colours in OpenGL
  • Colours are modelled using the red-green-blue
    (RGB) system

31
Colours in OpenGL
  • There are several ways of representing colour in
    OpenGL
  • Directly as RGB-tuples
  • Extended RGBA-tuples
  • Indexed mode
  • The RGBA mode has an extra component, alpha,
    which does not affect the colour directly
  • Alpha is used when blending colours
  • E.g. transparency effects
  • The indexed mode uses a fixed table of colours to
    look up colours

32
Colours in OpenGL
  • Colours are specified by the glColor() family of
    functions
  • Example glColor3f()
  • Specifies a colour by three floating point values
    in the range 0.01.0
  • The parameters represent R, G, and B, respectively

33
Transformations
  • A lot of transformations take place in any 3-D
    graphics environment
  • Rotations and translation for viewpoint and model
    control
  • Perspective and orthographic projections for
    transforming 3-D to 2-D
  • Texture mapping transforms 2-D into 3-D
  • And several others

34
Transformations
  • All transformations in OpenGL are carried out by
    matrices
  • There are different matrices for different
    purposes
  • The modelview matrix
  • GL_MODELVIEW
  • The projection matrix
  • GL_PROJECTION
  • The texture matrix
  • GL_TEXTURE
  • All matrices are post-multiplied
  • I.e. the current matrix M becomes MN when N is
    performed
  • Post-multiplication is closely linked to matrix
    stacks

35
Transformations
  • All matrices have a matrix stack associated with
    them
  • Only the top matrix is used
  • All matrix operations are performed on the top
    matrix
  • Remember that a stack operates on a last in-first
    out (lifo) principle

36
Transformations
  • The matrix stacks are useful because they provide
    a means of storing and retrieving previous
    transformation states
  • The stack principle is important
  • Makes hierarchical models possible
  • The operation closest to the primitive is the
    first to affect the primitive
  • Allows all models to share the same virtual camera

37
Transformations
  • Matrix operations are carried out on the current
    matrix stack
  • The current matrix stack is set as an OpenGL
    state
  • Use glMatrixMode(mode) to change the current
    matrix stack
  • The current matrix stack remains active until a
    new one is selected

38
Transformations
  • Stack operations
  • Save the current matrix (push)
  • Use the glPushMatrix() function
  • Restore a saved matrix (pop)
  • Use the glPopMatrix() function
  • Replacing the current matrix by the identity
    matrix
  • Use the glLoadIdentity() function

39
Transformations
  • Example Placing the camera and a model
  • First set the modelview matrix to represent the
    camera transformation
  • Use gluLookAt(pos,lookat,up)
  • Push the modelview matrix to save it
  • Set up the modelview matrix to reflect scenery
    placement
  • Draw the scenery
  • Pop the modelview matrix to restore the camera
    transformation

40
Transformations
  • Matrices
  • All matrices in OpenGL are 4x4
  • Why use 4-D matrices for 3-D graphics?
  • A 4x4 matrix can rotate and translate the same
    vector in one operation
  • A 4x4 matrix can make the otherwise non-linear
    perspective projection linear
  • Modern CPUs are very efficient at handling 4x4
    matrices

41
Transformations
  • Vectors
  • Are 4-element to match the matrices
  • Can be used for
  • Vertices (i.e. points in space)
  • Direction vectors (e.g. normals)
  • Colours
  • The 4th coordinate is usually set to 1 for
    vertices and 0 for direction vectors
  • Using 4 coordinates for a 3-D space is often
    called homogenous coordinates

42
Transformations
  • The three common transformations
  • Translation
  • glTranslate()
  • Rotation
  • glRotatef()
  • Scaling
  • glScalef()

43
Transformations
  • Transformation sequence
  • Should I rotate or translate first?
  • Remember the stack and post-multiplication
    principles!
  • The operations are carried out in last
    defined-first performed order
  • Try to think about the transformation sequence in
    terms of local coordinate systems instead of a
    global one

44
Transformations
  • Projection transformations
  • Define the coordinate system
  • OpenGL uses right-handed coordinates for the
    camera
  • x points right
  • y points up
  • z points away from the scene
  • Makes 3-D ready for the 2-D screen
  • The projection matrix determines how this is done
  • Think of the projection matrix as the internal
    parts of a camera
  • The projection matrix rarely changes during
    runtime
  • Examples Window resizing, zoom effects
  • The projection transformation takes place after
    all modeling transformations have been carried out

45
Transformations
  • Perspective projection
  • The projection normally used for realistic
    imaging
  • Perspective projection emulates a perfect pinhole
    camera
  • All light passes through a single point
  • A perspective projection matrix creates a viewing
    frustum
  • Only objects inside the frustum are visible in
    the final image

46
Transformations
  • Perspective transformations
  • Characteristics
  • Field-of-view
  • Focal length
  • Near-clipping plane
  • Far-clipping plane
  • Regular or oblique?
  • A question of the optical axis

47
Transformations
  • Perspective projection in OpenGL
  • The glFrustum() command for a general
    perspective projection
  • The gluPerspective() command for a regular
    perspective projection
  • The parameters for the functions define the
    frustum characteristics

48
Transformations
  • Orthographic projection
  • The projection used for making isometric views
  • Examples SimCity, The Sims 1, Civilization III
  • Also used for making screen space rendering
  • The viewing volume is a rectangular box
  • Light rays do not intersect (i.e. parallel rays)
  • Consequence Parallel lines remain parallel
  • Creates pseudorealistic, naivistic images
  • No depth from perspective

49
Transformations
  • Orthographic projections in OpenGL
  • A generic orthographic projection, glOrtho()
  • A special orthographic projection for 2-D,
    gluOrtho2D()

50
Tutorial
  • Have a look at the projection and transformation
    tutorials

51
Micro Project
  • Runs through all mini modules
  • Create a small OpenGL demo which looks nice and
    uses some of the features taught in this course
  • Must be presented at the last lecture
  • A requirement for passing the course
Write a Comment
User Comments (0)
About PowerShow.com