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

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

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

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

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

Objectives. Open GL Shading Language (GLSL)

Computer Graphics (CS 4731) & 2D Graphics Systems

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

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

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

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Supplement to Lecture 22

GLSL II. Objectives. Coupling GLSL to Applications Example applications

CS 432 Interactive Computer Graphics

Objectives. Coupling GLSL to Applications Example applications

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

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

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

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

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

2D graphics with WebGL

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

CSE Real Time Rendering Week 3 & 4

Shaders. Slide credit to Prof. Zwicker

COMP371 COMPUTER GRAPHICS

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

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

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

Objectives. OpenGL - Shaders. GLSL A Quick Review

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

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

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

An Overview GLUT GLSL GLEW

CS380: Computer Graphics Screen Space & World Space. Sung-Eui Yoon ( 윤성의 ) Course URL:

Philip Calderon CSE 520 Lab 3 Color Shader

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

Tutorial 04. Harshavardhan Kode. September 14, 2015

Erik Anchondo cse 520 lab 4

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

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

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

12.2 Programmable Graphics Hardware

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

CS452/552; EE465/505. Image Processing Frame Buffer Objects

Introduction to Computer Graphics. Hardware Acceleration Review

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Programmable Graphics Hardware

Programming with OpenGL Complete Programs Objectives Build a complete first program

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

Starting out with OpenGL ES 3.0. Jon Kirkham, Senior Software Engineer, ARM

Lecture 09: Shaders (Part 1)

Introduction to Computer Graphics with WebGL

CSE 167. Discussion 03 ft. Glynn 10/16/2017

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

Tutorial 1: Your First Triangle!

Lecture 11 Shaders and WebGL. October 8, 2015

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

CS452/552; EE465/505. Image Formation

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

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

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

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

- Surface Rendering -

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

CPSC 436D Video Game Programming

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

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

Programming Graphics Hardware

Geometry Shaders. And how to use them

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

We assume that you are familiar with the following:

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

Programming shaders & GPUs Christian Miller CS Fall 2011

3d Programming I. Dr Anton Gerdelan

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

CS 432 Interactive Computer Graphics

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Programming with WebGL Part 1: Background. CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

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

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

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

CS 450: COMPUTER GRAPHICS REVIEW: STATE, ATTRIBUTES, AND OBJECTS SPRING 2015 DR. MICHAEL J. REALE

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

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Mobile Application Programing: Android. OpenGL Operation

Introductory Seminar

Interaction. CSCI 420 Computer Graphics Lecture 3

GLSL Overview: Creating a Program

Mali & OpenGL ES 3.0. Dave Shreiner Jon Kirkham ARM. Game Developers Conference 27 March 2013

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

CS452/552; EE465/505. Review & Examples

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

Computação Gráfica. Computer Graphics Engenharia Informática (11569) 3º ano, 2º semestre. Chap. 4 Windows and Viewports

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

Transcription:

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

Objectives Basic shaders - Vertex shader - Fragment shader Programming shaders with GLSL Finish first program

void init(void) { GLuint abuffer; simple.cpp glgenvertexarrays( 1, &abuffer ); glbindvertexarray( abuffer ); GLfloat vertices[numvertices][2] = {{ -0.5, -0.5 },{ -0.5, 0.5 }, { 0.5, 0.5 }, { 0.5, -0.5 }, { -0.5, -0.5 }, { 0.5, 0.5 } }; GLuint buffer; glgenbuffers( 1, &buffer ); glbindbuffer( GL_ARRAY_BUFFER, buffer ); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, GL_STATIC_DRAW); // Load shaders and use the resulting shader program GLuint program = InitShader( vshader.glsl, fshader.glsl"); GLuint loc = glgetattriblocation( program, "vposition" ); glenablevertexattribarray( loc ); glvertexattribpointer(loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); gluseprogram( program ); } void display(void) { glclear(gl_color_buffer_bit); gldrawarrays(gl_triangles, 0, NumVertices); glflush(); } int main(int argc, char** argv) { glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT_RGBA); glutinitwindowsize(500, 500); glutcreatewindow("simple"); init(); glutdisplayfunc(display); glutmainloop(); } vshader.glsl attribute vec4 vposition; void main() { gl_position = vposition; } fshader.glsl void main() { gl_fragcolor = vec4(1.0,0.0,0.0,1.0); }

