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

Similar documents
Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/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

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

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

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

Programmable Graphics Hardware

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

12.2 Programmable Graphics Hardware

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

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

Programming shaders & GPUs Christian Miller CS Fall 2011

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

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

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

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

Programmable shader. Hanyang University

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

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

Lecture 09: Shaders (Part 1)

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

Objectives. Open GL Shading Language (GLSL)

Lighting and Texturing

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

Programmable Graphics Hardware

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

CPSC 436D Video Game Programming

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

OpenGL Programmable Shaders

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

Introduction to the OpenGL Shading Language (GLSL)

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

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

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

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

Sign up for crits! Announcments

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

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

Vertex & Fragment shaders

2D graphics with WebGL

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

1 of 5 9/10/ :11 PM

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

Programmable GPUs Outline

Stored Texture Shaders

Computergraphics Exercise 15/ Shading & Texturing

Lab 9 - Metal and Glass

Shaders. Slide credit to Prof. Zwicker

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

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

GPU Programming EE Final Examination

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

Tuesday, 23 March OpenGL Shading Language

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

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

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

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

CS 432 Interactive Computer Graphics

CS179: GPU Programming

Introduction to Shaders.

Mobile Application Programing: Android. OpenGL Operation

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

Computer Graphics (CS 4731) & 2D Graphics Systems

0.Features of the OpenGL Shading Language

OPENGL RENDERING PIPELINE

0.Features of the OpenGL Shading Language

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

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

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

BCA611 Video Oyunları için 3B Grafik

Yazhuo Liu Homework 3

INFOGR Computer Graphics

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

CMPS160 Shader-based OpenGL Programming

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

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

- Surface Rendering -

Objectives. Coupling GLSL to Applications Example applications

Waves, Displacement, Reflections. Arun Rao CS 594

COMP371 COMPUTER GRAPHICS

Mobile Application Programing: Android. OpenGL Operation

OpenGL shaders and programming models that provide object persistence

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

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

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

INFOGR Computer Graphics

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

GLSL: Creating GLSL Programs - Overview

Computer Graphics Coursework 1

GLSL Language Details

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

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

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

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

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

Transcription:

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

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

Built-in variables: Note: gl_position gl_projectionmatrix gl_modelviewmatrix gl_vertex gl_fragcolor transformed vertex, out data projection matrix modelview matrix vertex in model coordinates resulting fragment color Also a new built-in type: vec4 4 component vektor Some possibililties start to show up, right? GLSL basics A short tour of the language Identifiers Types Modifiers Constructors Operators Built-in functions and variables Activating shaders from OpenGL Communication with OpenGL

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, no long or double. Double exists as extension!

More types 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 Important! 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.

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 using diffuse only), by dot product with light direction Interpolate between vertices varying float shade; 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 gl_fragcolor = vec4(clamp(shade, 0, 1)); Gouraud shader Note: The variable 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 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.

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 Example: uniform sampler2d texture; gl_fragcolor = texture2d(texture, gl_texcoord[0].st); texture2d() performs texture access

Communication with host Important! The host must be able to set uniform and 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! Use in shader: uniform sampler2d tex; vec3 texval = vec3(texture2d(texture, gl_texcoord[0].st)); Example: Multitexturing Bind one texture per texturing unit Pass GLSL unit number 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 example: Select different texture depending of light level.

Example: Multitexturing uniform sampler2d worldtex; uniform sampler2d flowertex; gl_fragcolor = texture2d(flowertex, gl_texcoord[0].st); // Single tex gl_fragcolor = sin(pixelpos.z*10.0) * texture2d(flowertex, gl_texcoord[0].st) + (1.0 - sin(pixelpos.z*10.0)) * texture2d(worldtex, gl_texcoord[0].st); // Multi Example: Multitexturing Combines two textures

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 2) Activation Activate the program object for rendering 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);