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

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

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

Stored Texture Shaders

Preparing for Texture Access. Stored Texture Shaders. Accessing Texture Maps. Vertex Shader Texture Access

Introduction to the OpenGL Shading Language

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

OPENGL RENDERING PIPELINE

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

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

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

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

Zeyang Li Carnegie Mellon University

Introduction to Shaders.

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

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

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

Sign up for crits! Announcments

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Shaders. Slide credit to Prof. Zwicker

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

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

OpenGL. Shading Language. Third Edition

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

CMPS160 Shader-based OpenGL Programming

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

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

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

CPSC 436D Video Game Programming

Programming shaders & GPUs Christian Miller CS Fall 2011

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

1 of 5 9/10/ :11 PM

Programmable Graphics Hardware

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

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

Lab 9 - Metal and Glass

Supplement to Lecture 22

CS 354R: Computer Game Technology

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

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

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

Programming Graphics Hardware

Tuesday, 23 March OpenGL Shading Language

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

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

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

Mobile Application Programing: Android. OpenGL Operation

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

12.2 Programmable Graphics Hardware

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

CHAPTER 1 Graphics Systems and Models 3

Lecture 15 of 41. Scene Graphs: State Videos 1: CGA Shorts, Demos

0.Features of the OpenGL Shading Language

Introduction to the OpenGL Shading Language (GLSL)

INF3320 Computer Graphics and Discrete Geometry

0.Features of the OpenGL Shading Language

Programmable Graphics Hardware

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

Dave Shreiner, ARM March 2009

INF3320 Computer Graphics and Discrete Geometry

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

Content. Building Geometry Appearance Lights Model Loaders

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

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

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

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

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

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

Mobile Application Programing: Android. OpenGL Operation

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

Texture Mapping. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Introduction to Computer Graphics with WebGL

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

Computer Graphics Coursework 1

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

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

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

The Rasterization Pipeline

OpenGL shaders and programming models that provide object persistence

Hardware Shading: State-of-the-Art and Future Challenges

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

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

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

Evolution of GPUs Chris Seitz

Advanced Graphics. OpenGL and Shaders II. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

MXwendler Fragment Shader Development Reference Version 1.0

Objectives. Open GL Shading Language (GLSL)

Real-time Graphics 6. Reflections, Refractions

Intro to GPU Programming (OpenGL Shading Language) Cliff Lindsay Ph.D. Student CS WPI

Advanced Deferred Rendering Techniques. NCCA, Thesis Portfolio Peter Smith

Mobile graphics API Overview

Programmable shader. Hanyang University

Programming Graphics Hardware

COMP371 COMPUTER GRAPHICS

Mobile Application Programming: Android. OpenGL Operation

Transcription:

Shader Programming Technical Game Development II Professor Charles Rich Computer Science Department rich@wpi.edu Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book IMGD 4000 (D 10) 1 Shader Programming graphics hardware has replaced fixed functionality with programmability in vertex processing transformation lighting fragment (per-pixel) processing reading from texture memory procedurally computing colors, etc. OpenGL Shading Language (GLSL) is a open standard for programming such hardware other languages, e.g., RenderMan IMGD 4000 (D 10) 2 1

OpenGL Fixed Functionality Pipeline IMGD 4000 (D 10) 3 OpenGL Programmable Processors IMGD 4000 (D 10) 4 2

Vertex Processor per vertex only! IMGD 4000 (D 10) 5 Fragment (Pixel) Processor per pixel only! IMGD 4000 (D 10) 6 3

GLSL Language Similar to C, C++ Builtin vector and matrix operations: vec2, vec3, vec4 mat2, mat3, mat4 Texture lookup sampler1d, sampler2d, sampler3d IMGD 4000 (D 10) 7 Simple Shader Program Example Surface temperature coloring Assume temperature is known at each vertex in model smoothly color surface to indicate temperature at every point (using interpolation) IMGD 4000 (D 10) 8 4

Vertex Shader // parameters read from application (per primitive) uniform float CoolestTemp; uniform float TempRange; // incoming property of this vertex attribute float VertexTemp; // to communicate to the fragment shader varying float Temperature; void main() { // communicate this vertex's temperature scaled to [0.0, 1.0] Temperature = (VertexTemp - CoolestTemp) / TempRange; } // don't move this vertex gl_position = gl_modelviewprojectionmatrix * gl_vertex; IMGD 4000 (D 10) 9 Fragment Shader // parameters read from application (per primitive) uniform vec3 CoolestColor; uniform vec3 HottestColor; // interpolated value from vertex shader varying float Temperature; void main() { // compute a color using built-in mix() function vec3 color = mix(coolestcolor, HottestColor, Temperature); } // set this pixel's color (with alpha blend of 1.0) gl_fragcolor = vec4(color, 1.0); IMGD 4000 (D 10) 10 5

