1 Light Properties Property Exam ple Properties: Define - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Light Properties Property Exam ple Properties: Define - - PDF document

Adding Color CS 4 7 3 1 : Com put e r Gr a phics Som etim es light or surfaces are colored Le ct ur e 1 6 : I llum ina t ion M ode ls Pa r t 2 Treat R,G and B com ponents separately I llum ination equation goes from : Em m


slide-1
SLIDE 1

1

CS 4 7 3 1 : Com put e r Gr a phics Le ct ur e 1 6 : I llum ina t ion M ode ls Pa r t 2 Em m anuel Agu

Adding Color

  • Som etim es light or surfaces are colored
  • Treat R,G and B com ponents separately
  • I llum ination equation goes from :

I llu m = a m bie nt + diffuse + sp e cu la r = Ka x I + Kd x I x ( N .L) + Ks x I x ( R.V )

  • To:

I llu m_ r = Ka _ r x I _ r + Kd_ r x I _ r x ( N .L) + Ks_ r x I _ r x ( R.V ) I llu m_ g = Ka _ g x I _ g + Kd _ g x I _ g x ( N .L) + Ks_ g x I _ g x ( R.V ) I llu m_ b = Ka _ b x I _ b + Kd _ b x I _ b x ( N .L) + Ks_ b x I _ b x ( R.V ) n

Adding Color

8 9 .6 0.773911 0.773911 0.773911 0.2775 0.2775 0.2775 0.23125 0.23125 0.23125 Polished Silver 27.8974 0.992157 0.941176 0.807843 0.780392 0.568627 0.113725 0.329412 0.223529 0.027451 Brass 3 2 0 .5 0 .5 0 .5 0 .0 1 0 .0 1 0 .0 1 0 .0 0 .0 0 .0 Black plastic Exponent, n Specular Ksr, Ksg, ksb Diffuse Kdr, Kdg,kdb Ambient Kar, Kag, kab Material

Figure 8.17, Hill, courtesy of McReynolds and Blythe

Lighting in OpenGL

  • Adopt Phong light ing m odel
specular + diffuse + am bient light s Light ing is com put ed at vert ices
  • Interpolate across surface (Gouraud/smooth shading)
  • Use a constant illumination (get it from one of the vertices)
  • Setting up OpenGL Lighting:
Light Properties Enable/ Disable light ing Surface m at erial propert ies Provide correct surface norm als Light m odel properties
slide-2
SLIDE 2

2

Light Properties

  • Properties:
Colors / Posit ion and t ype / at t enuat ion

glLight fv( light , pr ope r t y, va lue )

(1) constant: specify which light you want to set the property E.g: GL_LI GHT0, GL_LI GHT1, GL_LI GHT2 … you can create multiple lights (OpenGL allows at least 8 lights) (2) constant: specify which light property you want to set the value E.g: GL_AMBI ENT, GL_DI FFUSE, GL_SPECULAR, GL_POSI TI ON

( check t he r ed book for m or e)

(3) The value you want to set to the property

1 2 3

Property Exam ple

  • Define colors and position a light

GLfloat light_ambient[ ] = { 0.0, 0.0, 0.0, 1.0} ; GLfloat light_diffuse[ ] = { 1.0, 1.0, 1.0, 1.0} ; GLfloat light_specular[ ] = { 1.0, 1.0, 1.0, 1.0} ; GLfloat light_position[ ] = { 0.0, 0.0, 1.0, 1.0} ; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); colors Position What if I set Posit ion t o ( 0,0,1,0) ?

Types of lights

  • OpenGL supports two types of lights
Local light ( point light ) I nfinit e light ( direct ional light )
  • Determ ined by the light positions you provide
w = 0: infinit e light source ( fast er) w ! = 0: point light – posit ion = ( x/ w , y/ w , z/ w )

GLfloat light_position[ ] = { x,y,z,w} ; glLightfv(GL_LIGHT0, GL_POSITION, light_position);

Turning on the lights

  • Turn on the power (for all the lights)
glEnable( GL_LI GHTI NG) ; glDisable ( GL_LI GHTI NG) ;
  • Flip each light’s switch
glEnable( GL_LI GHTn ) ( n = 0,1,2,…)
slide-3
SLIDE 3

3

Controlling light position

  • Modelview m atrix affects a light’s position
  • Two options:
  • Option a:
Treat light like vert ex Do pushMatrix, t ranslat e, rot at e, ..

glLight fv position, popm atrix

Then call gluLookat Light m oves independent ly of cam era
  • Option b:
Load ident it y m at rix in m odelview m atrix Call glLight fv t hen call gluLookat Light appears at t he eye ( like a m iner’s lam p)

Material Properties

  • The color and surface properties of a m aterial (dull,

shiny, etc)

  • How m uch the surface reflects the incident lights

(am bient/ diffuse/ specular reflecetion coefficients) glMaterialfv(face, property, value)

