Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Similar documents
The Transition from RenderMan to the OpenGL Shading Language (GLSL)

Stripes, Rings, and Dots!

Introduction to the OpenGL Shading Language (GLSL)

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

Programming shaders & GPUs Christian Miller CS Fall 2011

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

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

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

Supplement to Lecture 22

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

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

Introduction to the OpenGL Shading Language

± (1./ResS, 1./ResT) Using Fragment Shaders to Manipulate Images. Image Basics. Image Negative. t = 1.

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

Using Fragment Shaders to Manipulate Images

Using Fragment Shaders to Manipulate Images. Mike Bailey. Oregon State University. Image Basics. s = 0.

Bump Mapping. Mike Bailey. What is Bump-Mapping?

E.Order of Operations

Bump Mapping. Mike Bailey. Oregon State University. What is Bump-Mapping?

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

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

Lab 9 - Metal and Glass

GLSL Geometry Shaders

INF3320 Computer Graphics and Discrete Geometry

Sign up for crits! Announcments

Programmable Graphics Hardware

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

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

Graphics Hardware. Computer Graphics COMP 770 (236) Spring Instructor: Brandon Lloyd 2/26/07 1

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Sung-Eui Yoon ( 윤성의 )

Introduction to Shaders.

12.2 Programmable Graphics Hardware

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

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

OpenGL Programmable Shaders

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

Programmable Graphics Hardware

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

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

Rasterization Overview

GPU Programming EE Final Examination

Lecture 09: Shaders (Part 1)

1.2.3 The Graphics Hardware Pipeline

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Dave Shreiner, ARM March 2009

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Graphics Shaders. Theory and Practice. Second Edition. Mike Bailey. Steve Cunningham. CRC Press. Taylor&FnincIs Croup tootutor London New York

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

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

Levy: Constraint Texture Mapping, SIGGRAPH, CS 148, Summer 2012 Introduction to Computer Graphics and Imaging Justin Solomon

3D Scientific Visualization

Computergrafik. Matthias Zwicker. Herbst 2010

Zeyang Li Carnegie Mellon University

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

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

The Graphics Pipeline

GPU Programming EE Final Examination

GPU Programming EE Midterm Examination

Using Shaders for Lighting

Ciril Bohak. - INTRODUCTION TO WEBGL

GLSL v1.20. Scott MacHaffie Schrödinger, Inc.

Pipeline Operations. CS 4620 Lecture 14

Using Shaders for Lighting

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

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

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

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

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

Lecture 2. Shaders, GLSL and GPGPU

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

Artistic Rendering with Graphics Shaders

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

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

CS 130 Final. Fall 2015

Programmable shader. Hanyang University

Programmable GPUs Outline

Advanced Rendering Techniques

GPU Programming EE Final Examination

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

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

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

TSBK03 Screen-Space Ambient Occlusion

OPENGL RENDERING PIPELINE

LSU EE Homework 5 Solution Due: 27 November 2017

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

General Purpose Computation (CAD/CAM/CAE) on the GPU (a.k.a. Topics in Manufacturing)

Triangle Rasterization

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

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

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

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

Rendering Objects. Need to transform all geometry then

1 of 5 9/10/ :11 PM

Transcription:

Introduction to Shaders for Visualization Mike Bailey The Basic Computer Graphics Pipeline Model Transform View Transform Per-vertex Lighting Projection Transform Homogeneous Division Viewport Transform Framebuffer Rasters Ops Fragment Processing, Texturing, Per-fragment Lighting Rasterization 1

MC The Basic Computer Graphics Coordinate Systems WC EC EC CC Model Transform View Transform Per-vertex Lighting Projection Transform Homogeneous Division NDC Viewport Transform Framebuffer SC Rasters Ops SC Fragment Processing, Texturing, Per-fragment Lighting SC SC Rasterization MC = Model Coordinates WC = World Coordinates EC = Eye Coordinates CC = Clip Coordinates NDC = Normalized Device Coordinates SC = Screen Coordinates OpenGL gives you Access to two Transformations MC WC EC EC CC Model View Per-vertex Projection Transform Transform Lighting Transform These two are lumped together into a single matrix called the ModelView Matrix. In GLSL, this is called gl_modelviewmatrix This one is called the Projection Matrix. In GLSL, this is called gl_projectionmatrix GLSL also provides you with these two multiplied together. This is called gl_modelviewprojectionmatrix MC = Model Coordinates WC = World Coordinates EC = Eye Coordinates CC = Clip Coordinates 2

