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

Similar documents
CS475/CS675 - Computer Graphics. OpenGL Drawing

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

COMP371 COMPUTER GRAPHICS

Programming Graphics Hardware

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

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

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

CS 432 Interactive Computer Graphics

Rendering Objects. Need to transform all geometry then

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

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

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

CPSC 436D Video Game Programming

We assume that you are familiar with the following:

The Graphics Pipeline

Programming shaders & GPUs Christian Miller CS Fall 2011

Shaders. Slide credit to Prof. Zwicker

CS 432 Interactive Computer Graphics

The Graphics Pipeline

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

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

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

Three Main Themes of Computer Graphics

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

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

The Graphics Pipeline

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

OPENGL AND GLSL. Computer Graphics

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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

Ciril Bohak. - INTRODUCTION TO WEBGL

Models and Architectures

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

BCA611 Video Oyunları için 3B Grafik

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

INTRODUCTION TO OPENGL PIPELINE

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

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

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

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Mobile Application Programing: Android. OpenGL Operation

Geometry Shaders. And how to use them

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

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

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

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

CS452/552; EE465/505. Image Formation

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

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

12.2 Programmable Graphics Hardware

Introduction to Computer Graphics with WebGL

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

Introduction to OpenGL

Computer Graphics Seminar

The Application Stage. The Game Loop, Resource Management and Renderer Design

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

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

Computer Graphics. Anders Hast. måndag 25 mars 13

Supplement to Lecture 22

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

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

2: Introducing image synthesis. Some orientation how did we get here? Graphics system architecture Overview of OpenGL / GLU / GLUT

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

Programmable Graphics Hardware

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

CIS 581 Interactive Computer Graphics

Mobile graphics API Overview

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

Introduction to Shaders.

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

Understanding M3G 2.0 and its Effect on Producing Exceptional 3D Java-Based Graphics. Sean Ellis Consultant Graphics Engineer ARM, Maidenhead

CSE 167: Introduction to Computer Graphics Lecture #5: Visibility, OpenGL

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

CS130 : Computer Graphics Lecture 2: Graphics Pipeline. Tamar Shinar Computer Science & Engineering UC Riverside

An Overview GLUT GLSL GLEW

Mobile Application Programing: Android. OpenGL Operation

CS452/552; EE465/505. Review & Examples

Lecture 13: OpenGL Shading Language (GLSL)

CSC 8470 Computer Graphics. What is Computer Graphics?

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

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

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

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

CS 354R: Computer Game Technology

OpenGL shaders and programming models that provide object persistence

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

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

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

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

CS451Real-time Rendering Pipeline

OpenGL refresher. Advanced Computer Graphics 2012

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

CSE4030 Introduction to Computer Graphics

Grafica Computazionale: Lezione 30. Grafica Computazionale. Hiding complexity... ;) Introduction to OpenGL. lezione30 Introduction to OpenGL

Hands-On Workshop: 3D Automotive Graphics on Connected Radios Using Rayleigh and OpenGL ES 2.0

Transcription:

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

Graphics programming Typically deals with How to define a 3D scene with a virtual camera, 3D objects, light sources, textures, etc How to create a 2D projection of the 3D scene so that it can be displayed on a computer monitor Image source: http://www.realtimerendering.com

Real-time vs. offline rendering Real-time rendering: used in games and other interactive applications scene must be updated at 30-60 frames per second speed > image quality Offline rendering: used for visual effects, Pixar movies, etc rendering a single frame may take several hours! image quality > speed Image sources: id Software, Pixar

Polygon meshes Used to represent graphical 3D models Collections of vertices, edges, and faces The faces of a mesh can be arbitrary polygons, but are usually triangles or quadrilaterals The Stanford Bunny

Textures Allow us to add details without adding lots of polygons Original mesh Same mesh with shading and textures Model and textures: http://www.turbosquid.com

Shaders In real-time rendering, a shader is a small program that is compiled on the CPU (host) and executed on the GPU (device) The most common usage of shaders is to apply transformations on vertices and compute the level of light and color of fragments (pixel candidates) By using different shaders or shader inputs, we can drastically change the visual appearance of a 3D object Image source: http://www.realtimerendering.com

A trip through the programmable graphics pipeline

Vertex input

Vertex shader

Clipping and primitive assembly

Rasterization

Fragment shader

