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

Similar documents
5.2 Shading in OpenGL

Shading , Fall 2004 Nancy Pollard Mark Tomczak

Shading and Illumination

CS 148, Summer 2012 Introduction to Computer Graphics and Imaging

CS Surface Rendering

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

Illumination and Shading

Lighting. CSC 7443: Scientific Information Visualization

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

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

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

Why we need shading?

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

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

Illumination and Shading

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Illumination and Shading ECE 567

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

Lighting/Shading III. Week 7, Wed Mar 3

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

Rendering. Illumination Model. Wireframe rendering simple, ambiguous Color filling flat without any 3D information

INF3320 Computer Graphics and Discrete Geometry

Illumination & Shading: Part 1

Computer Graphics. Illumination and Shading

Display Issues Week 5

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

Illumination and Shading

Lecture 4 Advanced Computer Graphics (CS & SE )

Ligh%ng and Shading. Ligh%ng and Shading. q Is this a plate or a ball? q What color should I set for each pixel?

Light Sources. Spotlight model

Computer Graphics Lighting

Computer Graphics Lighting. Why Do We Care About Lighting?

Outline. Other Graphics Technology. OpenGL Background and History. Platform Specifics. The Drawing Process

Graphics and Computation Introduction to OpenGL

CEng 477 Introduction to Computer Graphics Fall

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

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

Illumination & Shading I

Projections and Hardware Rendering. Brian Curless CSE 557 Fall 2014

CS 432 Interactive Computer Graphics

Pipeline Operations. CS 4620 Lecture 14

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

CS 418: Interactive Computer Graphics. Basic Shading in WebGL. Eric Shaffer

Illumination & Shading

Computer Graphics I Lecture 11

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

Shading. CSE 457 Winter 2015

Introduction to OpenGL

Computer Graphics (CS 543) Lecture 8a: Per-Vertex lighting, Shading and Per-Fragment lighting

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

Lighting. Chapter 5. Chapter Objectives. After reading this chapter, you'll be able to do the following:

Shading. Brian Curless CSE 457 Spring 2015

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

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

Sung-Eui Yoon ( 윤성의 )

Surface Graphics. 200 polys 1,000 polys 15,000 polys. an empty foot. - a mesh of spline patches:

Light Transport Baoquan Chen 2017

Illumination and Shading with GLSL/WebGL

Understand how real-world lighting conditions are approximated by OpenGL

Surface Rendering Methods

LIGHTING AND SHADING

CSE 690: GPGPU. Lecture 2: Understanding the Fabric - Intro to Graphics. Klaus Mueller Stony Brook University Computer Science Department

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

Objectives. Introduce the OpenGL shading Methods 1) Light and material functions on MV.js 2) per vertex vs per fragment shading 3) Where to carry out

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

OpenGL Lighting Computer Graphics Spring Frank Palermo

Pipeline Operations. CS 4620 Lecture 10

Mach band effect. The Mach band effect increases the visual unpleasant representation of curved surface using flat shading.

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

Graphics Processing Units (GPUs) V1.2 (January 2010) Outline. High-Level Pipeline. 1. GPU Pipeline:

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

Shading. Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller/Fuhrmann

CSCI 420 Computer Graphics Lecture 14. Rasterization. Scan Conversion Antialiasing [Angel Ch. 6] Jernej Barbic University of Southern California

Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors Phong model

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

Computer graphics MN1

Virtual Reality for Human Computer Interaction

Rasterization. Rasterization (scan conversion) Digital Differential Analyzer (DDA) Rasterizing a line. Digital Differential Analyzer (DDA)

CS 130 Final. Fall 2015

Illumination Models & Shading

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

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

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

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

IntMu.Lab5. Download all the files available from

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

Graphics for VEs. Ruth Aylett

9. Illumination and Shading

Computer Graphics Coursework 1

[175 points] The purpose of this assignment is to give you practice with shaders in OpenGL.

University of British Columbia CPSC 314 Computer Graphics Sep-Dec Tamara Munzner (guest lecturing) Lighting/Shading

Graphics Processing Units (GPUs) V2.2 (January 2013) Jan Kautz, Anthony Steed, Tim Weyrich

Basic Graphics Programming

Graphics Pipeline & APIs