Face: m at erial propert y for which face ( e.g. GL_FRONT, GL_BACK, GL_FRONT_AND_BACK) Propert y: what m at erial propert y you want t o set ( e.g. GL_AMBI ENT, GL_DI FFUSE,GL_SPECULAR, GL_SHI NI NESS, GL_EMI SSI ON, et c) Value: t he value you can t o assign t o t he pr oper t y

Material Exam ple

  • Define am bient/ diffuse/ specular reflection and shininess

GLfloat m at_amb_diff[ ] = { 1.0, 0.5, 0.8, 1.0} ; GLfloat m at_specular[ ] = { 1.0, 1.0, 1.0, 1.0} ; GLfloat shininess[ ] = { 5.0} ; (range: dull 0 – very shiny 128) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, m at_am b_diff); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_speacular); glMaterialfv(GL_FRONT, GL_SHININESS, shininess);

  • refl. coeff.

Global light properties

  • glLightModelfv(property, value)
  • Enable two sided lighting
propert y = GL_LI GHT_MODEL_TWO_SI DE value = GL_TRUE ( GL_FALSE if you don’t want t wo sided

lighting)

  • Global am bient color
Propert y = GL_LI GHT_MODEL_AMBI ENT Value = ( red, green, blue, 1.0) ;
  • Check the red book for others
slide-4
SLIDE 4

4

Surface Norm als

  • Correct norm als are essential for correct lighting
  • Associate a norm al to each vertex

glBegin(… ) glNorm al3f( x,y,z) glVertex3f(x,y,z) … glEnd()

  • The norm als you provide need to have a unit length
You can use glEnable ( GL_ N ORM ALI ZE) t o hav e

OpenGL norm alize all t he norm als

Lighting revisit

  • Where is lighting performed in the graphics pipeline?

m odeling and viewing

v1, m 1 v2, m 2 v3, m 3

per vert ex lighting proj ect ion clipping interpolate vert ex colors viewport m apping Rast erizat ion texturing shading Display

Polygon shading m odel

  • Flat shading - com pute lighting once and assign the

color to the whole (m esh) polygon

Flat shading

  • Only use one vertex norm aland m aterial property to

com pute the color for the polygon

  • Benefit: fast to com pute
  • Used when:
Polygon is sm all enough Light source is far away ( why?) Eye is very far away ( why?)
  • OpenGL com m and: glShadeModel(GL_FLAT)
slide-5
SLIDE 5

5

Mach Band Effect

  • Flat shading suffers from “m ach band effect”
  • Mach band effect – hum an eyes accentuate the

discontinuity at the boundary

Side view of a polygonal surface perceived intensity

Sm ooth shading

  • Fix the m ach band effect – rem ove edge discontinuity
  • Com pute lighting for m ore points on each face

Fla t sha ding Sm oot h sha ding

Sm ooth shading

  • Two popular m ethods:
Gouraud shading ( used by OpenGL) Phong shading ( bet t er specular highlight , not in OpenGL)

Gouraud Shading

  • The sm ooth shading algorithm used in OpenGL

g lSh a d e M od e l( GL_ SM OOTH )

  • Lighting is calculated for each of the polygon vertices
  • Colors are interpolated for interior pixels
slide-6
SLIDE 6

6

Gouraud Shading

  • Per- vertex lighting calculation
  • Norm al is needed for each vertex
  • Per- vertex norm al can be com puted by averaging the

adjust face norm als

n n 1 n 2 n 3 n 4 n = ( n1 + n2 + n3 + n4) / 4.0

Gouraud Shading

  • Com pute vertex illum ination (color) before the

projection transform ation

  • Shade interior pixels: color interpolation (norm als are

not needed)

C1 C2 C3 Ca = lerp(C1, C2) Cb = lerp(C1, C3) Lerp( Ca, Cb) for all scanlines * lerp: linear int erpolat ion

Gouraud Shading

  • Linear interpolation
  • I nterpolate triangle color: use y distance to interpolate

the two end points in the scanline, and use x distance to interpolate interior pixel colors

a b v1 v2 x x = a / (a+ b) * v1 + b/ (a+ b) * v2

Gouraud Shading Problem

  • Lighting in the polygon interior can be inaccurate
slide-7
SLIDE 7

7

Phong Shading

  • I nstead of interpolation, we calculate lighting for each

pixel inside the polygon (per pixel lighting)

  • Need norm als for all the pixels – not provided by user
  • Phong shading algorithm interpolates the norm als and

com pute lighting during rasterization ( need t o m ap t he

norm al back t o w orld or eye space t hough)

Phong Shading

  • Norm al interpolation
  • Slow – not supported by OpenGL and m ost graphics

hardware

n1 n2 n3 nb = lerp(n1, n3) na = lerp(n1, n2) lerp(na, nb)

References

  • Hill, chapter 8