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

Similar documents
Mobile graphics API Overview

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

12.2 Programmable Graphics Hardware

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

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

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

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

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

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programmable Graphics Hardware

Programming Graphics Hardware

Shaders. Slide credit to Prof. Zwicker

Computer Graphics Coursework 1

OPENGL RENDERING PIPELINE

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

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

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

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

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

EDAF80 Introduction to Computer Graphics. Seminar 1. Hierarchical Transformation. Michael Doggett. Some slides by Carl Johan Gribel,

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

Mobile Application Programing: Android. OpenGL Operation

Zeyang Li Carnegie Mellon University

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

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Introduction to Shaders.

Supplement to Lecture 22

The Rasterization Pipeline

Tutorial 12: Real-Time Lighting B

Lab 9 - Metal and Glass

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

CS559 Computer Graphics Fall 2015

OpenGL shaders and programming models that provide object persistence

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

Mobile Application Programing: Android. OpenGL Operation

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book IMGD 4000 (D 10) 1

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

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

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

object (say a cube) will be made up of triangles, each with three vertices, each with known object coordinates.

Seminar 2. Tessellation & Interpolation. Pierre Moreau. EDAF80 Introduction to Computer Graphics

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

Lecture 2. Shaders, GLSL and GPGPU

Advanced Shading and Texturing

Introductory Seminar

Pipeline Operations. CS 4620 Lecture 14

Introduction to Computer Graphics. Hardware Acceleration Review

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Mobile Application Programming: Android. OpenGL Operation

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

GLSL Programming. Nicolas Holzschuch

Homework 3: Programmable Shaders

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

CS452/552; EE465/505. Lighting & Shading

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

Lecture 09: Shaders (Part 1)

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

COMP371 COMPUTER GRAPHICS

Shaders (some slides taken from David M. course)

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

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

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

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

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

CS 432 Interactive Computer Graphics

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

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

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

Computergraphics Exercise 15/ Shading & Texturing

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

The Graphics Pipeline

Introduction to the OpenGL Shading Language (GLSL)

CS 130 Exam I. Fall 2015

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

CMPS160 Shader-based OpenGL Programming. All slides originally from Prabath Gunawardane, et al. unless noted otherwise

Advanced Rendering & Shading

1 of 5 9/10/ :11 PM

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

The Graphics Pipeline

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

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

Geometry Shaders. And how to use them

Shading Language Update

Objectives. Open GL Shading Language (GLSL)

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

HW-Tessellation Basics

Deferred Rendering Due: Wednesday November 15 at 10pm

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Introduction to GLSL. Announcements. Agenda. Course Contents. Patrick Cozzi University of Pennsylvania CIS Fall 2012

2D graphics with WebGL

Shading and Texturing Due: Friday, Feb 17 at 6:00pm

CS 432 Interactive Computer Graphics

Transcription:

EDAF80 Introduction to Computer Graphics Seminar 3 Shaders Michael Doggett 2017 Slides by Carl Johan Gribel, 2010-13

Today OpenGL Shader Language (GLSL) Shading theory Assignment 3: (you guessed it) writing shaders!

OpenGL Shading Language (GLSL) C like language for programming GPUs Vertex & fragment shaders Labs: GLSL 4.1 (OpenGL 4.1) Have Geometry, Tessellation shaders Newest: GLSL 4.6

GLSL Types Types float, int, bool, vec2, vec3, vec4, mat3 sampler2d, samplercube Type qualifiers const uniform in, out between compile-time constant constant per primitive attributes passed to, from and the vertex & pixel shader in out in Vertex shader Fragment shader out

OpenGL Shading Language Structures and arrays with built in operators vec2, vec3, vec4, mat3, mat4 swizzle: vec3 = vec4.xyz... operator overloading: vec4 = mat4 * vec4 User defined functions Built-in functions sin, cos, pow, normalize, min, max, clamp, reflect, refract, sqrt, noise,...

OpenGL Shading Language Flow control if, if-else, for, while, do-while discard (fragment only) Preprocessor directives #define, #undef, #if, #else, #endif comments: //, /* */

Shader development tools ATI RenderMonkey nvidia FX Composer Mac OpenGL Shader Builder GLSL Devil etc This course: by hand Text-editor in Visual Studio Compiler messages (from the GPU) written to ImGui Log window

diffuse.vert layout (location = 0) in vec3 vertex; layout (location = 1) in vec3 normal; uniform mat4 vertex_model_to_world; uniform mat4 normal_model_to_world; uniform mat4 vertex_world_to_clip; out VS_OUT { vec3 vertex; vec3 normal; } vs_out; void main() { vs_out.vertex = vec3(vertex_model_to_world * vec4(vertex, 1.0)); vs_out.normal = vec3(normal_model_to_world * vec4(normal, 0.0)); } gl_position = vertex_world_to_clip * vertex_model_to_world * vec4(vertex, 1.0); MD16 8

