Introduction to Computer Graphics with WebGL

Similar documents
Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

CS452/552; EE465/505. Intro to Lighting

Overview. Viewing and perspectives. Planar Geometric Projections. Classical Viewing. Classical views Computer viewing Perspective normalization

Computer Viewing. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

WHY WE NEED SHADING. Suppose we build a model of a sphere using many polygons and color it with glcolor. We get something like.

Shading. Why we need shading. Scattering. Shading. Objectives

Three-Dimensional Graphics III. Guoying Zhao 1 / 67

Comp 410/510 Computer Graphics. Spring Shading

Illumination & Shading

Classical and Computer Viewing. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Shading I Computer Graphics I, Fall 2008

One or more objects A viewer with a projection surface Projectors that go from the object(s) to the projection surface

Introduction to Computer Graphics 7. Shading

Building Models. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Overview. Shading. Shading. Why we need shading. Shading Light-material interactions Phong model Shading polygons Shading in OpenGL

Computer Graphics. Shading. Based on slides by Dianna Xu, Bryn Mawr College

CS452/552; EE465/505. Models & Viewing

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

Introduction to Computer Graphics with WebGL

3D Viewing. CMPT 361 Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

3D Viewing. Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

Objectives. Introduce Phong model Introduce modified Phong model Consider computation of required vectors Discuss polygonal shading.

CS 4600 Fall Utah School of Computing

Lessons Learned from HW4. Shading. Objectives. Why we need shading. Shading. Scattering

Computer Viewing and Projection. Overview. Computer Viewing. David Carr Fundamentals of Computer Graphics Spring 2004 Based on Slides by E.

Lighting and Shading II. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Objectives. Continue discussion of shading Introduce modified Phong model Consider computation of required vectors

Why we need shading?

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Introduction to Computer Graphics 4. Viewing in 3D

C O M P U T E R G R A P H I C S. Computer Graphics. Three-Dimensional Graphics V. Guoying Zhao 1 / 65

Computer Viewing. Prof. George Wolberg Dept. of Computer Science City College of New York

Shading II. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Objectives. Shading II. Distance Terms. The Phong Reflection Model

3D Viewing. With acknowledge to: Ed Angel. Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

CITSTUDENTS.IN VIEWING. Computer Graphics and Visualization. Classical and computer viewing. Viewing with a computer. Positioning of the camera

CS452/552; EE465/505. Lighting & Shading

Fundamental Types of Viewing

Announcement. Project 1 has been posted online and in dropbox. Due: 11:59:59 pm, Friday, October 14

Shading II. CITS3003 Graphics & Animation

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

CS452/552; EE465/505. Review & Examples

Course no. DIS4566 National Chiao Tung Univ, Taiwan By: I-Chen Lin, Assistant Professor

Computer Graphics (CS 4731) Lecture 16: Lighting, Shading and Materials (Part 1)

Computer Graphics (CS 543) Lecture 7b: Intro to lighting, Shading and Materials + Phong Lighting Model

Computer Viewing Computer Graphics I, Fall 2008

Computer Viewing. CITS3003 Graphics & Animation. E. Angel and D. Shreiner: Interactive Computer Graphics 6E Addison-Wesley

Computer Graphics. Bing-Yu Chen National Taiwan University The University of Tokyo

Computer Graphics. Jeng-Sheng Yeh 葉正聖 Ming Chuan University (modified from Bing-Yu Chen s slides)

5.8.3 Oblique Projections

Models and Architectures

Chapter 5. Projections and Rendering

Viewing with Computers (OpenGL)

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Illumination & Shading I

Announcements. Submitting Programs Upload source and executable(s) (Windows or Mac) to digital dropbox on Blackboard

Lighting. Figure 10.1

Virtual Reality for Human Computer Interaction

Today. Global illumination. Shading. Interactive applications. Rendering pipeline. Computergrafik. Shading Introduction Local shading models

Graphics for VEs. Ruth Aylett

Reflection and Shading

Graphics for VEs. Ruth Aylett

Three Main Themes of Computer Graphics

