Illumination and Reflection in OpenGL CS 460/560. Computer Graphics. Shadows. Photo-Realism: Ray Tracing. Binghamton University.

Similar documents
Photorealism. Photorealism: Ray Tracing. Ray Tracing

Photorealism CS 460/560. Shadows Ray Tracing Texture Mapping Radiosity. Computer Graphics. Binghamton University. EngiNet. Thomas J.

Photorealism. Ray Tracing Texture Mapping Radiosity

General Hidden Surface Removal Algorithms. Binghamton University. EngiNet. Thomas J. Watson. School of Engineering and Applied Science CS 460/560

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

Shading and Illumination

Illumination Model. The governing principles for computing the. Apply the lighting model at a set of points across the entire surface.

Illumination and Shading

Methodology for Lecture. Importance of Lighting. Outline. Shading Models. Brief primer on Color. Foundations of Computer Graphics (Spring 2010)

CS 488. More Shading and Illumination. Luc RENAMBOT

9. Illumination and Shading

Illumination and Shading

Illumination & Shading I

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

CS 4731: Computer Graphics Lecture 16: Phong Illumination and Shading. Emmanuel Agu

Computer Graphics. Illumination and Shading

Shading in OpenGL. Outline. Defining and Maintaining Normals. Normalization. Enabling Lighting and Lights. Outline

Topic 12: Texture Mapping. Motivation Sources of texture Texture coordinates Bump mapping, mip-mapping & env mapping

Introduction to OpenGL

Illumination and Shading

5.2 Shading in OpenGL

Announcements. Written Assignment 2 out (due March 8) Computer Graphics

Photorealism: Ray Tracing

INF3320 Computer Graphics and Discrete Geometry

CEng 477 Introduction to Computer Graphics Fall

Why we need shading?

Illumination & Shading: Part 1

Virtual Reality for Human Computer Interaction

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

Surface Rendering Methods

Topic 11: Texture Mapping 11/13/2017. Texture sources: Solid textures. Texture sources: Synthesized

Computer Graphics. Lecture 13. Global Illumination 1: Ray Tracing and Radiosity. Taku Komura

Topic 11: Texture Mapping 10/21/2015. Photographs. Solid textures. Procedural

LIGHTING AND SHADING

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

CS Surface Rendering

Sung-Eui Yoon ( 윤성의 )

Virtual Reality for Human Computer Interaction

Ray Tracing. CSCI 420 Computer Graphics Lecture 15. Ray Casting Shadow Rays Reflection and Transmission [Ch ]

Lighting/Shading III. Week 7, Wed Mar 3

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

Lecture 15: Shading-I. CITS3003 Graphics & Animation

CS Computer Graphics: Hidden Surface Removal

Graphics for VEs. Ruth Aylett

CS5620 Intro to Computer Graphics

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

Global Rendering. Ingela Nyström 1. Effects needed for realism. The Rendering Equation. Local vs global rendering. Light-material interaction

Shading , Fall 2004 Nancy Pollard Mark Tomczak

Computer Graphics. Lecture 10. Global Illumination 1: Ray Tracing and Radiosity. Taku Komura 12/03/15

Ambient reflection. Jacobs University Visualization and Computer Graphics Lab : Graphics and Visualization 407

Shading Intro. Shading & Lighting. Light and Matter. Light and Matter

Illumination and Shading ECE 567

CS 148, Summer 2012 Introduction to Computer Graphics and Imaging

COMP371 COMPUTER GRAPHICS

Supplement to Lecture 16

CS 325 Computer Graphics

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

I have a meeting with Peter Lee and Bob Cosgrove on Wednesday to discuss the future of the cluster. Computer Graphics

Illumination & Shading

Reflection and Shading

Lighting and Shading

Introduction to 3D Graphics with OpenGL. Z-Buffer Hidden Surface Removal. Binghamton University. EngiNet. Thomas J. Watson

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

Ray Tracer Due date: April 27, 2011

Projections and Hardware Rendering. Brian Curless CSE 557 Fall 2014

Computer Graphics Lighting

Computer Graphics Lighting. Why Do We Care About Lighting?