OpenGL Cross-platform, low-level API for rendering 2D and 3D graphics First version released in 1992. The specification is currently managed by the Khronos Group. Used in games, simulations, scientific visualization, CAD, mobile applications, virtual reality, etc State-based Only deals with rendering, not windowing or input Callable from most programming languages (C, C++, Python, Java, C#, JavaScript, Rust, Haskell,...)

Flavors OpenGL OpenGL ES For desktop applications For embedded systems (smartphones, tablets, etc) WebGL Allows you to render 3D graphics in a web browser. Based on OpenGL ES.

No, you don't have to learn all this...

OpenGL functions Executed in the host CPU application Responsible for: creating and initializing buffers, shaders, textures, etc uploading data to GPU-accessible memory configuring render state submitting draw calls clearing and swapping buffers

OpenGL utility libraries Since OpenGL is only concerned about rendering, we need to use various utility libraries in our applications We will use the following utility libraries in the labs: GLFW (used for creating and managing windows) GLEW (OpenGL extension loader) GLM (mathematics library) AntTweakBar (widget for tweaking parameters)

GLFW Simple and portable library that provides an interface between the graphics system and the operating and window system Allows the user to create a window for displaying OpenGL graphics execute the rendering loop set up mouse and keyboard interaction

The OpenGL Extension Wrangler Library (GLEW) Cross-platform open-source C/C++ library for loading OpenGL extensions Will search for and pull in all the OpenGL extensions that you need (or at least the ones that are supported) After installing GLEW, you can use it by including GL/glew.h in your application

OpenGL Mathematics (GLM) library A header-only C++ mathematics library for graphics programming Provides vector and matrix datatypes, transforms, quaternions, noise functions, and much more Similar syntax as GLSL Used in the host (CPU) C++ application only, not in the GLSL* shader! Link: http://glm.g-truc.net/ *GLSL has its own built-in math functions and datatypes

AntTweakBar Minimal GUI library for tweaking rendering parameters We'll use it in Assignment 3 and the project

Vertex data Each vertex in a graphical model has one or several different attributes, e.g., Position Color Normal vector Texture coordinate Vertex data is loaded/generated on the CPU and uploaded to GPU-accessible memory via buffer objects

Surface normals A surface normal is a vector that describes the local orientation of a surface at some vertex or face. Important for shading. Direction Normal vectors Normal vectors color-coded as RGB colors

Vertex buffer objects (VBOs) Used for uploading arrays of vertex data/attributes to GPU memory Before submitting a draw call, we must bind the VBO(s) We can use a separate VBO for each vertex attribute, or (usually more efficient) interleave several vertex attributes in a single VBO Separate VBOs Positions Normals Texture coordinates Interleaved attributes in a single VBO

Vertex array object (VAOs) A VAO stores references to one or several VBOs, along with their states and configurations At drawing, you only have to bind the VAO Simplifies code: you don't have to bind and configure the VBOs separately at each draw call Recent OpenGL core profiles require that you use VAOs See https://www.opengl.org/wiki/vertex_specification for more details about VBOs and VAOs

The OpenGL Shading Language (GLSL) A high-level, cross-platform shading language for real-time rendering Based on the C programming language. Uses similar naming conventions, data types, and control structures. Built-in: vector and matrix types (vec2, vec3, mat4, ) various math and utility functions for graphics programming (exp, max, dot, cross, normalize, mix, smootstep,...) texture lookup functions

Shader types OpenGL supports five types of shaders: Vertex shaders Fragment shaders Geometry shaders Tesselation shaders Compute shaders We will only cover vertex and fragment shaders in this course

The vertex shader Applies (among other things) colors and transformations on the input vertices Passes varying data to the fragment shader

The vertex shader (cont.) Vertex displacement

The fragment shader Takes uniforms and interpolated data from the vertex shader and rasterizer as input Computes the final color of fragments (pixel candidates) by, e.g., evaluating lighting equations

The fragment shader (cont.) Per-fragment lighting

GLSL source files GLSL source code is usually stored in plain ASCII text files We'll use the suffixes *.vert for vertex shaders and *.frag for fragment shaders At program startup, the host application loads the GLSL source files into strings and compile them into shaders that can be executed on the GPU

Creating, compiling, and linking GLSL shader programs Main steps: 1) Read vertex and fragment shader source files into strings 2) Create vertex shader object from the vertex shader source string 3) Create fragment shader object from the fragment shader source string 4) Create a program object and attach the vertex and fragment shader objects to it 5) Compile the program 6) Link the compiled program and check for errors 7) Deattach and delete the shader objects

