Objectives. Coupling GLSL to Applications Example applications

Similar documents
GLSL II. Objectives. Coupling GLSL to Applications Example applications

Objectives. Open GL Shading Language (GLSL)

Reading Material. Vertex- and Fragment Shaders. Pipeline. Vertex Shader. Primitive Assembly. Input data can be. Produces

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

3D Game Programming Programmable Pipelines. Ming-Te Chi Department of Computer Science, National Chengchi University

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

Shadows & Programmable Shaders. Dr. Alexander G. Gee. Texture Mapping. Buffers Texture Mapping OpenGL Texture Mapping Compositing and Blending

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

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

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

Supplement to Lecture 22

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

Computer Graphics (CS 4731) & 2D Graphics Systems

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

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

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

- Surface Rendering -

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 #13: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Waves, Displacement, Reflections. Arun Rao CS 594

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

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

Programmable shader. Hanyang University

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

Lecture 3. Programming with OpenGL GLUT + GLEW. CS 354 Computer Graphics Sunday, January 20, 13

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

Programmable Graphics Hardware

Computer Graphics (CS 543) Lecture 8c: Environment Mapping (Reflections and Refractions)

CS 432 Interactive Computer Graphics

12.2 Programmable Graphics Hardware

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

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

WebGL: Hands On. DevCon5 NYC Kenneth Russell Software Engineer, Google, Inc. Chair, WebGL Working Group

Lecture 09: Shaders (Part 1)

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

Repetition of TDA361. Ulf Assarsson

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

CPSC 436D Video Game Programming

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

Shaders. Slide credit to Prof. Zwicker

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

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

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)!

Programming shaders & GPUs Christian Miller CS Fall 2011

1 of 5 9/10/ :11 PM

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

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

Lab 9 - Metal and Glass

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

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

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

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

1. Write a shader program that animates the morphing of Figure 'A' to Figure 'B' and back.

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

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

The Rasterization Pipeline

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

CS559 Computer Graphics Fall 2015

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

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

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

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

Zeyang Li Carnegie Mellon University

Erik Anchondo cse 520 lab 4

2a. The triangles scale increases (expands) when the 'e' key is pressed and decreases (contracts) when the 'c' key is pressed.

Converts geometric primitives into images Is split into several independent stages Those are usually executed concurrently

Lecture 31 Sections 9.1, 9.2, 9.3. Mon, Nov 23, 2009

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

Sign up for crits! Announcments

OPENGL RENDERING PIPELINE

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

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

CMPS160 Shader-based OpenGL Programming

Philip Calderon CSE 520 Lab 3 Color Shader

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

13 Advanced Rendering and Illumination Methods Advanced Rendering and Illumination Methods

BCA611 Video Oyunları için 3B Grafik

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

GPU Programming EE Midterm Examination

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

CMSC427 OpenGL and JOGL

Objectives. Introduce the OpenGL shading Methods 1) Light and material functions on MV.js 2) per vertex vs per fragment shading 3) Where to carry out

To Do. Advanced Computer Graphics. Real-Time Rendering. Motivation. Offline 3D Graphics Rendering. Evolution of 3D graphics rendering

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

Objectives. OpenGL - Shaders. GLSL A Quick Review

Introduction to the OpenGL Shading Language

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

2D graphics with WebGL

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

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

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

Pipeline Operations. CS 4620 Lecture 14

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

Lecture 11 Shaders and WebGL. October 8, 2015

Transcription:

GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico

Objectives Coupling GLSL to Applications Example applications 2

Linking Shaders to OpenGL OpenGL Extensions - ARB_shader_objects - ARB_vertex_shader - ARB_fragment_shader OpenGL 2.0 - Almost identical to using extensions - Avoids extension suffixes on function names 3

Program Object Container for shaders - Can contain multiple shaders - Other GLSL functions Multiple program objects can exist in an application Read and compile shaders, then add to program object 4

Initializing a Shader In this example, we assume both a vertex shader and a fragment shader are in files. Define a function initshader to do all required initialization - Read and compile shader - Form program object - Link everything together - Define any uniform or attribute variables 5

Reading a Shader Shader are added to the program object and compiled Usual method of passing a shader is as a null-terminated string using the function glshadersource If the shader is in a file, we can write a reader to convert the file to a string Usually we read a shader from a file into a single null-terminated string 6

