Hank Childs, University of Oregon

Slides:



Advertisements
Similar presentations
Hank Childs, University of Oregon Oct. 22 nd, 2014 CIS 441/541: Introduction to Computer Graphics Lecture 7: Arbitrary Camera Positions.
Advertisements

Transformations We want to be able to make changes to the image larger/smaller rotate move This can be efficiently achieved through mathematical operations.
Hank Childs, University of Oregon Oct. 24th, 2014 CIS 441/541: Introduction to Computer Graphics Lecture 8: Arbitrary Camera Positions.
3D Graphics Rendering and Terrain Modeling
Hank Childs, University of Oregon Oct. 17 th, 2014 CIS 441/541: Introduction to Computer Graphics Lecture 6: Lighting Review, Arbitrary Camera Position.
Based on slides created by Edward Angel
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Shading I.
University of New Mexico
Computer Graphics - Class 10
Computer Graphics (Fall 2005) COMS 4160, Lecture 16: Illumination and Shading 1
(conventional Cartesian reference system)
1 CSCE 641: Computer Graphics Lighting Jinxiang Chai.
Objectives Learn to shade objects so their images appear three- dimensional Learn to shade objects so their images appear three- dimensional Introduce.
Coordinate Systems X Y Z (conventional Cartesian reference system) X Y Z.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Introduction to 3D Graphics John E. Laird. Basic Issues u Given a internal model of a 3D world, with textures and light sources how do you project it.
CS 480/680 Computer Graphics Shading I Dr. Frederick C Harris, Jr.
Lecture 5: 3D Rendering Pipeline (II) Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology.
Polygon Shading. Assigning color to a shape to make graphical scenes look realistic, or artistic, or whatever effect we’re attempting to achieve But first.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CS 450: Computer Graphics REVIEW: OVERVIEW OF POLYGONS
Shading (introduction to rendering). Rendering  We know how to specify the geometry but how is the color calculated.
COMPUTER GRAPHICS CS 482 – FALL 2014 AUGUST 27, 2014 FIXED-FUNCTION 3D GRAPHICS MESH SPECIFICATION LIGHTING SPECIFICATION REFLECTION SHADING HIERARCHICAL.
Technology and Historical Overview. Introduction to 3d Computer Graphics  3D computer graphics is the science, study, and method of projecting a mathematical.
Rendering Overview CSE 3541 Matt Boggus. Rendering Algorithmically generating a 2D image from 3D models Raster graphics.
Taku KomuraComputer Graphics Local Illumination and Shading Computer Graphics – Lecture 10 Taku Komura Institute for Perception, Action.
Steve Sterley. Real World Lighting Physical objects tend to interact with light in three ways: Absorption (black body) Reflection (mirror) Transmission.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Basic Rendering Pipeline and Shading Spring 2012.
Hank Childs, University of Oregon Oct. 10, 2014 CIS 441/541: Introduction to Computer Graphics Lecture 4: Interpolating Colors and the Z-buffer.
Realtime NPR Toon and Pencil Shading Joel Jorgensen May 4, 2010.
A Few Things about Graphics Jian Huang Computer Science University of Tennessee.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CS 325 Introduction to Computer Graphics 03 / 29 / 2010 Instructor: Michael Eckmann.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Hank Childs, University of Oregon Volume Rendering, pt 1.
Local Illumination and Shading
1Ellen L. Walker 3D Vision Why? The world is 3D Not all useful information is readily available in 2D Why so hard? “Inverse problem”: one image = many.
Where We Stand So far we know how to: –Transform between spaces –Rasterize –Decide what’s in front Next –Deciding its intensity and color.
Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab.
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
OpenGL Shading. 2 Objectives Learn to shade objects so their images appear three-dimensional Introduce the types of light-material interactions Build.
Lighting and Reflection Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
Computer Graphics Lecture 25 Fasih ur Rehman. Last Class Shading.
Computer Graphics Lecture 30 Mathematics of Lighting and Shading - IV Taqdees A. Siddiqi
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
Computer Graphics (Fall 2006) COMS 4160, Lecture 16: Illumination and Shading 1
Hank Childs, University of Oregon
CPT450 – Computer Graphics
Hank Childs, University of Oregon
Math Fundamentals Maths revisit.
© University of Wisconsin, CS559 Spring 2004
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Computer Graphics Chapter 9 Rendering.
3D Transformations Source & Courtesy: University of Wisconsin,
Hank Childs, University of Oregon
Hank Childs, University of Oregon
3D Graphics Rendering PPT By Ricardo Veguilla.
Hank Childs, University of Oregon
Unit-7 Lighting and Shading
Hank Childs, University of Oregon
CSE 470 Introduction to Computer Graphics Arizona State University
Fundamentals of Computer Graphics Part 6 Shading
Isaac Gang University of Mary Hardin-Baylor
Chapter IX Lighting.
Game Programming Algorithms and Techniques
Computer Graphics Material Colours and Lighting
Computer Graphics (Fall 2003)
CS 480/680 Computer Graphics Shading.
Presentation transcript:

Hank Childs, University of Oregon CIS 441/541: Introduction to Computer Graphics Lecture 8, 9: 10 Math Basics, Lighting Introduction & Phong Lighting Hank Childs, University of Oregon Oct. 17th, 2016

Announcements

Class Cancellation Class cancelled on Monday, October 24th Will do YouTube lecture (if necessary) to stay on track

Project #1D (5%), Due Tues 10/18 Goal: interpolation of color and zbuffer Extend your project1C code File proj1d_geometry.vtk available on web (1.4MB) File “reader1d.cxx” has code to read triangles from file. No Cmake, project1d.cxx

QuackCon Extension If you actively participate, then due October 20th Does not roll over to 1E

Our goal with 1E: add shading

Shading Shading != Shadows Shading & Normals Shading: brighter where light hits square darker where light is tangent Shadow: one object obscures light from getting to another object Shading & Normals Normals – orientation of geometry with respect to light source Key ingredient in recipe for shading (so we will think about normals first and then learn how they are useful for shading after)

Lighting and Normals Two ways to treat normals: Constant over a triangle Varying over a triangle Constant over a triangle  flat shading Varying over a triangle  smooth shading

Flat vs Smooth Shading

Lighting and Normals Two ways to treat normals: Constant over a triangle Varying over a triangle Constant over a triangle  flat shading Take (C-A)x(B-A) as normal over whole triangle Varying over a triangle  smooth shading Calculate normal at vertex, then use linear interpolation How do you calculate normal at a vertex? How do you linearly interpolate normals?

Outline Math Basics Lighting Basics The Phong Model

Shading Our goal: 3 types of lighting to consider: For each pixel, calculate a shading factor Shading factor typically between 0 and 1, but sometimes >1 Shading >1 makes a surface more white 3 types of lighting to consider: Ambient Diffuse Specular Light everwhere Our game plan: Calculate all 3 and combine them. rough surface smooth surface

How to handle shading values greater than 1? Color at pixel = (1.0, 0.4, 0.8) Shading value = 0.5 Easy! Color = (0.5, 0.2, 0.4) (128, 52, 103) Shading value = 2.0 Color = (1.0, 0.8, 1.0)  (255, 204, 255) Color_R = 255*min(1, R*shading_value) This is how bright lights makes things whiter and whiter. But it won’t put in colors that aren’t there.

Ambient Lighting Ambient light Same amount of light everywhere in scene Can model contribution of many sources and reflecting surfaces Surface lit with ambient lighting only

Lambertian Surface Perfectly diffuse reflector Light scattered equally in all directions Extreme zoom-in of part of a diffuse surface … light is scattered in all directions (this image shows 5 of the directions) Slide inspired by Ed Angel Computer Graphics Book

Diffuse Lighting Surface Normal Lambertian Surface