Lighting. CSC 7443: Scientific Information Visualization

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

Photorealistic 3D Rendering for VW in Mobile Devices

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

So far, we have considered only local models of illumination; they only account for incident light coming directly from the light sources.

Chapter 7 - Light, Materials, Appearance

Lighting. To do. Course Outline. This Lecture. Continue to work on ray programming assignment Start thinking about final project

Graphics Pipeline & APIs

Models and Architectures

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

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

Lecture 4 Advanced Computer Graphics (CS & SE )

Local Illumination physics. Law of reflection and Snell s law of refraction

OpenGL Lighting Computer Graphics Spring Frank Palermo

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Pipeline Operations. CS 4620 Lecture 10

Rendering. Mike Bailey. Rendering.pptx. The Rendering Equation

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

Graphics for VEs. Ruth Aylett

Graphics Pipeline & APIs

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

Rendering Part I (Basics & Ray tracing) Lecture 25 December 1, 2015

521493S Computer Graphics. Exercise 3

Display Issues Week 5

Assignment 6: Ray Tracing

Interpolation using scanline algorithm

Chapter 11 Global Illumination. Part 1 Ray Tracing. Reading: Angel s Interactive Computer Graphics (6 th ed.) Sections 11.1, 11.2, 11.

Computer graphics Labs: OpenGL (1/3) Geometric transformations and projections

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Light Transport Baoquan Chen 2017

Volume Rendering. Computer Animation and Visualisation Lecture 9. Taku Komura. Institute for Perception, Action & Behaviour School of Informatics

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

Transcription:

Binghamton University EngiNet State University of New York EngiNet Thomas J. Watson School of Engineering and Applied Science WARNING All rights reserved. No Part of this video lecture series may be reproduced in any form or by any electronic or mechanical means, including the use of information storage and retrieval systems, without written approval from the copyright owner. 2007 The Research Foundation of the State University of New York CS 460/560 Computer Graphics Professor Richard Eckert May 2, 2007 Illumination and Reflection in OpenGL Shadows Photo-Realism: Ray Tracing 1

Illumination & Reflection in OpenGL OpenGL Uses the Phong Illumination/Reflection Model Final Phong Illumination/Reflection Model Result (Single White Light Source) Three color intensity equations: I(r,g,b) = Ambient + Point Diffuse + Point Specular I(r,g,b) =kd(r,g,b)*ia + Ip*kd(r,g,b)*(N. L) + Ip*ks*(R. V) n OpenGL generalizes this to include colored light sources Illumination & Reflection in OpenGL Define Light Sources Define Material Properties Define Normal Vectors to Polygons Specify Shading Model Enable Depth Testing (Z-Buffer) Lighting OpenGL supports 4 types of light: Ambient Diffuse Specular Emitted Can be up to 8 different light sources Defining a Light Source Set up Arrays of lighting values Intensities: GLfloat amblight0[ ] = {0.3f, 0.3f, 0.3f, 1.0f}; // R,G,B,? GLfloat difflight0[ ] = {0.5f, 0.5f, 0.5f, 1.0f}; GLfloat speclight0[ ] = {0.0f, 0.0f, 0.0f, 1.0f}; Position: GLfloat posnlight0[ ] = {1.0f, 1.0f, 1.0f, 0.0f); // x,y,z,w Pass Arrays to OpenGL gllightfv(gl_light0, GL_AMBIENT, amblight0); gllightfv(gl_light0, GL_DIFFUSE, difflight0); gllightfv(gl_light0, GL_SPECULAR, speclight0); gllightfv(gl_light0, GL_POSITION, posnlight0); Enabling a Light Source Turn on Lighting glenable(gl_lighting); Turn on a Light Source glenable(gl_light0); 2