521493S Computer Graphics Exercise 2 Solution (Chapters 4-5)

QUESTION BANK 10CS65 : COMPUTER GRAPHICS AND VISUALIZATION

CHAPTER 1 Graphics Systems and Models 3

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

CS130 : Computer Graphics Lecture 8: Lighting and Shading. Tamar Shinar Computer Science & Engineering UC Riverside

Lighting and Shading Computer Graphics I Lecture 7. Light Sources Phong Illumination Model Normal Vectors [Angel, Ch

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

Illumination in Computer Graphics

Today. Global illumination. Shading. Interactive applications. Rendering pipeline. Computergrafik. Shading Introduction Local shading models

Introduction to Computer Graphics with WebGL

Today s class. Simple shadows Shading Lighting in OpenGL. Informationsteknologi. Wednesday, November 21, 2007 Computer Graphics - Class 10 1

CSE 167: Introduction to Computer Graphics Lecture #6: Lights. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2016

CHAPTER5. We have learned to build three-dimensional graphical models and to LIGHTING AND SHADING

On the Midterm Exam. Monday, 10/17 in class. Closed book and closed notes. One-side and one page cheat sheet is allowed. A calculator is allowed

CS5620 Intro to Computer Graphics

Introduction to Computer Graphics with WebGL

Chapter 2 A top-down approach - How to make shaded images?

Computer Graphics. Illumination and Shading

Virtual Cameras & Their Matrices

Computer Graphics. Ray Tracing. Based on slides by Dianna Xu, Bryn Mawr College

CS 130 Final. Fall 2015

Pipeline Operations. CS 4620 Lecture 10

Illumination & Shading: Part 1

CSE 167: Lecture #7: Color and Shading. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

Lighting and Shading. Slides: Tamar Shinar, Victor Zordon

CS452/552; EE465/505. Transformations

So we have been talking about 3D viewing, the transformations pertaining to 3D viewing. Today we will continue on it. (Refer Slide Time: 1:15)

Viewing. Reading: Angel Ch.5

Chapter 8 Three-Dimensional Viewing Operations

Interactive Computer Graphics A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

COMP Computer Graphics and Image Processing. a6: Projections. In part 2 of our study of Viewing, we ll look at. COMP27112 Toby Howard

Chap 7, 2008 Spring Yeong Gil Shin

Viewing. Cliff Lindsay, Ph.D. WPI

Shading. Brian Curless CSE 557 Autumn 2017

Computer Graphics. Illumination Models and Surface-Rendering Methods. Somsak Walairacht, Computer Engineering, KMITL

Viewing and Projection

Transcription:

Introduction to Computer Graphics with WebGL Ed Angel Classical Viewing Computer Graphics with WebGL Ed Angel, 204 Classical Viewing Viewing requires three basic elements - One or more objects - A viewer with a projection surface - Projectors that go from the object(s) to the projection surface Classical views are based on the relationship among these elements - The viewer picks up the object and orients it how she would like to see it Each object is assumed to constructed from flat principal faces - Buildings, polyhedra, manufactured objects Computer Graphics with WebGL Ed Angel, 204 2 Planar Geometric Projections Standard projections project onto a plane Projectors are lines that either - converge at a center of projection - are parallel Such projections preserve lines - but not necessarily angles Nonplanar projections are needed for applications such as map construction Computer Graphics with WebGL Ed Angel, 204 3

4 Classical Projections Computer Graphics with WebGL Ed Angel, 204 Taxonomy of Planar Geometric Projections planar geometric projections parallel perspective multiview axonometric orthographic point 2 point 3 point oblique isometric dimetric trimetric Computer Graphics with WebGL Ed Angel, 204 5 Perspective vs Parallel Computer graphics treats all projections the same and implements them with a single pipeline Classical viewing developed different techniques for drawing each type of projection Fundamental distinction is between parallel and perspective viewing even though mathematically parallel viewing is the limit of perspective viewing Computer Graphics with WebGL Ed Angel, 204 6

7 Perspective Projection Computer Graphics with WebGL Ed Angel, 204 Parallel Projection Computer Graphics with WebGL Ed Angel, 204 8 Orthographic Projection Projectors are orthogonal to projection surface Computer Graphics with WebGL Ed Angel, 204 9

Multiview Orthographic Projection Projection plane parallel to principal face Usually form front, top, side views isometric (not multiview orthographic view) front in CAD and architecture, we often display three multiviews plus isometric top side Computer Graphics with WebGL Ed Angel, 204 0 Axonometric Projections Allow projection plane to move relative to object classify by how many angles of a corner of a projected cube are the same none: trimetric two: dimetric three: isometric θ θ 3 θ 2 Computer Graphics with WebGL Ed Angel, 204 Types of Axonometric Projections Computer Graphics with WebGL Ed Angel, 204 2

Oblique Projection Arbitrary relationship between projectors and projection plane Computer Graphics with WebGL Ed Angel, 204 3 Advantages and Disadvantages Can pick the angles to emphasize a particular face - Architecture: plan oblique, elevation oblique Angles in faces parallel to projection plane are preserved while we can still see around side In physical world, cannot create with simple camera; possible with bellows camera or special lens (architectural) Computer Graphics with WebGL Ed Angel, 204 4 Perspective Projection Projectors coverge at center of projection Computer Graphics with WebGL Ed Angel, 204 5

Three-Point Perspective No principal face parallel to projection plane Three vanishing points for cube Computer Graphics with WebGL Ed Angel, 204 6 Two-Point Perspective On principal direction parallel to projection plane Two vanishing points for cube Computer Graphics with WebGL Ed Angel, 204 7 One-Point Perspective One principal face parallel to projection plane One vanishing point for cube Computer Graphics with WebGL Ed Angel, 204 8

Introduction to Computer Graphics with WebGL Ed Angel Positioning the Camera Computer Graphics with WebGL Ed Angel, 204 From the Beginning In the beginning: - fixed function pipeline - Model-View and Projection Transformation - Predefined frames: model, object, camera, clip, ndc, window After deprecation - pipeline with programmable shaders - no transformations - clip, ndc window frames MV.js reintroduces original capabilities Computer Graphics with WebGL Ed Angel, 204 2 Computer Viewing There are three aspects of the viewing process, all of which can be implemented in the pipeline, - Positioning the camera Setting the model-view matrix - Selecting a lens Setting the projection matrix - Clipping Setting the view volume Computer Graphics with WebGL Ed Angel, 204 3

4 The WebGL Camera In WebGL, initially the object and camera frames are the same - Default model-view matrix is an identity The camera is located at origin and points in the negative z direction WebGL also specifies a default view volume that is a cube with sides of length 2 centered at the origin - Default projection matrix is an identity Computer Graphics with WebGL Ed Angel, 204 Default Projection Default projection is orthogonal 2 clipped out z=0 Computer Graphics with WebGL Ed Angel, 204 5 Repositioning the Camera Frame If we want to visualize objects with both positive and negative z values we can either - Move the camera in the positive z direction Translate the camera frame - Move the objects in the negative z direction Translate the world frame Both of these views are equivalent and are determined by the model-view matrix - Need a translation (translate(0.0,0.0,-d);) d > 0 Computer Graphics with WebGL Ed Angel, 204 6

7 Moving Camera back from Origin default frames frames after translation by d d > 0 Computer Graphics with WebGL Ed Angel, 204 Moving the Camera We can move the camera to any desired position by a sequence of rotations and translations Example: side view - Rotate the camera - Move it away from origin - Model-view matrix C = TR Computer Graphics with WebGL Ed Angel, 204 8 WebGL code Remember that last transformation specified is first to be applied // Using MV.js var t = translate (0.0, 0.0, -d); var ry = rotatey(90.0); var m = mult(t, ry); or var m = mult(translate (0.0, 0.0, -d), rotatey(90.0);); Computer Graphics with WebGL Ed Angel, 204 9

lookat lookat(eye, at, up) Computer Graphics with WebGL Ed Angel, 204 0 The lookat Function The GLU library contained the function glulookat to form the required model-view matrix through a simple interface Note the need for setting an up direction Replaced by lookat() in MV.js - Can concatenate with modeling transformations Example: isometric view of cube aligned with axes var eye = vec3(.0,.0,.0); var at = vec3(0.0, 0.0, 0.0); var up = vec3(0.0,.0, 0.0); var mv = lookat(eye, at, up); Computer Graphics with WebGL Ed Angel, 204 Other Viewing APIs The lookat function is only one possible API for positioning the camera Others include - View reference point, view plane normal, view up (PHIGS, GKS-3D) - Yaw, pitch, roll - Elevation, azimuth, twist - Direction angles Computer Graphics with WebGL Ed Angel, 204 2

Introduction to Computer Graphics with WebGL Ed Angel Projection Computer Graphics with WebGL Ed Angel, 204 Basic Vertex Shader attribute vec4 vposition; attribute vec4 vcolor; varying vec4 fcolor; uniform mat4 modelviewmatrix; uniform mat4 projectionmatrix; void main() { gl_position = projectionmatrix*modelviewmatrix*vposition; fcolor = vcolor; } Computer Graphics with WebGL Ed Angel, 204 2 Projection Normalization The default projection in the eye (camera) frame is orthogonal For points within the default view volume x p = x y p = y z p = 0 Most graphics systems use view normalization - All other views are converted to the default view by transformations that determine the projection matrix - Allows use of the same pipeline for all views Computer Graphics with WebGL Ed Angel, 204 3

4 x p = x y p = y z p = 0 w p = Homogeneous Coordinate Representation default orthographic projection M = p p = Mp In practice, we can let M = I and set the z term to zero later Computer Graphics with WebGL Ed Angel, 204 Simple Perspective Center of projection at the origin Projection plane z = d, d < 0 Computer Graphics with WebGL Ed Angel, 204 5 Perspective Equations Consider top and side views x p = y p = z p = d Computer Graphics with WebGL Ed Angel, 204 6

7 Homogeneous Coordinate Form consider q = Mp where M = p = q = Computer Graphics with WebGL Ed Angel, 204 Perspective Division However w, so we must divide by w to return from homogeneous coordinates This perspective division yields x p = y p = z p = d the desired perspective equations We specify the desired clipping volume with MV.js functions that are equivalent to deprecated OpenGL functions Computer Graphics with WebGL Ed Angel, 204 8

Introduction to Computer Graphics with WebGL Ed Angel WebGL Projection Computer Graphics with WebGL Ed Angel, 204 WebGL Orthogonal Viewing ortho(left,right,bottom,top,near,far) near and far measured from camera Computer Graphics with WebGL Ed Angel, 204 2 ortho.html Computer Graphics with WebGL Ed Angel, 204 3

Viewing a Cube camera Computer Graphics with WebGL Ed Angel, 204 4 Sphere Equations x = rsinθ cosφ y = rsinθ sinφ z = rcosθ θ constant: circles of constant longitude φ constant: circles of constant latitude Computer Graphics with WebGL Ed Angel, 204 5 Rotating Camera var render = function() { gl.clear( gl.color_buffer_bit gl.depth_buffer_bit); eye = vec3(radius*math.sin(theta)*math.cos(phi), radius*math.sin(theta)*math.sin(phi), radius*math.cos(theta)); modelviewmatrix = lookat(eye, at, up); projectionmatrix = ortho(left, right, bottom, ytop, near, far); gl.uniformmatrix4fv( modelviewmatrixloc, false, flatten(modelviewmatrix) ); gl.uniformmatrix4fv( projectionmatrixloc, false, flatten(projectionmatrix) ); gl.drawarrays( gl.triangles, 0, numvertices ); requestanimframe(render); } Computer Graphics with WebGL Ed Angel, 204 6

7 WebGL Perspective frustum(left,right,bottom,top,near,far) Computer Graphics with WebGL Ed Angel, 204 Using Field of View With frustum it is often difficult to get the desired view perpective(fovy, aspect, near, far) often provides a better interface front plane aspect = w/h Computer Graphics with WebGL Ed Angel, 204 8 Computing Matrices Compute in JS file, send to vertex shader with gl.uniformmatrix4fv Dynamic: update in render() or shader Computer Graphics with WebGL Ed Angel, 204 9

Rotating Camera var render = function(){ gl.clear( gl.color_buffer_bit gl.depth_buffer_bit); eye = vec3(radius*math.sin(theta)*math.cos(phi), radius*math.sin(theta)*math.sin(phi), radius*math.cos(theta)); modelviewmatrix = lookat(eye, at, up); projectionmatrix = perspective(fovy, aspect, near, far); gl.uniformmatrix4fv( modelviewmatrixloc, false, flatten(modelviewmatrix) ); gl.uniformmatrix4fv( projectionmatrixloc, false, flatten(projectionmatrix) ); gl.drawarrays( gl.triangles, 0, NumVertices ); requestanimframe(render); } Computer Graphics with WebGL Ed Angel, 204 0

Introduction to Computer Graphics with WebGL Ed Angel Orthogonal Projection Matrices Computer Graphics with WebGL Ed Angel, 204 Normalization Rather than derive a different projection matrix for each type of projection, we can convert all projections to orthogonal projections with the default view volume This strategy allows us to use standard transformations in the pipeline and makes for efficient clipping Computer Graphics with WebGL Ed Angel, 204 2 Pipeline View model-view transformation nonsingular projection transformation perspective division 4D 3D clipping against default cube projection 3D 2D Computer Graphics with WebGL Ed Angel, 204 3

4 Notes We stay in four-dimensional homogeneous coordinates through both the modelview and projection transformations - Both these transformations are nonsingular - Default to identity matrices (orthogonal view) Normalization lets us clip against simple cube regardless of type of projection Delay final projection until end - Important for hidden-surface removal to retain depth information as long as possible Computer Graphics with WebGL Ed Angel, 204 Orthogonal Normalization ortho(left,right,bottom,top,near,far) normalization find transformation to convert specified clipping volume to default Computer Graphics with WebGL Ed Angel, 204 5 Orthogonal Matrix Two steps - Move center to origin T(-(left+right)/2, -(bottom+top)/2,(near+far)/2)) - Scale to have sides of length 2 S(2/(left-right),2/(top-bottom),2/(near-far)) # 2 right left & % 0 0 right left right left ( % ( % 2 top + bottom 0 0 ( P = ST = % top bottom top bottom ( % 2 far + near ( % 0 0 near far far near ( % ( $ 0 0 0 ' Computer Graphics with WebGL Ed Angel, 204 6