Shader Reader char* readshadersource(const char* shaderfile) { struct stat statbuf; FILE* fp = fopen(shaderfile, "r"); char* buf; } stat(shaderfile, &statbuf); buf = (char*) malloc(statbuf.st_size + 1 * sizeof(char)); fread(buf, 1, statbuf.st_size, fp); buf[statbuf.st_size] = '\0'; fclose(fp); return buf; 7

Error Checker Generally, we should check error status set by GLSL functions static void checkerror(glint status, const char *msg) { if (!status) { printf("%s\n", msg); exit(exit_failure); } } 8

initshader 1 static void initshader(const GLchar* vshaderfile, const GLchar* fshaderfile) { GLint status = glgeterror()==gl_no_error; GLchar *vsource, *fsource; GLuint vshader, fshader; /* read shader files */ vsource = readshadersource(vshaderfile); checkerror(status, "Failed to read vertex shader"); fsource = readshadersource(fshaderfile); checkerror(status, "Failed to read fragment shader");

initshader 2 /* create program and shader objects */ vshader = glcreateshader(gl_vertex_shader); fshader = glcreateshader(gl_fragment_shader); program = glcreateprogram(); /* attach shaders to the program object */ glattachshader(program, vshader); glattachshader(program, fshader); 10

initshader 3 /* read shaders */ glshadersource(vshader, 1, (const GLchar**) &vsource, NULL); glshadersource(fshader, 1, (const GLchar**) &fsource, NULL); /* compile shaders */ glcompileshader(vshader); glcompileshader(fshader); /* error check */ glgetshaderiv(vshader, GL_COMPILE_STATUS, &status); checkerror(status, "Failed to compile the vertex shader.");

initshader 4 glgetshaderiv(fshader, GL_COMPILE_STATUS, &status); checkerror(status, "Failed to compile the fragment shader."); /* link */ gllinkprogram(program); glgetshaderiv(program, GL_LINK_STATUS, &status); checkerror(status, "Failed to link the shader program object."); /* use program object */ } gluseprogram(program); 12

Vertex Attributes Vertex attributes are named in the shaders Linker forms a table Application can get index from table and tie it to an application variable Similar process for uniform variables Can define both types in initshader 13

Vertex Attribute Example GLint colorattr; colorattr = glgetattriblocation(myprogobj, "mycolor"); /* mycolor is name in shader */ GLfloat color[4]; glvertexattrib4fv(colorattrib, color); /* color is variable in application */ 14

Uniform Variable Example GLint angleparam; angleparam = glgetuniformlocation(myprogobj, "angle"); /* angle defined in shader */ /* my_angle set in application */ GLfloat my_angle; my_angle = 5.0 /* or some other value */ gluniform1f(myprogobj, angleparam, my_angle); 15

Vertex Shader Applications Moving vertices - Morphing - Wave motion -Fractals Lighting - More realistic models - Cartoon shaders 16

Wave Motion Vertex Shader uniform float time; uniform float xs, zs; void main() { float s; s = 1.0 + 0.1*sin(xs*time)*sin(zs*time); gl_vertex.y = s*gl_vertex.y; gl_position = gl_modelviewprojectionmatrix*gl_vertex; } 17

Particle System uniform vec3 init_vel; uniform float g, m, t; void main() { vec3 object_pos; object_pos.x = gl_vertex.x + vel.x*t; object_pos.y = gl_vertex.y + vel.y*t + g/(2.0*m)*t*t; object_pos.z = gl_vertex.z + vel.z*t; gl_position = gl_modelviewprojectionmatrix* vec4(object_pos,1); } 18