Material Reflection Properties Ambient Diffuse These are usually the same Specular Material Reflection Properties Set up Material Arrays ambient/diffuse reflection coefficients GLfloat mat_ambdiff[ ] = {0.0f, 0.7f, 0.0f, 1.0f}; // diff. refl. coeffs. // 70% of green light reflected diffusely, no red or blue specular reflection coefficient GLfloat mat_spec[ ] = {1.0f, 1.0f, 1.0f, 1.0f}; // spec. refl. coeffs. // bright white light reflected specularly(100% R, G, B) Pass Material Arrays to OpenGL glmaterialfv(gl_front, GL_AMBIENT_AND_DIFFUSE, mat_ambdiff); glmaterialfv(gl_front, GL_SPECULAR, mat_spec); glmaterialf(gl_front, GL_SHININESS, 20.0f); // last parameter: specular exponent (0-128) Defining Normals Must compute normals for all polygons OpenGL has no function to do that So write your own See notes from last class Assume the result is: double n[3]; Use this when you define the polygon glbegin(gl_polygon) glnormal3f ( (GLfloat)n[0], (GLfloat)n[1], (GLfloat)n[2] ); // glvertex3f() calls here for polygon vertices glend(); Specify a Shading Model & Enable Depth Testing glshademodel(gl_flat); // use GL_SMOOTH // for Gouraud shading glenable(gl_depth_test); glclear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT); // clear frame buffer and z-buffer Some sample code - view class::ondraw() glshademodel(gl_smooth); glenable(gl_depth_test); glclearcolor(1.0f, 1.0f, 1.0f, 1.0f); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_modelview); glloadidentity(); glmaterialfv(gl_front, GL_AMBIENT_AND_DIFFUSE, mat_ambdiff); glmaterialfv(gl_front, GL_SPECULAR, mat_spec); // The lighting.. glmaterialf(gl_front, GL_SHININESS, 20.0f); // and material.. gllightfv(gl_light0, GL_AMBIENT, amblight0); // arrays were.. gllightfv(gl_light0, GL_DIFFUSE, difflight0); // set up.. gllightfv(gl_light0, GL_SPECULAR, speclight0); // before this.. gllightfv(gl_light0, GL_POSITION, posnlight0); // code. glenable(gl_lighting);glenable(gl_light0); DrawCube(); // Helper function to define cube vertices/polygons/normals glflush(); Code from DrawCube() function gltranslatef(0.0f, 0.0f, -3.0f); // position cube inside viewing volume glrotatef(20.0f, 1.0f, 0.0f, 0.0f); glrotatef(20.0f, 0.0f, 1.0f, 0.0f); // Draw the polygons of the cube, only front face is given here: double p1[] = {-0.5, 0.5, 0.5}; double p2[] = {-0.5, -0.5, 0.5}; double p3[] = {0.5, -0.5, 0.5}; double n[3]; CalcNormal(p1, p2, p3, n); glbegin(gl_polygon); // only 1 face here, other 5 must be defined glnormal3f((glfloat)n[0], (GLfloat)n[1], (GLfloat)n[2]); glvertex3f(-0.5f, 0.5f, 0.5f); glvertex3f(-0.5f, -0.5f, 0.5f); glvertex3f(0.5f, -0.5f, 0.5f); glvertex3f(0.5f, 0.5f, 0.5f); glend(); 3

Code from CalcNormal (double *p1, double *p2, double *p3, double *n) // Form two vectors from the points. double a[3], b[3]; a[0] = p2[0] - p1[0]; a[1] = p2[1] - p1[1]; a[2] = p2[2] - p1[2]; b[0] = p3[0] - p1[0]; b[1] = p3[1] - p1[1]; b[2] = p3[2] - p1[2]; // Calculate the cross product of the two vectors. n[0] = a[1] * b[2] - a[2] * b[1]; n[1] = a[2] * b[0] - a[0] * b[2]; n[2] = a[0] * b[1] - a[1] * b[0]; // Normalize the new vector. double length = sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]); n[0] = n[0] / length; n[1] = n[1] / length; n[2] = n[2] / length; Shadows Very important to our perception of depth Shadow position/orientation give information as to how objects relate to each other in space Sharp Shadows from Point Sources Soft Shadows from Extended Sources Shadows from Point Sources Look at shadows from point sources If a point is in shadow, set Phong Ip to 0 Source gets no light from point source So no reflection from point source Still must include ambient term Lots of algorithms One of simplest: Shadow Z-Buffer 4