7 Final Projection Set z =0 Equivalent to the homogeneous coordinate transformation M orth = Hence, general orthogonal projection in 4D is P = M orth ST Computer Graphics with WebGL Ed Angel, 204 Oblique Projections The OpenGL projection functions cannot produce general parallel projections such as However if we look at the example of the cube it appears that the cube has been sheared Oblique Projection = Shear + Orthogonal Projection Computer Graphics with WebGL Ed Angel, 204 8 General Shear top view side view Computer Graphics with WebGL Ed Angel, 204 9

xy shear (z values unchanged) Shear Matrix H(θ,φ) = Projection matrix General case: P = M orth H(θ,φ) P = M orth STH(θ,φ) Computer Graphics with WebGL Ed Angel, 204 0 Equivalency Computer Graphics with WebGL Ed Angel, 204 Effect on Clipping The projection matrix P = STH transforms the original clipping volume to the default clipping volume object top view z = DOP clipping volume near plane far plane Computer Graphics with WebGL Ed Angel, 204 x = - z = - DOP x = distorted object (projects correctly) 2

Introduction to Computer Graphics with WebGL Ed Angel Perspective Projection Matrices Computer Graphics with WebGL Ed Angel, 204 Simple Perspective Consider a simple perspective with the COP at the origin, the near clipping plane at z = -, and a 90 degree field of view determined by the planes x = ±z, y = ±z Computer Graphics with WebGL Ed Angel, 204 2 Perspective Matrices Simple projection matrix in homogeneous coordinates M = Note that this matrix is independent of the far clipping plane Computer Graphics with WebGL Ed Angel, 204 3