Example Simple triangle

GLSL vertex shader (triangle.vert) #version 150 // specifies the GLSL version in vec4 a_position; // input vertex position void main() { // just sets the output vertex position // to the input vertex position gl_position = a_position; }

GLSL fragment shader (triangle.frag) #version 150 out vec4 frag_color; // output fragment color void main() { // sets the output fragment color to white gl_fragcolor = vec4(1.0, 1.0, 1.0, 1.0); }

Variables for communicating with shaders Three types of variables: Attribute variables Varying variables Uniform variables

Attribute variables A GLSL vertex shader can access vertex attributes via the in qualifier. Example: #version 150 in vec4 a_position; // input vertex position in vec3 a_color; // input vertex color out vec3 v_color; // output vertex color void main() { v_color = a_color; gl_position = a_position; }

Varying variables Provide an interface for passing data (color, normals, texture coordinates, etc) between the vertex shader and the fragment shader By default, varying data will be linearly interpolated over the geometric primitive

Varying variable defined in the vertex shader The vertex shader uses the out qualifier to pass varying data to the fragment shader #version 150 in vec4 a_position; // input vertex position in vec3 a_color; // input vertex color out vec3 v_color; // output vertex color void main() { v_color = a_color; gl_position = a_position; }

Varying data accessed in the fragment shader The fragment shader accesses the data via the in qualifier, and writes outputs via the out qualifier #version 150 in vec3 v_color; // interpolated vertex color out vec4 frag_color; // output fragment color void main() { frag_color = vec4(v_color, 1.0); }

Uniform variables Data that should be constant for all vertices and fragments can be passed to the shaders as uniforms Examples of data commonly passed as uniforms: transforms material properties (color, shininess, opacity, etc) light sources texture samplers time flags for enabling/disabling parts of the shading

Uniform variables Can be accessed in both the vertex shader and the fragment shader via the uniform qualifier #version 150 out vec4 frag_color; // output fragment color uniform vec3 u_color; void main() { // sets the output fragment color to the // color defined by the uniform variable frag_color = vec4(u_color, 1.0); }

gldrawarrays and gldrawelements Draw commands for rendering graphics primitives (points, lines, triangles) from array data stored in VBOs or VAOs void drawtriangle(gluint program, GLuint vao) { gluseprogram(program); glbindvertexarray(vao); gldrawarrays(gl_triangles, 0, 3); glbindvertexarray(0); gluseprogram(0); }

OpenGL primitives The following geometric primitives are the basic building blocks in OpenGL applications: Point sprites: GL_POINTS Lines: GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP Triangles: GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN Image source: http://www.realtimerendering.com

Coordinate systems The OpenGL graphics pipeline defines six different coordinate systems: Object (or model) coordinates World coordinates Eye (or camera) coordinates Clip coordinates Normalized device coordinates Window (or screen) coordinates Filip will talk more about them next week

Transforms Used to manipulate the position, orientation, size, and shape of objects in 2D/3D scenes We also use transforms to define our virtual camera and go from one coordinate system to another Usually represented as 3-by-3 or 4-by-4 matrices Examples of basic transforms: Translation Rotation Scaling Filip will cover this topic

Transforms in shader-based OpenGL Typically, we construct our transforms in the host C++ program (using, e.g., GLM) and pass them to the vertex shader as uniform variables On the GPU side, the vertex shader applies the transforms on the incoming vertex data #version 150 uniform mat4 u_transform; in vec4 a_position; // input vertex position void main() { gl_position = u_transform * a_position; }

Summary The host CPU application loads/creates vertex data, shaders, textures, etc, uploads them to GPU-accessible memory, and submits draw calls Shaders do all the visual magic! Vertices and fragments are processed in parallel on the GPU.

OpenGL web resources Good tutorials for modern shader-based OpenGL programming: http://learnopengl.com/ http://openglbook.com/ http://duriansoftware.com/joe http://open.gl/ http://tomdalling.com/blog/category/modern-opengl/ Reference pages: http://opengl.anteru.net/ http://www.khronos.org/files/opengl-quick-reference-card.pdf