Shadow Z-Buffer Algorithm A two-stage process 1. Take Light Source as viewpoint & compute depths Store results in shadow Z-buffer Z [x ][y ] Each Z [x ][y ] will contain distance of closest surface to light source 2. Normal Z-Buffer rendering But if (x,y) is closest (visible), transform to light space coordinates (x,y,z ) If z > Z [x ][y ] point is in shadow Some object is closer to light & will block it So only include ambient term in computation Shadow Z-Buffer Set up shadow Z-buf Z [x ][y ] using coordinate system whose origin is at light source Z-buf [x][y]=infinity for all x,y for each polygon for each pixel x,y calculate z if z < Z-buf [x][y] transform x,y,z to light coord space x,y,z if z > Z [x ][y ] reduce intensity (include only ambient) Z-buf [x][y]=z;fb[x][y]=intensity Photorealism Ray Tracing Texture Mapping Radiosity Photorealism-- Taking into Account Global Illumination Light can arrive at surfaces indirectly This light called global illumination To now we ve approximated it with a constant, diffuse ambient term This is wrong Need to take into account all the multiply reflected light in the scene Two different approaches: Ray Tracing -- specularly reflected light Radiosity -- diffusely reflected light Photorealism: Ray Tracing See CS-460/560 Notes at: http://www.cs.binghamton.edu/~reckert/460/raytrace.htm http://www.cs.binghamton.edu/~reckert/460/texture.htm Persistence of Vision Ray Tracer (free): http://povray.org/ 5

Ray Tracing What is seen by viewer depends on: rays of light that arrive at his/her eye So to get correct results: Follow all rays of light from all light sources Each time one hits an object, compute the reflected color/intensity Store results for those that go through projection plane pixels into observer s eye Paint each pixel in the resulting color Forward Ray Tracing Infinite number of rays from each source At each intersection with an object could have an infinite number of reflected rays Completely intractable Would take geological times to compute Backward Ray Tracing Look only at rays observer sees Follow rays backwards from eye point through pixels on screen Check if they intersect objects If so, can intersection point see a light source? If so, compute intensity of reflected light If not, point is in the shadow If object has reflectivity/transparency Follow reflected/transmission rays Simple Ray Tracing Set up scene (position objects and light sources) For each pixel on screen Form eye ray --vector from viewpoint through pixel For each object in scene If object is intersected by eye ray & is closest, Store intersection point & object ID Apply illumination model to closest intersection point Plot pixel in resulting color (Another way of doing hidden surface Removal) Difficulty is getting intersection points for complex objects 6

Recursive Ray Tracing Good for scenes with lots of specular reflection and transparency Also gives shadows automatically At each intersection point send out: Shadow feeler rays toward light sources Reflection rays in ideal reflection direction Transmission rays in refraction direction Treat last two as eye rays Recursive algorithm Recursive Ray Tracing Algorithm depth = 0 for each pixel (x,y) Calculate direction from eyepoint to pixel TraceRay (eyepoint, direction, depth, *color) FrameBuf [x,y] = color TraceRay(start_pt, direction_vector, recur_depth, *color) if (recur_depth > MAXDEPTH) color = Background_color else // Intersect ray with all objects // Find intersection point that is closest to start_point if (no intersection) color = Background_color else // Send out shadow rays toward light sources local_color = contribution of illumination model at int. pt. // Calculate direction of reflection ray TraceRay(int_pt, refl_dir., depth+1, *refl_color) // Calculate direction of transmitted ray TraceRay (int_pt., trans_dir., depth+1, *trans_color) color = Combine (local_color,refl_color, trans_color) Combining Color from Reflection Ray Add attenuated reflected color intensity to local color intensity: color = local_color + k * refl_color here refl_color is I(r,g,b) - color returned by reflection ray local_color is I(r,g,b) - color computed by illumination model at intersection point k is an attenuation factor (<1) Combining Color from Transmission Ray Observer sees a mixture of light reflected off surface and light transmitted through surface So combine colors (interpolate) I(r,g,b) = k *I local (r,g,b) + (1 - k )*I transmitted (r,g,b) k is opacity factor coefficient k =0 => perfectly transparent, k =1 ==> perfectly opaque 7