Modified Phong Vertex Shader I void main(void) /* modified Phong vertex shader (without distance term) */ { float f; /* front facing factor */ /* compute normalized normal, light, view, half-angle vectors in eye coordinates */ /* must use inverse transpose of modelview matrix to preserve angles */ vec3 norm = normalize(gl_normalmatrix*gl_normal);

Modified Phong Vertex Shader II vec3 lightv = normalize(gl_lightsource[0].position -gl_modelviewmatrix*gl_vertex); vec3 viewv = - normalize(gl_modelviewmatrix*gl_vertex); vec3 halfv = normalize(lightv + norm); if(dot(lightv, norm) > 0.0) f = 1.0; else f = 0.0; 20

Modified Phong Vertex Shader III /* compute diffuse, ambient, and specular contributions */ vec4 diffuse = max(0, dot(lightv, norm)) *gl_frontmaterial.diffuse *LightSource[0].diffuse; vec4 ambient = gl_frontmaterial.ambient *LightSource[0].ambient; vec4 specular = f*gl_frontmaterial.specular* gl_lightsource[0].specular) *pow(max(0, dot( norm, halfv)), gl_fro vec3 color = vec3(ambient + diffuse + specular gl_frontcolor = vec4(color, 1); gl Position = gl ModelViewProjectionMatrix*gl

Modified Phong Vertex Shader IV vec3 color = vec3(ambient + diffuse + specular) gl_frontcolor = vec4(color, 1); /* position must still be computed in clip coordinates */ } gl_position = gl_modelviewprojectionmatrix*gl_vertex; 22

Pass Through Fragment Shader /* pass-through fragment shader */ void main(void) { gl_fragcolor = gl_frontcolor; } 23

Per Fragment Modified Phong Shader We can apply the same shading model on a per fragment basis Vertex shader can compute normal and position in eye coordinates - More efficient than computing each on a per fragment basis - Use rasterizer to interpolate varying variables 24

Vertex Shader for per Fragment Lighting /* vertex shader for per-fragment Phong shading */ varying vec3 normale; varying vec4 positione; void main() { normale = gl_normalmatrixmatrix*gl_normal; positione = gl_modelviewmatrix*gl_vertex; gl_position = gl_modelviewprojectionmatrix*gl_vertex; } 25

Fragment Shader for Modified Phong Lighting I varying vec3 normale; varying vec4 positione; void main() { vec3 norm = normalize(normale); vec3 lightv = normalize(gl_lightsource[0].position-positione.xyz); vec3 viewv = normalize(positione); vec3 halfv = normalize(lightv + viewv); vec4 diffuse = max(0, dot(lightv, viewv)) *gl_frontmaterial.diffuse*gl_lightsource[0].diffuse; vec4 ambient = gl_frontmaterial.ambient*gl_lightsource[0].ambient; 26

Fragment Shader for Modified Phong Lighting II int f; if(dot(lightv, viewv)> 0.0) f =1.0); else f = 0.0; vec3 specular = f*pow(max(0, dot(norm, halfv), gl_frontmaterial.shininess) *gl_frontmaterial.specular*gl_lightsource[0].specular); vec3 color = vec3(ambient + diffuse + specular); gl_fragcolor = vec4(color, 1.0); } 27

Vertex vs Fragment Shader per vertex lighting per fragment lighting 28

Samplers Provides access to a texture object Defined for 1, 2, and 3 dimensional textures and for cube maps In shader: uniform sampler2d mytexture; Vec2 texcoord; Vec4 texcolor = texture2d(mytexture, texcoord); In application: texmaplocation = glgetuniformlocation(myprog, mytexture ); gluniform1i(texmaplocation, 0); /* assigns to texture unit 0 */ 29

Fragment Shader Applications Texture mapping smooth shading environment mapping bump mapping 30

Cube Maps We can form a cube map texture by defining six 2D texture maps that correspond to the sides of a box Supported by OpenGL Also supported in GLSL through cubemap sampler vec4 texcolor = texturecube(mycube, texcoord); Texture coordinates must be 3D 31

Environment Map Use reflection vector to locate texture in cube map 32

Environment Maps with Shaders Environment map usually computed in world coordinates which can differ from object coordinates because of modeling matrix - May have to keep track of modeling matrix and pass it shader as a uniform variable Can also use reflection map or refraction map (for example to simulate water) 33

Environment Map Vertex Shader uniform mat4 modelmat; uniform mat3 invmodelmat; uniform vec4 eyew; void main(void) { vec4 positionw = modelmat*gl_vertex; vec3 normw = normalize(gl_normal*invmodelmat.xyz); vec3 lightw = normalize(eyew.xyz-positionw.xyz); eyew = reflect(normw, eyew); gl_position = gl_modelviewprojectionmatrix*gl_vertex; } 34

Environment Map Fragment Shader /* fragment shader for reflection map */ varying vec3 reflectw; uniform samplercube MyMap; void main(void) { gl_fragcolor = texturecube(mymap, reflectw); } 35

Bump Mapping Perturb normal for each fragment Store perturbation as textures 36

Normalization Maps Cube maps can be viewed as lookup tables 1-4 dimensional variables Vector from origin is pointer into table Example: store normalized value of vector in the map - Same for all points on that vector - Use normalization map instead of normalization function - Lookup replaces sqrt, mults and adds 37