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

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

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

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

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

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

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

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

COMP371 COMPUTER GRAPHICS

CS 432 Interactive Computer Graphics

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

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

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

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

Geometry Shaders. And how to use them

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

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

Tutorial 04. Harshavardhan Kode. September 14, 2015

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

CPSC 436D Video Game Programming

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

Tutorial 1: Your First Triangle!

Supplement to Lecture 22

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

OpenGL refresher. Advanced Computer Graphics 2012

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

Z-buffer Pipeline and OpenGL. University of Texas at Austin CS384G - Computer Graphics

CS452/552; EE465/505. Review & Examples

An Introduction to OpenGL Programming

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

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

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

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

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

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

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

Computer Graphics (CS 4731) & 2D Graphics Systems

Introduction to Computer Graphics with WebGL

Lecture 09: Shaders (Part 1)

We assume that you are familiar with the following:

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

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

CS452/552; EE465/505. Models & Viewing

CS 432 Interactive Computer Graphics

gvirtualxray Tutorial 01: Creating a Window and an OpenGL Context Using GLUT

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

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

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

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

Rendering Objects. Need to transform all geometry then

An Overview GLUT GLSL GLEW

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

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

CS452/552; EE465/505. Transformations

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

Programming with OpenGL Complete Programs Objectives Build a complete first program

Introduction to Computer Graphics. Hardware Acceleration Review

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Shaders. Slide credit to Prof. Zwicker

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

6.S096 Lecture 9 Visualization

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

GLSL Overview: Creating a Program

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

3d Programming I. Dr Anton Gerdelan

8 Three-Dimensional Object Representations. Chapter 8. Three-Dimensional Object Representations. Department of Computer Science and Engineering 8-1

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

INTRODUCTION TO OPENGL PIPELINE

CS452/552; EE465/505. Image Formation

Erik Anchondo cse 520 lab 4

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

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

Introduction to Computer Graphics with OpenGL/GLUT

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

Lecture 4 of 41. Lab 1a: OpenGL Basics

2D graphics with WebGL

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

Texture Mapping. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

CS 548: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE

Computer Graphics. Bing-Yu Chen National Taiwan University

Programming shaders & GPUs Christian Miller CS Fall 2011

RECITATION - 1. Ceng477 Fall

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

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

Computer Graphics. OpenGL

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

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

Introduction to Computer Graphics with WebGL

Computer Graphics 1 Computer Graphics 1

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

Computer graphics MN1

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

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

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

Exercise 1 Introduction to OpenGL

Transcription:

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders Prateek Shrivastava CS12S008 shrvstv@cse.iitm.ac.in 1

GLSL Data types Scalar types: float, int, bool Vector types: vec2, vec3, vec4 ivec2, ivec3, ivec4 bvec2, bvec3, bvec4 Matrix types: mat2, mat3, mat4 Texture sampling: sampler1d, sampler2d, sampler3d,samplercube C++ Style Constructors vec3 a = vec3(1.0, 2.0, 3.0); Standard C/C++ arithmetic and logic operators Operators overloaded for matrix and vector operations Mat4 m; Vec4 a, b, c; b = a*m; c = m*a;

GLSL BASICS For vectors can use [ ], xyzw, rgba or stpq vec3 v; v[1], v.y, v.g, v.t all refer to the same element Swizzling: vec3 a, b; a.xy = b.yx;

Representing geometric objects Geometric objects are represented using vertices A vertex is a collection of generic attributes positional coordinates colors texture coordinates any other data associated with that point in space Vertex data must be stored in vertex buffer objects (VBOs) VBOs must be stored in vertex array objects (VAOs)

OpenGL Geometric Primitives All primitives are specified by vertices GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

First Program Rendering a cube with colors at each vertex Example demonstrates: initializing and organizing vertex data for rendering. simple object modeling

Initializing the Cube s Data We ll build each cube face from individual triangles Need to determine how must storage is required (6 faces)(2 triangles/face)(3 vertices/triangle) const int NumVertices = 36; To simplify communicating with GLSL, we ll use a vec4 class (implemented in C++) similar to GLSL s vec4 type we ll also typedef it to add logical meaning typedef vec4 point4; typedef vec4 color4;

Initializing the Data Before we can initialize our VBO, we need to stage the data Our cube has two attributes per vertex position color We create two arrays to hold the VBO data point4 points[numvertices]; color4 colors[numvertices];

Cube Data // Vertices of a unit cube centered at origin, sides aligned with axes point4 vertex_positions[8] = { point4( -0.5, -0.5, 0.5, 1.0 ), point4( -0.5, 0.5, 0.5, 1.0 ), point4( 0.5, 0.5, 0.5, 1.0 ), point4( 0.5, -0.5, 0.5, 1.0 ), point4( -0.5, -0.5, -0.5, 1.0 ), point4( -0.5, 0.5, -0.5, 1.0 ), point4( 0.5, 0.5, -0.5, 1.0 ), point4( 0.5, -0.5, -0.5, 1.0 ) }; // RGBA colors color4 vertex_colors[8] = { color4( 0.0, 0.0, 0.0, 1.0 ), // black color4( 1.0, 0.0, 0.0, 1.0 ), // red color4( 1.0, 1.0, 0.0, 1.0 ), // yellow color4( 0.0, 1.0, 0.0, 1.0 ), // green color4( 0.0, 0.0, 1.0, 1.0 ), // blue color4( 1.0, 0.0, 1.0, 1.0 ), // magenta }; color4( 1.0, 1.0, 1.0, 1.0 ), // white color4( 0.0, 1.0, 1.0, 1.0 ) // cyan

