34
Ambient and Diffuse Light Lecture 16 Robb T. Koether Hampden-Sydney College Mon, Sep 25, 2017 Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 1 / 28

Lecture 16 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures...GLfloat* invtrans(mat4 m); void toShader(GLuint model_loc, GLuint norm_loc);

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

  • Ambient and Diffuse LightLecture 16

    Robb T. Koether

    Hampden-Sydney College

    Mon, Sep 25, 2017

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 1 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 2 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 3 / 28

  • Lighting

    The lighting model includes three “kinds” of light.

    Ambient lightSource is from all directions.Independent of the surface’s orientation.Reflected equally in all directions.

    Diffuse lightSource is from a specific direction.Dependent on the surface’s orientation.Reflected equally in all directions.

    Specular lightSource is from a specific directionDependent on the surface’s orientation.Reflected more in some directions than in others.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 4 / 28

  • Lighting

    The lighting model includes three “kinds” of light.Ambient light

    Source is from all directions.Independent of the surface’s orientation.Reflected equally in all directions.

    Diffuse lightSource is from a specific direction.Dependent on the surface’s orientation.Reflected equally in all directions.

    Specular lightSource is from a specific directionDependent on the surface’s orientation.Reflected more in some directions than in others.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 4 / 28

  • Lighting

    The lighting model includes three “kinds” of light.Ambient light

    Source is from all directions.Independent of the surface’s orientation.Reflected equally in all directions.

    Diffuse lightSource is from a specific direction.Dependent on the surface’s orientation.Reflected equally in all directions.

    Specular lightSource is from a specific directionDependent on the surface’s orientation.Reflected more in some directions than in others.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 4 / 28

  • Lighting

    The lighting model includes three “kinds” of light.Ambient light

    Source is from all directions.Independent of the surface’s orientation.Reflected equally in all directions.

    Diffuse lightSource is from a specific direction.Dependent on the surface’s orientation.Reflected equally in all directions.

    Specular lightSource is from a specific directionDependent on the surface’s orientation.Reflected more in some directions than in others.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 4 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 5 / 28

  • Ambient Light

    Definition (Ambient Light)Ambient light has no specific source. It illuminates all surfaces equally.The intensity of the reflected light is independent of the angle ofincidence and the angle of reflection.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 6 / 28

  • Ambient Light

    Regardless of the orientation of the surface, the strength of therelection is the same.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 7 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 8 / 28

  • Diffuse Light

    Definition (Diffuse Light)Diffuse light is reflected equally in all directions, but it depends thedirection of the light source. The intensity of the reflection depends onthe angle of incidence, but does not depend on the angle of reflection.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 9 / 28

  • Diffuse Light

    The angle of incidence varies over a curved surface.The more the light direction deviates from the normal, the weakerthe reflection.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 10 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 11 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 12 / 28

  • Ambient Lighting Model

    Let color be an RGB vector that represents the surface’s innatecolor.Let ambient be an RGB vector that represents the intensity ofthe ambient light.The ambient reflection is computed as

    color*ambient

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 13 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 14 / 28

  • Diffuse Lighting Model

    Letdiffuse be an RGB vector that represents the intensity of thediffuse light.light_dir be a unit vector indicating the direction of the lightsource.norm be a unit vector normal to the surface.

    The diffuse reflection is computed ascolor*diffuse*dot(light_dir, norm)

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 15 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 16 / 28

  • Coordinate Systems

    Computing ambient shading is simple, but we must be very carefulwhen computing diffuse shading.

    When computing the dot product L · N of the light vector L and thenormal N, we must be sure that they are in the same coordinatesystem.The light vector is initially in world coordinate system.The normal vector is initially in model coordinates, so it must betransformed to world coordinates.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 17 / 28

  • Coordinate Systems

    Computing ambient shading is simple, but we must be very carefulwhen computing diffuse shading.When computing the dot product L · N of the light vector L and thenormal N, we must be sure that they are in the same coordinatesystem.

    The light vector is initially in world coordinate system.The normal vector is initially in model coordinates, so it must betransformed to world coordinates.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 17 / 28

  • Coordinate Systems

    Computing ambient shading is simple, but we must be very carefulwhen computing diffuse shading.When computing the dot product L · N of the light vector L and thenormal N, we must be sure that they are in the same coordinatesystem.The light vector is initially in world coordinate system.

    The normal vector is initially in model coordinates, so it must betransformed to world coordinates.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 17 / 28

  • Coordinate Systems

    Computing ambient shading is simple, but we must be very carefulwhen computing diffuse shading.When computing the dot product L · N of the light vector L and thenormal N, we must be sure that they are in the same coordinatesystem.The light vector is initially in world coordinate system.The normal vector is initially in model coordinates, so it must betransformed to world coordinates.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 17 / 28

  • Coordinate Systems

    To make the vectors compatible, we must transform N into worldcoordinates, using the model matrix.However, vectors do not transform in the same way as vertices.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 18 / 28

  • Coordinate Systems

    Let n be a vector and v = B − A be a vector from a point B on asurface to a point A on the surface.Let M1 be the model matrix and let M2 be the transformationmatrix for the normal vector.In model coordinates, we have nTv = 0, because n ⊥ v.Therefore, we need to have (M2n)

    T M1v = 0 as well.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 19 / 28

  • Coordinate Systems

    We have

    (M2n)T M1v =

    (nTMT2

    )M1v

    = nT(

    MT2M1)

    v

    If MT2M1 is the identity matrix, then

    nT(

    MT2M1)

    v = nTIv

    = nTv= 0.

    Therefore, M2n ⊥ M1v.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 20 / 28

  • Coordinate Systems

    Therefore, M2n ⊥ M1v provided that

    M2 =(

    MT1)−1

    .

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 21 / 28

  • Coordinate Systems

    It so happens that for any translation matrix T and for any rotationmatrix R, (

    TT)−1

    = T,(RT

    )−1= R.

    However, for scaling matrices S, in general(ST

    )−16= S.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 22 / 28

  • The ModelStack Class

    The ModelStack FunctionGLfloat* invtrans(mat4 m);void toShader(GLuint model_loc, GLuint norm_loc);

    The function invtrans() will take a 4× 4 matrix m and return apointer to 3× 3 matrix that is the inverse transpose of m.In the ModelStack class, I have overloaded the toShader()function to pass both the model matrix and the normal matrix.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 23 / 28

  • The ModelStack Class

    The ModelStack Functionmodel_stack.toShader(model_loc, norm_loc);

    The above function call is typical of what we should use whenprogramming the lighting model.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 24 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 25 / 28

  • Assignment 12

    Assignment 12Add ambient and diffuse light to the Canal Boat program, lightingup the lock walls and the canal boat.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 26 / 28

  • Outline

    1 Lighting

    2 Ambient Light

    3 Diffuse Light

    4 Programming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    5 Assignment 12

    6 Assignment

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 27 / 28

  • Assignment

    AssignmentRead pp. 359 - 368, Classic Lighting Model.

    Robb T. Koether (Hampden-Sydney College) Ambient and Diffuse Light Mon, Sep 25, 2017 28 / 28

    LightingAmbient LightDiffuse LightProgramming the ShadersAmbient Lighting ModelDiffuse Lighting ModelCoordinate Systems

    Assignment 12Assignment