CS 432 Interactive Computer Graphics

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

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

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Shading II. CITS3003 Graphics & Animation

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

CS452/552; EE465/505. Lighting & Shading

CS 4600 Fall Utah School of Computing

Illumination and Shading

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

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

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

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

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

Introduction to Computer Graphics with WebGL

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

5.2 Shading in OpenGL

Computer Graphics Coursework 1

CS 432 Interactive Computer Graphics

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

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

Illumination Models & Shading

CS 432 Interactive Computer Graphics

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

Pipeline Operations. CS 4620 Lecture 14

Texture Mapping. Michael Kazhdan ( /467) HB Ch. 14.8,14.9 FvDFH Ch. 16.3, , 16.6

OpenGL shaders and programming models that provide object persistence

CS Surface Rendering

CS5620 Intro to Computer Graphics

Illumination and Shading with GLSL/WebGL

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

Shading and Illumination

CHAPTER 1 Graphics Systems and Models 3

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

CS 432 Interactive Computer Graphics

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

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

CS 432 Interactive Computer Graphics

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

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

Homework #2. Shading, Projections, Texture Mapping, Ray Tracing, and Bezier Curves

Illumination & Shading I

CS559 Computer Graphics Fall 2015

The Rasterization Pipeline

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Shading. Slides by Ulf Assarsson and Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

Recollection. Models Pixels. Model transformation Viewport transformation Clipping Rasterization Texturing + Lights & shadows

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

Spring 2012 Final. CS184 - Foundations of Computer Graphics. University of California at Berkeley

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

Deferred Rendering Due: Wednesday November 15 at 10pm

To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Computer Graphics

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

Shaders. Slide credit to Prof. Zwicker

CS452/552; EE465/505. Intro to Lighting

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

Homework 3: Programmable Shaders

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

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

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Pipeline Operations. CS 4620 Lecture 10

Why we need shading?

CS 148, Summer 2012 Introduction to Computer Graphics and Imaging

9. Illumination and Shading

Shading. Slides by Ulf Assarsson and Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

Shading 1: basics Christian Miller CS Fall 2011

Computer Graphics I Lecture 11

Interpolation using scanline algorithm

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

CS Illumination and Shading. Slide 1

Shading , Fall 2004 Nancy Pollard Mark Tomczak

Game Graphics & Real-time Rendering

CS 130 Exam I. Fall 2015

Computer Graphics with OpenGL ES (J. Han) Chapter 6 Fragment shader

CS 4620 Program 3: Pipeline

Game Graphics & Real-time Rendering

Visualisatie BMT. Rendering. Arjan Kok

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

OpenGL Lighting Computer Graphics Spring Frank Palermo

SUMMARY. CS380: Introduction to Computer Graphics Texture Mapping Chapter 15. Min H. Kim KAIST School of Computing 18/05/03.

Comp 410/510 Computer Graphics. Spring Shading

-=Bui Tuong Phong's Lighting=- University of Utah, but with shaders. Anton Gerdelan Trinity College Dublin

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

Computer Graphics. Illumination and Shading

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

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

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

1 of 5 9/10/ :11 PM

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

OPENGL RENDERING PIPELINE

CS 130 Final. Fall 2015

Introduction to Programmable GPUs CPSC 314. Real Time Graphics

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

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

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

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

Transcription:

CS 432 Interactive Computer Graphics Lecture 6 Part 2 Lighting and Shading in OpenGL Matt Burlick - Drexel University - CS 430 1

OpenGL Shading Need Vertex Normals Material properties Lights Position of viewer Matt Burlick - Drexel University - CS 430 2

Specifying a Point Light Source For each light sources we can set an RGBA for the diffuse, specular, and ambient components and for the position vec4 diffuse = vec4( ); vec4 ambient = vec4( ); vec4 specular = vec4( ); vec4 light_pos = vec4( ); Matt Burlick - Drexel University - CS 430 3

Distance and Direction The source colors are given in RGBA The position is given in homogenous coordinates If ww = 1, we are specifying a finite location If ww = 0 we are specifying a parallel source with a given direction vector The coefficients in distance terms are usually quadratic 1 dd 2 where dd is the distance from the point being rendered to the light source Matt Burlick - Drexel University - CS 430 4

Material Properties Material properties should match the terms in the light model ww component gives opacity vec4 ambient = vec4( ); vec4 diffuse = vec4( ); vec4 specular = vec4( ) GLfloat shine = 100.0 Matt Burlick - Drexel University - CS 430 5

Emissive Term We can simulate a light source by giving a material an emissive component This component is unaffected by any sources or transformations Matt Burlick - Drexel University - CS 430 6

Our Drawable Objects Note: Now you may want/need to store additional things in your drawable objects Normals (put in some buffer) Material properties Matt Burlick - Drexel University - CS 430 7

Gouraud and Phong Shading There are two common ways to do shading. Based on where to interpolate the shading Ends up with doing our calculations in the vertex shader vs fragment shader Phong Shading Apply modified Phong model at each vertex to get vertex shade Interpolate vertex shades across each polygon (via passing to fragment shader) Gouraud Shading Interpolate vectors need for shading by passing them to fragment shader Apply modified Phong model at each fragment Matt Burlick - Drexel University - CS 430 8

