CS184 Section 2
Lighting and Transformations

Brandon Wang
February 5, 2013
Material adapted from James O'Brien, Fu-Chung Huang, Ravi Ramamoorthi, and Carlo Sequin.

Logistics

Today

Phong Illumination Model

How we determine the color of what we see

i.e. Given our representations of geometry and lights, what color is at a specific pixel?

Ambient term

\mathbf{R}_{A} = \mathbf{k}_{a} \cdot \mathbf{I}

\mathbf{k}_{a}
Ambient color of the material
\mathbf{I}
Light color

Does not depend on direction of light, or direction to viewer!

Diffuse term

\mathbf{R}_{D} = \mathbf{k}_{d} \cdot \mathbf{I} max\left(\mathbf{\hat{l}} \cdot \mathbf{\hat{n}}, 0\right)

\mathbf{k}_{d}
Diffuse color of the material
\mathbf{I}
Light color
\hat{l}
Light direction
\hat{n}
Surface normal

Specular Term

\mathbf{R}_{S} = \mathbf{k}_{s} \cdot \mathbf{I} max\left(\mathbf{\hat{r}} \cdot \mathbf{\hat{v}}, 0\right)^{p}

\mathbf{k}_{s}
Specular color of the material
\mathbf{I}
Light color
\mathbf{\hat{r}}
Reflected Angle
\mathbf{\hat{v}}
Direction to viewer
\mathbf{\hat{n}}
Surface normal
p
Shininess

Phong Illumination Model

\mathbf{R} = \mathbf{k}_{a} \cdot \mathbf{I} + \mathbf{k}_{d} \cdot \mathbf{I} max\left(\mathbf{\hat{l}} \cdot \mathbf{\hat{n}}, 0\right) + \mathbf{k}_{s} \cdot \mathbf{I} max\left(\mathbf{\hat{r}} \cdot \mathbf{\hat{v}}, 0\right)^{p}\\

Ambient Term

\mathbf{R} = \underline{\mathbf{k}_{a} \cdot \mathbf{I}} + \mathbf{k}_{d} \cdot \mathbf{I} max\left(\mathbf{\hat{l}} \cdot \mathbf{\hat{n}}, 0\right) + \mathbf{k}_{s} \cdot \mathbf{I} max\left(\mathbf{\hat{r}} \cdot \mathbf{\hat{v}}, 0\right)^{p}\\

Diffuse Term

\mathbf{R} = \mathbf{k}_{a} \cdot \mathbf{I} + \underline{\mathbf{k}_{d} \cdot \mathbf{I} max\left(\mathbf{\hat{l}} \cdot \mathbf{\hat{n}}, 0\right)} + \mathbf{k}_{s} \cdot \mathbf{I} max\left(\mathbf{\hat{r}} \cdot \mathbf{\hat{v}}, 0\right)^{p}\\

Specular Term

\mathbf{R} = \mathbf{k}_{a} \cdot \mathbf{I} + \mathbf{k}_{d} \cdot \mathbf{I} max\left(\mathbf{\hat{l}} \cdot \mathbf{\hat{n}}, 0\right) + \underline{\mathbf{k}_{s} \cdot \mathbf{I} max\left(\mathbf{\hat{r}} \cdot \mathbf{\hat{v}}, 0\right)^{p}}\\

Questions

  1. How would the diffuse and specular term look like for a matte object? (Think chalk). What would some other objects be that have similar terms?
  2. How would the diffuse and specular term look like for a shiny plastic? (Think metal). What would some other objects be that have similar terms?
  3. What is flat shading?
  4. What is Gouraud shading?
  5. What is Phong shading?
  6. What is a point light?

Gouraud vs Phong shading

AS1

In AS1, you will be programming the Phong Illumination Model!

example_01.cpp

Download it from the class website.

example_01

void myDisplay() {

  glClear(GL_COLOR_BUFFER_BIT);	

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();	


  // Start drawing
  circle(viewport.w / 2.0 , viewport.h / 2.0 , min(viewport.w, viewport.h) / 3.0);

  glFlush();
  glutSwapBuffers();
}

    

example_01

void circle(float centerX, float centerY, float radius) {
  glBegin(GL_POINTS);
  int i,j;  // Pixel indices

  for (i=0;i<viewport.w;i++) {
    for (j=0;j<viewport.h;j++) {
      // Location of the center of pixel relative to center of sphere
      float x = (i+0.5-centerX);
      float y = (j+0.5-centerY);
      float dist = sqrt(sqr(x) + sqr(y));

      if (dist<=radius) {
        // This is the front-facing Z coordinate
        float z = sqrt(radius*radius-dist*dist);

        setPixel(i,j, 1.0, 0.0, 0.0);
        // This is amusing, but it assumes negative color values are treated reasonably.
        // setPixel(i,j, x/radius, y/radius, z/radius );
      }
    }
  }
  glEnd();
}


example_00

void setPixel(int x, int y, GLfloat r, GLfloat g, GLfloat b) {
  glColor3f(r, g, b);
  glVertex2f(x + 0.5, y + 0.5);   // The 0.5 is to target pixel
  // centers 
  // Note: Need to check for gap
  // bug on inst machines.
}

AS1

We've drawn a circle, but we want you treat it as a sphere.

"Lifting" the points onto a sphere.

Generating a normal.

Argument parsing.

Transformations

Other slides and worksheet.

Matrix Stacks