Docstoc

Slide 1 - IT-Universitetet i København

Document Sample
Slide 1 - IT-Universitetet i København Powered By Docstoc
					Game Engine Programming
          Monday
       September 15th

        Autumn 2008
                      Today
• Part I
   – Modeling primitives
   – Clipping
   – Representation of Objects
• Part II
   – Drawing in OpenGL
   – Object representation in C++
   – Programming Assignment 1 discussion
  Review: Graphics Pipeline
   Modeling
Transformations   • Modeling
                    – 3D models defined
 Illumination
                      in their own
   Clipping
                      coordinate system
                    – Orienting 3D models
View Projection       within the scene

Scan Conversion


    Display
  Review: Graphics Pipeline
   Modeling
Transformations   • Illumination
                    – Determining lighting
 Illumination
                      according to
   Clipping
                      material properties,
                      surface properties,
View Projection
                      and light sources
                    – Lighting model
Scan Conversion


    Display
  Review: Graphics Pipeline
   Modeling
Transformations   • Clipping
                    – Removing portions
 Illumination
                      of objects outside
   Clipping
                      the view volume

View Projection


Scan Conversion


    Display
  Review: Graphics Pipeline
   Modeling
Transformations   • View Projection
                    – Projected on a 2D
 Illumination
                      image plane (screen
   Clipping
                      space)

View Projection


Scan Conversion


    Display
  Review: Graphics Pipeline
   Modeling
Transformations   • Scan Conversion
                    – Rasterizing objects
 Illumination
                      into pixels.
   Clipping         – Determining color,
                      depth, etc.
View Projection


Scan Conversion


    Display
         Display Primitives and
             Rasterization
• Primitives
  – Points
  – Lines
  – Triangles
Rasterization (scan conversion) is the process of
  converting primitives to pixels.
               Rasterization
• Framebuffer model (2D array of picture
  elements)
• Efficient algorithms for finding approximate
  samples for objects on screen
           Line Rasterization
• Bresenham’s algorithm (DDA)
       Bresenham’s algorithm




F(x,y) < 0, when line < midpoint
F(x,y) > 0, when line > midpoint
Bresenham’s algorithm
              Drawing Circles
• Circle Equation
                 f(x,y) = X2+Y2 = r2
                    Clipping
Assumption: Draw everything in the scene
Reality: Some things are fully/partially cut off

  View
  Plane
                   Clipping
• Clipping: Calculating what is (and is not) in the
  viewport.
                     Clipping

• Used to identify which parts of drawing
  primitives need to be projected on the view
  plane.
  – Portion of primitives that lie inside the view
    volume.
• Not drawing stuff behind the eye, hidden-
  surface removal, shadows, efficiency.
            Clipping algorithm
• Naïve approach:
  for each line segment
      for each edge of viewport
          find intersection points
          pick “nearest” point
      if anything is left, draw it

• Is this good enough ?
• How can we improve this ?
                 Improvements
• Check to see if end points of line segments are
  in or out of the viewport

                 xmin           xmax

          ymax




          ymin
                Improvements
• Accept if …

• Reject if …

• Can we do better ?
  – Hasn’t someone already thought of this ?
   Cohen-Sutherland’s algorithm
• Divide viewport into different regions
  – Assign each region a code

           1001xmin     1000    xmax
                                       1010
          ymax



           0001         0000           0010

          ymin
           0101         0100       0110
   Cohen-Sutherland’s algorithm
• Determine bits through simple tests
  – x < xmax
  – Assign codes to each end point of a line
  – Accept line if both codes are 0, reject if &
    operation is non-zero.
  – If one is inside, one is outside
     • Pick an edge of the viewport that intersects the line
     • Find intersection point


  Are we done ?
            What about this ?
                                              p2
     1001            1000            1010



    0001            0000               0010


       p1

     0101           0100               0110




Calculate code sequence for this example
What about this ?

                    p2




p1
Break
Illumination
                             Colour

– Colour of a vertex in OpenGL can be set explicitly
  with RGB (or RGBA) values using
          glColor4f(r, g, b, a), r=red, g=green, b=blue, a=alpha
– Alpha value determines transparency and is
  enabled only when blending is enabled.
          glEnable(GL_BLEND)
– Blend function must be chosen to describe how to
  blend colours of overlapping geometry

Chapter 6 in OGL book
            Colour Terminology
•   Hue
•   Saturation
•   Brightness
•   Additive/Subtractive models
    – RGB, CMYK
• Gamut
       Illumination and Shading
• Methods that attempt to simulate interaction
  of light with rendered objects.
  – Simple approximations
  – Physically based models
• In OpenGL illumination comes from three
  sources:
  – Ambient
  – Diffuse
  – Specular
Illumination and Shading
             Ambient Lighting
• Background lighting due to scattering of
  emitted light over different surfaces.
  – Amount of light from an emitter (source)
  – Amount of light present in the scene independent
    of the light sources
  – Percentage of ambient light each polygon reflects
    (ambient reflection co-efficient)
• Intensity is independent of direction to light
  source and viewer.
            Ambient Lighting
• Flat look
• Actual calculations are
  complicated so a simple
  approximation is used
Directional Light Sources
              Diffuse Lighting
• Diffuse surface reflects incident light equally in
  all directions.
• Intensity is proportional to cosine of the
  incident light angle.
• If light source is at infinity…
• Intensity of light at any point on a polygon is
  computed as:
        I = Ipkd(N.L)
              Lambert’s Law
• Ideal diffuse reflectors follow Lambert’s law

• Law : reflected energy from a small surface
  area in a particular direction is proportional
  to the cosine of the angle between that
  direction and the surface normal
           Specular Reflection
• Light reflected from shiny objects to form
  highlights
• Depends on direction of light source and
  direction to viewer
• Behavior is according to Snell/Descarte’s laws
Summary
                References
• Leonard McMillan’s MIT ECE 6.837 class notes
  from Fall 98
• David Luebeck’s lecture notes from CS 445,
  2003 at UVA
• Chris Healey’s notes from CSC562 at NCSU