CSE 167: Introduction to Computer Graphics Lecture #5: Illumination Model

Similar documents
CSE 167: Introduction to Computer Graphics Lecture #5: Visibility, OpenGL

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

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

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

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

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

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

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

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

Projection and viewing. Computer Graphics CSE 167 Lecture 4

CMSC427 Shading Intro. Credit: slides from Dr. Zwicker

Lecture 4 of 41. Lab 1a: OpenGL Basics

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

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

CSE 167: Lecture #8: Lighting. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

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

Computer Graphics. Illumination and Shading

CSE 167: Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Pipeline Operations. CS 4620 Lecture 10

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

Illumination & Shading

Comp 410/510 Computer Graphics. Spring Shading

CPSC 314 LIGHTING AND SHADING

Pipeline Operations. CS 4620 Lecture 14

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Shading. Brian Curless CSE 557 Autumn 2017

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Introduction to Visualization and Computer Graphics

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

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

Computer Graphics. Illumination and Shading

w Foley, Section16.1 Reading

Exercise 1 Introduction to OpenGL

The Rasterization Pipeline

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

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

CSE 167: Lecture #4: Vertex Transformation. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

CSE 167: Lecture #8: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Shading I Computer Graphics I, Fall 2008

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

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Rendering: Reality. Eye acts as pinhole camera. Photons from light hit objects

Introduction Rasterization Z-buffering Shading. Graphics 2012/2013, 4th quarter. Lecture 09: graphics pipeline (rasterization and shading)

CSC 8470 Computer Graphics. What is Computer Graphics?

CS 4620 Program 3: Pipeline

Illumination & Shading: Part 1

CMSC427 Advanced shading getting global illumination by local methods. Credit: slides Prof. Zwicker

OpenGl Pipeline. triangles, lines, points, images. Per-vertex ops. Primitive assembly. Texturing. Rasterization. Per-fragment ops.

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

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

CS5620 Intro to Computer Graphics

Models and Architectures

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

Shading. Brian Curless CSE 457 Spring 2017

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

Deferred Rendering Due: Wednesday November 15 at 10pm

Illumination Models & Shading

COMP environment mapping Mar. 12, r = 2n(n v) v

Problem Set 4 Part 1 CMSC 427 Distributed: Thursday, November 1, 2007 Due: Tuesday, November 20, 2007

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

Introduction to Computer Graphics 7. Shading

The Rasterizer Stage. Texturing, Lighting, Testing and Blending

CS 498 VR. Lecture 18-4/4/18. go.illinois.edu/vrlect18

Shading. Reading. Pinhole camera. Basic 3D graphics. Brian Curless CSE 557 Fall Required: Shirley, Chapter 10

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

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

Computer Graphics. Bing-Yu Chen National Taiwan University

Topic 9: Lighting & Reflection models 9/10/2016. Spot the differences. Terminology. Two Components of Illumination. Ambient Light Source

CPSC / Illumination and Shading

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

Draw the basic Geometry Objects. Hanyang University

Topic 9: Lighting & Reflection models. Lighting & reflection The Phong reflection model diffuse component ambient component specular component

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Introduction. Lighting model Light reflection model Local illumination model Reflectance model BRDF

CS452/552; EE465/505. Intro to Lighting

OpenGL refresher. Advanced Computer Graphics 2012

6.837 Introduction to Computer Graphics Assignment 5: OpenGL and Solid Textures Due Wednesday October 22, 2003 at 11:59pm

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

Reading. Shading. An abundance of photons. Introduction. Required: Angel , 6.5, Optional: Angel 6.4 OpenGL red book, chapter 5.

Local Illumination. CMPT 361 Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

Complex Shading Algorithms

Class of Algorithms. Visible Surface Determination. Back Face Culling Test. Back Face Culling: Object Space v. Back Face Culling: Object Space.

CEng 477 Introduction to Computer Graphics Fall

Shading 1: basics Christian Miller CS Fall 2011

Computer Graphics 1 Computer Graphics 1

Classic Rendering Pipeline

Illumination and Shading

CSE 681 Illumination and Phong Shading

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Why we need shading?

Color and Light CSCI 4229/5229 Computer Graphics Fall 2016

Virtual Reality for Human Computer Interaction

Graphics Pipeline & APIs

Graphics Hardware and Display Devices

Homework #2. Shading, Ray Tracing, and Texture Mapping

CS 4600 Fall Utah School of Computing

Transcription:

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

Announcements Tomorrow: assignment 1 due Grading starts at 2pm in labs 260 and 270 Need to upload code to TritonEd by 2pm We ll be grading at least until 4pm 2

Visibility At each pixel, we need to determine which triangle is visible 3

Painter s Algorithm Paint from back to front Need to sort geometry according to depth Every new pixel always paints over previous pixel in frame buffer May need to split triangles if they intersect Intuitive, but outdated algorithm - created when memory was expensive Needed for translucent geometry even today 4

Z-Buffering Store z-value for each pixel Depth test Initialize z-buffer with farthest z value During rasterization, compare stored value to new value Update pixel only if new value is smaller setpixel(int x, int y, color c, float z) if(z<zbuffer(x,y)) then zbuffer(x,y) = z color(x,y) = c z-buffer is dedicated memory reserved in GPU memory Depth test is performed by GPU very fast 5