Diffuse Lighting Lambertian Surface Surface Normal No light reflects off the (top) surface (Light direction and surface normal are perpendicular)

Diffuse Lighting Lambertian Surface Surface Normal When the light squarely hits the surface, then that’s when the most light is reflected

How much light should be reflected in this case? Diffuse Lighting Surface Normal N L α Lambertian Surface How much light should be reflected in this case? A: cos(α) And note that: cos(0) = 1 cos(90) = 0

Diffuse Lighting Lambertian Surface V1 Surface Normal V2 N L α How much light makes it to viewer V1? Viewer V2? A: cos(α) for both Lambertian surfaces reflect light equally in all directions

Diffuse Lighting Diffuse light Light distributed evenly in all directions, but amount of light depends on orientation of triangles with respect to light source. Different for each triangle Surface lit with diffuse lighting only

What is a dot product? A.B = A.x*B.x + A.y*B.y Physical interpretation: A.B = cos(α)/(||A||*||B||) (B.x, B.y) (A.x,B.y) α

(assuming L and N are normalized) Diffuse Lighting V1 Surface Normal V2 N L α Lambertian Surface You can calculate the diffuse contribution by taking the dot product of L and N, Since L.N = cos(α) (assuming L and N are normalized)

What about cases where L.N < 0?

What about cases where L.N < 0? Non-sensical … takes away light? Common solution: Diffuse light = max(0, L.N)

But wait… If you have an open surface, then there is a “back face”. The back face has the opposite normal.

But wait… If you have an open surface, then there is a “back face”. The back face has the opposite normal. How can we deal with this case?

But wait… If you have an open surface, then there is a “back face”. The back face has the opposite normal. How can we deal with this case? Idea #1: encode all triangles twice, with different normals Idea #2: modify diffuse lighting model

But wait… If you have an open surface, then there is a “back face”. The back face has the opposite normal. How can we deal with this case? Idea #1: encode all triangles twice, with different normals Idea #2: modify diffuse lighting model Diffuse light = abs(L.N) This is called two-sided lighting

Two-sided lighting We will use two-sided lighting for project 1E, since we have open surfaces Note that Ed Angel book assumes closed surfaces and recommends one-sided lighting

One-sided lighting with open surfaces is disappointing

The most valuable thing I learned in Freshman Physics “angle in = angle out” α

The most valuable thing I learned in Freshman Physics “angle in = angle out” α α

Light reflects in all directions. Specular Lighting Highest proportion of light reflecting Smooth Surface Light reflects in all directions. But the surface is smooth, not Lambertian, so amount of reflected light varies. So how much light??

How much light reflects with specular lighting? Highest proportion of light reflecting V Smooth Surface Consider V located along reflection ray. Answer: most possible Call this “1”

How much light reflects with specular lighting? Highest proportion of light reflecting V Smooth Surface Consider V located along perpendicular ray. Answer: none of it Call this “0”

How much light reflects with specular lighting? Highest proportion of light reflecting V Smooth Surface How much light gets to point V?

How much light reflects with specular lighting? Highest proportion of light reflecting V α Smooth Surface How much light gets to point V? A: proportional to cos(α)

How much light reflects with specular lighting? Highest proportion of light reflecting V α Smooth Surface How much light gets to point V? A: proportional to cos(α) (Shininess strength) * cos(α) ^ (shininess coefficient)

γ: The Shininess Coefficient Values of γ between 100 and 200 correspond to metals Values between 5 and 10 give surface that look like plastic cosγ α γ=1 γ=2 γ=5 -90 f 90 α Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

How much light reflects with specular lighting? Highest proportion of light reflecting V α Smooth Surface How much light gets to point V? A: proportional to cos(α) (Shininess strength) * cos(α) ^ (shininess coefficient)

How much light reflects with specular lighting? Highest proportion of light reflecting (R) V α Smooth Surface Great! We know that cos(α) is V.R.

