in OpenGL

•• Setting Setting StateState glShadeModelglShadeModel(( GLGL__SMOOTHSMOOTH || GL_FLAT);GL_FLAT);

•GL_FLAT:•GL_FLAT: –flat–flat shadingshading •• GL_SMOOTH: GL_SMOOTH: –Gouraudshading–Gouraudshading

Jacobs University and Lab

320322: Graphics and Visualization 482 : properties

• Gouraud shading produces smooth transitions of light intensities, but silhouettes remain polygonal. • However, typically triangles are small such that this cannot be noticed.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 483 Gouraud shading: properties

• Using Gouraud shading, illumination is only evaluated at the vertices of the triangles. • Specular highlights that do not stretch over vertices of the mesh are missed.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 484 Gouraud shading: properties

• During , specular highlights can show some flickering when using Gouraud shading.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 485 6.3

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 486 Phong shading

• Improve shading by applying the illumination model at each unprojected pixel, i.e., at those points of a triangle that are projected onto a pixel of the screen.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 487 Phong shading algorithm

• Use a scanline that traverses the triangle top-down in screen space. • For each start and end point of a pixel row, linearly interpolate the surface normal along the triangle‘s edges. • For each pixel row, linearly interpolate the surface normal between start and end point of that row. • Apply the illumination model at each unprojected pixel value using the interpolated surface normal.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 488 Phong shading algorithm

• Apply same interpolation formula as in Gouraud shading, but interpolate normals instead of color values.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 489 Phong shading

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 490 Phong shading: properties

• Specular highlights are always correctly and exactly computed. • Highlights appear much smoother. • No flickering during .

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 491 Comparison: flat, Gouraud, Phong shading

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 492 Phong shading: properties

• The evaluation of the illumination model at each unprojected screen pixel is expensive. • Illumination needs to be applied in object space rather than screen space. • The pixel can be easily unprojected when storing depth information (cf. depth buffer).

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 493 Phong shading implementation

• Phong shading is not supported by OpenGL. • It is nowadays implemented using fragment . • The normals can be handed to the encoded as colors. • Gouraud shading using these colors then interpolates the normals.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 494 7.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 495 Global Illumination

• The Phong, Blinn-Phong, and BRDF models are local illumination models, i.e., they only take into account the local geometry of an object. • Global illumination models take into account the entire geometry that is present in the scene. • In particular, global illumination considers the interference of objects with each other. • For example, when one object occludes the light source with respect to another object, the other object should appear to be in the shadow of the first object.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 496 7.1

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 497 Ray tracing

• Ray tracing is a global illumination method. • It allows for the computation of all shadows in the scene.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 498 Observation

• In nature, light from a light source gets reflected at a point p. • The amount of incoming light that is reflected in direction v to the viewer determines the color of the object at position p.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 499 Ray tracing idea

• Trace the light ray through the scene. • Since many light rays are being sent out from each light source in all directions, this can be computationally very intense. • For efficiency purposes, trace in backward direction. • Start rays from the viewpoint. • Send a ray through each pixel of the screen onto the scene. • Trace the ray to the light sources.

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 500 Ray tracing idea

light 2 light 1

secondary rays

viewpoint object 1

primary ray object 2

screen • Primary rays are sent from the viewer through the center of each pixel of the screen • When the primary rays hit an object, secondary rays are sent from the point of intersection towards each light source. Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 501 Ray tracing idea

light 2 light 1

secondary rays

viewpoint object 1

object 2 primary rays screen • If a secondary ray hits an object, that object occludes the light source. • The color of the pixel is computed by applying a local ilumination model at the position, where the primary ray hits the object, taking into account all light sources that are not occluded. Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 502 Ray-tracing idea

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 503 Ray-tracing idea

Jacobs University Visualization and Computer Graphics Lab

320322: Graphics and Visualization 504