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

Similar documents
How to use tessellation to add geometric detail to your scenes. How to use geometry shaders to process whole primitives and create geometry on the fly

OUTLINE. Tessellation in OpenGL Tessellation of Bezier Surfaces Tessellation for Terrain/Height Maps Level of Detail

HW-Tessellation Basics

CS195V Week 3. GLSL Programming

A Tessellated Parametric Triangular Bézier Patch

Tessellation Shaders

CS770/870 Fall 2015 Advanced GLSL

Mobile Application Programing: Android. OpenGL Operation

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

Mobile Application Programing: Android. OpenGL Operation

Mobile Application Programming: Android. OpenGL Operation

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

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

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

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

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

Geometry Shaders. And how to use them

Shaders. Slide credit to Prof. Zwicker

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

OPENGL RENDERING PIPELINE

TSBK03 Screen-Space Ambient Occlusion

Introduction to Computer Graphics. Hardware Acceleration Review

Sign up for crits! Announcments

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

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

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

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

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

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

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

The Projection Matrix

CPSC 436D Video Game Programming

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

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

Zeyang Li Carnegie Mellon University

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

2D rendering takes a photo of the 2D scene with a virtual camera that selects an axis aligned rectangle from the scene. The photograph is placed into

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

Many rendering scenarios, such as battle scenes or urban environments, require rendering of large numbers of autonomous characters.

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

object (say a cube) will be made up of triangles, each with three vertices, each with known object coordinates.

The Graphics Pipeline

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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

CS 432 Interactive Computer Graphics

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

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

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

The Graphics Pipeline

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

OPENGL AND GLSL. Computer Graphics

E.Order of Operations

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

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

We assume that you are familiar with the following:

Rasterization Overview

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

The OpenGL Shading Language

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

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

Pipeline Operations. CS 4620 Lecture 14

CMSC427 Fall 2017 OpenGL Notes A: Starting with JOGL and OpenGL Objectives of notes Readings Computer Graphics Programming in OpenGL with Java

Graphics Shaders. Theory and Practice. Second Edition. Mike Bailey. Steve Cunningham. CRC Press. Taylor&FnincIs Croup tootutor London New York

Rasterization. CS 4620 Lecture Kavita Bala w/ prior instructor Steve Marschner. Cornell CS4620 Fall 2015 Lecture 16

Computergrafik. Matthias Zwicker. Herbst 2010

Lecture 2. Shaders, GLSL and GPGPU

Introduction to the OpenGL Shading Language (GLSL)

COMP371 COMPUTER GRAPHICS

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

Terrain Rendering (Part 1) Due: Thursday November 30 at 10pm

Programming shaders & GPUs Christian Miller CS Fall 2011

TDA362/DIT223 Computer Graphics EXAM (Same exam for both CTH- and GU students)

Three Main Themes of Computer Graphics

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

BCA611 Video Oyunları için 3B Grafik

Shading Language Update

CS4620/5620: Lecture 14 Pipeline

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

The Rasterization Pipeline

CS 498 VR. Lecture 18-4/4/18. go.illinois.edu/vrlect18

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

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

Procedural modeling and shadow mapping. Computer Graphics CSE 167 Lecture 15

Mobile graphics API Overview

GPU Programming EE Final Examination

CS 4620 Program 3: Pipeline

Adaptive Point Cloud Rendering

EECE 478. Learning Objectives. Learning Objectives. Rasterization & Scenes. Rasterization. Compositing

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

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

Triangle Rasterization

CS559 Computer Graphics Fall 2015

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

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

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

CSE 167: Lecture #17: Procedural Modeling. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

Introduction to the OpenGL Shading Language

Transcription:

CSE 4431/5331.03M Advanced Topics in 3D Computer Graphics TA: Margarita Vinnikov mvinni@cse.yorku.ca

The OpenGL 4.x pipeline 2 new Programmable stages Tessellation Control Shader(GL_TESS_CONTROL_SHADER) Tessellation Evaluation Shader(GL_TESS_EVALUATION_SHADER) 1 new Fixed function stage Tessellation primitive generator aka tessellator 1 new primitive type Patches (GL_PATCHES)

Tessellator Uses tessellation levels to decompose a patch into a new set of primitive Each vertex is assigned a (u, v) or (u, v, w) coordinate

The patch primitive The input vertices are often referred to as control points Arbitrary number of vertices (1 to 32) glpatchparameteri(gl_patch_vertices, patchvcount) Only primitive type allowed when a tessellation control shaderis active No implied geometric ordering

Tessellation Control Shader(TCS) Output of a tessellation control shader is a new patch i.e., a new collection of vertices Responsible for generating: The per-patch inner and outer tessellation factors Computes LOD per patch gl_tesslevelouter[4] gl_tesslevelinner[2] The position and other attributes for each output control point Per-patch user-defined varyings Runs once for each vertex Patch discarded if gl_tesslevelouter[x] <= 0;// (Usefullfor Culling) gl_tesslevelouter[x] = NaN;// Optional If not present tessellation level will be set to their default value Default value can be changed using: glpatchparameterfv(gl_patch_default_outer_level, outerlevels) glpatchparameterfv(gl_patch_default_inner_level, innerlevels)

Tessellation Control Shader: Sample layout(vertices = 3) out; uniform float tesslevelouter; uniform float tesslevelinner; void main() { if (gl_invocationid == 0) { gl_tesslevelouter[0] = tesslevelouter; gl_tesslevelouter[1] = tesslevelouter gl_tesslevelouter[2] = tesslevelouter; gl_tesslevelinner[0] = tesslevelinner; } gl_out[gl_invocationid].gl_position= gl_in[gl_invocationid].gl_position; }

Name gl_invocationid contains the invocation index of the current shader Declaration in int gl_invocationid ; Description In the tessellation control language, gl_invocationid contains the index of the output patch vertex assigned to the shader invocation. It is assigned an integer value in the range [0, N-1] where N is the number of output patch vertices. In the geometry language, gl_invocationid identifies the invocation number assigned to the geometry shader invocation. It is assigned an integer value in the range [0, N-1] where N is the number of geometry shader invocations per primitive. Version Support [1] Versions 1.50 to 3.30 - geometry shaders only. Variabl e Name gl_invoc ationid OpenGL Shading Language Version 1.10 1.20 1.30 1.40 1.50 3.30 4.00 4.10 4.20 4.30 - - - - 1. Determine that it is a first invocation by 1. checking that gl_invocationid is zero 2. Calculate the tessellation levels for the whole patch

Tessellation Evaluation Shader(TES) Compute the position of each vertex produced by the tessellator Control the tessellation pattern Can specify orientation of generated triangles ccw(default) cw Capable of generating points instead of lines or triangles point_mode

Tessellation Subdivision Modes equal_spacing tesslevel= clamp(tesslevel, 1, maxtesslevel) Rounded to nearest integer fractional_even_spacing tesslevel= clamp(tesslevel, 2, maxtesslevel) Rounded to next even integer fractional_odd_spacing tesslevel= clamp(tesslevel, 1, maxtesslevel-1) Rounded to next odd integer Example: Inner and outer tessellation factors are set to 5.3. equal_spacing mode the number of segments along each of the outer edges of the triangle is 6 the next integer after 5.3. fractional_even_spacing spacing there are 4 equal-sized segments 4 is the next lower even integer to 5.3) 2 additional smaller segments. fractional_odd_spacing, there are 5 equal-sized segments (5 being the next lower odd integer to 5.3) 2 very skinny segments that make up the rest.

Tessellation Primitive Modes Use to determine how OpenGL breaks up patches into primitives before passing them on to rasterization. Set using an input layout qualifier in the tessellation evaluation shader One of quads, triangles, or isolines. Controls the form of the primitives produced by the tessellator, the interpretation of the gl_tesscoord input variable in the TES.

Tessellation Evaluation Shader: Sample layout(triangles, equal_spacing, ccw) in; //use viewmat and projmat; void main() { gl_position= vec4(gl_in[0].gl_position.xyz* gl_tesscoord.x+ gl_in[1].gl_position.xyz* gl_tesscoord.y+ gl_in[2].gl_position.xyz* gl_tesscoord.z+ 1.0); }

OpenGL Tessellation -Setup char *tcssource; // Null terminated string GLuinttcs= glcreateshader(gl_tess_control_shader); glshadersource(tcs, 1, tcssource, NULL); glcompileshader(tcs); glattachshader(program, tcs); char* tessource; // Null terminated string GLuinttes= glcreateshader(gl_tess_evaluation_shader); glshadersource(tes, 1, tessource, NULL); glcompileshader(tes); glattachshader(program, tes); gllinkprogram(program); glpatchparameteri(gl_patch_vertices, n); // tell OpenGL that every patch has n verts gldrawarrays(gl_patches, firstvert, vertcount); // draw a bunch of patches

Water-tight tessellation Cracks may occur due to floating point precision a + b + c!= c + b + a Use GLSL precise qualifier Ensure s that computations are done in their stated order precise out vec4 position; out vec4 position; precise position; // make existing variable precise

Notes Don t forget to switch to GL_TRIANGLES when disabling tessellation Frutum & Occlusion Culling Consider using the Tessellation Control Shaderto Cull patches not in frustum gl_tesslevelouter[x] = 0 Don t forget to take displacement into consideration Don t render occluded patches Use occlusion queries

Simple tessellation shaders Create simple triangle tessellation control shader example with gl_tesslevelinner[x] = 5.0; gl_tesslevelouter[x] = 8.0; Create simple triangle tessellation evaluation shader Pass model view and projection matrices for the model to rotate the model from your application Bonus: Pass inner and outer parameters from opengl application Create dynamic tessellation change Do the same for quade Adopt to 3d models such as icosahedron http://prideout.net/blog/?p=48

Putting it all together - Terrain Rendering

Setup and Vertex Array We will generate a grid of 64 64 patches A possible solution is by generating coordinates vertex shader : const vec4 vertices[] = vec4[](vec4(-0.5, 0.0, -0.5, 1.0), vec4( 0.5, 0.0, -0.5, 1.0), vec4(-0.5, 0.0, 0.5, 1.0), vec4( 0.5, 0.0, 0.5, 1.0)); You can access each vertex with gl_vertexid Use gldrawarraysinstanced(gl_patches, 0, 4, 64 * 64) instead of gldrawarrays Use glpatchparameteri(gl_patch_vertices, 16) x and y offsets for the patch are calculated by gl_instanceid modulo 64 gl_instanceid divided by 64 int x = gl_instanceid & 63; int y = gl_instanceid >> 6; vec2 offs = vec2(x, y); vs_out.tc = (vertices[gl_vertexid].xz + offs + vec2(0.5)) / 64.0; gl_position = vertices[gl_vertexid] + vec4(float(x - 32), 0.0, float(y - 32), 0.0); Output will be defined as follows: out VS_OUT { vec2 tc; } vs_out;

Name gl_vertexid contains the index of the current vertex Declaration in highp int gl_vertexid ; Description gl_vertexid is a vertex language input variable that holds an integer index for the vertex. The index is impliclty generated by gldrawarrays and other commands that do not reference the content of the GL_ELEMENT_ARRAY_BUFFER, or explicitly generated from the content of the GL_ELEMENT_ARRAY_BUFFERby commands such as gldrawelements. Version Support Name gl_instanceid contains the index of the current primitive in an instanced draw command Declaration in highp int gl_instanceid ; Description gl_instanceid is a vertex language input variable that holds the integer index of the current primitive in an instanced draw command such asgldrawarraysinstanced. If the current primitive does not originate from an instanced draw command, the value of gl_instanceid is zero. Version Support OpenGL ES Shading Language Version Variable Name 1.00 3.00 gl_vertexid - OpenGL ES Shading Language Version Variable Name 1.00 3.00 gl_vertexid -

Tessellation control shader Project the corners of the patch into normalized device coordinates Multiply incoming coordinates by model-viewprojection matrix Divide each of the 4 points by their own.w component Check that all of the z coordinates of the projected control points are less than zero Set the outer tessellation levels to zero This is an optimization that culls entire patches that are behind the viewer Project 4 into xy plane by ignoring z components Calculate the length of each of the 4 edges of the patch in normalized device space Calculate the tessellation levels of each edge As a function of its length by using a scale and bias Set outer tessellation factors By calculating the edge lengths in the horizontal or vertical directions Set the inner tessellation factors to the minimum of the outer tessellation factors By calculated the edge lengths in the horizontal or vertical directions #version 430 core layout (vertices = 4) out; in VS_OUT { vec2 tc; } tcs_in[]; out TCS_OUT { vec2 tc; } tcs_out[]; uniform mat4 mvp; void main(void){ if ( ){ vec4 p0 =.. vec4 p3 = p0 /=.. p3 /= p3.w; if ( ){ gl_tesslevelouter[0] =.. gl_tesslevelouter[3] = }Else{ float l0 = length(p2.xy - `````````````````p0.xy)*16.0+1.0; float l1 = float l2 = float l3 = gl_tesslevelouter[0] = l0; gl_tesslevelinner[0] = min(l1, l3); gl_tesslevelinner[1] = } } gl_out[gl_invocationid].gl_position = gl_in[gl_invocationid].gl_position; tcs_out[gl_invocationid].tc = tcs_in[gl_invocationid].tc; }

Tessellation evaluation shader Calculate texture coordinate of the generated vertex Linearly interpolating the texture coordinates from TCS Calculate position of the outgoing vertex Interpolate the incoming control point positions Uses texture coordinate to offset the vertex in the y direction Multiply the result by the modelview-projection matrix Make sure you pass required info from your opengl application #version 430 core layout (quads, fractional_odd_spacing)in; uniform sampler2d tex_displacement; uniform mat4 mvp; uniform float dmap_depth; in TCS_OUT { vec2 tc; } tes_in[]; out TES_OUT { vec2 tc; } tes_out; void main(void) { vec2 tc1 = mix(tes_in[0].tc, tes_in[1].tc, gl_tesscoord.x); vec2 tc2 = vec2 tc = mix(tc2, tc1, gl_tesscoord.y); vec4 p1 = mix(gl_in[0].gl_position, gl_in[1].gl_position, gl_tesscoord.x); vec4 p2 = vec4 p = p.y += texture(tex_displacement, tc).r * dmap_depth; } gl_position = ; tes_out.tc = ;

Fragment shader Use texture coordinate from the TES to look up a color for the fragment. #version 430 core out vec4 color; uniform sampler2d tex_color; in TES_OUT { vec2 tc; } fs_in; out vec4 color; void main(void) { color = texture(tex_color, fs_in.tc); }

Bonus Add lighting Add bump mapping Make dynamic scene