Z-Buffering in OpenGL In OpenGL applications: Ask for a depth buffer when you create your GLFW window. glfwopenwindow(512, 512, 8, 8, 8, 0, 16, 0, GLFW_WINDOW) Place a call to glenable(gl_depth_test) in your program's initialization routine. Ensure that your znear and zfar clipping planes are set correctly (glm::perspective(fovy, aspect, znear, zfar)) and in a way that provides adequate depth buffer precision. Pass GL_DEPTH_BUFFER_BIT as a parameter to glclear. Note that the z buffer is non-linear: it uses smaller depth bins in the foreground, larger ones further from the camera. 6

Z-Buffer Fighting Problem: polygons which are close together don t get rendered correctly. Errors change with camera perspective flicker Cause: differently colored fragments from different polygons are being rasterized to same pixel and depth not clear which is in front of which Solutions: move surfaces farther apart, so that fragments rasterize into different depth bins bring near and far planes closer together use a higher precision depth buffer. Note that OpenGL often defaults to 16 bit even if your graphics card supports 24 bit or 32 bit depth buffers 7

Translucent Geometry Need to depth sort translucent geometry and render with Painter s Algorithm (back to front) Problem: incorrect blending with cyclically overlapping geometry Solutions: Back to front rendering of translucent geometry (Painter s Algorithm), after rendering opaque geometry Does not always work correctly: programmer has to weigh rendering correctness against computational effort Theoretically: need to store multiple depth and color values per pixel (not practical in real-time graphics) 8

Introduction to OpenGL Using slides from SIGGRAPH course: 9

OpenGL and GLFW Overview What is OpenGL & what can it do for me? OpenGL in windowing systems Why GLFW A GLFW program template 10

What Is OpenGL? Graphics rendering API high-quality color images composed of geometric and image primitives window system independent operating system independent 11

OpenGL as a Renderer Geometric primitives points, lines and polygons Image Primitives images and bitmaps separate pipeline for images and geometry linked through texture mapping Rendering depends on state colors, materials, light sources, etc. 12

Related APIs GLU (OpenGL Utility Library) part of OpenGL NURBS, tessellators, quadric shapes, etc. GLFW (OpenGL Utility Toolkit) portable windowing API not officially part of OpenGL 13

Preliminaries Headers Files #include <GL/gl.h> #include <GL/glu.h> #include <GLFW/glfw3.h> Libraries Enumerated Types OpenGL defines numerous types for compatibility GLfloat, GLint, GLenum, etc. 14

GLFW Basics Application Structure Configure and open window Initialize OpenGL state Enter event processing loop 15

Sample Program #include <GLFW/glfw3.h> int main(void) { GLFWwindow* window; /* Initialize the library */ if (!glfwinit()) return -1; /* Create a windowed mode window and its OpenGL context */ window = glfwcreatewindow(640, 480, "Hello CSE 167", NULL, NULL); if (!window) { glfwterminate(); return -1; } /* Make the window's context current */ glfwmakecontextcurrent(window); /* Initialize OpenGL here */ /* Loop until the user closes the window */ while (!glfwwindowshouldclose(window)) { /* Render here with OpenGL */ /* Swap front and back buffers */ glfwswapbuffers(window); } /* Poll for and process events */ glfwpollevents(); } glfwterminate(); return 0; 16

OpenGL Initialization Set up whatever state you are going to use void init( void ) { glclearcolor( 0.0, 0.0, 0.0, 1.0 ); glcleardepth( 1.0 ); } glenable( GL_LIGHT0 ); glenable( GL_LIGHTING ); glenable( GL_DEPTH_TEST ); 17

Elementary Rendering Geometric Primitives Managing OpenGL State OpenGL Buffers 18

OpenGL Geometric Primitives All geometric primitives are specified by vertices GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON GL_TRIANGLES GL_TRIANGLE_STRIP 19 GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP

OpenGL s State Machine All rendering attributes are encapsulated in the OpenGL State rendering styles texture mapping control of programmable shaders 20

Manipulating the OpenGL State Appearance is controlled by current state for each ( primitive to render ) { update OpenGL state render primitive } 21

Manipulating the OpenGL State Setting the State glpointsize( size ); gllinestipple( repeat, pattern ); Enabling Features glenable( GL_LIGHTING ); gldisable( GL_TEXTURE_2D ); 22

23 Debugging OpenGL Code

