Introduction to 3D Game Graphics

Document Sample
Introduction to 3D Game Graphics Powered By Docstoc
					         Introduction to 3D Game
              Dr. Xiaoyu Zhang

CS 536            Xiaoyu Zhang, CSUSM   1
                Why Make Games?
                            • It’s fun! People like to play games
                            • Game industry is huge and grows
                            • Game programming is challenging-
                              inspiring a new generation of young
                              designers and programmers
                               – Maybe the most challenging and
         Half-life 2               creative project you have done so
                               – Drives research and development in
                                   many areas such as graphics and AI

CS 536                 Xiaoyu Zhang, CSUSM                         2
    Elements of 3D Game Programming
• Game programming consists of at least following elements
         –   Graphics
         –   Input devices
         –   Music and sound
         –   Game logic and AI
         –   Game physics
         –   Networking
         –   User interface and menu system
• 3D graphics is the cornerstone of 3D game programming

CS 536                               Xiaoyu Zhang, CSUSM     3
                     Game Development
•     A simplistic game architecture shows
      that each element of a game is
      divided into its own piece and
      communicates with other elements                     Audio
•     Games feature some of the most
      complex blends of technology and
      software designs                                                               Input

•     Require high level abstract thinking
                                                                       Game Logic
      and implementation and necessary              Rendering Engine      AI
      background on math, physics, and
•     We will focus on developing the                                               Network
      Rendering Engine                                Game World

    CS 536                           Xiaoyu Zhang, CSUSM                               4
              The Course: General Topics
• This class will focus on Game Graphics:
         –   Basic 3D Computer Graphics theory
         –   Graphics programming with OpenGL
         –   Rendering algorithms underlying 3D game engines
         –   Modern graphic hardware and GPU shader programming

• We won’t study:
         – Gameplay, storylines, (much) AI, game art, production process,
           artist tools, network layers, OO game design, (much) audio, (much)
         – Feel free to explore more that you are interested in.

CS 536                             Xiaoyu Zhang, CSUSM                      5
                                  Tools used
• Microsoft visual studio .net
         – You should be familiar with C/C++ programming and basic data
           structures. (CS 311)
         – Visual Studio .Net is available on Campus computers

• OpenGL and DirectX are two dominant computer graphics
  (CG) libraries.
         – OpenGL
             • Platform independent graphics library
             • Available on all windows and other major platforms
         – DirectX
             • More than just a rendering library. Many other components
             • Newest version DirectX 9

CS 536                                 Xiaoyu Zhang, CSUSM                 6
• OpenGL is a cross-platform low-level rendering and modeling library.
• It’s designed as a streamlined, hardware-independent interface to be
  implemented on many different hardware platforms
• Intuitive, procedural interface with C binding
         – Several hundred C functions providing access to features of your graphics
• Widely accepted industry standard and evolves slowly. Currently in version
• It’s widely used from games to modeling to scientific visualization. A lot of
  games are developed using OpenGL

CS 536                                Xiaoyu Zhang, CSUSM                              7
                           What is DirectX
• DirectX is Microsoft API to provide direct access to
• Only for window, currently in version 9
• DirectX components:
         –   Direct Graphics – 2D and 3D graphics (not used in this class)
         –   DirectInput – interface to input devices
         –   DirectAudio – play sound and music
         –   DirectPlay – communication across networks
         –   DirectShow – multimedia support

CS 536                               Xiaoyu Zhang, CSUSM                     8
          OpenGL vs. DirectX
• OpenGL is a pure and more comprehensive 3D
  graphics API; DirectX is a more complete game
  development API
• OpenGL is portable across platforms; DirectX
  is mainly for windows
• One Situation: use OpenGL for graphics and
  DirectX only necessary to keep portability
  across platforms.

CS 536             Xiaoyu Zhang, CSUSM        9


• Rendering is the process of converting a 3D scene into a 2D Raster
   – The scene composed of models (geometries) in 3D space.
     Models are composed of primitives, for example triangles, supported by
     the rendering system.
   – Models created manually or procedurally.
   – The 2D image is typically displayed on monitor, printed on a laser printer,
     or written to a raster in memory or a file.

 CS 536                           Xiaoyu Zhang, CSUSM                         10
                                Raster Image
