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

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

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

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

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

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

Supplement to Lecture 22

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

Programmable Graphics Hardware

Lecture 09: Shaders (Part 1)

12.2 Programmable Graphics Hardware

Programming shaders & GPUs Christian Miller CS Fall 2011

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

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

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

Programmable shader. Hanyang University

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

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

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

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

Objectives. Open GL Shading Language (GLSL)

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

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

Lighting and Texturing

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

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

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

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

Introduction to the OpenGL Shading Language (GLSL)

OpenGL Programmable Shaders

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

Programmable Graphics Hardware

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

CPSC 436D Video Game Programming

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

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

Sign up for crits! Announcments

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Vertex & Fragment shaders

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

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

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

GPU Programming EE Final Examination

Lab 9 - Metal and Glass

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

An Introduction to 3D Computer Graphics, Stereoscopic Image, and Animation in OpenGL and C/C++ Fore June

1 of 5 9/10/ :11 PM

Stored Texture Shaders

Computergraphics Exercise 15/ Shading & Texturing

2D graphics with WebGL

CMPS160 Shader-based OpenGL Programming

Tuesday, 23 March OpenGL Shading Language

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

- Surface Rendering -

Programmable GPUs Outline

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

Introduction to Shaders.

0.Features of the OpenGL Shading Language

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

OpenGL shaders and programming models that provide object persistence

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

0.Features of the OpenGL Shading Language

Programmable Shading. University of Texas at Austin CS354 - Computer Graphics

Shaders. Slide credit to Prof. Zwicker

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

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

Yazhuo Liu Homework 3

CS 432 Interactive Computer Graphics

COMP371 COMPUTER GRAPHICS

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

To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Foundations of Computer Graphics (Spring 2012)

Mobile Application Programing: Android. OpenGL Operation

1)Write a shader program that renders a regular pentagon with textured image like the one shown below.

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

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

Computer Graphics (CS 4731) & 2D Graphics Systems

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

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

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

CS179: GPU Programming

GLSL Language Details

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

Design Focus. GLSL Language Details. Additions from C++ Additions for Graphics

GLSL: Creating GLSL Programs - Overview

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

Objectives. Coupling GLSL to Applications Example applications

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

INFOGR Computer Graphics

GPU Programming EE Final Examination

OPENGL RENDERING PIPELINE

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

Waves, Displacement, Reflections. Arun Rao CS 594

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

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

INTRODUCTION TO OPENGL PIPELINE

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

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

Transcription:

GLSL OpenGL Shading Language Language with syntax similar to C Syntax somewhere between C och C++ No classes. Stranight ans simple code. Remarkably understandable and obvious! Avoids most of the bad things with C/C++. Some advantages come from the limited environment! Algol descentant, easy to learn if you know any of its followers. GLSL Example Vertex shader: void main() { gl_position = gl_projectionmatrix * gl_modelviewmatrix * gl_vertex; } Pass-through shader, implements the minimal functionality of the fixed pipeline

GLSL Example Fragment shader: void main() { gl_fragcolor = vec4(1.0, 1.0, 1.0, 1.0); } Set-to-white shader Exempel Pass-through vertex shader + set-to-white fragment shader // Vertex shader void main() { gl_position = gl_projectionmatrix * gl_modelviewmatrix * gl_vertex; } // Fragment shader void main() { gl_fragcolor = vec4(1.0, 1.0, 1.0, 1.0); }

uilt-in variables: Note: l_position transformed vertex, out data l_projectionmatrix projection matrix l_modelviewmatrix modelview matrix l_vertex vertex in model coordinates l_fragcolor resulting fragment color lso a new built-in type: ec4 4 component vektor ome possibililties start to show up, right? Also note: Matrix multiplication using the * operator Shaders always start in main() Comment: This multiplication is extremely common: gl_position = gl_projectionmatrix * gl_modelviewmatrix * gl_vertex; alias: gl_modelviewprojectionmatrix r ftransform();

