INFOGR Computer Graphics

Similar documents
INFOGR Computer Graphics

INFOGR Computer Graphics. J. Bikker - April-July Lecture 10: Shading Models. Welcome!

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

Computer Graphics Coursework 1

INFOGR Computer Graphics

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

INFOGR Computer Graphics. J. Bikker - April-July Lecture 10: Ground Truth. Welcome!

INFOGR Computer Graphics

Shaders. Slide credit to Prof. Zwicker

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

CS559 Computer Graphics Fall 2015

Complex Shading Algorithms

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

INFOGR Computer Graphics. Jacco Bikker - April-July Lecture 14: Grand Recap. Welcome!

The Rasterization Pipeline

Practical 1: OpenTK Tutorial

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

CS452/552; EE465/505. Intro to Lighting

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

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

The Projection Matrix

INFOGR Computer Graphics. J. Bikker - April-July Lecture 11: Visibility. Welcome!

Computergraphics Exercise 15/ Shading & Texturing

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

Lecture 7 - Path Tracing

Computer Graphics. Illumination and Shading

CS452/552; EE465/505. Lighting & Shading

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

CS 130 Final. Fall 2015

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

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

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

OPENGL RENDERING PIPELINE

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

CS 4620 Midterm, October 23, 2018 SOLUTION

Lab 9 - Metal and Glass

Programming shaders & GPUs Christian Miller CS Fall 2011

CHAPTER 1 Graphics Systems and Models 3

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

Introduction to Visualization and Computer Graphics

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Advanced Graphics. The Shader knows. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

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

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

INFOGR Computer Graphics. Jacco Bikker - April-July Lecture 3: Ray Tracing (Introduction) Welcome!

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

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

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

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

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

The Projection Matrix

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

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Practical 1: OpenTK Tutorial

CS5620 Intro to Computer Graphics

INFOMAGR Advanced Graphics. Jacco Bikker - February April Welcome!

CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

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

Today. Texture mapping in OpenGL. Texture mapping. Basic shaders for texturing. Today. Computergrafik

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Pipeline Operations. CS 4620 Lecture 14

Tutorial 12: Real-Time Lighting B

Homework 3: Programmable Shaders

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

Computergrafik. Matthias Zwicker. Herbst 2010

Introduction to Computer Graphics with WebGL

Computer Graphics. Lecture 14 Bump-mapping, Global Illumination (1)

Using Shaders for Lighting

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

Ambient Occlusion. Ambient Occlusion (AO) "shadowing of ambient light "darkening of the ambient shading contribution

CS 4620 Program 3: Pipeline

CS 498 VR. Lecture 19-4/9/18. go.illinois.edu/vrlect19

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

Visualisatie BMT. Rendering. Arjan Kok

Parallax Bumpmapping. Whitepaper

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Ch 10: Game Event Management. Quiz # 4 Discussion

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

Movie: For The Birds. Announcements. Ray Tracing 1. Programming 2 Recap. Programming 3 Info Test data for part 1 (Lines) is available

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

The Graphics Pipeline

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

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

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

Shading and Illumination

Introduction to Computer Graphics. Farhana Bandukwala, PhD Lecture 14: Light Interacting with Surfaces

Programming Graphics Hardware

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

Illumination Models & Shading

Ray Tracer Due date: April 27, 2011

CS 432 Interactive Computer Graphics

Models and Architectures

Illumination. Michael Kazhdan ( /657) HB Ch. 14.1, 14.2 FvDFH 16.1, 16.2

Supplement to Lecture 22

Transcription:

INFOGR Computer Graphics Jacco Bikker & Debabrata Panja - April-July 2017 Lecture 10: Shaders Welcome!

INFOGR2016/17

Today s Agenda: Recap: Diffuse Materials The Phong Shading Model Environment Mapping Normal Mapping Rendering Short Fur

INFOGR Lecture 10 Shaders 15 Diffuse Basics of Shading A diffuse material scatters incoming light equally in all directions. Two aspects to this: 1. Diffuse materials are view-independent. 2. We need to know how much light is arriving. Arriving: irradiance, expressed in Joules per second per m 2. Or, for our purposes, per unit area. N L θ cos θ = N L

INFOGR Lecture 10 Shaders 16 Diffuse Basics of Shading So, in the fragment shader: Calculate L: L = lightpos intersectionpoint Use N: already passed via vertex shader Apply attenuation, scale by material color Multiply by light color Emit fragment color. But wait N L θ cos θ = N L

INFOGR Lecture 10 Shaders 17 Diffuse model space Basics of Shading Ehm So, in the fragment shader: Calculate L: L = lightpos intersectionpoint Use N: already passed via vertex shader Apply attenuation, scale by material color Multiply by light color Emit fragment color. model space to screen space But wait We have significant problems: 1. How do we specify a light position 2. In which space do we operate?

