COMP371 COMPUTER GRAPHICS

Similar documents
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

Shaders. Slide credit to Prof. Zwicker

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

CS 432 Interactive Computer Graphics

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

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

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

CPSC 436D Video Game Programming

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

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

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

Rendering Objects. Need to transform all geometry then

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

Geometry Shaders. And how to use them

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

Programming shaders & GPUs Christian Miller CS Fall 2011

OPENGL RENDERING PIPELINE

Introduction to Shaders.

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

CS452/552; EE465/505. Review & Examples

12.2 Programmable Graphics Hardware

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

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

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

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

Programmable Graphics Hardware

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

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

Lecture 2. Shaders, GLSL and GPGPU

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

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

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

Introductory Seminar

Computer graphics 2: Graduate seminar in computational aesthetics

OpenGL Performances and Flexibility

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

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

Graphics Hardware. Instructor Stephen J. Guy

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

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

INTRODUCTION TO OPENGL PIPELINE

CS 432 Interactive Computer Graphics

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

OpenGL Performances and Flexibility. Visual Computing Laboratory ISTI CNR, Italy

Computergraphics Exercise 15/ Shading & Texturing

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

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

Lecture 11 Shaders and WebGL. October 8, 2015

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

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

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

COSC342: Computer Graphics

Computer Graphics Seminar

Shaders (some slides taken from David M. course)

From system point of view, a graphics application handles the user input, changes the internal state, called the virtual world by modeling or

Tutorial 04. Harshavardhan Kode. September 14, 2015

CS452/552; EE465/505. Image Formation

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

OpenGL shaders and programming models that provide object persistence

Tutorial 1: Your First Triangle!

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

3d Programming I. Dr Anton Gerdelan

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Programmable Graphics Hardware

Supplement to Lecture 22

An Overview GLUT GLSL GLEW

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

The Graphics Pipeline

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

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

last time put back pipeline figure today will be very codey OpenGL API library of routines to control graphics calls to compile and load shaders

Recall: Indexing into Cube Map

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

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

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

Ciril Bohak. - INTRODUCTION TO WEBGL

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

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

CS 548: COMPUTER GRAPHICS INTRODUCTION TO OPENGL AND GLUT SPRING 2015 DR. MICHAEL J. REALE

CS451Real-time Rendering Pipeline

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

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

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

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

Interaction. CSCI 420 Computer Graphics Lecture 3

Discussion 3. PPM loading Texture rendering in OpenGL

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

Tutorial 3: Texture Mapping

We assume that you are familiar with the following:

Mobile Application Programing: Android. OpenGL Operation

Content. Building Geometry Appearance Lights Model Loaders

Computer Graphics Coursework 1

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

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

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

CS559 Computer Graphics Fall 2015

Transcription:

COMP371 COMPUTER GRAPHICS SESSION 12 PROGRAMMABLE SHADERS

Announcement Programming Assignment #2 deadline next week: Session #7 Review of project proposals 2

Lecture Overview GPU programming 3

GPU Pipeline 4

Programmable GPU Pipeline cont d 5

Programmable Hardware Most modern graphics cards allow parts of the GPU pipeline to be modified by a developer Increased functionality with hardware support Complex graphics algorithms with fast implementation. General Computing Until recently, the programmable parts were Vertex Processor Fragment (Pixel) Processor OpenGL 4 also allows the programming of Geometric processors and Compute Processors for General Purpose GPU programming All these programs are called Shaders. 6

Programmable GPU Pipeline 7

Vertex Processor Responsible for the following operations: Vertex transformation Normal transformation and normalization Texture Coordinates generation and transformation Lighting (per vertex) Color & Material (per vertex) A Vertex shader replaces the entire functionality of the fixed vertex processor! When writing a shader, one must replace the entire functionality. Vertex Shaders operate simultaneously on all vertices (in parallel) No vertex information can affect another vertex. 8

Vertex Shader 9

Fragment Processor Operates on the fragments AFTER the interpolation and rasterization of vertex data. Allows the following programming: Operation on interpolated vertex values (Phong - in next lecture) Texture access & application Fog Color sum, etc. All fragment shaders work in parallel Again, no access to neighbouring fragments. Does not replace back-end operations (alpha blending, and such). 10

Fragment Shader 11

OpenGL in Application Setup Create a Framebuffer, Depth Buffer, all the buffers you need, with the resolution you need Customize with glfwwindowhint Create with GLFW: glfwcreatewindow Set OpenGL Rendering States Example glclearcolor(0.0f, 0.0f, 0.0f, 0.0f); glenable(gl_depth_test); gldepthfunc(gl_less); 12