4 Generalization N = after perspective division, the point (x, y, z, ) goes to x = x/z y = y/z Z = -(α+β/z) which projects orthogonally to the desired point regardless of α and β Computer Graphics with WebGL Ed Angel, 204 Picking α and β If we pick α = β = the near plane is mapped to z = - the far plane is mapped to z = and the sides are mapped to x = ±, y = ± Hence the new clipping volume is the default clipping volume Computer Graphics with WebGL Ed Angel, 204 5 Normalization Transformation distorted object projects correctly original clipping volume original object new clipping volume Computer Graphics with WebGL Ed Angel, 204 6

7 Normalization and Hidden-Surface Removal Although our selection of the form of the perspective matrices may appear somewhat arbitrary, it was chosen so that if z > z 2 in the original clipping volume then the for the transformed points z > z 2 Thus hidden surface removal works if we first apply the normalization transformation However, the formula z = -(α+β/z) implies that the distances are distorted by the normalization which can cause numerical problems especially if the near distance is small Computer Graphics with WebGL Ed Angel, 204 WebGL Perspective frustum allows for a nonsymmetric viewing frustum (although perspective does not) Computer Graphics with WebGL Ed Angel, 204 8 OpenGL Perspective Matrix The normalization in frustum requires an initial shear to form a right viewing pyramid, followed by a scaling to get the normalized perspective volume. Finally, the perspective matrix results in needing only a final orthogonal transformation P = NSH our previously defined perspective matrix shear and scale Computer Graphics with WebGL Ed Angel, 204 9

Why do we do it this way? Normalization allows for a single pipeline for both perspective and orthogonal viewing We stay in four dimensional homogeneous coordinates as long as possible to retain threedimensional information needed for hidden-surface removal and shading We simplify clipping Computer Graphics with WebGL Ed Angel, 204 0 Perspective Matrices frustum # 2 * near right left % 0 0 right left right left % % 2 * near top + bottom 0 0 P = % top bottom top bottom % far + near % 0 0 far near % $ 0 0 0 2* far * near far near & ( ( ( ( ( ( ( ' perspective # near % 0 0 0 right % % near 0 0 0 P = % top % far + near % 0 0 far near % $ 0 0 0 2* far * near far near & ( ( ( ( ( ( ( ' Computer Graphics with WebGL Ed Angel, 204

Introduction to Computer Graphics with WebGL Ed Angel Meshes Computer Graphics with WebGL Ed Angel, 204 Meshes Polygonal meshes are the standard method for defining and displaying surfaces - Approximations to curved surfaces - Directly from CAD packages - Subdivision Most common are quadrilateral and triangular meshes - Triangle strips and fans Computer Graphics with WebGL Ed Angel, 204 2 Height Fields For each (x, z) there is a unique y Sampling leads to an array of y values If y ij = f(x i, z j ) then the four points (x i, y ij, z j ), (x (i+), y (i+)j, z j ), (x i, y i(j+), z j+ ), (x (i+), y (i+)(j+), z (j+) form a quadrilateral Display as quadrilateral or triangular mesh using strips Computer Graphics with WebGL Ed Angel, 204 3

Honolulu Plot Using Line Strips Computer Graphics with WebGL Ed Angel, 204 4 Lines on Back and Hidden Faces sombrero or Mexican hat function (sin πr)/(πr) Computer Graphics with WebGL Ed Angel, 204 5 Using Polygons We can use four adjacent data points to form a quadrilateral and thus two triangles which can be shaded Note using a single color for all polygons will not work well - need lighting and shading to see the 3D nature of the data But what if we want to see the grid? - avoids the need for lighting calculations We can display each quadrilateral twice - First as two filled white triangles - Second as a black line loop Computer Graphics with WebGL Ed Angel, 204 6

Hat Using Triangles and Lines Computer Graphics with WebGL Ed Angel, 204 7 Polygon Offset Even though we draw the polygon first followed by the lines, small numerical errors cause some of fragments on the line to be display behind the corresponding fragment on the triangle Polygon offset (gl.polygonoffset) moves fragments slightly away from camera Apply to triangle rendering Computer Graphics with WebGL Ed Angel, 204 8 Hat with Polygon Offset Computer Graphics with WebGL Ed Angel, 204 9

Other Mesh Issues How do we construct a mesh from disparate data (unstructured points) Technologies such as laser scans can produced tens of millions of such points Delaunay triangulation Can we use one triangle strip for an entire 2D mesh? Mesh simplification Computer Graphics with WebGL Ed Angel, 204 0

Introduction to Computer Graphics with WebGL Ed Angel Lighting and Shading Computer Graphics with WebGL Ed Angel, 204 Why we need shading Suppose we build a model of a blue sphere using many polygons and color it using a single color. We get something like But we want something that looks like Computer Graphics with WebGL Ed Angel, 204 2 Shading Why does the image of a real sphere look like Light-material interactions cause each point to have a different color or shade Need to consider - Light sources - Material properties - Location of viewer - Surface orientation Computer Graphics with WebGL Ed Angel, 204 3

4 Scattering Light strikes A - Some scattered - Some absorbed Some of scattered light strikes B - Some scattered - Some absorbed Some of this scattered light strikes A and so on Computer Graphics with WebGL Ed Angel, 204 Rendering Equation The infinite scattering and absorption of light can be described by the rendering equation - Cannot be solved in general - Ray tracing is a special case for perfectly reflecting surfaces Rendering equation is global and includes - Shadows - Multiple scattering from object to object Computer Graphics with WebGL Ed Angel, 204 5 Global Effects shadow translucent surface multiple reflection Computer Graphics with WebGL Ed Angel, 204 6

7 Local vs Global Rendering Correct shading requires a global calculation involving all objects and light sources - Incompatible with pipeline model which shades each polygon independently (local rendering) However, in computer graphics, especially real time graphics, we are happy if things look right - Exist many techniques for approximating global effects Computer Graphics with WebGL Ed Angel, 204 Light-Material Interaction Light that strikes an object is partially absorbed and partially scattered (reflected) The amount reflected determines the color and brightness of the object - A surface appears red under white light because the red component of the light is reflected and the rest is absorbed The reflected light is scattered in a manner that depends on the smoothness and orientation of the surface Computer Graphics with WebGL Ed Angel, 204 8 Light Sources General light sources are difficult to work with because we must integrate light coming from all points on the source Computer Graphics with WebGL Ed Angel, 204 9

Simple Light Sources Point source - Model with position and color - Distant source = infinite distance away (parallel) Spotlight - Restrict light from ideal point source Ambient light - Same amount of light everywhere in scene - Can model contribution of many sources and reflecting surfaces Computer Graphics with WebGL Ed Angel, 204 0 Surface Types The smoother a surface, the more reflected light is concentrated in the direction a perfect mirror would reflected the light A very rough surface scatters light in all directions smooth surface rough surface Computer Graphics with WebGL Ed Angel, 204

Introduction to Computer Graphics with WebGL Ed Angel The Phong Model Computer Graphics with WebGL Ed Angel, 204 Phong Model A simple model that can be computed rapidly Has three components - Diffuse - Specular - Ambient Uses four vectors - To source - To viewer - Normal - Perfect reflector Computer Graphics with WebGL Ed Angel, 204 2 Ideal Reflector Normal is determined by local orientation Angle of incidence = angle of relection The three vectors must be coplanar r = 2 (l n ) n - l Computer Graphics with WebGL Ed Angel, 204 3

4 Lambertian Surface Perfectly diffuse reflector Light scattered equally in all directions Amount of light reflected is proportional to the vertical component of incoming light - reflected light ~cos θ i - cos θ i = l n if vectors normalized - There are also three coefficients, k r, k b, k g that show how much of each color component is reflected We can also add a /d 2 term to account for the distance from the light source Computer Graphics with WebGL Ed Angel, 204 Specular Surfaces Most surfaces are neither ideal diffusers nor perfectly specular (ideal reflectors) Smooth surfaces show specular highlights due to incoming light being reflected in directions concentrated close to the direction of a perfect reflection specular highlight Computer Graphics with WebGL Ed Angel, 204 5 Modeling Specular Relections Phong proposed using a term that dropped off as the angle between the viewer and the ideal reflection increased I r ~ k s I cos α φ reflected shininess coef intensity incoming intensity absorption coef φ Computer Graphics with WebGL Ed Angel, 204 6

7 The Shininess Coefficient Values of α between 00 and 200 correspond to metals Values between 5 and 0 give surface that look like plastic cos α φ -90 Computer Graphics with WebGL Ed Angel, 204 φ 90

Introduction to Computer Graphics with WebGL Ed Angel Applying the Phong Model Computer Graphics with WebGL Ed Angel, 204 Ambient Light Ambient light is the result of multiple interactions between (large) light sources and the objects in the environment Amount and color depend on both the color of the light(s) and the material properties of the object Add k a I a to diffuse and specular terms reflection coef intensity of ambient light Computer Graphics with WebGL Ed Angel, 204 2 Distance Terms The light from a point source that reaches a surface is inversely proportional to the square of the distance between them We can add a factor of the form /(a + bd +cd 2 ) to the diffuse and specular terms The constant and linear terms soften the effect of the point source Computer Graphics with WebGL Ed Angel, 204 3

4 Light Sources In the Phong Model, we add the results from each light source Each light source has separate diffuse, specular, and ambient terms to allow for maximum flexibility even though this form does not have a physical justification Separate red, green and blue components Hence, 9 coefficients for each point source - I dr, I dg, I db, I sr, I sg, I sb, I ar, I ag, I ab Computer Graphics with WebGL Ed Angel, 204 Material Properties Material properties match light source properties - Nine absorbtion coefficients k dr, k dg, k db, k sr, k sg, k sb, k ar, k ag, k ab - Shininess coefficient α Computer Graphics with WebGL Ed Angel, 204 5 Adding Up the Components For each light source and each color component, the Phong model can be written (without the distance terms) as I =k d I d l n + k s I s (v r ) α + k a I a For each color component we add contributions from all sources Computer Graphics with WebGL Ed Angel, 204 6

7 Modified Phong Model The specular term in the Phong model is problematic because it requires the calculation of a new reflection vector and view vector for each vertex Blinn suggested an approximation using the halfway vector that is more efficient Computer Graphics with WebGL Ed Angel, 204 The Halfway Vector h is normalized vector halfway between l and v h = ( l + v )/ l + v Computer Graphics with WebGL Ed Angel, 204 8 Using the halfway vector Replace (v r ) α by (n h ) β β is chosen to match shininess Note that halfway angle is half of angle between r and v if vectors are coplanar Resulting model is known as the modified Phong or Phong-Blinn lighting model - Specified in OpenGL standard Computer Graphics with WebGL Ed Angel, 204 9

Example Only differences in these teapots are the parameters in the modified Phong model Computer Graphics with WebGL Ed Angel, 204 0 Computation of Vectors l and v are specified by the application Can computer r from l and n Problem is determining n For simple surfaces it can be determined but how we determine n differs depending on underlying representation of surface WebGL leaves determination of normals to application Computer Graphics with WebGL Ed Angel, 204 Plane Normals Equation of plane: ax+by+cz+d = 0 We know that plane is determined by three points p 0, p 2, p 3 or normal n and p 0 Normal can be obtained by n = (p 2 -p 0 ) (p -p 0 ) Computer Graphics with WebGL Ed Angel, 204 2

Normal to Sphere Implicit function f(x,y.z)=0 Normal given by gradient Sphere f(p)=p p- n = [ f/ x, f/ y, f/ z] T =p Computer Graphics with WebGL Ed Angel, 204 3 Computing Reflection Direction Angle of incidence = angle of reflection Normal, light direction and reflection direction are coplaner Want all three to be unit length r = 2(l n)n l Computer Graphics with WebGL Ed Angel, 204 4