Gouraud vs Phong If the polygon mesh approximates surfaces with high curvature, Gouraud may look smoother Gouraud shading requires much more work Can now be done reasonably in fragment shader Phong Shading Matt Burlick - Drexel University - CS 430 9

Vertex Lighting Shaders 1 The vertex shader continued on next slide Matt Burlick - Drexel University - CS 430 10

Vertex Lighting Shaders II II aa = LL aa kk aa II dd = RR dd LL dd kk dd = ll nn LL dd kk dd II ss = RR ss LL ss kk ss = h nn αα LL ss kk ss Matt Burlick - Drexel University - CS 430 11

Vertex Lighting Shaders IV The fragment shader //frag shader //just the default passthrough in vec4 color; out vec4 fcolor; void(){ fcolor = color; } Matt Burlick - Drexel University - CS 430 12

Shading by Fragment Alternatively we can do per-fragment shading We can compute the pass the vectors (normal, to eye, to light) from the vertex shader to the fragment shader Resulting in interpolated values Then in the fragment shader we compute the fragment s color Matt Burlick - Drexel University - CS 430 13

Fragment Lighting Shaders I The vertex shader Matt Burlick - Drexel University - CS 430 14

Fragment Lighting Shaders III The fragment shader Matt Burlick - Drexel University - CS 430 15

Performance Notes If possible, it may be advantageous to pass in prodambient=lightambient*matambient, etc.. Instead of the light/material properties This way it would only be computed once on the CPU As opposed to being computed for every vertex (or fragment) Matt Burlick - Drexel University - CS 430 16

Example: Shading a Sphere Let s play with shading by shading a sphere First we need to model/build a sphere! In a previous lecture and assignment you saw two ways to generate a sphere Parametrically Recursive Subdivision Either way, in the end we have vertices used to specify triangles. Matt Burlick - Drexel University - CS 430 17

Example: Building a Sphere (header file) Matt Burlick - Drexel University - CS 430 18

Example: Lights class Light { public: Light(vec4 p, vec4 a, vec4 s, vec4 d) : position(p), ambient(a), specular(s), diffuse(d) {} vec4 getposition(){return position;} vec4 getambient(){return ambient;} vec4 getdiffuse(){return diffuse;} vec4 getspecular(){return specular;} private: vec4 position; vec4 ambient; vec4 diffuse; vec4 specular; }; Matt Burlick - Drexel University - CS 430 19

Example: Building a Sphere (Building) Matt Burlick - Drexel University - CS 430 20

Example: Building a Sphere (Building, Continued ) Matt Burlick - Drexel University - CS 430 21

Example: Building a Sphere (Building) void Sphere::build() { sqrt2 = (float)sqrt(2.0); sqrt6 = (float)sqrt(6.0); } index = 0; tetrahedron(4); //assigngouradvertices(); Matt Burlick - Drexel University - CS 430 22

Example: Building a Sphere (material) void Sphere::setMaterial(vec4 a, vec4 d, vec4 s, float sh) { diffuse = d; specular = s; ambient = a; shine = sh; } Matt Burlick - Drexel University - CS 430 23

Example: Building a Sphere (draw) Matt Burlick - Drexel University - CS 430 24

Shading with the Vertex Shader Matt Burlick - Drexel University - CS 430 25

Ways to make this look better? More subdivisions more triangles 4 subdivisions = 3,072 vertices 10 subdivisions = 12,582,912 vertices Matt Burlick - Drexel University - CS 430 26

Ways to make this look better? Use Gouraud s approach Assign a vertex s normal to be the average of the normals assigned to it by different polygons Matt Burlick - Drexel University - CS 430 27

Ways to make this look better? Since we have a formula for a sphere, can we assign normals by a formula? We know the parametric form of a sphere: pp θθ, φφ = xx yy zz = ccccccccccccccφφ ssssssθθssssssss cccccccc To get the normal at point pp we can take the cross product of the gradient with respect to each parameter: nn = dddd dddd dddd dddd For our equation of a sphere this becomes ccccccccccccccφφ nn = ssssssφφ ssssssθθssssssss cccccccc = sssssssspp ssssssss is just a scalar so our unit normal vector is just: nn = pp Matt Burlick - Drexel University - CS 430 28

Ways to make this look better? So let s just assign each vertex s normal to be the normalized value of it s location Matt Burlick - Drexel University - CS 430 29

Multiple Lights Often we ll have multiple lights in a scene In fact this will be asked of you in a homework assignment. Some shader versions allow us to create structures in our shaders However that s not supported in GLSL version 1.3 Also some shader versions allow for looping through arrays of structures However this tends to work system-to-system So to ensure maximum compatibility, we ll just hardcode in support for multiple lights Matt Burlick - Drexel University - CS 430 30

Multiple Lights #version 150 uniform vec4 lightpos1; uniform vec4 lightambient1, lightdiffuse1, lightspecular1; uniform int enabled1; uniform vec4 lightpos2; uniform vec4 lightambient2, lightdiffuse2, lightspecular2; uniform int enabled2; out vec4 color; void main(){ vec4 color1 = vec4(0,0,0,0); if(enabled1==1) color1= //compute color due to light1 vec4 color2 = vec4(0,0,0,0); if(enabled2==1) color2= //compute color due to light2 color=color1+color2; color.a=1.0; } Matt Burlick - Drexel University - CS 430 31