Create Vertex Array/Buffer Objects Create a Vertex Array Object which will manage your Vertex Buffer Objects glgenvertexarrays(1, &vao); Vertex Buffer Objects contains geometry information in Model Space. Draw calls will later use the VBO data. Create an Array containing all of your geometry data (Position, Normals, Colors, UV Coordinates, etc) float vertexbuffer[] = {1.0f, 2.0f,. }; Create a Vertex Buffer Object glgenbuffers(1, &vbo); glbindbuffer(gl_array_buffer, vbo); glbufferdata(gl_array_buffer, sizeof(vertexbuffer), vertexbuffer, GL_STATIC_DRAW); 13

Load Shaders You can have multiple shader pairs (Vertex/Fragment) for different type of geometry To create Shader Objects, you must load, compile, and link your shaders (normally in the program initialization) glcreateshader glcompileshader glattachshader gllinkprogram 14

Frame Initialization When the OpenGL Setup is done, you are ready to draw! Before drawing, make sure your scene is updated for the next frame. This means, the scene goes forward by a time step (typically 1/60 second) Animations, Physics, Camera, etc Clear the rendering context to start drawing a new frame from scratch (frame buffer, depth buffer, etc) glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); 15

Drawing Bind Shader gluseprogram(shaderid); Set Shader Constants (eg: VAR matrix in Shader) GLuint loc = glgetuniformlocation(shaderid, VAR"); gluniformmatrix4fv(loc, 1, GL_FALSE, &varmatrix); Bind Vertex Array Object glbindvertexarray(vao); 16

Drawing cont d Set Shader Input Data (VBO) glenablevertexattribarray(0); // 1 st input in vertex shader glbindbuffer(gl_array_buffer, vbo); glvertexattribpointer( 0,// attribute 3,// size GL_FLOAT,// type GL_FALSE,// normalized? sizeof(vertex), // stride (void*)0 ); // offset Draw Call gldrawarrays(gl_triangle_strip, 0, numvertices); Cleanup gldisablevertexattribarray(0); 17

Finishing a Frame Swap Buffers Modern renderers typically use double-buffering While front-buffer is sent to video controller Draw in the back-buffer When done drawing, swap the buffers With GLFW this is achieved with the command: glfwswapbuffers(...) 18

Shading Languages The first approaches used an assembler language of the cards (with significantly different instruction sets on both shaders). Both OpenGL and DirectX also provide a different API load shaders pass user-defined data delete High level languages Cg (NVidia), GLSL (opengl), HLSL (DirectX) 19

GLSL A high-level procedural language (similar to C++). As of OpenGL 2.0, it is a standard, with a simple gl_xyz API from OpenGL applications Former implementations of GLSL API to OpenGL used OpenGL extensions The same language, with small differences, is used for all types of shaders. Natively supports graphical needs (such as containing types like vectors and matrices). 20

Can I use GLSL? Almost every current high-end GPU supports it How to check Run glewinfo.exe [on windows] It will tell you which features you can use 21

Language Syntax Similar to C/C++ Data Types vector, matrix, texture Control Flow same as C. e.g.: for, if, while 22

Language Syntax cont d Built-in functions for math interpolation (mix, step, smoothstep) geometric matrix vector relational texture shadow vertex, fragment noise 23

Communication OpenGL Shaders Uniform variables Application Vertex Shader Fragment shader Input variables Output variables 24

Uniform Variables uniform datatype dataname suitable for values that remain constant along a primitive, frame, or even the whole scene can be read (but not written) in both vertex and fragment shaders 25

Example 26

Typical Uniform Variables Some Matrices uniform mat4 ModelViewMatrix; uniform mat3 NormalMatrix; uniform mat4 TextureMatrix[n]; Lighting, depth, material defined as some structures uniform MaterialParameters FrontMaterial; uniform LightSourceParameters LightSource[MaxLights]; And many variables it refers OpenGL state variables 27

Input Variables in datatype dataname Read only variables In Vertex Shader Typically Vertex Data (position, normal, uv, color) In Fragment Shader Output variables from Vertex Shader become input to Fragment Shader The variable names must match 28

Input Variable Example for Vertex Shader or: glbindattriblocation(p, 0, "position");. glenablevertexattribarray(0);. 29

Output Variables out datatype dataname In Vertex Shader Output variables are inputs in Fragment Shader Values will be interpolated in Fragment Shader Declared in both vertex and fragment shader type and name should be matched 30

Vertex Shader Program #version 330 Fragment Shader Program #version 330 uniform mat4 view_matrix, model_matrix, proj_matrix; in vec3 out_color; in vec3 in_position; out vec4 frag_colour; in vec3 in_color; out vec3 out_color; void main () { void main () { frag_colour = vec4 (out_color, 1.0); mat4 MVP = proj_matrix * view_matrix * model_matrix; gl_position = MVP * vec4 (in_position, 1.0); }; out_color = in_color; }; After loading the shaders: At every drawing iteration: 31

32

33

Review GPU programming 34

Next Lecture Lighting and Shading 35

36

Studying Support Slides 37