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

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

Introduction to OpenGL. CS 4620 Balazs Kovacs, 2014 Daniel Schroeder, 2013 Pramook Khungurn, 2012

Shaders. Slide credit to Prof. Zwicker

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

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

OPENGL RENDERING PIPELINE

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

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

COMP371 COMPUTER GRAPHICS

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

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

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

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

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

Mobile graphics API Overview

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

Programming shaders & GPUs Christian Miller CS Fall 2011

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

12.2 Programmable Graphics Hardware

2D graphics with WebGL

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

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

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

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

We assume that you are familiar with the following:

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

Introduction to Shaders.

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

Ciril Bohak. - INTRODUCTION TO WEBGL

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

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

Computer Graphics Coursework 1

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

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

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

CPSC 436D Video Game Programming

Programmable Graphics Hardware

Sign up for crits! Announcments

Supplement to Lecture 22

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

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

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Lecture 2. Shaders, GLSL and GPGPU

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

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

CS450/550. Pipeline Architecture. Adapted From: Angel and Shreiner: Interactive Computer Graphics6E Addison-Wesley 2012

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

CS 432 Interactive Computer Graphics

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

Programmable Graphics Hardware

Lecture 13: OpenGL Shading Language (GLSL)

Shaders (some slides taken from David M. course)

Mobile Application Programing: Android. OpenGL Operation

Programming Graphics Hardware

The Graphics Pipeline

OPENGL AND GLSL. Computer Graphics

Models and Architectures

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

Cornell CS4620 Fall 2011!Lecture Kavita Bala (with previous instructors James/Marschner) Cornell CS4620 Fall 2011!Lecture 1.

Graphics Hardware. Instructor Stephen J. Guy

Dave Shreiner, ARM March 2009

CS427 Multicore Architecture and Parallel Computing

CS 4620 Program 3: Pipeline

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

CS559 Computer Graphics Fall 2015

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

BCA611 Video Oyunları için 3B Grafik

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

OpenGL refresher. Advanced Computer Graphics 2012

Pipeline Operations. CS 4620 Lecture 14

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

CS 354R: Computer Game Technology

An Overview GLUT GLSL GLEW

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

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

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

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

Computer Science 175. Introduction to Computer Graphics lib175 time: m/w 2:30-4:00 pm place:md g125 section times: tba

OpenGL shaders and programming models that provide object persistence

Content. Building Geometry Appearance Lights Model Loaders

CS452/552; EE465/505. Image Formation

TSBK03 Screen-Space Ambient Occlusion

Could you make the XNA functions yourself?

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, designed for rendering 2D graphics and interactive 3D graphics.

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

Computer graphics 2: Graduate seminar in computational aesthetics

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Introduction to Computer Graphics with WebGL

I expect to interact in class with the students, so I expect students to be engaged. (no laptops, smartphones,...) (fig)

CS475/CS675 - Computer Graphics. OpenGL Drawing

CS451Real-time Rendering Pipeline

CS179: GPU Programming

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

Transcription:

CS4621/5621 Fall 2015 Computer Graphics Practicum Intro to OpenGL/GLSL Professor: Kavita Bala Instructor: Nicolas Savva with slides from Balazs Kovacs, Eston Schweickart, Daniel Schroeder, Jiang Huang and Pramook Khungurn over the years.

Announcements Course website: http://www.cs.cornell.edu/courses/cs4621/2015fa/ Piazza for practicum: https://piazza.com/cornell/fall2015/cs46215621/ First assignment GPUray (out soon) Graphics hardware diagnostic package

Books and resources

Today OpenGL over the decades Graphics Pipeline overview LWJGL basics (more of this next time) GLSL basics (our focus today) GPUray

GPUray Overview PPA1: port a simplified version of ray1 to GPU Mostly deal with the fragment shader LWJGL bindings (communicating with the GPU) Shader compiling and linking GLSL code

GPUray Demo

What is OpenGL? Open Graphics Library A low level API for 2D/3D rendering with the Graphics Hardware (GPU) Cross-platform (Windows, OS X, Linux, ios, Android,...) Developed by SGI in 1992 2014: OpenGL 4.5 2008: OpenGL 3.0 2006: OpenGL 2.1 Main alternative: DirectX/3D