Shader Execution Vertex shader is run once per vertex Fragment shader is run once per pixel Many such executions can happen in parallel No communication or ordering between executions no vertex-to-vertex no pixel-to-pixel IMGD 4000 (D 10) 11 Moving Vertices in Vertex Shader uniform vec3 LightPosition; uniform vec3 SurfaceColor; uniform vec3 Offset; uniform float ScaleIn; uniform float ScaleOut; varying vec4 Color; // color calculation for pixel shader void main() { vec3 normal = gl_normal; vec3 vertex = gl_vertex.xyz + noise3(offset + gl_vertex.xyz * ScaleIn) * ScaleOut; normal = normalize(gl_normalmatrix * normal); vec3 position = vec3(gl_modelviewmatrix * vec4(vertex,1.0)); vec3 lightvec = normalize(lightposition - position); float diffuse = max(dot(lightvec, normal), 0.0); if (diffuse < 0.125) diffuse = 0.125; } Color = vec4(surfacecolor * diffuse, 1.0); gl_position = gl_modelviewprojectionmatrix * vec4(vertex,1.0); IMGD 4000 (D 10) 12 6

Trivial Fragment Shader varying vec4 Color; void main() { gl_fragcolor = Color; } IMGD 4000 (D 10) 13 Procedural Textures - Stripes IMGD 4000 (D 10) 14 7

Fragment Shader for Stripes uniform vec3 StripeColor; uniform vec3 BackColor; uniform vec3 Width; uniform float Fuzz; uniform float Scale; varying vec3 DiffuseColor; varying vec3 SpecularColor; void main() { float scaledt = fract(gl_texcoord[0].t * Scale); } float frac1 = clamp(scaledt / Fuzz, 0.0, 1.0); float frac2 = clamp((scaledt Width) / Fuzz, 0.0, 1.0); vec3 finalcolor = mix(backcolor, StripeColor, frac1) finalcolor = finalcolor * DiffuseColor + SpecularColor; gl_fragcolor = vec4(finalcolor, 1.0); IMGD 4000 (D 10) 15 Loading Shaders in jme Node model = (Node) new BinaryImporter().load( getclass().getresource("king_black.model")); rootnode.attachchild(model); GLSLShaderObjectsState shader = display.getrenderer().createglslshaderobjectsstate(); shader.load(getclass().getresource("noise.vert"), getclass().getresource("noise.frag")); shader.setuniform("lightposition", 0.0f, 0.0f, 4.0f); shader.setuniform("surfacecolor", 1.0f, 1.0f, 1.0f); shader.setuniform("offset", 0.85f, 0.86f, 0.84f); shader.setuniform("scalein", 1.0f); shader.setuniform("scaleout", 1.0f); shader.setenabled(true); rootnode.setrenderstate(shader); IMGD 4000 (D 10) 16 8

Shaders in Other Engines C4 material type selection (predefined shaders) shader editor (restricted functionality) no direct access to shader code Unity predefined shaders can write your own in GLSL IMGD 4000 (D 10) 17 Lots More You Can Do With Shaders Procedural Textures patterns (stripes, etc.) bump mapping Lighting Effects Shadows Surface Effects refraction, diffraction Animation morphing particles IMGD 4000 (D 10) 18 9

Lots More... Anti-aliasing Non-photorealistic effects hatching, meshes technical illustration Imaging sharpen, smooth, etc. Environmental effects (RealWorldz) terrain sky ocean IMGD 4000 (D 10) 19 Shader Programming Seems to lie on the boundary between art and tech programming is hard-core (parallel algorithms) but intended result is often mostly aesthetic IMGD 4000 (D 10) 20 10

4/1/10 Screen shot of the SolidWorks application, showing a jigsaw rendered with OpenGL shaders to simulate a chrome body, galvanized steel housing, and cast iron blade. (Courtesy of SolidWorks Corporation)" IMGD 4000 (D 10) 21 Different glyphs applied to a cube using the glyph bombing shader described in Section 10.6. (3Dlabs, Inc.)" IMGD 4000 (D 10) 22 11

The lattice shader presented in Section 11.3 is applied to the cow model. (3Dlabs, Inc.)" IMGD 4000 (D 10) 23 A simple box and a torus that have been bump-mapped using the procedural method described in Section 11.4. (3Dlabs, Inc.)" IMGD 4000 (D 10) 24 12

A variety of materials rendered with Ward's BRDF model (see Section 14.3) and his measured/fitted material parameters." IMGD 4000 (D 10) 25 Brick shader with and without antialiasing. On the left, the results of the brick shader presented in Chapter 6. On the right, results of antialiasing by analytic integration using the brick shader described in Section 17.4.5. (3Dlabs, Inc.)" IMGD 4000 (D 10) 26 13

A variety of screen shots from the 3Dlabs RealWorldz demo. Everything in this demo is generated procedurally using shaders written in the OpenGL Shading Language. This includes the planets themselves, the terrain, atmosphere, clouds, plants, oceans, and rock formations. Planets are modeled as mathematical spheres, not height fields. These scenes are all rendered at interactive rates on current generation graphics hardware" IMGD 4000 (D 10) 27 14