CGT520 Lighting. Lighting. T-vertices. Normal vector. Color of an object can be specified 1) Explicitly as a color buffer

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

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

Transcription:

CSCI 420 Computer Graphics Lecture 10 Shading in OpenGL Normal Vectors in OpenGL Polygonal Shading Light Source in OpenGL Material Properties in OpenGL Approximating a Sphere [Angel Ch. 6.5-6.9] Jernej Barbic University of Southern California Outline 1 2 Defining and Maintaining Normals Define unit normal before each vertex glnormal3f(nx, ny, nz); glvertex3f(x 1, y 1, z 1 ); glvertex3f(x 2, y 2, z 2 ); glvertex3f(x 3, y 3, z 3 ); same normal for all vertices glnormal3f(nx 1, ny 1, nz 1 ); glvertex3f(x 1, y 1, z 1 ); glnormal3f(nx 2, ny 2, nz 2 ); glvertex3f(x 2, y 2, z 2 ); glnormal3f(nx 3, ny 3, nz 3 ); glvertex3f(x 3, y 3, z 3 ); different normals Normalization Length of normals changes under some modelview transformations (but not under translations and rotations) Ask OpenGL to automatically re-normalize glenable(gl_normalize); Faster alternative (works only with translate, rotate and uniform scaling) glenable(gl_rescale_normal); 3 4 Outline Enabling Lighting and Lights Lighting master switch must be enabled: glenable(gl_lighting); Each individual light must be enabled: glenable(gl_light0); OpenGL supports at least 8 light sources 5 6 1

What Determines Vertex Color in OpenGL Is OpenGL lighting enabled? NO YES Reminder: Phong Lighting Light components for each color: Ambient (L a ), diffuse (L d ), specular (L s ) Material coefficients for each color: Ambient (k a ), diffuse (k d ), specular (k s ) Distance q for surface point from light source Color determined by glcolor3f(...) Ignored: normals lights material properties Color determined by Phong lighting which uses: normals lights material properties l = unit vector to light n = surface normal r = l reflected about n v = vector to viewer See also: http://www.sjbaker.org/steve/omniv/opengl_lighting.html 7 8 Global Ambient Light Set ambient intensity for entire scene GLfloat al[] = {0.2, 0.2, 0.2, 1.0; gllightmodelfv(gl_light_model_ambient, al); The above is default Also: local vs infinite viewer gllightmodeli(gl_light_model_local_viewer, GL_TRUE); Local viewer: Correct specular highlights More expensive, but sometimes more accurate Non-local viewer: Assumes camera is far from object Approximate, but faster (this is default) 9 Defining a Light Source Use vectors {r, g, b, a for light properties Beware: light positions will be transformed by the modelview matrix GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0; GLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0; gllightfv(gl_light0, GL_AMBIENT, light_ambient); gllightfv(gl_light0, GL_DIFFUSE, light_diffuse); gllightfv(gl_light0, GL_SPECULAR, light_specular); gllightfv(gl_light0, GL_POSITION, light_position); 10 Point Source vs Directional Source Directional light given by position vector GLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0; gllightfv(gl_light0, GL_POSITION, light_position); Point source given by position point GLfloat light_position[] = {-1.0, 1.0, -1.0, 1.0; gllightfv(gl_light0, GL_POSITION, light_position); Spotlights Create point source as before Specify additional properties to create spotlight GLfloat sd[] = {-1.0, -1.0, 0.0; gllightfv(gl_light0, GL_SPOT_DIRECTION, sd); gllightf(gl_light0, GL_SPOT_CUTOFF, 45.0); gllightf(gl_light0, GL_SPOT_EXPONENT, 2.0); 11 12 2