INFOGR Lecture 10 Shaders 18 Diffuse Spaces Default matrix in game.cs: Matrix4 transform = Matrix4.CreateFromAxisAngle( new Vector3( 0, 1, 0 ), a ); transform *= Matrix4.CreateTranslation( 0, -4, -15 ); transform *= Matrix4.CreatePerspectiveFieldOfView( 1.2f, 1.3f,.1f, 1000 ); This produces: a teapot that spins around it s pivot a camera located at (0, 4, 15) or the teapot spins at (0, -4, 15), camera is at (0, 0, 0 ). The last line adds perspective. We need a base system in which we can define a light position: world space.

INFOGR Lecture 10 Shaders 19 Diffuse Spaces Getting model space coordinates to world space: Matrix4 transform = Matrix4.CreateFromAxisAngle( new Vector3( 0, 1, 0 ), a ); Matrix4 toworld = transform; transform *= Matrix4.CreateTranslation( 0, -4, -15 ); transform *= Matrix4.CreatePerspectiveFieldOfView( 1.2f, 1.3f,.1f, 1000 ); We need some additional changes now: public void Render( Shader shader, Matrix4 transform, Matrix4 toworld, Texture texture ) {... }

INFOGR Lecture 10 Shaders 20 Diffuse Changes The vertex shader now takes two matrices: // transforms uniform mat4 transform; uniform mat4 toworld; // full transform: model space to screen space // model space to world space...and uses them: gl_position = transform * vec4( vposition, 1.0 ); worldpos = toworld * vec4( vposition, 1.0f ); normal = toworld * vec4( vnormal, 0.0f );

INFOGR Lecture 10 Shaders 21 Diffuse Changes The shader class needs to know about the two matrices: public int uniform_mview; public int uniform_2wrld;... uniform_mview = GL.GetUniformLocation( programid, "transform" ); uniform_2wrld = GL.GetUniformLocation( programid, "toworld" ); And the mesh class needs to pass both to the shader: // pass transforms to vertex shader GL.UniformMatrix4( shader.uniform_mview, false, ref transform ); GL.UniformMatrix4( shader.uniform_2wrld, false, ref toworld );

INFOGR Lecture 10 Shaders 22 Diffuse Changes The new fragment shader, complete: #version 330 in vec2 uv; in vec4 normal; in vec4 worldpos; uniform sampler2d pixels; out vec4 outputcolor; uniform vec3 lightpos; // set the light int lightid = GL.GetUniformLocation( shader.programid, "lightpos" ); GL.UseProgram( shader.programid ); GL.Uniform3( // interpolated texture coordinates lightid, // interpolated normal, world space 0.0f, 10.0f, 0.0f // world space position of fragment ); // texture sampler // shader output // light position in world space In game.cs, Init(): void main() // fragment shader { vec3 L = lightpos - worldpos.xyz; float dist = L.length(); L = normalize( L ); vec3 lightcolor = vec3( 10, 10, 8 ); vec3 materialcolor = texture( pixels, uv ).xyz; float attenuation = 1.0f / (dist * dist); outputcolor = vec4( materialcolor * max( 0.0f, dot( L, normal.xyz ) ) * attenuation * lightcolor, 1 ); }

INFOGR Lecture 10 Shaders 23 Diffuse

Today s Agenda: Recap: Diffuse Materials The Phong Shading Model Environment Mapping Normal Mapping Rendering Short Fur

INFOGR Lecture 10 Shaders 25 Phong Glossy Materials A glossy material reflects, but the reflection is somewhat fuzzy: Using a ray tracer we achieve this effect by sending multiple rays in directions close to the reflection vector R.

INFOGR Lecture 10 Shaders 26 Phong Glossy Materials N

INFOGR Lecture 10 Shaders 27 Phong Glossy Materials Let: L be a vector from the fragment position b to the light; R be the vector V b reflected in the plane with normal N then: V a V b V c if R = L then R L = 1 and: R if R L then R L 1 N a b c

INFOGR Lecture 10 Shaders 28 Phong Glossy Materials Locations near b receive almost as much light as b. But how much? L V α

INFOGR Lecture 10 Shaders 29 Phong The Full Phong M phong = c ambient + c diff N L L diff + c spec (L R) α L spec The Phong material model is a combination of: Specular illumination: (L R) α, times the specular color of the light, times the specular color of the material; Diffuse illumination: N L, times the diffuse color of the light, times the diffuse color of the material; An ambient color.

INFOGR Lecture 10 Shaders 30 Phong