How much light reflects with specular lighting? Highest proportion of light reflecting (R) V α Smooth Surface Great! We know that cos(α) is V.R. But what is R? It is a formula: R = 2*(L.N)*N - L

Two-sided lighting For specular lighting, we will use one-sided lighting for project 1E It just looks better No “bounce back” light Diffuse: abs(L.N) Specular: max(0, S*(R.V)γ)

Outline Math Basics Lighting Basics The Phong Model

Phong Model Combine three lighting effects: ambient, diffuse, specular

Phong Model Simple version: 1 light, with “full intensity” (i.e., don’t add an intensity term) Phong model Shading_Amount = Ka + Kd*Diffuse + Ks*Specular Signature: double CalculatePhongShading(LightingParameters &, double *viewDirection, double *normal) What should view direction be?

Context Projects 1B-1E all assume geometry has been transformed so that it overlaps with the screen The view direction (needed for many lighting equations) is lost / hard to calculate After the transformation, the view direction is close to (0, 0, -1) We will use this value for 1E Explicitly: use a view direction of (0, 0, -1) for all shading calculations in 1E

A Better Place to Calculate Shading If we have geometry in 3D space, then view direction makes sense (Triangle vertex) minus (camera position) Then we can calculate shading then (in 3D space) And keep the shading value with us as we transform the triangle to overlap with the pixels on the screen This is how GPUs do it We will change our projects to do this more correct approach in 1F And I may switch 1E and 1F in future terms

Lighting parameters

Project #1E (6%) (due Oct 24th) Goal: add Phong shading Extend your project1D code File proj1e_geometry.vtk available on web (9MB) File “reader1e.cxx” has code to read triangles from file. No Cmake, project1e.cxx

Changes to data structures class Triangle { public: double X[3], Y[3], Z[3]; double colors[3][3]; double normals[3][3]; }; reader1e.cxx will not compile until you make these changes reader1e.cxx will initialize normals at each vertex

More comments New: more data to help debug I will make the shading value for each pixel available. I will also make it available for ambient, diffuse, specular. Don’t forget to do two-sided lighting This project in a nutshell: LERP normal to a pixel You all are great at this now!! Add method called “CalculateShading”. My version of CalculateShading is about ten lines of code. Modify RGB calculation to use shading.

Where Hank spent his debugging time… Concave surface Lighting direction Convex surface Lighting direction

Where Hank spent his debugging time…

Now Let’s Start On Arbitrary Camera Positions

Basic Transforms

Starting easy … two-dimensional MP = P’ Matrix M transforms point P to make new point P’. (a b) (x) (a*x + b*y) (c d) X (y) = (c*x + d*y) M takes point (x,y) to point (a*x+b*y, c*x+d*y)

MP = P’ Matrix M transforms point P to make new point P. (1 0) (x) (x) (0 1) X (y) = (y) Identity Matrix

MP = P’ Matrix M transforms point P to make new point P. (x,y) (2x,y) (2 0) (x) (2x) (0 1) X (y) = (y) Scale in X, not in Y

Scale in both dimensions MP = P’ Matrix M transforms point P to make new point P. (x,y) (s 0) (x) (sx) (0 t) X (y) = (ty) (sx,ty) Scale in both dimensions

MP = P’ Matrix M transforms point P to make new point P. (x,y) (0 1) (x) (y) (1 0) X (y) = (x) (y,x) Switch X and Y

Rotate 90 degrees clockwise MP = P’ Matrix M transforms point P to make new point P. (x,y) (0 1) (x) (y) (-1 0) X (y) = (-x) (y,-x) Rotate 90 degrees clockwise

Rotate 90 degrees counter-clockwise MP = P’ Matrix M transforms point P to make new point P. (x,y) (0 -1) (x) (-y) (1 0) X (y) = (x) (-y,x) Rotate 90 degrees counter-clockwise