Outline Defining Material Properties GLfloat mat_a[] = {0.1, 0.5, 0.8, 1.0; GLfloat mat_d[] = {0.1, 0.5, 0.8, 1.0; GLfloat mat_s[] = {1.0, 1.0, 1.0, 1.0; GLfloat low_sh[] = {5.0; glmaterialfv(gl_front, GL_AMBIENT, mat_a); glmaterialfv(gl_front, GL_DIFFUSE, mat_d); glmaterialfv(gl_front, GL_SPECULAR, mat_s); glmaterialfv(gl_front, GL_SHININESS, low_sh); OpenGL is a state machine: material properties stay in effect until changed. 13 14 Color Material Mode Alternative way to specify material properties Uses glcolor Must be explicitly enabled and disabled glenable(gl_color_material); /* affect all faces, diffuse reflection properties */ glcolormaterial(gl_front_and_back, GL_DIFFUSE); glcolor3f(0.0, 0.0, 0.8); /* draw some objects here in blue */ glcolor3f(1.0, 0.0, 0.0); /* draw some objects here in red */ gldisable(gl_color_material); Outline 15 16 Polygonal Shading Now we know vertex colors either via OpenGL lighting, or by setting directly via glcolor3f if lighting disabled How do we shade the interior of the triangle? Polygonal Shading Curved surfaces are approximated by polygons How do we shade? Flat shading Interpolative shading Gouraud shading Phong shading (different from Phong illumination!)? 17 18 3

Flat Shading Enable with glshademodel(gl_flat); Shading constant across polygon Color of last vertex determines interior color Only suitable for very small polygons Flat Shading Assessment Inexpensive to compute Appropriate for objects with flat faces Less pleasant for smooth surfaces v2 v0 v1 19 20 Interpolative Shading Enable with glshademodel(gl_smooth); Interpolate color in interior Computed during scan conversion (rasterization) Much better than flat shading More expensive to calculate (but not a problem for modern graphics cards) Gouraud Shading Invented by Henri Gouraud, Univ. of Utah, 1971 Special case of interpolative shading How do we calculate vertex normals for a polygonal surface? Gouraud: 1. average all adjacent face normals 2. use n for Phong lighting 3. interpolate vertex colors into the interior Requires knowledge about which faces share a vertex 21 22 Data Structures for Gouraud Shading Sometimes vertex normals can be computed directly (e.g. height field with uniform mesh) More generally, need data structure for mesh Key: which polygons meet at each vertex Phong Shading ( per-pixel lighting ) Invented by Bui Tuong Phong, Univ. of Utah, 1973" At each pixel (as opposed to at each vertex) : 1. Interpolate normals (rather than colors) 2. Apply Phong lighting to the interpolated normal Significantly more expensive Done off-line or in GPU shaders (not supported in OpenGL directly) 23 24 4

Phong Shading Results Michael Gold, Nvidia Polygonal Shading Summary Gouraud shading Set vertex normals Calculate colors at vertices Interpolate colors across polygon Must calculate vertex normals! Must normalize vertex normals to unit length! Single light Phong Lighting Gouraud Shading Two lights Phong Lighting Gouraud Shading Two lights Phong Lighting Phong Shading 25 26 Outline Example: Icosahedron Define the vertices #define X.525731112119133606 #define Z.850650808352039932 static GLfloat vdata[12][3] = { {-X, 0.0, Z, {X, 0.0, Z, {-X, 0.0, -Z, {X, 0.0, -Z, {0.0, Z, X, {0.0, Z, -X, {0.0, -Z, X, {0.0, -Z, -X, {Z, X, 0.0, {-Z, X, 0.0, {Z, -X, 0.0, {-Z, -X, 0.0 ; For simplicity, this example avoids the use of vertex arrays 27 28 Defining the Faces Index into vertex data array static GLuint tindices[20][3] = { {1,4,0, {4,9,0, {4,9,5, {8,5,4, {1,8,4, {1,10,8, {10,3,8, {8,3,5, {3,2,5, {3,7,2, {3,10,7, {10,6,7, {6,11,7, {6,0,11, {6,1,0, {10,1,6, {11,0,9, {2,11,9, {5,2,9, {11,2,7 ; Be careful about orientation! Drawing the Icosahedron Normal vector calculation next glbegin(gl_triangles); for (i = 0; i < 20; i++) { iconormvec(i); glvertex3fv(&vdata[tindices[i][0]] [0]); glvertex3fv(&vdata[tindices[i][1]] [0]); glvertex3fv(&vdata[tindices[i][2]] [0]); glend(); Should be encapsulated in display list 29 30 5

Calculating the Normal Vectors Normalized cross product of any two sides GLfloat d1[3], d2[3], n[3]; void iconormvec (int i) { for (k = 0; k < 3; k++) { d1[k] = vdata[tindices[i][0]] [k] vdata[tindices[i][1]] [k]; d2[k] = vdata[tindices[i][1]] [k] vdata[tindices[i][2]] [k]; normcrossprod(d1, d2, n); glnormal3fv(n); The Normalized Cross Product Omit zero-check for brevity void normalize(float v[3]) { GLfloat d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); v[0] /= d; v[1] /= d; v[2] /= d; void normcrossprod(float u[3], float v[3], float out[3]) { out[0] = u[1]*v[2] u[2]*v[1]; out[1] = u[2]*v[0] u[0]*v[2]; out[2] = u[0]*v[1] u[1]*v[0]; normalize(out); 31 32 The Icosahedron Using simple lighting setup Sphere Normals Set up instead to use normals of sphere Unit sphere normal is exactly sphere point glbegin(gl_triangles); for (i = 0; i < 20; i++) { glnormal3fv(&vdata[tindices[i][0]][0]); glvertex3fv(&vdata[tindices[i][0]][0]); glnormal3fv(&vdata[tindices[i][1]][0]); glvertex3fv(&vdata[tindices[i][1]][0]); glnormal3fv(&vdata[tindices[i][2]][0]); glvertex3fv(&vdata[tindices[i][2]][0]); glend(); 33 34 Icosahedron with Sphere Normals Recursive Subdivision flat shading interpolation General method for building approximations Research topic: construct a good mesh Low curvature, fewer mesh points High curvature, more mesh points Stop subdivision based on resolution Some advanced data structures for animation Interaction with textures Here: simplest case Approximate sphere by subdividing icosahedron 35 36 6

Methods of Subdivision Bisecting angles Computing center Bisecting sides Here: bisect sides to retain regularity Bisection of Sides Draw if no further subdivision requested void subdivide(glfloat v1[3], GLfloat v2[3], GLfloat v3[3], int depth) { GLfloat v12[3], v23[3], v31[3]; int i; if (depth == 0) { drawtriangle(v1, v2, v3); for (i = 0; i < 3; i++) { v12[i] = (v1[i]+v2[i])/2.0; v23[i] = (v2[i]+v3[i])/2.0; v31[i] = (v3[i]+v1[i])/2.0;... 37 38 Extrusion of Midpoints Re-normalize midpoints to lie on unit sphere void subdivide(glfloat v1[3], GLfloat v2[3], GLfloat v3[3], int depth) {... normalize(v12); normalize(v23); normalize(v31); subdivide(v1, v12, v31, depth-1); subdivide(v2, v23, v12, depth-1); subdivide(v3, v31, v23, depth-1); subdivide(v12, v23, v31, depth-1); Start with Icosahedron In sample code: control depth with + and - void display(void) {... for (i = 0; i < 20; i++) { subdivide(&vdata[tindices[i][0]][0], &vdata[tindices[i][1]][0], &vdata[tindices[i][2]][0], depth); glflush(); 39 40 One Subdivision Two Subdivisions Each time, multiply number of faces by 4 flat shading interpolation flat shading interpolation 41 42 7

Three Subdivisions Reasonable approximation to sphere Example Lighting Properties GLfloat light_ambient[]={0.2, 0.2, 0.2, 1.0; GLfloat light_diffuse[]={1.0, 1.0, 1.0, 1.0; GLfloat light_specular[]={0.0, 0.0, 0.0, 1.0; gllightfv(gl_light0, GL_AMBIENT, light_ambient); gllightfv(gl_light0, GL_DIFFUSE, light_diffuse); gllightfv(gl_light0, GL_SPECULAR, light_specular); flat shading interpolation 43 44 Example Material Properties GLfloat mat_specular[]={0.0, 0.0, 0.0, 1.0; GLfloat mat_diffuse[]={0.8, 0.6, 0.4, 1.0; GLfloat mat_ambient[]={0.8, 0.6, 0.4, 1.0; GLfloat mat_shininess={20.0; glmaterialfv(gl_front, GL_SPECULAR, mat_specular); glmaterialfv(gl_front, GL_AMBIENT, mat_ambient); glmaterialfv(gl_front, GL_DIFFUSE, mat_diffuse); glmaterialf(gl_front, GL_SHININESS, mat_shininess); Summary glshademodel(gl_smooth); /*enable smooth shading */ glenable(gl_lighting); /* enable lighting */ glenable(gl_light0); /* enable light 0 */ 45 46 8