Vertex Shader Input layout (location = 0) in vec3 vertex; layout (location = 1) in vec3 normal; uniform mat4 vertex_model_to_world; uniform mat4 normal_model_to_world; uniform mat4 vertex_world_to_clip; layout specifies the location of input data in the vertex buffer from node.cpp, Node::render. Other mat4 setup there as well MD16 Matrix data is loaded with gluniformmatrix4fv(..) gluniformmatrix4fv(glgetuniformlocation(_program, "vertex_model_to_world"), 1, GL_FALSE, glm::value_ptr(world)); 9

Vertex Shader Output out VS_OUT { vec3 vertex; vec3 normal; } vs_out; void main() { vs_out.vertex = vec3(vertex_model_to_world * vec4(vertex, 1.0)); vs_out.normal = vec3(normal_model_to_world * vec4(normal, 0.0)); } gl_position = vertex_world_to_clip * vertex_model_to_world * vec4(vertex, 1.0); VS_OUT is a struct with the output values This must match the input to the fragment shader MD16 10

Fragment Shader uniform vec3 light_position; in VS_OUT { vec3 vertex; vec3 normal; } fs_in; out vec4 frag_color; void main() { vec3 L = normalize(light_position - fs_in.vertex); frag_color = vec4(1.0) * clamp(dot(normalize(fs_in.normal), L), 0.0, 1.0); } VS_OUT struct matches Vertex Shader output and contains interpolated values vertex is the world position of the current pixel L is the normalised vector to the light MD16 light_position set with gluniform3fv(glgetuniformlocation(program, "light_position"), 1, glm::value_ptr(light_position)); 11

in/out: interpolation over triangle out Vertex shader: out Fragment shader: in out

Today OpenGL Shader Language (GLSL) Shading theory Assignment 3: (you guessed it) writing shaders!

Phong shading Phong shading at pixel: (assuming normalized vectors) color = AmbientColor + DiffuseColor * n L + SpecularColor * (reflect(-l, n) V) Shininess ambient ambient + diffuse V R -L n L ambient + diffuse + specular

Phong shading Phong shading at pixel: (assuming normalized vectors) color = AmbientColor + DiffuseColor * N from L + material, texture, cube map etc SpecularColor * (reflect(-l,n) V) Shininess R -L N L V

Cube mapping N V R -V

Cube mapping: Skybox

Cube mapping: Skybox Attach cube map to large sphere (large enough to contain all scene content) Write a skybox -shader that looks up colors from the cube map in the normal direction Make sure back-face culling is disabled Inside of sphere will appear as surrounding sky/landscape

Adding a Cube Map Complete loadtexturecubemap and use it auto my_cube_map_id = eda221::loadtexturecubemap("sunset_sky/posx.png","sunset_sky/negx.png", "sunset_sky/posy.png","sunset_sky/negy.png", "sunset_sky/posz.png","sunset_sky/negz.png", true); Add a cube map to your current node big_sphere.add_texture( my_cube_map", my_cube_map_id, GL_TEXTURE_CUBE_MAP); node.cpp will bind the cube map and set the uniform (check the code) In your fragment shader add uniform samplercube my_cube_map; Then use it in your fragment shader frag_color = texture(my_cube_map, mytexcoords); MD16 19

Normal mapping Obtain normal from normal-map instead of interpolation from vertices Requires a defined tangent space Interpolated normal Normal from bump-map Bump + texture

Tangent space Basis vectors: tangent t, binormal b, normal n Basis matrix: TBN Derived from the surface equation assignment 2!

Spaces overview Screen space Camera space Projection/viewport matrix World space View matrix Model space World matrix TBN matrix Tangent space

Bump map look-up Normals stored as (r, g, b) where each component range [0,1] Map to [-1, 1]: n = (r, g, b)*2 1 Blue-ish color since n = (0, 0, 1) maps to RGB = (0.5, 0.5, 1)

Bump map normal Normal not ready to use yet; light vector is in world space, so normal must be as well Transform normal from tangent to world space: WorldIT * TBN * n Now we can proceed with light calculations

Bump map: summary Look up (r, g, b) from texture Map from [0, 1] to [-1, 1]: n Transform to world space: WorldIT * TBN * n Use this normal when performing light calculations

Today OpenGL Shader Language (GLSL) Shading theory Assignment 3: (you guessed it) writing shaders!

Assignment 3 Implement the following shader techniques: Texture mapping Phong shading Cube mapping Bump mapping (Normal mapping) Either one ShaderProgram (vs + fs) per technique or one über-shader with all of them simultaneously Use R key to reload shaders

Bump mapping shader All tangent space basis vectors are needed (normal, tangent, binormal) Complete vertex definiton: struct Vertex { f32 x, y, z, /* vertex position */ texx, texy, texz, /* texture coord s */ nx, ny, nz /* normal */ tx, ty, tz /* tangent */ bx, by, bz; /* binormal */ };

Bump mapping shader Hint Set t, b, n as in-attributes to the vertex shader and make them vary per fragment using out Construct TBN-matrix in fragment shader using mat3 Check out res/textures for more textures

The Witness The Witness long screenshot Uses precomputed Global Illumination Sunlight is dynamic, GI is precomputed 30

Today OpenGL Shader Language (GLSL) Shading theory Assignment 3: writing shaders! More questions about code/opengl/glsl? Ask in the forum learnopengl.com