Today s Agenda: Recap: Diffuse Materials The Phong Shading Model Environment Mapping Normal Mapping Rendering Short Fur

INFOGR Lecture 10 Shaders 32 Mirrors Reflections Reflections in a ray tracer are easy: R = L 2(L N)N But what about rasterizers?

INFOGR Lecture 10 Shaders 33 Mirrors Planar Reflections

INFOGR Lecture 10 Shaders 34 Mirrors

INFOGR Lecture 10 Shaders 35 Mirrors Planar Reflections We can fake reflections in a rasterizer by duplicating the scene: The mirror is not really there; it s just a hole through which we see a copy of the scene.

INFOGR Lecture 10 Shaders 36 Mirrors

INFOGR Lecture 10 Shaders 37 Mirrors Environment Mapping Reflections on complex surfaces are faked using an environment map. This is done exactly as in a ray tracer: at the fragment position, we have V and N; based on these we calculate the reflected vector R; we use R to look up a value in the skydome texture. Limitations: we will not reflect anything but the skydome; the reflection is static.

INFOGR Lecture 10 Shaders 38 Mirrors

Today s Agenda: Recap: Diffuse Materials The Phong Shading Model Environment Mapping Normal Mapping Rendering Short Fur

INFOGR Lecture 10 Shaders 40 Bumps Recap: Normal Interpolation

INFOGR Lecture 10 Shaders 41 Bumps Normal Maps A normal map is similar to a texture: we use textures to lookup a color at a particular position on a mesh; we use normal to lookup a normal at a particular position. Normal maps generally store normals in tangent space.

INFOGR Lecture 10 Shaders 42 Bumps Normal Map Data A normal map stores 2 or 3 components per texel. For 3 components: x, y, z [0.. 255]. To get this to the correct range: Cast to float Divide by 128 x, y, z [0.. 2] Subtract 1 x, y, z [ 1.. 1]

INFOGR Lecture 10 Shaders 43 Bumps Normal Map Data A normal map stores 2 or 3 components per texel. For 2 components: x, y [0.. 255]. To reconstruct the normal, we first apply the same conversion as we did for three components. Then: x 2 + y 2 + z 2 = 1 x 2 + y 2 + z 2 = 1 z 2 = 1 (x 2 + y 2 ) z = ± 1 (x 2 + y 2 ) z = 1 (x 2 + y 2 )

INFOGR Lecture 10 Shaders 44 Bumps Tangent Space Things are easy when x = 1 0 0, y = 0 1 0 and z = 0 0 1 N = M x x + M y y + M z z = M x 1 0 0 + M y 0 1 0 + M z 0 0 1 = M x M y M z z M x

INFOGR Lecture 10 Shaders 45 Bumps Tangent Space Things are still easy for arbitrary x, y and z N = M x x + M y y + M z z Obtaining this x, y and z: z = (interpolated) surface normal x is perpendicular to z y is perpendicular to z and x See Crytek for details*. z M x http://docs.cryengine.com/display/sdkdoc4/tangent+space+normal+mapping https://fenix.tecnico.ulisboa.pt/downloadfile/845043405449073/tangent%20space%20calculation.pdf

INFOGR Lecture 10 Shaders 46 Bumps

INFOGR Lecture 10 Shaders 47 Bumps

Today s Agenda: Recap: Diffuse Materials The Phong Shading Model Environment Mapping Normal Mapping Rendering Short Fur

INFOGR Lecture 10 Shaders 49 Fur Fur, Grass etc.

INFOGR Lecture 10 Shaders 50 Fur Fur, Grass etc.

INFOGR Lecture 10 Shaders 51 Fur Fur, Grass etc.

INFOGR Lecture 10 Shaders 52 Fur Fur, Grass etc.

INFOGR Lecture 10 Shaders 53 Fur Fur, Grass etc. #version 330... // shell offset uniform float shelloffset; // vertex shader void main() { gl_position = transform * vec4( vposition + shelloffset * vnormal, 1.0 ); worldpos = toworld * vec4( vposition + shelloffset * vnormal, 1.0f ); normal = toworld * vec4( vnormal, 0.0f ); uv = vuv; }

INFOGR Lecture 10 Shaders 54 Fur Fur, Grass etc. In game.cs: for( int i = 0; i < 30; i++ ) { GL.UseProgram( shader.programid ); GL.Uniform1( offsetid, (float)i * 0.02f ); mesh.render( shader, prevmat[19 - i / 4], prevwld[19 - i / 4], fur ); }

INFOGR Lecture 10 Shaders 55 Fur

INFOGR Computer Graphics Jacco Bikker & Debabrata Panja - April-July 2017 END OF lecture 10: Shaders Next lecture: Visibility