A Simplified Pipeline Model

Vertex Shader Applications Moving vertices - Morphing - Wave motion - Fractals Lighting - More realistic models - Cartoon-like shading

Fragment Shader Applications Per fragment lighting calculations per vertex lighting per fragment lighting

Fragment Shader Applications Texture mapping smooth shading environment mapping bump mapping

Writing Shaders First programmable shaders were programmed in an assembly-like manner OpenGL extensions added for vertex and fragment shaders Cg (C for graphics) C-like language for programming shaders - Works with both OpenGL and DirectX - Interface to OpenGL complex OpenGL Shading Language (GLSL)

GLSL OpenGL Shading Language Part of OpenGL 2.0 and up High level C-like language New data types - Matrices - Vectors - Samplers As of OpenGL 3.1, application must provide shaders

Simple Vertex Shader attribute vec4 vposition; void main(void) { gl_position = vposition; } attribute qualifier is deprecated

Simple Vertex Shader (latest) in vec4 vposition; void main(void) { gl_position = vposition; }

Execution Model Vertex data Shader Program GPU Application Program Vertex Processor Primitive Assembly gldrawarrays Vertex

Simple Fragment Shader void main(void) { gl_fragcolor = vec4(1.0, 1.0, 1.0, 1.0); }

Simple Fragment Shader (latest version) out vec4 fragcolor; void main(void) { fragcolor = vec4(1.0, 1.0, 1.0, 1.0); }

Execution Model Application Shader Program GPU Rasterizer Fragment Fragment Processor Fragment color Frame Buffer

Data Types C types: int, float, bool Vectors: -float vec2, vec3, vec4 - Also int (ivec) and boolean (bvec) Matrices: mat2, mat3, mat4 Stored by columns Standard referencing m[row][column] C++ style constructors - vec3 a = vec3(1.0, 2.0, 3.0) - vec2 b = vec2(a)

Pointers There are no pointers in GLSL We can use C structs which can be copied back from functions Because matrices and vectors are basic types, they can be passed into and out from GLSL functions, e.g. mat3 func(mat3 a)

Qualifiers GLSL has many of the same qualifiers such as const as C/C++ Need also other qualifiers due to the nature of the execution model such as -in, out, uniform (most recent) -varying, attribute (deprecated or removed) Variables (depending on the qualifier type) can change - per vertex - per fragment - at any time in the application Vertex attributes are interpolated by the rasterizer into fragment attributes

Attribute Qualified Attribute-qualified variables can change per vertex or per fragment, such as in and attribute There are a few built in variables such as gl_position but most have been deprecated User defined (in application program) - Use in qualifier to get to shader in float temperature in vec3 velocity Could also use attribute instead (but removed by Opengl 4.0) - attribute float temperature

Uniform Qualifier Variables that are constant among all processed vertices such as uniform Can be changed in application and sent to shaders Cannot be changed in shader Used to pass information to shader such as the projection matrix (which does not change per vertex)

Varying Qualified Variables that are passed from vertex shader to fragment shader, such as varying and out Automatically interpolated by the rasterizer Old style used the varying qualifier: varying vec4 color; Now use out in vertex shader and in in the fragment shader: out vec4 color;

Example: Vertex Shader in vec4 vposition; const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); out vec4 color_out; void main(void) { gl_position = vposition; color_out = red; }