•     3D models are rendered to 2D raster
•     A Raster image is a regular grid of
      sampling points.
       – Each sample is usually called a
         pixel, short for picture element.
       – a pixel provides a sampled
         representation of rendered
         geometric primitives.
       – Typically a pixel has color, alpha,
         depth and other information
•     Computer monitor is the most
      common raster display device

    CS 536                               Xiaoyu Zhang, CSUSM   11
                                 3D Primitives
•   Rendering is a process of computing the values of pixels. It can be done by
    software or hardware (Graphics card). Very computational intensive.

•   The rendering primitives typically supported in hardware are
         – Points
         – Line Segments
         – Polygons (often restricted to convex polygons).

•   Modeling primitives include these, but also
         – Smooth curves
         – Smooth surfaces
         – Other...

•   A software renderer, for example raytracer or Renderman, may support modeling
    primitives directly, or may convert into polygonal or linear approximations for
    hardware rendering.

CS 536                                    Xiaoyu Zhang, CSUSM                     12
                      Game Graphics
• Game graphics must be interactive, more than 20
  frames / sec
         – Hardware required
• Also want realistic visual
         – Trade-off between rendering quality and time
• Games and graphics hardware technology are
  pushing each other to higher levels
   – Faster, cheaper and programmable graphics
     hardware produce better rendering
   – More advanced rendering algorithms require
     better graphics hardware

CS 536                         Xiaoyu Zhang, CSUSM        13
             True-Color Frame Buffers

• The rasterized image is stored in the framebuffer that resides in the
  graphics memory.
    – The framebuffer contains color buffer, depth buffer, and
• Each pixel color requires at least 3 bytes. One byte for each primary color.
    – Each pixel can be one of 2^24 colors

 CS 536                             Xiaoyu Zhang, CSUSM                     14
                  Indexed Frame buffers

•     Each pixel uses one byte
•     Each byte is an index into a color map
•     Color-map Animations
•     Each pixel may be one of 2^24 colors, but only 256 color be displayed at a

    CS 536                         Xiaoyu Zhang, CSUSM                       15
                       Single Buffering Raster Displays

•     Display synchronized with CRT sweep
•     Update framebuffer while it’s being scanned
•     Generally, updates are visible. Flickering display.

    CS 536                                Xiaoyu Zhang, CSUSM   16
                 Double Buffering

• Adds a second frame buffer
• Update the back buffer. Updates are invisible
• Swaps the front and back buffers during vertical blanking

 CS 536                    Xiaoyu Zhang, CSUSM                17
                           Student Demo
• Each student will have a 10 - 15 minute “demo time”
         – Students pick a topic (e.g., a program, a game, a tool …) for the
             • Focus: game graphics, not game play or MPEG movies, etc.
             • Briefly talk about why you think it’s cool and interesting. How you
               may apply it to this course.
         – This is a major part of class participation
         – Students responsible for bringing platform to classroom if

         – Students rotate duty each class
             • Send around a sign-up sheet

CS 536                               Xiaoyu Zhang, CSUSM                         18
• We will use following tools / libraries in this
         – Visual Studio .Net
         – CVS – concurrent version control system
         – SDL – simple direct media layer

CS 536                      Xiaoyu Zhang, CSUSM      19
                     Graphics Pipeline
• Classically, ``model'' to ``scene'' to ``image'' conversion is
  broken into finer steps, called the graphics pipeline.
   – Part of the pipeline often implemented in graphics hardware to
     get interactive speeds.
    – A fragment refers to a pixel before it is written into the frame-buffer.

      Application                 Geometry                     Fragment
      Processing                  Processing                   Processing

          CPU                                           GPU
 CS 536                           Xiaoyu Zhang, CSUSM                        20
                                          Classic Graphics Pipeline
                                                                      Graphics State

                                  Vertex                        Assembly                             Pixel                    Video
                                Processor                     & Rasterization                      Processor                 Memory
                  Vertices                                                           Fragments
                    (3D)                                                            (pre-pixels)                Final       (Textures)
                                            transformed,                                                   (Color, Depth)
                                             Lit vertices
                                           projection to 2D
     CPU                                                                        GPU
