Computer Graphics Introducing OpenGL - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Computer Graphics Introducing OpenGL

Description:

Each of these implementations is written to best suit the platform ... ported by others easily (although Wine, a windows emulator, has a limited subset) ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 18
Provided by: lauren80
Category:

less

Transcript and Presenter's Notes

Title: Computer Graphics Introducing OpenGL


1
Computer GraphicsIntroducing OpenGL
  • CO2409 Computer Graphics
  • Week 20

2
Lecture Contents
  • Introducing OpenGL
  • Comparison of OpenGL Direct3D
  • Initialisation
  • Matrices
  • Geometry and Rendering

3
What is OpenGL?
  • OpenGL is a specification for a graphics API
  • The specification is platform-independent
  • Has implementations on several platforms
  • Windows (built in since Windows 98)
  • Linux and other Unix platforms
  • Mac, Playstation 3 etc
  • Each of these implementations is written to best
    suit the platform
  • Several versions exist in some cases
  • E.g. video card manufacturers often provide their
    own optimised OpenGL implementations with their
    drivers

4
History of OpenGL
  • Developed by Silicon Graphics as an update to
    their own IrisGL API (early 90s?)
  • They produced very high-end graphics workstations
  • Decided to make the specification for their new
    API open to other companies
  • They wanted to give the implementation
    responsibility to the hardware manufacturers
  • Whilst retaining control over the API
    specification
  • Created a review board for the spec in 1992
  • They monitor revisions to the specification and
    validate implementations

5
History of OpenGL cont
  • The review board consists of most major graphics
    hardware and software companies, including
  • Nvidia, ATI and Evans Sutherland
  • Intel, Apple and Sun Microsystems
  • Microsoft were on the board for many years, but
    left in 2003
  • DirectX competes with OpenGL
  • Was a spat regarding Vista support for OpenGL,
    resolved in March 2006
  • Proper OpenGL support in Vista has taken a while
  • The latest OpenGL specification is 2.1
  • Although many versions from 1.1 on are still in
    use

6
Comparison of OpenGL Direct3D
  • Long running debate about the relative merits
  • Portability
  • DirectX only exists on Windows platforms
    (including Xbox 360). Closed specification, so
    cannot be ported by others easily (although Wine,
    a windows emulator, has a limited subset)
  • OpenGL available on many platforms and easy to
    port due to open specification
  • Ease of use
  • DirectX uses an OO style, partially based on the
    COM model. DirectX is harder to read, but uses
    better practice
  • OpenGL is procedural, although OO extensions are
    available. OpenGL reads very simply, but lacks
    rigour

7
OpenGL vs Direct3D cont
  • Ease of use (continued)
  • DirectX is primarily designed to provide direct
    access to hardware accelerated features
  • OpenGL is designed to provide useful features and
    provide hardware acceleration if possible
  • DirectX allows users to manage resources
  • OpenGL handles resource management, users are
    unaware of this process (or even if hardware
    acceleration is being used at all)
  • DirectX documentation is fairly good. Tutorials
    easy to find, but must work around legacy
    documentation
  • OpenGL similar excellent documentation,
    tutorials easy to find, but often targeting older
    versions

8
OpenGL vs Direct3D cont
  • Performance
  • Not much difference, as long as
  • Using latest DirectX / best available OpenGL
    drivers
  • Using best practices for coding each API
  • Functionality
  • OpenGL has more functionality due to its user
    focus
  • Also provides an extensions mechanism to allow
    3rd party extensions to the API
  • However the performance of any additional
    functionality is likely to be hardware limited
  • Extensions also cause portability and usability
    issues

9
Getting Started with OpenGL
  • Installation of OpenGL varies depending on the
    features required
  • The basic OpenGL 1.1 is installed by default on
    many platforms
  • But if you wish to use a later revisions or
    extensions, you must manually download the
    separate components
  • No unified installer as with DirectX
  • Visual Studio 2005 links automatically to the
    OpenGL 1.1 headers and libraries
  • Built in to Windows
  • For this version we can start programming
    immediately

10
Initialisation
  • OpenGL is a state machine
  • At any one time the API is in a given state
  • Which affects the how other function calls
    perform
  • First noticeable during initialisation
  • No device to create - no interface pointer
    through which to access the API
  • Gamers see architecture discussions in Games Dev
    1
  • We create a Rendering Context and set it as
    current
  • All subsequent calls will use this rendering
    context without needing to specify it
  • Easy for programmer to lose track of state
    (effectively many invisible globals)