Rotate “a” degrees counter-clockwise MP = P’ Matrix M transforms point P to make new point P. (x’, y’) (x,y) a (cos(a) sin(a)) (x) (cos(a)*x+sin(a)*y)) (-sin(a) cos(a)) X (y) = (-sin(a)*x+cos(a)*y) Rotate “a” degrees counter-clockwise

Combining transformations How do we rotate by 90 degrees clockwise and then scale X by 2? Answer: multiply by matrix that multiplies by 90 degrees clockwise, then multiple by matrix that scales X by 2. But can we do this efficiently? (0 1) (2 0) (0 1) (-1 0) X (0 1) = (-2 0)

Reminder: matrix multiplication (a b) (e f) (a*e+b*g a*f+b*h) (c d) X (g h) = (c*e+d*g c*f+d*h)

Combining transformations How do we rotate by 90 degrees clockwise and then scale X by 2? Answer: multiply by matrix that rotates by 90 degrees clockwise, then multiply by matrix that scales X by 2. But can we do this efficiently? (0 1) (2 0) (0 1) (-1 0) X (0 1) = (-2 0)

Combining transformations How do we scale X by 2 and then rotate by 90 degrees clockwise? Answer: multiply by matrix that scales X by 2, then multiply by matrix that rotates 90 degrees clockwise. (2 0) (0 1) (0 2) (0 1) X (-1 0) = (-1 0) (0 1) (2 0) (0 1) (-1 0) X (0 1) = (-2 0) Multiply then scale Order matters!!

Translations Translation is harder: (a) (c) (a+c) (b) + (d) = (b+d) But this doesn’t fit our nice matrix multiply model… What to do??

Homogeneous Coordinates (1 0 0) (x) (x) (0 1 0) X (y) = (y) (0 0 1) (1) = (1) Add an extra dimension. A math trick … don’t overthink it.

Homogeneous Coordinates (1 0 dx) (x) (x+dx) (0 1 dy) X (y) = (y+dy) (0 0 1) (1) (1) Translation We can now fit translation into our matrix multiplication system.

Arbitrary Camera Positions

How do we specify a camera? The “viewing pyramid” or “view frustum”. Frustum: In geometry, a frustum (plural: frusta or frustums) is the portion of a solid (normally a cone or pyramid) that lies between two parallel planes cutting it.

New terms Coordinate system: Example: (3, 4, 6) really means… A system that uses coordinates to establish position Example: (3, 4, 6) really means… 3x(1,0,0) 4x(0,1,0) 6x(0,0,1) Since we assume the Cartesian coordinate system

New terms Frame: Cartesian example: (3,4,6) A way to place a coordinate system into a specific location in a space Cartesian example: (3,4,6) It is assumed that we are speaking in reference to the origin (location (0,0,0)). A frame F is a collection (v1, v2, …, vn, O) is a frame over a space if (v1, v2, … vn) form a basis over that space. What is a basis??  linear algebra term

What does it mean to form a basis? For any vector v, there are unique coordinates (c1, …, cn) such that v = c1*v1 + c2*v2 + … + cn*vn

What does it mean to form a basis? For any vector v, there are unique coordinates (c1, …, cn) such that v = c1*v1 + c2*v2 + … + cn*vn Consider some point P. The basis has an origin O There is a vector v such that O+v = P We know we can construct v using a combination of vi’s Therefore we can represent P in our frame using the coordinates (c1, c2, …, cn)

Example of Frames Frame F = (v1, v2, O) What are F’s coordinates for the point (6, 6)?

Example of Frames Frame F = (v1, v2, O) What are F’s coordinates for the point (6, 6)? Answer: (-2, 3)

Our goal Camera space: Camera located at origin, looking down -Z Triangle coordinates relative to camera frame O O World space: Triangles in native Cartesian coordinates Camera located anywhere Image space: All viewable objects within -1 <= x,y,z <= +1 x y z Screen space: All viewable objects within -1 <= x, y <= +1 Device space: All viewable objects within 0<=x<=width, 0 <=y<=height