What does a Vertex Shader Do? The basic function of a vertex shader is to take the vertex coordinates as supplied by the application, and perform whatever transformation of them is required. At the same time, the vertex shader can perform various analyses based on those vertex coordinates and prepare variable values for later on in the graphics process. Here s What a Shader Looks Like varying vec4 Color; varying float X, Y, Z; varying float LightIntensity; void main( void ) vec3 TransNorm = normalize( gl_normalmatrix * gl_normal ); vec3 LightPos = vec3( 0., 0., 10. ); vec3 ECposition = ( gl_modelviewmatrix * gl_vertex ).xyz; LightIntensity = dot( normalize(lightpos - ECposition), TransNorm ); LightIntensity = abs( LightIntensity ); Color = gl_color; vec3 MCposition = gl_vertex.xyz; X = MCposition.x; Y = MCposition.y; Z = MCposition.z; gl_position = gl_modelviewprojectionmatrix * gl_vertex; Don t worry about the details right now, just take comfort in the fact that it is C- like and that there appears to be a lot of support routines for you to use 3

A Vertex Shader Replaces These Operations: Vertex transformations Normal transformations Normal normalization Handling of per-vertex lighting Handling of texture coordinates A Vertex Shader Does Not Replace These Operations: View volume clipping Homogeneous division Viewport mapping Backface culling Polygon mode Polygon offset 4

What does a Fragment Shader Do? The basic function of a fragment shader is to take uniform variables, the output from the rasterizer, and texture information and then compute the color of the pixel for each fragment. This figure illustrates this process, showing first how the distinct vertices of a primitive are processed by the rasterizer to form the set of fragments that make up the primitive. A Fragment Shader Replaces These Operations: Color computation Texturing Color arithmetic Handling of per-pixel lighting Fog Blending Discarding fragments 5

A Fragment Shader Does Not Replace These Operations: Stencil test Z-buffer test Stippling Simple Fragment Shader: Setting the Color varying float LightIntensity; uniform vec4 Color; void main( ) gl_fragcolor= vec4( LightIntensity * Color.rgb, 1. ); varying vec3 mycolor; void main(void) gl_fragcolor = vec4( mycolor, 1.0 ); 6

Fragment Shader: Discarding Fragments varying vec4 Color; varying float LightIntensity; uniform float Density; uniform float Frequency; void main( ) vec2 st = gl_texcoord[0].st; vec2 stf = st * Frequency; if( all( fract( stf ) >= Density ) ) discard; gl_fragcolor = vec4( LightIntensity*Color Color.rgb, rgb 1. ); Sample Vertex Shader: Stripes in Model and Eye Coordinates varying vec4 Color; varying float X, Y, Z; varying float LightIntensity; void main( void ) vec3 TransNorm = normalize( gl_normalmatrix * gl_normal ); vec3 LightPos = vec3( 0., 0., 10. ); vec3 ECposition = ( gl_modelviewmatrix * gl_vertex ).xyz; LightIntensity = dot(normalize(lightpos - ECposition), TransNorm); LightIntensity = abs( LightIntensity ); Color = gl_color; vec3 MCposition = gl_vertex.xyz; #ifdef EYE_COORDS X = ECposition.x; Y = ECposition.y; Z = ECposition.z; #endif #ifdef MODEL_COORDS X = MCposition.x; Y = MCposition.y; Z = MCposition.z; #endif gl_position = gl_modelviewprojectionmatrix * gl_vertex; The Fragment shader then sets the color based on the X, value. 7

varying vec4 Color; varying float X, Y, Z; varying float LightIntensity; uniform float A; uniform float P; uniform float Tol; Sample Frament Shader: Stripes in Model and Eye Coordinates void main( void ) const vec3 WHITE = vec4( 1., 1., 1. ); float f = fract( A*X ); float t = smoothstep( 0.5-P-Tol, 0.5-P+Tol, f ) - smoothstep( 0.5+P-Tol, 0.5+P+Tol, f ); vec3 color = mix( WHITE, Color.rgb, t ); gl_fragcolor= vec4( LightIntensity*color, 1. ); Sample Vertex Shader: Stripes in Model and Eye Coordinates The 2 shaders might (momentarily) look the same, but they don t act the same! 8

Per-vertex vs. Per-fragment Lighting In per-vertex lighting, the normal at each vertex is turned into a light intensity. That intensity is then interpolated throughout the polygon. This gives splotchy polygon artifacts, like this. In per-fragment lighting, the normal is interpolated throughout the polygon and turned into a lighted intensity at each fragment. This gives smoother results, like this. Think carefully about what you want as a varying variable it can make a difference! Image Basics Treat the image as a texture and read it into the fragment shader To get from the current texel to a neighboring texel, add ± (1./ResS, 1./ResT) to the current (S,T) ResT ResS 9

Image Negative uniform sampler2d ImageUnit; void main() vec2 st = gl_texcoord[0].st; vec3 rgb = texture2d( ImageUnit, st ).rgb; vec3 neg = vec3(1.,1.,1.) - rgb; gl_fragcolor = vec4( neg, 1. ); Brightness Contrast 10

Saturation Sharpening Edge Detection 11