How does it fit in? Tap massive power of GPU hardware to render images Use GPU without caring about the exact details of the hardware

What OpenGL does for us Controls GPU Lets user specify resources...: Geometry (vertices and primitives) Textures Shaders (programmable pieces of rendering pipeline) Etc....and use them: Rasterize and draw geometry

How we will use OpenGL OpenGL version We use 2.x-3.x (plus extensions) Code avoids older, deprecated parts of OpenGL standard LWJGL Lightweight Java Game Library Java bindings for OpenGL API CS 4620/4621 Framework Simplifies creating and using OpenGL resources

OpenGL 25 years ago http://www.neilturner.me.uk/shots/opengl-big.jpg

OpenGL 2009 ->

What changed? 25 years ago: Vertex transformation/fragment shading hardcoded into GPUs Now: More parts of the GPU are programmable (but not all)

What changed? 25 years ago (Fixed pipeline): Transform vertices with modelview/projection matrices Shade with Phong lighting model only Contemporary (Programmable hardware): Custom vertex transformation Custom lighting model More complicated visual effects Shadows Displaced and detailed surfaces Simple reflections and refractions

WebGL shader demo Shadertoy https://www.shadertoy.com/browse GLSL sandbox http://glslsandbox.com/ three.js http://threejs.org/examples/

Creating an OpenGL Program using LWJGL

LWJGL OpenGL originally written for C. LWJGL contains OpenGL binding for Java www.lwjgl.org/ Gives Java interface to C OpenGL commands Manages framebuffer (framebuffer: a buffer that holds the image that is displayed on the monitor)

MainGame A window which can display GameScreens Initializes OpenGL context Forwards keyboard and mouse events to the event dispatcher Usage Inherit from MainGame and implement methods Create instance and call run method