Required Fragment Shader in vec4 color_out; out vec4 fragcolor; void main(void) { fragcolor = color_out; //old version //gl_fragcolor = color_out; }

Attribute and Varying Qualifiers Starting with GLSL 1.5 attribute and varying qualifiers have been replaced by in and out qualifiers Accessed in the same way from application Vertex shader example: #version 1.4 attribute vec4 vposition; varying vec4 color; #version 1.5 in vec4 vposition; out vec4 color;

Note The attribute qualifier is effectively equivalent to an input qualifier in vertex shaders. The varying qualifier is equivalent to the input of a fragment shader or the output of a vertex shader.

Operators and Functions Standard C functions - Trigonometric - Arithmetic - Normalize, reflect, length Overloading of vector and matrix operations mat4 a; vec4 b, c, d; c = b*a; // a column vector stored as a 1d array d = a*b; // a row vector stored as a 1d array

Swizzling and Selection Can refer to array elements by element using [] or selection (.) operator with -x, y, z, w -r, g, b, a -s, t, p, q -a[2], a.b, a.z, a.p are the same Swizzling operator lets us manipulate components vec4 a; a.yz = vec2(1.0, 2.0);

Objectives Coupling shaders to applications - Reading - Compiling - Linking Vertex Attributes Setting up uniform variables Example applications

Getting Your Shaders into OpenGL All at the runtime! (see init function)

Linking Shaders with Application Read shaders Compile shaders Create a program object containing shaders Link everything together see InitShader.cpp provided by the textbook code

Linking Shaders Main concern is Link variables in application with variables in shaders - Vertex attributes - Uniform variables

Program Object Container for shaders - Can contain multiple shaders - Other GLSL functions GLuint myprogobj; myprogobj = glcreateprogram(); /* define shader objects here */ gllinkprogram(myprogobj); gluseprogram(myprogobj);

Reading a Shader Shaders are added to the program object and compiled Usual method of passing a shader is as a nullterminated string using the function glshadersource If the shader is in a file, we can write a reader to convert the file to a string

Shader Reader #include <stdio.h> static char* readshadersource(const char* shaderfile) { FILE* fp = fopen(shaderfile, "r"); if ( fp == NULL ) { return NULL; } fseek(fp, 0L, SEEK_END); long size = ftell(fp); fseek(fp, 0L, SEEK_SET); char* buf = new char[size + 1]; fread(buf, 1, size, fp); buf[size] = '\0'; fclose(fp); } return buf;

Adding a Vertex Shader GLuint myvshaderobj; GLchar vshaderfile[] = my_vertex_shader ; GLchar* vsource = readshadersource(vshaderfile); myvshaderobj = glcreateshader(gl_vertex_shader); glshadersource(myvshaderobj, 1, &vsource, NULL); glcompileshader(myvshaderobj); glattachshader(myprogobj, myvshaderobj); see InitShader.cpp provided by the textbook code

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

Associate shader variables with vertex arrays GLuint loc = glgetattriblocation( program, "vposition" ); glenablevertexattribarray( loc ); glvertexattribpointer( loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); #define BUFFER_OFFSET( offset ) ((GLvoid*) (offset))

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(angleparam, my_angle);

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

Wave Motion Vertex Shader in vec4 vposition; uniform float xs, zs, // frequencies uniform float h; // height scale uniform float time; void main() { vec4 t = vposition; t.y = vposition.y + h*sin(time + xs*vposition.x) + h*sin(time + zs*vposition.z); gl_position = t; }

Particle System Vertex Shader in vec3 vposition; uniform mat4 ModelViewProjectionMatrix; uniform vec3 init_vel; uniform float g, m, t; void main() { vec3 object_pos; object_pos.x = vposition.x + vel.x*t; object_pos.y = vposition.y + vel.y*t + g/(2.0*m)*t*t; object_pos.z = vposition.z + vel.z*t; gl_position = ModelViewProjectionMatrix*vec4(object_pos,1); }

Built-in Shader Variables gl_position - Output vertex position from vertex shader gl_ FragCoord - Input fragment position in fragment shader gl_fragdepth - Input depth value in fragment shader gl_fragcolor (deprecated and removed) others