OpenGL error state: glgeterror() OpenGL has an error state Use glgeterror() to find location of error. It will clear the error flag. Then gluerrorstring() to parse the error message void printglerror(const char* msg) { const GLenum err = glgeterror(); if(err!= GL_NO_ERROR) { const char* str = (const char*)gluerrorstring(err); cerr << OpenGL error: " << msg << ", " << str << endl; } } 24

Tips for Visual Debugging Collisions, view frustum culling: Show bounding boxes/spheres for all objects Problems with shading: Display normal vectors on vertices as line segments pointing in the direction of the vector. Example: Normal Visualizer (pictured above). Or interpret surface normals as RGB colors by shifting x/y/z range from -1..1 to 0..1. Display direction and other vectors: Display normal vectors as described above. Objects don t get rendered: Normal Visualizer Normal shading Find out if they won t render or are just off screen by temporarily overwriting GL_MODELVIEW and GL_PROJECTION matrices with simpler ones, and/or zooming out by increasing the field of view angle. 25

OpenGL Debugging Tools Overview with many links: https://www.opengl.org/wiki/debugging_tools Nvidia tools (Nsight and others): https://developer.nvidia.com/gameworks-tools-overview 26

27

Shading

Shading Compute interaction of light with surfaces Requires simulation of physics Global illumination Multiple bounces of light Computationally expensive, minutes per image Used in movies, architectural design, etc. 29

Global Illumination 30

Interactive Applications No physics-based simulation Simplified models Reproduce perceptually most important effects Local illumination Only one bounce of light between light source and viewer 31 One bounce of light Surface

Rendering Pipeline Scene data Modeling and viewing transformation Shading Projection Scan conversion, visibility Position object in 3D Determine colors of vertices Per vertex shading Map triangles to 2D Draw triangles Per pixel shading 32 Image

Local Illumination Gives material its color Light can be reflected by Mirror White wall Glossy metal etc. 33

Local Illumination Model reflection of light at surfaces Assumption: no subsurface scattering Bidirectional reflectance distribution function (BRDF) Given light direction, viewing direction, how much light is reflected towards the viewer For any pair of light/viewing directions! 34

Local Illumination Simplified model Sum of 3 components Covers a large class of real surfaces diffuse specular ambient 35

Local Illumination Simplified model Sum of 3 components Covers a large class of real surfaces diffuse specular ambient 36

Diffuse Reflection Ideal diffuse material reflects light equally in all directions View-independent Matte, not shiny materials Paper Unfinished wood Unpolished stone 37

Diffuse Reflection Beam of parallel rays shining on a surface Area covered by beam varies with the angle between the beam and the normal The larger the area, the less incident light per area Incident light per unit area is proportional to the cosine of the angle between the normal and the light rays Object darkens as normal turns away from light Lambert s cosine law (Johann Heinrich Lambert, 1760) Diffuse surfaces are also called Lambertian surfaces n n n 38

Diffuse Reflection Given Unit (normalized!) surface normal n Unit (normalized!) light direction L Material diffuse reflectance (material color) k d Light color (intensity) c l Diffuse color c d is: 39 Proportional to cosine between normal and light

Diffuse Reflection Notes Parameters k d, c l are r,g,b vectors Need to compute r,g,b values of diffuse color c d separately Parameters in this model have no precise physical meaning c l : strength, color of light source k d : fraction of reflected light, material color 40

Diffuse Reflection Provides visual cues Surface curvature Depth variation Lambertian (diffuse) sphere under different lighting directions 41

Local Illumination Simplified model Sum of 3 components Covers a large class of real surfaces diffuse specular ambient 42

Specular Reflection Shiny surfaces Polished metal Glossy car finish Plastics Specular highlight Blurred reflection of the light source Position of highlight depends on viewing direction Specular highlight 43

Specular Reflection Ideal specular reflection is mirror reflection Perfectly smooth surface Incoming light ray is bounced in single direction Angle of incidence equals angle of reflection 44

Projection of vector on another vector 45

Law of Reflection Angle of incidence equals angle of reflection 46

Specular Reflection Many materials are not perfect mirrors Glossy materials 47 Glossy teapot

Glossy Materials Assume surface composed of small mirrors with random orientation (micro-facets) Smooth surfaces Micro-facet normals close to surface normal Sharp highlights Rough surfaces Micro-facet normals vary strongly Blurry highlight 48 Polished Smooth Rough Very rough

Glossy Surfaces Expect most light to be reflected in mirror direction Because of micro-facets, some light is reflected slightly off ideal reflection direction Reflection Brightest when view vector is aligned with reflection Decreases as angle between view vector and reflection direction increases 49

Phong Shading Model Developed by Bui Tuong Phong in1973 Specular reflectance coefficient k s Phong exponent p Greater p means smaller (sharper) highlight 50

Phong Shading Model 51

Blinn Shading Model (Jim Blinn, 1977) Modification of Phong Shading Model Defines unit halfway vector Halfway vector represents normal of micro-facet that would lead to mirror reflection to the eye 52

Blinn Shading Model The larger the angle between micro-facet orientation and normal, the less likely Use cosine of angle between them Shininess parameter s Very similar to Phong Model 53

Local Illumination Simplified model Sum of 3 components Covers a large class of real surfaces diffuse specular ambient 54

Ambient Light In real world, light is bounced all around scene Could use global illumination techniques to simulate Simple approximation Add constant ambient light at each point: k a c a Ambient light color: c a Ambient reflection coefficient: k a Areas with no direct illumination are not completely dark 55

Complete Blinn-Phong Shading Model Blinn-Phong model with several light sources I All colors and reflection coefficients are vectors with 3 components for red, green, blue diffuse specular ambient 56