GLSL basics A tour of the language (with some examples) Character set Preprocessor directives Comments Identifiers Types Modifiers Constructors Operators Built-in functions and variables Activating shaders from OpenGL Communication with OpenGL Character set Alphanumerical characters: a-z, A-Z, _, 0-9. + - / * % < > [ ] { } ^ & ~ =! : ;? # for preprocessor directives (!) space, tab, FF, CR, FL Note! Tolerates both CR, LF och CRLF! Case sensitive BUT Characters and strings do not exist! a, Hej mm

The preprocessor #define #undef #if etc _VERSION_ is useful for handling version differences. It will hardly be possible to avoid in the long run. #include does not exist! Comments /* This is a comment that spans more than one line */ // but personally I prefer the one-line version Just like we are used to! So litter your code with comments!

Identifiers Just like C: alphanumerical characters, first nondigit BUT Reserved identifiers, predefined variables, have the prefix gl_! It is not allowed to declare your own variables with the gl_ prefix! Types There are some well-known scalar types: void: return value for procedures bool: Boolean variable, that is a flag int: integer value float: floating-point value However, long and double do not exist.

Mer typer Vector types: vec2, vec3, vec4: Floating-point vectors with 2, 3 or 4 components bvec2, bvec3, bvec4: Boolean vectors ivec2, ivec3, ivec4: Integer vectors mat2, mat3, mat4: Floating-point matrices of size 2x2, 3x3, 4x4 mportant! Modifiers Variable usage is declared with modifiers: const attribute uniform varying If none of these are used, the variable is local in its scope and can be read and written as you please.

const constant, assigned at compile time, can not be changed attribute and uniform attribute is argument from OpenGL, per-vertexdata uniform is argument from OpenGL, per primitive. Can not be changed within a primitive Many predefined variables are attribute or uniform.

varying data that should be interpolated between vertices Written in vertex shader Read (only) by fragment shaders In both shaders they must be declared varying. In the fragment shader, they are read only. Examples: texture coordinates, normal vectors for Phong shading, vertex color, light value for Gouraud shading Example: Gouraud shader No, we didn t learn shaders to do Gouraud shading, but it is a simple example Transform normal vectors Calculate shading value per vertex, (here sing diffuse only), by dot product with light irection Interpolate between vertices

varying float shade; void main() Gouraud shader Vertex shader vec3 norm; const vec3 light = {0.58, 0.58, 0.58}; gl_position = gl_projectionmatrix * gl_modelviewmatrix * gl_vertex; norm = normalize(gl_normalmatrix * gl_normal); shade = dot(norm, light); varying float shade; Gouraud shader Fragment shader void main() { gl_fragcolor = vec4(clamp(shade, 0, 1)); }

Gouraud shader Note: The v ariable shade is varying, interpolated between vertices! dot() och normalize() do what you expect. clamp() clamps a variable within a desired interval. gl_normal is the normal vector in model coordinates gl_normalmatrix transform for normal vectors The contstant vector light() is here hard coded Gouraud shader Result Very good - for this model

Example: Phong shader A more meaningful example Transform normal vectors Interpolate normal vectors between vertices Calculate shading value per fragment Practically the same operations, but the light calculation are done in the fragment shader varying vec3 norm; Phong shader Vertex shader void main() { gl_position = gl_projectionmatrix * gl_modelviewmatrix * gl_vertex; norm = normalize(gl_normalmatrix * gl_normal); }

varying vec3 norm; Phong shader Fragment shader void main() { float shade; const vec3 light = {0.58, 0.58, 0.58}; } shade = dot(normalize(norm), light); shade = clamp(shade, 0, 1); gl_fragcolor = vec4(shade); Phong shader Result Nice and smooth!

Texture coordinates Built-in variables: gl_multitexcoord0 is texture coordinate for vertex for texture unit 0. gl_texcoord[0] is a built-in varying for interpolating texture coordinates. gl_texcoord[0].s and gl_texcoord[0].t give the S and T components separately. Example: Procedural texture Texture generated by fragment shader! Vertex shader passes on texture coordinates Texture coordinates are used in a texture generating function in the fragment shader Simpler than you might think!