11
Initialisation Example (DX)
  • Initialisation steps in DirectX
  • // Set required device parameters
  • D3DPRESENT_PARAMETERS d3dpp
  • d3dpp.Windowed TRUE
  • //...other settings
  • // Create a device using these settings
  • g_pD3D-gtCreateDevice(D3DADAPTER_DEFAULT,
    D3DDEVTYPE_HAL, hWnd
  • D3DCREATE_SOFTWARE_VERTEXPROC
    ESSING,
  • d3dpp, g_pd3dDevice )
  • // Use the device
  • g_pd3dDevice-gtSetRenderState( D3DRS_ZENABLE, TRUE
    )

12
Initialisation Example (GL)
  • Initialisation steps in OpenGL
  • // Set required pixel format for rendering
    context
  • PIXELFORMATDESCRIPTOR pixelFormat
  • desiredFormat.dwFlags PFD_SUPPORT_OPENGL
  • PFD_DRAW_TO_WINDOW
    PFD_DOUBLEBUFFER
  • //...other settings
  • // Set this pixel format and create a rendering
    context
  • SetPixelFormat( g_hDC, iPixelFormat,
    pixelFormat)
  • g_hRC wglCreateContext( g_hDC )
  • wglMakeCurrent( g_hDC, g_hRC ) // Use this
    context from here
  • // Using the current context, enable
    depth-testing state
  • glEnable( GL_DEPTH_TEST )

13
Matrices
  • Matrices in OpenGL use a right-handed system
  • DirectX uses a left-handed system
  • In OpenGL, X is right and Y is up as with
    DirectX, but Z is towards us
  • I.e. The camera / models look forward down
    negative Z
  • Converting between the two systems is fiddly
  • Cant just negate the Z values
  • Will reverse the clockwise / counter-clockwise
    order of the triangle vertices
  • Also the X Y rotations will go in the opposite
    direction
  • A simple solution is suggested in the lab

14
Matrices cont
  • OpenGL uses a combined world/view matrix
  • Although it calls it the model/view matrix
  • These matrices are separate in DirectX
  • Built by repeatedly combining matrices
  • Starting with the camera (view) matrices, then
    applying the models world matrices
  • OpenGL also has a projection matrix
  • Same as DirectX
  • Again uses a state based system to set up

15
Matrix Example (GL)
  • Building a matrix in OpenGL
  • Note that the calls rely on the initial state
    function
  • // Switch matrix mode (state) to use the
    model/view matrix
  • glMatrixMode( GL_MODELVIEW ) // Assume already
    calculated view part
  • glPushMatrix() // Store the current matrix (view
    part)
  • // Add in the world matrix (reverse order from
    DirectX)
  • // Each operation is accumulated onto the current
    matrix
  • glTranslatef( m_Pos.x, m_Pos.y, m_Pos.z ) //
    Translation
  • glRotatef( m_Rot.x, 1.0f, 0.0f, 0.0f ) // X
    Rotation
  • glRotatef( m_Rot.y, 0.0f, 1.0f, 0.0f ) // Y
    Rotation
  • glRotatef( m_Rot.z, 0.0f, 0.0f, 1.0f ) // Z
    Rotation
  • glScalef( m_Scale, m_Scale, m_Scale ) //
    Scaling
  • //... Do some rendering
  • glPopMatrix() // Restore the view matrix for the
    next model...

16
Geometry and Rendering
  • OpenGL can store geometry in lists of vertices
    and indices in a similar manner to DirectX
  • These lists can be arrays stored in system memory
    rather than hardware buffers
  • Vertices can also be customised to some degree
  • Whether they include UVs, Normals etc.
  • Simple to render a set of primitives
  • Specify the vertex/index arrays to use
  • Issue a draw function

17
Rendering Example (GL)
  • Rendering a primitive in OpenGL
  • Note that there are more primitive types than in
    DirectX
  • OpenGL supports quads, arbitrary polygons, line
    loops
  • // Set the vertex array to use and the type of
    each vertex
  • // Vertex 3 floats, Normal 3 floats, Tex
    coord 2 floats
  • glInterleavedArrays( GL_T2F_N3F_V3F, 0,
    m_Vertices )
  • // Draw a set of primitives using an index array
  • glDrawElements( GL_TRIANGLES, m_NumIndices,
    GL_UNSIGNED_SHORT,
  • m_Indices )
  • There are several other ways to render
  • Without index buffers,
  • One vertex at a time
Write a Comment
User Comments (0)
About PowerShow.com