•     Traditional (OpenGL) pipeline has fixed functions
•     Vertex processor
        –    Model/view transforms
        –    lighting are really applied to “vertices” not “primitives”, Making this the vertex pipeline
        – Projection to 2D Space
        – Clipping
•     Fragment (pixel) processor
        –    Texture mapping
        –    Per-pixel lighting
        –    Visibility test (Z-buffer)

    CS 536                                                    Xiaoyu Zhang, CSUSM                                                21

CS 536       Xiaoyu Zhang, CSUSM   22
               Modeling transformations
•    We start with 3-D models defined in
     their own model space

•    Modeling transformations orient
     models within a common coordinate
     frame called world space

•    All objects, light sources, and the viewer
     live in world space

•    Transformations are represented as

    CS 536                             Xiaoyu Zhang, CSUSM   23
             Viewing Transformation
• Another change of coordinate
• Maps points from world space
  into camera space
• Viewing position is transformed
  to the origin
• Viewing direction is oriented
  along some axis
• A viewing volume is defined for

 CS 536                       Xiaoyu Zhang, CSUSM   24
• The projection step maps all of 3-D
  objects onto our desired viewing
    – Greatly simplified by the fact that
      viewing transformations map the
      eye to the origin and the viewing
      direction to -z axis.
    – Projection transforms from 3D to 2D
      space. We should expect projection
      matrices to be less than full rank
    – Two types of projections: Parallel
      Projection and Perspective

 CS 536                            Xiaoyu Zhang, CSUSM   25
          Viewing Frustum and Clipping
• The right picture shows the view
  volume that is visible for a
  perspective projection window,
  called viewing frustum.
• It is determined by a near and far
  cutting planes and four other
• Anything outside of the frustum is
  not shown on the projected
  image, and doesn’t need to be
• The process of remove invisible
  objects from rendering is called
  clipping                                                  far

 CS 536                        Xiaoyu Zhang, CSUSM                26
          Assembly and Rasterization
• Transformations are applied to
  vertices. 3D scene is represented
  by primitives
• Vertices are assembled into
  primitives and rasterized into
  pixels (fragments).
• Rasterization is also called “scan
  conversion”, converts primitives
  into pixels.
    – Vertex attributes, e.g. color, are
      interpolated for the pixels.
• Further operations may be
  applied to fragments
   – Texture mapping
   – Visibility testing
   – Per pixel lighting

 CS 536                             Xiaoyu Zhang, CSUSM   27
                               Texture Mapping
•     Texture mapping is a technique to
      color a pixel using 2D images. It adds
      sophisticated color patterns to a
•     The texture lives in a 2D space
      –      Parameterize points in the texture
             with 2 coordinates: (s,t)
•     Texture mapping process looks up the
      color of a pixel from the 2D texture
      with the coordinates (s, t) of the

    CS 536                                   Xiaoyu Zhang, CSUSM   28
                   Pipeline Functionalities
              Application    vertex processor            pixel processor

         Handle input       Transform                 Rasterize (fill pixels)

                                                      Interpolate vertex
         Simulation & AI    Lighting
                                                      Texture mapping

                                                      Z- and stencil buffer
         LOD selection                                Blending


CS 536                          Xiaoyu Zhang, CSUSM                             29
                     Programmable Graphics Pipeline

                                                       Graphics State

                           Vertex               Assembly                               Pixel
                                                                                      Pixel                    Video
Application              Processor
                         Processor            & Rasterization                       Processor
                                                                                    Processor                 Memory
              Vertices                                                Fragments
                (3D)                                                 (pre-pixels)                Final       (Textures)
                                                                                            (Color, Depth)
                                      Lit Vertices

   CPU                                                           GPU                        Render-to-texture

    • Note:
                                                          • Here’s what’s cool:
           – Vertex processor does all
                                                                  – Can now program vertex
             transform and lighting
           – Pipe widths vary
                • Intra-GPU pipes wider than
                                                                  – Can now program pixel
                  CPUGPU pipe                                      processor!
                • Thin GPUCPU pipe

  CS 536                                       Xiaoyu Zhang, CSUSM                                                30
• Game programming is fun and challenging
• We will use OpenGL for game graphics
• Graphics pipeline is now programmable

CS 536            Xiaoyu Zhang, CSUSM       31

Shared By: