Lighting Models - PowerPoint PPT Presentation

1 / 83
About This Presentation
Title:

Lighting Models

Description:

perform RGB transformation on bitmap. for (int iy = 0; iy temp_text.height; iy ... extract pixel from lightmap bitmap. USHORT lpixel = lbuffer[iy*temp_text. ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 84
Provided by: BMA2
Category:

less

Transcript and Presenter's Notes

Title: Lighting Models


1
Lighting Models
  • CIS 488/588
  • Bruce R. Maxim
  • UM-Dearborn

2
Lighting
  • Huge topic in computer graphics
  • Nearly all 3D rendering packages make use of ray
    tracing to determine the shadows and reflective
    surfaces in the screen images
  • Software-based graphics engines cannot support
    the same complex lighting models that hardware
    (e.g. pixel and vertex shaders)

3
Color Models
  • Color can be represented using separate RGB
    values or using an index into a table
  • Some RGB models use integers 0-255 for each color
    and some use real numbers with values 0.0-1.0 for
    each
  • Float values are easier to work with
    mathematically, but integers can be faster for
    rendering since they can be written straight to
    the video buffer

4
Color Addition
  • When adding two RGB integer colors you need to
    catch overflow problems
  • C1 (r1, g1, b1)
  • C2 (r2, g2, b2)
  • C C1 C2
  • C (max(r1 r2, 255),
  • max(r1 r2, 255),
  • max(r1 r2, 255))

5
Color Modulation
  • Color modulation is basically multiplication
  • Can be done using a real scalar (brightness
    modulation) or by multiplying the RGB values in a
    pairwise manner
  • C1 (r1, g1, b1)
  • C2 (r2, g2, b2)
  • C s C1 (s r1, s g1, s b1)
  • C C1 C2 (r1 r2, g1 g2, b1 b2)
  • Again, we need to beware of overflow

6
Demo8_1
  • // copy texture into temp for rendering and
    scaling
  • Copy_Bitmap(temp_text,0,0, texturescurr_textur
    e,0,0,TEXTSIZE, TEXTSIZE)
  •  
  • ///////////////////////////////////////////
  • // our little image processing algorithm )
  • //Cmodulated sC1 (sr1, sg1, sb1)
  •  
  • USHORT pbuffer (USHORT )temp_text.buffer
  •  

7
Demo8_1
  • // perform RGB transformation on bitmap
  • for (int iy 0 iy lt temp_text.height iy)
  • for (int ix 0 ix lt temp_text.width ix)
  • USHORT pixel pbufferiytemp_text.width
    ix
  • int ri,gi,bi // used to extract the rgb
    values
  • _RGB565FROM16BIT(pixel, ri,gi,bi)
  • // perform scaling operation and test for
    overflow
  • if ((ri (float)ri scalef) gt 255)
    ri255
  • if ((gi (float)gi scalef) gt 255)
    gi255
  • if ((bi (float)bi scalef) gt 255)
    bi255
  • // rebuild RGB and test for overflow and
    copy to buffer
  • pbufferiytemp_text.width ix
    _RGB16BIT565(ri,gi,bi)
  • // end for ix
  • // draw texture
  • Draw_Bitmap16(temp_text, back_buffer,
    back_lpitch,0)

8
Light Modulation
  • Pixel Shading plus Modulation (Demo8_2)
  • Pixel_destxyrgb pixel_sourcexyrgb
    ambient
  • pixel_sourcexyrgb light_mapxyrgb
  • Pixel Shading plus Modulation (Demo8_2b)
  • Pixel_destxyrgb pixel_sourcexyrgb
    ambient
  • light_mapxyrgb
  • Please note these are done separately for each
    RGB value

9
Demo8_2
  • // Pixel_destx,yrgb pixel_sourcex,yrgb
    ambient
  • // pixel_sourcex,yrgb
    light_mapx,yrgb
  • USHORT sbuffer (USHORT )texturescurr_texture
    .buffer
  • USHORT lbuffer (USHORT )lightmapscurr_lightma
    p.buffer
  • USHORT tbuffer (USHORT )temp_text.buffer 
  • // perform RGB transformation on bitmap
  • for (int iy 0 iy lt temp_text.height iy)
  • for (int ix 0 ix lt temp_text.width ix)
  • int rs,gs,bs // used to extract the source
    rgb values
  • int rl, gl, bl // light map rgb values
  • int rf,gf,bf // the final rgb terms
  • // extract pixel from source bitmap
  • USHORT spixel sbufferiytemp_text.width
    ix 

10
Demo8_2
  • // extract RGB value
  • _RGB565FROM16BIT(spixel, rs,gs,bs)
  • // extract pixel from lightmap bitmap
  • USHORT lpixel lbufferiytemp_text.width
    ix
  • // extract RGB values
  • _RGB565FROM16BIT(lpixel, rl,gl,bl)
  • // ambient base texture term modulation term
  • rf ( scalef (float)rs )
  • ( (float)rs(float)rl/(float)64 )
  • gf ( scalef (float)gs )
  • ( (float)gs(float)rl/(float)64 )
  • bf ( scalef (float)bs )
  • ( (float)bs(float)rl/(float)64 )
  •  

11
Demo8_2
  • // test for overflow
  • if (rf gt 255) rf255
  • if (gf gt 255) gf255
  • if (bf gt 255) bf255
  • // rebuild RGB and test for overflow and write
    back to buffer
  • tbufferiytemp_text.width ix
    _RGB16BIT565(rf,gf,bf)
  • // end for ix
  • // draw textures
  • Draw_Bitmap16(temp_text, back_buffer,
    back_lpitch,0)
  • Draw_Bitmap16
  • (texturescurr_texture, back_buffer,
    back_lpitch,0)
  • Draw_Bitmap16
  • (lightmapscurr_lightmap, back_buffer,
    back_lpitch,0)

12
Demo8_2b
  • // Pixel_destx,yrgb pixel_sourcex,yrgb
    ambient
  • // light_mapx,yrgb
  • USHORT sbuffer (USHORT )texturescurr_texture
    .buffer
  • USHORT lbuffer (USHORT )lightmapscurr_lightma
    p.buffer
  • USHORT tbuffer (USHORT )temp_text.buffer
  • // perform RGB transformation on bitmap
  • for (int iy 0 iy lt temp_text.height iy)
  • for (int ix 0 ix lt temp_text.width ix)
  • int rs,gs,bs // used to extract the source
    rgb values
  • int rl, gl, bl // light map rgb values
  • int rf,gf,bf // the final rgb terms
  • // extract pixel from source bitmap
  • USHORT spixel sbufferiytemp_text.width
    ix

13
Demo8_2b
  •   // extract RGB values
  • _RGB565FROM16BIT(spixel, rs,gs,bs) 
  • // extract pixel from lightmap bitmap
  • USHORT lpixel lbufferiytemp_text.width
    ix
  •   // extract RGB values
  • _RGB565FROM16BIT(lpixel, rl,gl,bl)
  •   // simple formula base scale lightmap
  • rf ( scalef (float)rl ) ( (float)rs )
  • gf ( scalef (float)gl ) ( (float)gs )
  • bf ( scalef (float)bl ) ( (float)bs )
  •   // test for overflow
  • if (rf gt 255) rf255
  • if (gf gt 255) gf255
  • if (bf gt 255) bf255

14
Demo8_2b
  •   // rebuild RGB and test for overflow and write
    back to buffer
  • tbufferiytemp_text.width ix
    _RGB16BIT565(rf,gf,bf)
  • // end for ix
  • // draw textures
  • Draw_Bitmap16(temp_text, back_buffer,
    back_lpitch,0)
  • Draw_Bitmap16
  • (texturescurr_texture, back_buffer,
    back_lpitch,0)
  • Draw_Bitmap16
  • (lightmapscurr_lightmap, back_buffer,
    back_lpitch,0)

15
Alpha Blending
  • Process of adding two or more pixel colors
    together to arrive at a destination pixel
  • Blending means that percentages of each source
    are used to arrive a final pixel
  • Used to simulate transparency and shadows
  • Basic Two-Source Alpha Blending
  • Pixel_destxyrgb alpha pixel_source1xyr
    gb
  • (1 alpha) pixel_source2xyrgb

16
Demo8_3
  • //Pixel_destxyrgb alpha
    pixel_source1xyrgb
  • // (1-alpha)
    pixel_source2xyrgb
  • USHORT s1buffer (USHORT )textures1curr_textur
    e1.buffer
  • USHORT s2buffer (USHORT )textures2curr_textur
    e2.buffer
  • USHORT tbuffer (USHORT )temp_text.buffer 
  • // perform RGB transformation on bitmap
  • for (int iy 0 iy lt temp_text.height iy)
  • for (int ix 0 ix lt temp_text.width ix)
  • int rs1,gs1,bs1 // used to extract the
    source rgb values
  • int rs2, gs2, bs2 // light map rgb values
  • int rf,gf,bf // the final rgb terms
  • // extract pixel from source bitmap
  • USHORT s1pixel s1bufferiytemp_text.width
    ix

17
Demo8_3
  •   // extract RGB values
  • _RGB565FROM16BIT(s1pixel, rs1,gs1,bs1)
  • // extract pixel from lightmap bitmap
  • USHORT s2pixel s2bufferiytemp_text.width
    ix
  • // extract RGB values
  • _RGB565FROM16BIT(s2pixel, rs2,gs2,bs2)
  •   // alpha blend them
  • rf (alphaf (float)rs1 ) ((1 - alphaf)
    (float)rs2 )
  • gf (alphaf (float)gs1 ) ((1 - alphaf)
    (float)gs2 )
  • bf (alphaf (float)bs1 ) ((1 - alphaf)
    (float)bs2 )
  •   // test for overflow
  • if (rf gt 255) rf255
  • if (gf gt 255) gf255
  • if (bf gt 255) bf255

18
Demo8_3
  •   // rebuild RGB and test for overflow copy to
    buffer
  • tbufferiytemp_text.width ix
    _RGB16BIT565(rf,gf,bf)
  • // end for ix
  • // draw textures
  • Draw_Bitmap16(temp_text, back_buffer,
    back_lpitch,0)
  • Draw_Bitmap16
  • (textures1curr_texture1, back_buffer,
    back_lpitch,0)
  • Draw_Bitmap16
  • (textures2curr_texture2, back_buffer,
    back_lpitch,0)

19
Ambient Light
  • This is the light that is all around us with no
    specific light source
  • Ambient lighting can be modeled using an additive
    tem that is the product of and ambient reflective
    surface color and the ambient light intensity
    (use floating point RGB values)
  • Itotala Csambient Iambient

20
Diffuse Light - 1
  • The light that is scattered off an object due to
    a light source and the reflectivity of the
    surface itself
  • Diffuse light is irrelevant to viewer position
  • Intensity of Diffuse Light as a Function of
    Angle Between Surface Normal and Incident Light
    Source Vectors
  • I (n l) cos ?

21
Diffuse Lighting Models
  • Single Source Diffuse
  • Itotald Rsdiffuse Idiffuse (n l)
  • Single-Source Ambient and Diffuse
  • Itotalad Rsdiffuse Idiffuse Rsdiffuse
    Idiffuse (n l)
  • Ambient and Diffuse for Multiple Sources
  • Itotalad Rsdiffuse Idiffuse Rsdiffuse ?
    I(i)diffuse (ni li)

22
Specular Lighting
  • Objects the seem to be shiny reflections of the
    light sources themselves
  • Caused by lots of microfacets on the surface have
    the same orientations
  • Related to the light source position (l), surface
    normal (n), viewer position (v), and reflection
    (r) vectors

23
Specular Lighting Models
  • Single Source Specular
  • Itotals Rsspecular Ispecular max(r v,
    0)sp
  • Single-Source Specular with Source Test
  • Itotals Rsspecular Ispecular
  • max(r v, 0)sp (n l) gt 0 ? 10
  • Specular for Multiple Sources
  • Itotals Rsspecular
  • ? I(i)specular max(ri vi, 0)sp
    (ni li) gt 0 ? 10

24
Emissive Lighting
  • Easiest lighting model to implement
  • It is the surface self-illumination
  • Could almost be thought of as a light source that
    does not illuminate other objects
  • Itotale Rsemission

25
Total Lighting Equation
  • Single Pixel with Multiple Light Sources
  • Itotalaeds Rsambient Iambient Rsemission
  • Rsdiffuse ? I(i)diffuse (ni li)
  • Rsspecular
  • ? I(i)specular max(ri vi, 0)sp (ni
    li) gt 0 ? 10

26
Directional Lights
  • Lights so far away that all the light rays that
    strike the surface are parallel (also known as
    infinite lights)
  • The intensity is always the same as the initial
    intensity and color independent of distance
  • I(d)dir I0dir Cldir

27
Point Lights
  • Modeled as single points in 3D space
  • Falloff (attenuation) based on distance from
    light is modeled with constants (kc, kl, kq)
  • Model is (kq can be set to zero)
  • d p s
  • p point light source
  • s point on surface
  • I(d)point I0point Clpoint / (kc kl d kq
    d2)

28
Spotlights
  • Extremely expensive computationally
  • Spotlight at position p, with direction l,
    strikes surface point s
  • The angle ? to define region known as the umbra
    and angle ? to define the region known as the
    penumbra

29
(No Transcript)
30
Spotlight Cases 1 2
  • Case 1 If ? gt ? then dont light the point
  • I(d)spotlight 0
  • Case 2 If ? gt ? then inside umbra
  • d p s
  • I(d)spotlight I0spotlight Clspotlight / (kc
    kl d kq d2)

31
Spotlight Case 3
  • Case 3 If ?/2 lt ? lt ?/2 then outside umbra and
    inside penumbra
  • d p s
  • pf power factor
  • I(d)spotlight I0spotlight Clspotlight
    max((l s),0)pf /
  • (kc kl d kq d2)
  • Can be simplified to
  • I(d)spotlight I0spotlight Clspotlight / (kc
    kl d kq d2)
  • (cos ? - cos ?/2 )pf / (cos ?/2
    - cos ?/2 )

32
New 3D Pipeline
  • Object
  • Local to World
  • Object Removal
  • Back-face Removal
  • Lighting
  • World to Camera
  • Camera to Perspective
  • Perspective to Screen
  • Rasterize

33
Support for Materials
  • PLG/PLX format does not have any support for
    materials (textures)
  • The graphics engine needed to have a number of
    materials that can applied to polygons during
    rendering by looking up a material ID or pointer
    to use access the material

34
Initializing Material Library
  • int Reset_Materials_MATV1(void)
  • // this function resets all the materials
  • static int first_time 1 
  • // if this is the first time then zero
    EVERYTHING out
  • if (first_time)
  • memset(materials, 0, MAX_MATERIALSsizeof(MATV1)
    )
  • first_time 0
  • // end if

35
Initializing Material Library
  • // scan thru materials and release all textures,
    if any?
  • for (int curr_matt 0 curr_matt lt
    MAX_MATERIALS curr_matt)
  • // regardless if the material is active check
    to see if there // is a dangling texture map
  • Destroy_Bitmap(materialscurr_matt.texture)
  • // now it's safe to zero memory out
  • memset(materialscurr_matt, 0,
    sizeof(MATV1))
  • // end if
  • return(1)
  • // end Reset_Materials_MATV1
  • Functions to access the material library will
    come later

36
Initializing All Lights to Off
  • int Reset_Lights_LIGHTV1(void)
  • // function simply resets all lights in the
    system
  • static int first_time 1
  •  memset(lights, 0, MAX_LIGHTSsizeof(LIGHTV1))
  • // reset number of lights
  • num_lights 0
  • // reset first time
  • first_time 0
  • // return success
  • return(1)
  • // end Reset_Lights_LIGHTV1

37
Creating Lights
  • int Init_Light_LIGHTV1(int index,// index to
    (0..MAX_LIGHTS-1)
  • int _state, // state of
    light
  • int _attr, // type of light
  • RGBAV1 _c_ambient, // ambient light
    intensity
  • RGBAV1 _c_diffuse, // diffuse light
    intensity
  • RGBAV1 _c_specular, // specular
    light intensity
  • POINT4D_PTR _pos, // position of
    light
  • VECTOR4D_PTR _dir, // direction of
    light
  • float _kc, // attenuation
    factors
  • float _kl,
  • float _kq,
  • float _spot_inner, // inner angle
    for spotlight
  • float _spot_outer, // outer angle
    for spotlight
  • float _pf) // power factor
    spotlights

38
Creating Lights
  • // this function initializes a light based on
    the flags sent
  • // in _attr, values that aren't needed are set
    to 0 by caller
  • // make sure light is in range
  • if (index lt 0 index gt MAX_LIGHTS)return(0)
  • // all good, initialize the light (many fields
    may be dead)
  • lightsindex.state _state //
    state of light
  • lightsindex.id index // id
    of light
  • lightsindex.attr _attr // type
    of light
  • lightsindex.c_ambient _c_ambient //
    ambient light
  • lightsindex.c_diffuse _c_diffuse //
    diffuse light
  • lightsindex.c_specular _c_specular //
    specular light 
  • lightsindex.kc _kc //
    attenuation
  • lightsindex.kl _kl
  • lightsindex.kq _kq

39
Creating Lights
  • if (_pos)
  • VECTOR4D_COPY(lightsindex.pos, _pos)
  • if (_dir)
  • VECTOR4D_COPY(lightsindex.dir, _dir)
  • // normalize it
  • VECTOR4D_Normalize(lightsindex.dir)
  • // end if
  • lightsindex.spot_inner _spot_inner //
    inner angle spot
  • lightsindex.spot_outer _spot_outer //
    outer angle spot
  • lightsindex.pf _pf //
    power factor
  • // return light index as success
  • return(index)
  • // end Create_Light_LIGHTV1

40
Creating Ambient Light
  • ambient_light Init_Light_LIGHTV1(0, // 0
    for ambient
  • LIGHTV1_STATE_ON, //
    turn on
  • LIGHTV1_ATTR_AMBIENT, //
    light type
  • _RGBA32BOT(255,255,255,0), //
    pure white
  • 0, 0, NULL, NULL, // NA
    for ambient
  • 0, 0, 0, 0, 0, 0)
  • Once created changing state can be done globally
  • Lightsambient_light.state LIGHTV1_STATE_OFF

41
Creating Directional Light
  • VECTOR4D sun_dir 0, -1, 0, 0
  • sun_light Init_Light_LIGHTV1(1, // 1
    for sunlight
  • LIGHTV1_STATE_ON, //
    turn on
  • LIGHTV1_ATTR_INFINITE, //
    light type
  • 0, _RGBA32BOT(255,255,0 ,0), //
    pure yellow
  • 0, NULL, sun_dir, //
    dir (0,-1,0)
  • 0, 0, 0, 0, 0, 0) // NA

42
Creating Point Light
  • VECTOR4D sun_pos 0, 10000, 0, 0
  • sun_light Init_Light_LIGHTV1(1, // 1
    for sunlight
  • LIGHTV1_STATE_ON, //
    turn on
  • LIGHTV1_ATTR_POINT, //
    light type
  • 0, _RGBA32BOT(255,255,0 ,0), //
    pure yellow
  • 0, sun_pos, NULL, //
    pos on y-axis
  • 0.0f, 0.001f, 0.0f, //
    linear atten
  • 0.0f, 0.0f, 0.0f) // NA

43
Creating Spotlight
  • VECTOR4D spot_pos 1000, 1000, -1000, 0
  • VECTOR4D spot_dir -1, -1, 1, 0
  • float umbra 30
  • float penumbra 60
  • falloff 1.0
  • sun_light Init_Light_LIGHTV1(5, //
    5 for spot
  • LIGHTV1_STATE_ON, //
    turn on
  • LIGHTV1_SPOTLIGHT, //
    light type
  • 0, _RGBA32BOT(128,128,128,0), //
    50 white
  • 0, sun_pos, sun_dir, //
    pos dir
  • 0.0f, 0.0f, 0.0f, //
    NA
  • umbra, penumbra, falloff) //
    spot info

44
Shading
  • 8-bit modes are 3 times faster than 16-bit
  • 16-bit modes are easier to track since there are
    11 mapping of intensities in RGB space to pixels
  • If you have blue material and red ambient light
    nothing will be visible in RGB space
  • (0,0,255) (1.0,0,0) (0,0,0)
  • Easier to assume white ambient light
  • (0,0,255) (1.0,1.0,1.0) (0,0,255)

45
Indexed Mode Lighting
  • We to convert RGB values into color indices and
    then write those values to the screen
  • We will need to do a closest-match scan of the
    color pallet and select the one that comes
    closest (similar to hashing/lookup)
  • As a good compromise use a 16-bit look up table
    (0-65535) translate 16-bit to 8-bit color pallet
    (0-255)
  • final_pixel_index rgblookupfinal_pixelrgb16

46
Simplified Intensity Model
  • The expensive part of shading is calculation of
    the final RGB value
  • If we ignore light color and material color we
    can simplify the model a great deal
  • We can create a a table that has 256 rows (one
    for each pallet color) and each row has 256
    entries (the best matches for each intensity
    value)
  • We write a shader translation lookup is
  • Rgbintensityloopupcolorintensity

47
Color Intensity Algorithm - 1
  • Given color c RGB(50,20,100)
  • c RGB(red,green,blue)
  • Step 1 Compute largest component value
  • for this example blue 100
  • Step 2 Set maximum component to 255 and
  • compute ratio
  • blue100, blue 255, ratio 255/100 2.55

48
Color Intensity Algorithm - 2
  • Step 3 scale all other channels by ratio
  • red red ratio 50 2.55 127.5
  • green green ratio 20 2.55 51.0
    blue blue ratio 100 2.55 255
  • c RGB(128,51,255)

49
Indexed_Intensity_Table_Builder
  • // these look up tables are used by the 8-bit
    lighting engine
  • // the first one holds a color translation table
    in the form of each
  • // row is a color 0..255, and each row consists
    of 256 shades of that
  • // color the data in each row is the
    color/intensity indices and the
  • // resulting value is an 8-bit index into the
    real color lookup that
  • // should be used as the color
  •  
  • // the second table works by each index being a
    compressed 16bit RG
  • // value the data indexed by that RGB value IS
    the index 0..255 of
  • // the real color lookup that matches the desired
    color the closest
  •  
  • UCHAR rgbilookup256256 // intensity RGB
    8-bit lookup storage
  • UCHAR rgblookup65536 // RGB 8-bit color
    lookup

50
Indexed_Intensity_Table_Builder
  • int RGB_16_8_Indexed_Intensity_Table_Builder
  • (LPPALETTEENTRY src_palette, // source
    palette
  • UCHAR rgbilookup256256, // lookup table
  • int intensity_normalization)
  • // this function takes the source palette to
    compute the intensity
  • // shading table with the table will be formatted
    such that each row
  • // is a color index, and each column is the shade
    0..255 desired, the
  • // output is a single byte index in either case,
    it's up to the caller
  • // to send in the rgbilookup table pre-allocated
    64k buffer byte
  • // 256256the function doesn't allocate memory
    for the caller
  • // the function builds the table by looping thru
    each color in the
  • // color palette and then for each color, it
    scales the color to
  • // maximum intensity without overflow the RGB
    channels and then uses
  • // this as the 100 intensity value of the color,
    then the algorithm
  • // computes the 256 shades of the color, and then
    uses the standard
  • // least squares scan the find the colors in the
    palette and stores
  • // them in the row of the current color under
    intensity translation

51
Constant Shading
  • Simplest shading model
  • Does not take anything into account (no ambient
    light, no diffuse light, etc.)
  • Polygons will be drawn with solid color based on
    the stored index or RGB value stored for each
    polygon
  • In Demo8_4 this is done by replacing the
    wireframe rendering call with Draw_RENDERLIST4DV1_
    Solid16

52
Demo8_4
  • void Draw_RENDERLIST4DV1_Solid16
  • (RENDERLIST4DV1_PTR rend_list,
  • UCHAR video_buffer, int lpitch)
  • // this function "executes" the render list or
    in other words
  • // draws all the faces in the list in wire frame
    16bit mode
  • // note there is no need to sort wire frame
    polygons, but
  • // later we will need to, so hidden surfaces
    stay hidden
  • // also, we leave it to the function to
    determine the
  • // bitdepth and call the correct rasterizer
  •  
  • // at this point, all we have is a list of
    polygons and it's
  • // time to draw them

53
Demo8_4
  • for (int poly0 poly lt rend_list-gtnum_polys
    poly)
  • // render this polygon if and only if it's not
  • // clipped, not culled, active, and visible
  • if (!(rend_list-gtpoly_ptrspoly-gtstate
  • POLY4DV1_STATE_ACTIVE)
  • (rend_list-gtpoly_ptrspoly-gtstate
  • POLY4DV1_STATE_CLIPPED )
  • (rend_list-gtpoly_ptrspoly-gtstate
  • POLY4DV1_STATE_BACKFACE) )
  • continue // move onto next poly

54
Demo8_4
  • // draw the triangle
  • Draw_Triangle_2D16(rend_list-gtpoly_ptrspoly-gt
    tvlist0.x,
  • rend_list-gtpoly_ptrspoly-gt
    tvlist0.y,
  • rend_list-gtpoly_ptrspoly-gttvlist
    1.x,
  • rend_list-gtpoly_ptrspoly-gt
    tvlist1.y,
  • rend_list-gtpoly_ptrspoly-gttvlist2.
    x,
  • rend_list-gtpoly_ptrspoly-gt
    tvlist2.y,
  • rend_list-gtpoly_ptrspoly-gt
    color,
  • video_buffer, lpitch)
  • // end for poly
  • // end Draw_RENDERLIST4DV1_Solid16

55
Flat Shading
  • Sometimes known as faceted shading
  • Shading entire polygon based on the light hitting
    a single pixel
  • Assume entire polygon is made of single material
  • Works best for surfaces made of flat (not
    spherical) surfaces

56
Lighting Objects
  • For now LaMothe only wants to light objects and
    rendering lists
  • We need to be able to save the original color of
    each polygon without rewriting our graphics
    engine completely
  • Answer is, since polygon color is stored in
    32-bit integer and we only need 16-bits for color
    the upper 16-bits will be shaded color lower
    16-bits will be original color

57
New Insertion Function
  • int Insert_OBJECT4DV2_RENDERLIST4DV12
  • (RENDERLIST4DV2_PTR rend_list,
  • OBJECT4DV2_PTR obj,int insert_local0)
  • // converts the entire object into a face list
    and then
  • // inserts the visible, active, non-clipped,
    non-culled
  • // polygons into the render list, also note the
    flag
  • // insert_local control whether or not the
    vlist_local or
  • // vlist_trans vertex list is used, thus you can
    insert an
  • // object "raw" totally untransformed if you set
    insert_local
  • // to 1, default is 0, that is you would only
    insert an object
  • // after at least the local to world transform
    the last
  • // parameter is used to control if their has been
    a lighting
  • // step that has generated a light value stored
    in the upper
  • // 16-bits of color, if lighting_on 1 then this
    value is
  • // used to overwrite the base color of the
    polygon when its
  • // sent to the rendering list

58
New Insertion Function
  • // test if we should overwrite color with
    upper 16-bits
  • if (lighting_on1)
  • // fix color upc
  • curr_poly-gtcolor (int)(base_color
    0xffff)
  • // end if

59
New Insertion Function
  • // test if we should overwrite color with upper
  • // 16-bits
  • if (lighting_on1)
  • // save color for a sec
  • base_color (unsigned int)(curr_poly-gtcolor)
  • curr_poly-gtcolor (int)(base_color gtgt 16)
  • // end if

60
Flat Shading 16-Bit Mode
  • Polygons with constant shading pass through
    lighting model without changes
  • Polygons with flat shading bit set will be
    affected by ambient, infinite, point, and spot
    lights in the light list
  • 1. Compute each surface normal
  • 2. For each light in list compute light intensity
  • 3. Sum results
  • 4. Write results to upper 16-bits

61
Lighting Function
  • int Light_OBJECT4DV1_World16
  • (OBJECT4DV1_PTR obj, // object to
    process
  • CAM4DV1_PTR cam, // camera position
  • LIGHTV1_PTR lights, // light list
    (might have more than one)
  • int max_lights) // maximum lights
    in list
  • // 16-bit version of function
  • // function lights an object based on the sent
    lights and camera. The
  • // function supports constant/pure shading
    (emmisive), flat shading
  • // with ambient, infinite, point lights, and spot
    lights
  • // note that this lighting function is rather
    brute force and simply
  • // follows the math, however there are some
    clever integer operations
  • // that are used in scale 256 rather than going
    to floating point

62
Lighting Function
  • Basically a straightforward implementation of the
    chapter material
  • Supports two different types of spotlights
  • Whatever lights are in the database will be
    summed up and converted back to the pixel format
    (5.6.5 or 5.5.5) desired
  • The pixel value is stored in the upper 16-bits
    of the polygon color field

63
Lighting a Renderlist
  • int Light_RENDERLIST4DV1_World16
  • (RENDERLIST4DV1_PTR rend_list, // list to
    process
  • CAM4DV1_PTR cam, // camera position
  • LIGHTV1_PTR lights, // light list
  • int max_lights) // maximum lights in list
  • // 16-bit version of function
  • // function lights the enture rendering list
    based on the
  • // sent lights and camera. the function supports
  • // constant/pure shading (emmisive), flat shading
    with
  • // ambient, infinite, point lights, and spot
    lights

64
Demo8_5
  • Allows you to experiment with different lighting
    schemes for the scene
  • When demo starts all lights are enabled
  • The vertical stabilizers on the tank use emissive
    colors (lights that dont emit light)

65
8-bit Flat Shading
  • Everything is 8-bit and from a single pallet and
    pixel formats wont matter
  • The basic strategy is to
  • Obtain the color index of polygon
  • Lookup the RGB values from the system palette
  • Use the palette values for the lighting
    calculations
  • Convert final RGB values to 5.6.5 encoded index
  • Use the new index into a table containing the
    index of the closest match in the system palette

66
Demo8_5_8b
  • This version makes use of 8-bit lighting and
    required the creation of 8-bit object and
    rendering list lighting functions
  • You need to use a palette that has a small color
    space and a large intensity space to avoid
    mapping fuchsia to brown
  • This likely means your art will appear tinted
    using similar colors (8 to 16 shades of the 16
    colors chosen)

67
Gouraud Shading
  • Shading the entire polygon using the color for a
    single pixel makes an object appear faceted
  • For objects like spheres (that are most
    definitely not faceted) this looks unrealistic
  • For Gouraud shading the color of the polygon
    color is determined by interpolation of its
    vertex colors and intensities
  • Shared vertices have color/intensity computed by
    averaging surface normals of the neighboring
    polygons

68
Gouraud Problems
  • Gouraud shading is the defacto standard for 99
    of all graphics accelerators
  • Shading occurs when polygon is rasterized this
    yields some perspective distortion
  • The interpolation algorithm can costly when used
    during rasterization
  • Interpolation is less of a problem if done during
    texture mapping

69
Reducing Problem Impact
  • You dont need to computer color intensities at
    each vertex
  • Vertex color intensities can be manually assigned
  • Basic strategy will be to pick a color for the
    left of the current scan line and the right side
    of the current scan line and interpolate smoothly
    between the two colors as the scan line is
    rendered

70
Phong Shading
  • More realistic than Gouraud
  • Reduces the impact of the perspective problem
  • Picks up specular effects much better
  • Phong computes the shading at every pixel
  • Phong interpolates the normal vectors across the
    polygon in screen space and then lights the pixel
    based on the interpolated normal

71
Phong Problems
  • Cant be done in software and is difficult to do
    using hardware
  • LaMothe will not be using it in this book

72
Visibility
  • Visibility concerned with whether a polygon
    will be visible in the rendered scene
  • Rendering order determines the order in which
    polygons are given to renderer
  • It is possible to cull polygons from the geometry
    by determining which polygons will not be visible
    and given the renderer only an ordered list of
    visible polygons to process

73
(No Transcript)
74
Painters Algorithm
  • A painter paints the background first and then
    covers it up with the foreground objects
  • To accomplish this during rendering requires
    knowing the z-values and drawing them back to
    front after sorting based on descending ordering
    of z-values
  • Painters algorithm has problems with long,
    overlapping, and concave polygons

75
0
76
Sort Implementation - 1
  • void Sort_RENDERLIST4DV1(RENDERLIST4DV1_PTR
    rend_list, int sort_method)
  • // this function sorts the rendering list based
    on the polygon
  • // z-values the specific sorting method is
    controlled by sending in
  • // control flags qsort is built-in quick sort
  • // define SORT_POLYLIST_AVGZ 0 - sorts on
    average of all vertices
  • // define SORT_POLYLIST_NEARZ 1 - sorts on
    closest poly z vertex
  • // define SORT_POLYLIST_FARZ 2 - sorts on
    farthest poly z vertex
  • switch(sort_method)
  • case SORT_POLYLIST_AVGZ // - sorts on
    average of all vertices
  • qsort((void )rend_list-gtpoly_ptrs,
    rend_list-gtnum_polys,
  • sizeof(POLYF4DV1_PTR),
    Compare_AvgZ_POLYF4DV1)
  • break
  •  
  • // end Sort_RENDERLIST4DV1

77
Sort Implementation - 1
  • case SORT_POLYLIST_NEARZ // - sorts on
    closest poly z vertex
  • qsort((void )rend_list-gtpoly_ptrs,
    rend_list-gtnum_polys,
  • sizeof(POLYF4DV1_PTR),
    Compare_NearZ_POLYF4DV1)
  • break
  • case SORT_POLYLIST_FARZ // - sorts on
    farthest poly z vertex
  • qsort((void )rend_list-gtpoly_ptrs,
    rend_list-gtnum_polys,
  • sizeof(POLYF4DV1_PTR),
    Compare_FarZ_POLYF4DV1)
  • break
  • default break
  • // end switch
  • // end Sort_RENDERLIST4DV1

78
Sample Comparison - 1
  • int Compare_NearZ_POLYF4DV1(const void arg1,
    const void arg2)
  • // this function comapares the closest z's of
    two polygons and is
  • // used by the depth sort surface ordering
    algorithm
  • float z1, z2
  • POLYF4DV1_PTR poly_1, poly_2
  • // dereference the poly pointers
  • poly_1 ((POLYF4DV1_PTR )(arg1))
  • poly_2 ((POLYF4DV1_PTR )(arg2))
  •  
  • // compute the near z of each polygon
  • z1 MIN(poly_1-gttvlist0.z,
    poly_1-gttvlist1.z)
  • z1 MIN(z1, poly_1-gttvlist2.z)
  •  
  • z2 MIN(poly_2-gttvlist0.z,
    poly_2-gttvlist1.z)
  • z2 MIN(z2, poly_2-gttvlist2.z)

79
Sample Comparison - 2
  • // compare z1 and z2, such that polys' will be
    sorted in
  • // descending Z order
  • if (z1 gt z2)
  • return(-1)
  • else
  • if (z1 lt z2)
  • return(1)
  • else
  • return(0)
  • // end Compare_NearZ_POLYF4DV1

80
Demo8_6
  • // remove backfaces
  • if (backface_mode1)
  • Remove_Backfaces_RENDERLIST4DV1(rend_list,
    cam)
  • // light scene all at once
  • if (lighting_mode1)
  • Light_RENDERLIST4DV1_World16(rend_list, cam,
    lights, 4)
  • // apply world to camera transform
  • World_To_Camera_RENDERLIST4DV1(rend_list, cam)
  • // sorting must come before projection
  • if (zsort_mode 1)
  • Sort_RENDERLIST4DV1(rend_list,
    SORT_POLYLIST_AVGZ)
  • // apply camera to perspective transformation
  • Camera_To_Perspective_RENDERLIST4DV1(rend_list,
    cam)
  • // apply screen transform
  • Perspective_To_Screen_RENDERLIST4DV1(rend_list,
    cam)

81
Demo8_7
  • This is a command line program that calls
    LaMothes parsing functions
  • The test files are
  • TEST1.TXT and TEST2.TXT
  • Basically allows you to display files and search
    files for pattern strings

82
Demo8_8
  • Windows programs tests loader for 3D Studio .ASC
    files (not binary)
  • Limited to 1024 vertices and polygons (can be
    increased by editing source file)
  • Test files
  • sphere01.asc
  • car01.asc
  • hammer02.asc
  • hammer03.asc

83
Demo8_9
  • Windows programs tests loader for trueSpace ASCII
    .COB files
  • Details of .COB format are on text CD
    (3D_DOS\File Formats\)
  • Test files
  • sphere01.cob
  • car01.cob
  • hammer03.cob
Write a Comment
User Comments (0)
About PowerShow.com