Procedural texture Vertex shader void main() { gl_position = gl_projectionmatrix * gl_modelviewmatrix * gl_vertex; gl_texcoord[0] = gl_multitexcoord0; } Simple pass-through shader, but here including passing on texture coordinates Procedural texture Fragment shader void main() { gl_fragcolor = vec4(1.0, 1.0, 1.0, 0.0); } float a = sin(gl_texcoord[0].s*30)/2+0.5; float b = sin(gl_texcoord[0].t*30)/2+0.5; gl_fragcolor = vec4(a, b, 1.0, 0.0); Simple! The fragment color is a function of S and T, in this case a simple sin for each. Note sin(), one out of many commmon mathematical functions, built-in!

Procedural texture Result Texture data In order to use predefined texture data, they should be communicated from OpenGL! This is done by a uniform, a variable that can not be changed within a primitive. samplers : pre-defined type for referencing texture data

Texture access Exempel: uniform sampler2d texture; void main() { gl_fragcolor = texture2d(texture, gl_texcoord[0].st); } texture2d() performs texture access Communication with host Important! The host must be able to set unoformand attribute variables for GLSL to read. GLSL can only output information through fragments. OpenGL sends address and names to GLSL with special calls.

Example: uniform float: float myfloat; GLint loc; loc = glgetuniformlocation(p, myfloat ); gluniform1f(loc, myfloat); p: Ref to shader program, as installed earlier, loc: address to variable Now the variable can be used in GLSL: uniform float myfloat; Note that the string passed to glgetuniformlocation specifies the name in GLSL! Example: texture, uniform sampler: GLuint tex; glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, tex); loc = glgetuniformlocation(prog, "tex"); gluniform1i(loc, 0); zero to gluniform1i = texture unit number! Används i shader: uniform sampler2d tex; vec3 texval = vec3(texture2d(texture, gl_texcoord[0].st));

Example: Multitexturing Bind one texture per texturing unit Pass GLSL enhetsnummer and name Declare as samplers in GLSL Many possibilities: Combine texture data using arbitrary function. Make one texture sensitive to lighting and another not. Use texture as bump map My simple exmaple: Select different texture dependning of light level. Example: Multitexturing (Lighting omitted, calculates light from two light sources, spec/spec2) uniform sampler2d tex; uniform sampler2d bump;... vec3 texval = vec3(texture2d(tex, gl_texcoord[0].st)); if (spec+spec2 > klimit) texval = vec3(texture2d(bump, gl_texcoord[0].st));

Example: Multitexturing Switches texture in highlights Compilation and execution Done in two steps: 1) Initialization, compilation Create a program object Create a shader object and pass source code to it Compile the shader programs ) Activation Activate the program object for rendering

Create a program object lcreateprogram glcreateprogramobjectarb) he program object is the root node to all nformation OpenGL has about our shaders. reate one for each shader pair in your pplication. Create shader objects lcreateshader (glcreateshaderobjectarb) ead source code and pass to the shader bject: lshadersource (glshadersourcearb) ompile! lcompileshader (glcompileshaderarb)

Attach and link For both vertex and fragment shader: glattachshader (glattachobjectarb) Link: gllinkprogram (gllinkprogramarb) The entire initialization in code PROG = glcreateprogram(); VERT = glcreateshader(gl_vertex_shader); text = readtextfile( shader.vert ); glshadersource(vert, 1, text, NULL); glcompileshader(vert); Same for fragment shader glattachshader(prog, VERT); glattachshader(prog, FRAG); gllinkprogram(prog);

Activate the program for rendering Givet ett installerat och kompilerat programobjekt: extern GLuint PROG; // Was GLhandleARB activate: gluseprogram(prog); deactivate: gluseprogram(0);