Generating cube face from vertices // quad() generates two triangles for each face and assigns colors to the vertices int Index = 0; // global variable indexing into VBO arrays void quad( int a, int b, int c, int d ) { } colors[index] = vertex_colors[a]; points[index] =vertex_positions[a]; Index++; colors[index] = vertex_colors[b]; points[index] =vertex_positions[b]; Index++; colors[index] = vertex_colors[c]; points[index] =vertex_positions[c]; Index++; colors[index] = vertex_colors[a]; points[index] =vertex_positions[a]; Index++; colors[index] = vertex_colors[c]; points[index] =vertex_positions[c]; Index++; colors[index] = vertex_colors[d]; points[index] =vertex_positions[d]; Index++;

Generating cube from faces // generate 12 triangles: 36 vertices and 36 colors void colorcube() { } quad( 1, 0, 3, 2 ); quad( 2, 3, 7, 6 ); quad( 3, 0, 4, 7 ); quad( 6, 5, 1, 2 ); quad( 4, 5, 6, 7 ); quad( 5, 4, 0, 1 );

Vertex Array Objects (VAOs) VAOs store the data of an geometric object. Steps in using a VAO 1. generate VAO names by calling glgenvertexarrays() 2. bind a specific VAO for initialization by calling glbindvertexarray() 3. update VBOs associated with this VAO 4. bind VAO for use in rendering This approach allows a single function call to specify all the data for an objects previously, you might have needed to make many calls to make all the data current

VAOs in code // Create a vertex array object GLuint vao; glgenvertexarrays( 1, &vao ); glbindvertexarray( vao );

Storing vertex Attributes Vertex data must be stored in a VBO, and associated with a VAO The code-flow is similar to configuring a VAO 1. generate VBO names by calling glgenbuffers() 2. bind a specific VBO for initialization by calling glbindbuffer( GL_ARRAY_BUFFER, ) 3. load data into VBO using glbufferdata( GL_ARRAY_BUFFER, ) 4. bind VAO for use in rendering glbindvertexarray()

VBOs in code // Create and initialize a buffer object GLuint buffer; glgenbuffers( 1, &buffer ); glbindbuffer( GL_ARRAY_BUFFER, buffer ); glbufferdata( GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW ); glbuffersubdata( GL_ARRAY_BUFFER, 0, sizeof(points), points ); glbuffersubdata( GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors );

Connecting Vertex Shaders with Geometric Data Application vertex data enters the OpenGL pipeline through the vertex shader Need to connect vertex data to shader variables requires knowing the attribute location Attribute location can either be queried by calling glgetvertexattriblocation()

Vertex Array Code // set up vertex arrays (after shaders are loaded) GLuint vposition = glgetattriblocation( program, "vposition" ); glenablevertexattribarray( vposition ); glvertexattribpointer( vposition, 4, GL_FLOAT, GL_FALSE, 0,BUFFER_OFFSET(0) ); GLuint vcolor = glgetattriblocation( program, "vcolor" ); glenablevertexattribarray( vcolor ); glvertexattribpointer( vcolor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)) );

Drawing Geometric Primitives For contiguous groups of vertices gldrawarrays( GL_TRIANGLES, 0, NumVertices ); Usually invoked in display callback Initiates vertex shader

GLSL variable types Attribute const A variable used only in vertex shaders; Is set by the application per vertex and is generally sent from the application to the graphics card by OpenGL functions Eg. color, normal, normal matrix or texture coordinates. Compile time constant and can't be accessed outside the shader that defines it.(like c++) in, out, inout Copy vertex attributes and other variable to/ from shaders in vec2 tex_coord; out vec4 color;

GLSL variables

GLSL variable types Uniform: variable from application (outside the shader) Can be changed at most once per primitive. uniform float time; uniform vec4 rotation; Varying variables: They provide communication from vertex Shaders to Fragment Shaders. Vertex Shaders compute information for each vertex and write them to varying variables to be interpolated across a graphics primitive and then used by a fragment shader. How often the data they represent changes: Uniform: Infrequently and never within a graphics primitive. Attribute: Frequently, often as frequently as each vertex. Varying: Most frequently, with each fragment as it s interpolated by the rasterizer.

GLSL limitations C-like with some limitations. No typecasts (use constructors instead). No automatic promotion (although some GLSL compilers handle this). No pointers, enums, strings. No file-based pre-processor directives. Only use 1D arrays (no bounds checking).

Vertex Shaders In the fixed-function geometry pipeline A GLSL Vertex shader: Replaces these operations Vertex Transformation. Normal Transformation. Normal normalization. Handling per-vertex lighting. Handling texture Coordinates. Doesn t replaces View volume clipping. Homogeneous division. Viewport mapping. Backface culling. Polygon mode. Polygon offset.

Vertex Shader 25

A BASIC VERTEX SHADER #version 400 layout(location=0) in vec4 in_position; layout(location=1) in vec4 in_color; out vec4 ex_color; void main(void){ gl_position = in_position; ex_color = in_color; }

Rotation Vertex Shader for the CUBE in vec4 vposition; in vec4 vcolor; out vec4 color; uniform vec3 theta; void main() { // Compute the sines and cosines of theta for // each of the three axes in one computation. vec3 angles = radians( theta ); vec3 c = cos( angles ); vec3 s = sin( angles ); // Remember: these matrices are column-major mat4 rx = mat4( 1.0, 0.0, 0.0, 0.0, 0.0, c.x, s.x, 0.0, 0.0, -s.x, c.x, 0.0, 0.0, 0.0, 0.0, 1.0 );

mat4 ry = mat4( c.y, 0.0, -s.y, 0.0, 0.0, 1.0, 0.0, 0.0, s.y, 0.0, c.y, 0.0, 0.0, 0.0, 0.0, 1.0 ); mat4 rz = mat4( c.z, -s.z, 0.0, 0.0, s.z, c.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ); } color = vcolor; gl_position = rz * ry * rx * vposition;

Fragment Shaders In the fixed-function geometry pipeline A GLSL Vertex shader: Replaces these operations Color Computation. Texturing. Per-pixel lighting. Fog. Discarding Pixels in fragments. Doesn t replaces Blending. Stencil Test. Depth Test. Scissor Test. Stippling operations. Raster operations performed as a pixel is being written to the frame buffer.

A simple Fragment Shader #version 400 in vec4 ex_color; out vec4 out_color; void main(void) { This data may include, but is not limited to: raster position depth interpolated attributes (color, texture coordinates, etc.) stencil alpha window ID } out_color = ex_color; A fragment is the data necessary to generate a single pixel's worth of a drawing primitive in the frame buffer. In general, a fragment can be thought of as the data needed to shade the pixel, plus the data needed to test whether the fragment survives to become a pixel (depth, alpha, stencil, scissor, window ID, etc.).

Vertex Shader for textures in vec4 vposition; in vec4 vcolor; in vec2 vtexcoord; out vec4 color; out vec2 texcoord; void main() { color = vcolor; texcoord = vtexcoord; gl_position = vposition; }

Fragment Shader for texture in vec4 color; in vec2 texcoord; uniform sampler2d texture; void main() { gl_fragcolor = color * texture( texture, texcoord ); }

Getting shader to OpenGL Shaders need to be compiled and linked to form an executable shader program OpenGL provides the compiler and linker A program must contain vertex and fragment shaders other shaders are optional Create Program Create Shader Load Shader Source Compile Shader Attach Shader to Program Link Program glcreateprogram() glcreateshader() glshadersource() glcompileshader() glattachshader() gllinkprogram() These steps need to be repeated for each type of shader in the shader program Use Program gluseprogram()

void CreateShaders(void) { GLenum ErrorCheckValue = glgeterror(); VertexShaderId = glcreateshader(gl_vertex_shader); glshadersource(vertexshaderid, 1, &VertexShader, NULL); glcompileshader(vertexshaderid); FragmentShaderId = glcreateshader(gl_fragment_shader); glshadersource(fragmentshaderid, 1, &FragmentShader, NULL); glcompileshader(fragmentshaderid); ProgramId = glcreateprogram(); glattachshader(programid, VertexShaderId); glattachshader(programid, FragmentShaderId); gllinkprogram(programid); gluseprogram(programid); } ErrorCheckValue = glgeterror(); if (ErrorCheckValue!= GL_NO_ERROR) { fprintf(stderr,"error: Could not create the shaders: %s \n", gluerrorstring(errorcheckvalue)); exit(-1); }

In the program const GLchar* FragmentShader = { "#version 400\n"\ "in vec4 ex_color;\n"\ "out vec4 out_color;\n"\ }; "void main(void)\n"\ "{\n"\ " out_color = ex_color;\n"\ "}\n"

Finishing the Cube Program int main( int argc, char **argv ) { glutinit( &argc, argv ); glutinitdisplaymode( GLUT_RGBA GLUT_DOUBLE GLUT_DEPTH ); glutinitwindowsize( 512, 512 ); glutcreatewindow( "Color Cube" ); glewinit(); init(); glutdisplayfunc( display ); glutkeyboardfunc( keyboard ); glutmainloop(); return 0; }

Cube Program GLUT Callbacks void display( void ) { glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); gldrawarrays( GL_TRIANGLES, 0, NumVertices ); glutswapbuffers(); } void keyboard( unsigned char key, int x, int y ) { switch( key ) { case 033: case 'q': case Q': exit( EXIT_SUCCESS ); break; } }