Using Textures as Data frag file, I uniform sampler2d uniform sampler2d uniform sampler2d uniform float uniform float uniform float uniform float uniform float uniform float uniform float VisibleUnit; InfraRedUnit; WaterVaporUnit; Visible; InfraRed; WaterVapor; VisibleThreshold; InfraRedThreshold; WaterVaporThreshold; Brightness; void main() vec3 visiblecolor = texture2d( VisibleUnit, gl_texcoord[0].st ).rgb; vec3 infraredcolor = texture2d( InfraRedUnit, gl_texcoord[0].st ).rgb; infraredcolor = vec3(1.,1.,1.) - infraredcolor; vec3 watervaporcolor = texture2d( WaterVaporUnit, gl_texcoord[0].st ).rgb; vec3 rgb; frag file, II Using Textures as Data if( visiblecolor.r - visiblecolor.g >.25 && visiblecolor.r - visiblecolor.b >.25 ) rgb = vec3( 1., 1., 0. ); // state outlines become yellow else rgb = Visible*visibleColor + InfraRed*infraredColor + WaterVapor*watervaporColor; rgb /= 3.; vec3 coefs = vec3( 0.296, 0.240, 0.464 ); float visibleinten = dot(coefs,visiblecolor); float infraredinten = dot(coefs,infraredcolor); float watervaporinten = dot(coefs,watervaporcolor); if( visibleinten > VisibleThreshold && infraredinten < InfraRedThreshold && watervaporinten > WaterVaporThreshold ) rgb = vec3( 0., 1., 0. ); else rgb *= Brightness; rgb = clamp( rgb, 0., 1. ); gl_fragcolor = vec4( rgb, 1. ); 12

Using Textures as Data Where is it Likely to Snow? Visible Infrared Water vapor Point Cloud from a 3D Texture Dataset Low values culled Full data 13

frag file uniform float uniform float uniform sampler3d Min; Max; TexUnit; const float SMIN = 0.; const float SMAX = 100.; void main( void ) vec4 rgba = texture3d( TexUnit, gl_texcoord[0].stp ); float scalar = rgba.r; if( scalar < Min ) discard; if( scalar > Max ) discard; float t = ( scalar - SMIN ) / ( SMAX - SMIN ); vec3 rgb = Rainbow( t ); gl_fragcolor = vec4( rgb, 1. ); Visualization Transfer Functions vec3 Rainbow( float t ) t = clamp( t, 0., 1. ); vec3 rgb; // b -> c rgb.r = 0.; rgb.g = 4. * ( t - (0./4.) ); rgb.b = 1.; // c -> g if( t >= (1./4.) ) rgb.r = 0.; rgb.g = 1.; rgb.b = 1. - 4. * ( t - (1./4.) ); // g -> y if( t >= (2./4.) ) rgb.r = 4. * ( t - (2./4.) ); rgb.g = 1.; rgb.b = 0.; vec3 HeatedObject( float t ) t = clamp( t, 0., 1. ); vec3 rgb; rgb.r = 3. * ( t - (0./6.) ); rgb.g = 0.; rgb.b = 0.; if( t >= (1./3.) ) rgb.r = 1.; rgb.g = 3. * ( t - (1./3.) ); if( t >= (2./3.) ) rgb.g = 1.; rgb.b = 3. * ( t - (2./3.) ); // y -> r if( t >= (3./4.) ) rgb.r = 1.; rgb.g = 1. - 4. * ( t - (3./4.) ); rgb.b = 0.; return rgb; 14

Visualization -- Don t Send Colors to the GPU, Send the Raw Data Use the GPU to turn the data into graphics on-the-fly Visualization by Chris Janik 3D Probe Assigning the Transfer Function to Arbitrary Geometry 15

frag file uniform float Min; uniform float Max; uniform sampler3d TexUnit; varying vec4 ECposition; const float SMIN = 0.; const float SMAX = 120.; void main( void ) vec3 stp = clamp( ( ECposition.xyz + 1. ) / 2., 0., 1. ); // maps [-1.,1.] to [0.,1.] vec4 rgba = texture3d( TexUnit, stp ); float scalar = rgba.r; if( scalar < Min ) discard; if( scalar > Max ) discard; float t = ( scalar - SMIN ) / ( SMAX - SMIN ); vec3 rgb = Rainbow( t ); gl_fragcolor = vec4( rgb, 1. ); Cutting Planes 16

frag file uniform float Min; uniform float Max; uniform sampler3d TexUnit; varying vec3 MCposition; const float SMIN = 0.; const float SMAX = 120.; void main( void ) vec3 stp = ( MCposition + 1 ) / 2.; // maps [-1.,1.] to [0.,1.] if( any(stp) < 0. any(stp) > 1. ) discard; vec4 rgba = texture3d( TexUnit, stp ); float scalar = rgba.r; if( scalar < Min scalar > Max ) discard; float t = ( scalar - SMIN ) / ( SMAX - SMIN ); vec3 rgb = Rainbow( t ); //vec3 rgb = HeatedObject( t ); gl_fragcolor = vec4( rgb, 1., ); Volume Rendering Ray Casting 17

Extruding Shapes Along Flow Lines Extruding a block arrow along a spiral flow line Adding moving humps to create a peristaltic effect Bump-Mapping for Terrain Visualization Visualization by Nick Gebbie 18