MainGame @Override protected void buildscreenlist() { // Create objects inherited from GameScreen and // initialize screenlist attribute } @Override protected void fullinitialize() { // Code Executed Before Window Is Created } @Override protected void fullload() { // Code Executed With An Active OpenGL Context }

GameScreen Can display images created by OpenGL OpenGL context Stores OpenGL state (geometry, buffers, etc.) Usage: Inherit from class and implement methods Create instance in MainGame.buildScreenList

GameScreen @Override public void update(gametime gametime) { // Animation: Update position of scene objects, camera } @Override public void draw(gametime gametime) { // Drawing: Use LWJGL to draw to the screen } @Override public void onentry(gametime gametime) { // Initialization code } @Override public void onexit(gametime gametime) { // Destruction, free allocated resources here }

Events MainGame can trigger certain events Something happens (e.g. user resizes window) MainGame forwards event to the event dispatcher KeyboardEventDispatcher MouseEventDispatcher Objects interested in the event can sign up as listeners e.g. KeyboardEventDispatcher.OnKeyPressed.add( ) These events let us interact with OpenGL

OpenGL Commands and Resources (a quick demo today more on this next time)

Demo: Hello World!

Framework and GL Resources OpenGL API has objects that hold rendering resources Geometry, textures, shader programs, etc. Framework represents these with Java classes GLProgram (shader programs) GLBuffer (used to specify geometry) Constructing an object creates OpenGL resource Object's data lives in GPU memory Allows faster access while rendering

To use a GLSL program Follow the next 7 steps: 1. Create shader objects. 2. Read source code from files and feed them to the 3. 4. 5. 6. 7. shader objects just created. Compile the shader. Create a program object. Attach the shaders to the program. Link the program. Tell OpenGL to use your shader program.

GLSL basics

GLSL GLSL : Graphics Library Shading Language Syntax similar to C/C++ Language used to write shaders - vertex, tessellation, geometry, fragment, compute - We only mostly cover fragment shaders today Based on OpenGL - First available in OpenGL 2.0 (2004) Alternatives: Nvidia Cg and Microsoft HLSL

Modern Graphics Pipeline

Modern Graphics Pipeline

What is a Shader Program? A small program to control parts of the graphics pipeline Consists of 2 (or more) separate parts: - Vertex shader controls vertex transformation - Fragment shader controls fragment shading

GLSL Program Specifies how OpenGL should draw geometry Program: A collection of shaders that run together - At least one vertex shader or one fragment shader At any time, the GPU runs only one program - Must specify program to use before drawing geometry

Vertex Shader Transform vertices from object space to clip space Compute other data that are interpolated with vertices - Color - Normals - Texture coordinates - Etc.

Fragment Shaders Compute the color of a fragment (i.e. a pixel) Take interpolated data from vertex shaders Can read more data from: - Textures - User specified values

What Can a Fragment Shader Do? More complicated, detailed materials: Glossy Reflective, refractive Rough, bumpy, lots of nooks and crannies Wooden

What Can a Fragment Shader Do? Shadows (including soft edges) http://www.fabiensanglard.net/shadowmappingpcf/8x8kernel_nvidia.jpg

GLSL Data Types Both in GLSL and Java float, int GLSL has, but Java does not have vec2, vec3, vec4: vectors mat2, mat3, mat4: matrices sampler1d, sampler2d, sample3d, samplercube, etc: textures Java has, but GLSL does not have Object String etc...

vec2 Represents a vector in 2D (each component is a float) vec2 a; a.x = 0.0; a.y = 1.0; // a = (0,1) vec2 b; b.s = 10.0; b.t = 12.5; // b = (10,12.5) vec2 c; c[0] = 9.0; c[1] = 8.0; // c= (9,8)

vec3 vec3 a; a.x = 10.0; a.y = 20.0; a.z = 30.0; a.r = 0.1; a.g = 0.2; a.b = 0.3; a.s = 1.0, a.t = 2.0; a.p = 3.0; // a = (10, 20, 30) // a = (0.1, 0.2, 0.3) // a = (1, 2, 3) vec3 b = vec3(4.0, 5.0, 6.0); vec3 c = a + b; // c = (5, 7, 9) vec3 d = a - b; // d = (-3, -3, -3) vec3 e = a * b; // e = (4, 10, 18) vec3 f = a * 3; // e = (3, 6, 9) float g = dot(a,b); // g = 32 vec3 h = cross(a,b); // h = (-5,6,-3) float i = length(a); // i = 3.742

vec4 vec4 a; a.x = 10.0; a.y = 20.0; a.z = 30.0; a.w = 40.0; // a = (10, 20, 30, 40) a.r = 0.1; a.g = 0.2; a.b = 0.3; a.a = 0.4; // a = (0.1, 0.2, 0.3, 0.4) a.s = 1.0; a.t = 2.0; a.p = 3.0; a.q = 4.0; // a = (1, 2, 3, 4) vec4 b = vec4(5, 6, 7, 8); vec4 c = a + b; // c vec4 d = a - b; // d vec4 e = a * b; // e vec4 f = a * 3; // f float g = length(a); = (6, 8, 10, 12) = (-4, -4, -4, -4) = (5, 12, 21, 32) = (3, 6, 9, 12) // g = 5.477

mat2 Represents a 2 by 2 matrix (each component is a float) mat2 A = mat2(1.0, 2.0, 3.0, 4.0); // in column-major order vec2 x = vec2(1.0, 0.0); vec2 y = vec2(0.0, 1.0); vec2 a = A * x; // a = (1,2) vec2 b = A * y; // b = (3,4)

mat3 mat3 A = mat3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); // in column-major order vec3 x = vec3(1.0, 0.0, 0.0); vec3 y = vec3(0.0, 1.0, 0.0); vec3 z = vec3(0.0, 0.0, 1.0); vec3 a = A * x; // a = (1,2,3) vec3 b = A * y; // b = (4,5,6) vec3 c = A * z; // c = (6,7,8)

mat4 4x4 matrices (can store affine transformations) mat4 A = mat4(1.0, 5.0, 9.0, 13.0, 2.0, 6.0, 10.0, 14.0, vec4 vec4 vec4 vec4 x y z w = = = = vec4(1.0, vec4(0.0, vec4(0.0, vec4(0.0, 0.0, 1.0, 0.0, 0.0, vec4 vec4 vec4 vec4 a b c d = = = = A A A A a b c d * * * * x; y; z; w; // // // // = = = = 3.0, 7.0, 11.0, 15.0, 0.0, 0.0, 1.0, 0.0, 4.0, 8.0, 12.0 16.0); // in column-major order 0.0); 0.0); 0.0); 1.0); (1,2,3,4) (5,6,7,8) (9,10,11,12) (13,14,15,16)

Array We can declare fixed-size arrays (size known at compile time) Use C syntax. float A[4]; A[0] = 5; A[3] = 10; vec4 B[10]; B[3] = vec4(1,2,3,4); B[8].y = 10.0;

Swizzling Used to construct a vector from another vector by referring to multiple components at one time. vec4 a = vec4(1,2,3,4); vec3 b = a.xyz; // b = (1,2,3) vec2 c = a.qp; // c = (4,3) vec4 d = a.xxyy; // d = (1,1,2,2)

Type Conversion Syntax: <<variable>> = <<type>>( <<value>> ); Expression on RHS = constructor expression. Example: float a = 1.0; int b = int(a);

Shader Structure /* Multiple-lined comment */ // Single-lined comment // // Global variable definitions // void main() { // // Function body // }

Vertex Shader Grid.vert: #version 120 uniform mat4 VP; attribute vec4 vpos; void main() { gl_position = VP * vpos; } Each time the screen is drawn, this main() function is called once per vertex, as if it were in a for loop.

Vertex Shader Grid.vert: #version 120 uniform mat4 VP; attribute vec4 vpos; void main() { gl_position = VP * vpos; } The first thing to do is specify the GLSL version. (Note: syntax in other versions can be rather different!)

Vertex Shader Grid.vert: #version 120 uniform mat4 VP; attribute vec4 vpos; void main() { gl_position = VP * vpos; } Uniforms are one type of input to the shader. They are the same for each vertex drawn during one draw function. We will see how to set them later.

Vertex Shader Grid.vert: #version 120 uniform mat4 VP; attribute vec4 vpos; void main() { gl_position = VP * vpos; } Attribute variables link to vertex attributes, or data associated with each vertex. This one is set to the vertex position buffer. Each time main() is executed, vpos is set to the vertex currently being processed.

Vertex Shader Grid.vert: #version 120 uniform mat4 VP; attribute vec4 vpos; void main() { gl_position = VP * vpos; } gl_position is a special variable that holds the position of the vertex in clip space. Since a vertex shader s main output is the position in clip space, it must always set gl_position. This vertex shader just transforms each vertex position (by the VP matrix).

Fragment Shader Grid.frag: #version 120 Each time the screen is drawn, this main() function is called once per pixel. uniform vec4 ugridcolor; void main() { gl_fragcolor = ugridcolor; //vec4(1,1,1,1); }

Fragment Shader Grid.frag: #version 120 uniform vec4 ugridcolor; gl_fragcolor is a special variable that stores the color of the output fragment. Since a fragment shader computes the color of a fragment, it must always set gl_fragcolor. void main() { gl_fragcolor = ugridcolor; //vec4(1,1,1,1); }

Fragment Shader Grid.frag: #version 120 uniform vec4 ugridcolor; void main() { gl_fragcolor = ugridcolor; //vec4(1,1,1,1); } vec4 is a data type of 4D vector. Can be used to store: - homogeneous coordinates - RGBA color vec4(1,1,1,1) constructs an RGBA tuple with R=1, G=1, B=1, A=1, which is white. (Note it is commented out here. We are passing the same information for the color using the vec4 uniform ugridcolor)

OpenGL/GLSL Plumbing Suppose we have already created the program We tell OpenGL to use it. We then instruct OpenGL to draw the two triangles: HelloWorldScreen.java: // The vertices in our vertex float [] vertexpositions = { -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f }; //... // In the draw method program.use(); gldrawelements(...); GLProgram.unuse(); buffer, initialized earlier // // // // vertex vertex vertex vertex 0 1 2 3

CS 4620/4621 Framework Contains GLProgram class to abstract OpenGL calls: Added convenience methods Help keep conventions straight Controls mapping between attribute variables and vertex buffers

Now, to create a GLSL program Create a GLProgram object private GLProgram program; public void onentry(gametime gametime) { program = new GLProgram(); program.quickcreateresource( "cs4620/gl/grid.vert", // Path to vertex shader "cs4620/gl/grid.frag", // Path to fragment shader null); // Optional attribute list }

GLSL passing data around