The OpenGL Shading Language

Similar documents
Tuesday, 23 March OpenGL Shading Language

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

GLSL Language Details

Design Focus. GLSL Language Details. Additions from C++ Additions for Graphics

12.2 Programmable Graphics Hardware

Programmable Graphics Hardware

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

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

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

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

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

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

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

Lecture 09: Shaders (Part 1)

Supplement to Lecture 22

Introduction to the OpenGL Shading Language

GLSL Geometry Shaders

Programming shaders & GPUs Christian Miller CS Fall 2011

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

Mobile Application Programing: Android. OpenGL Operation

Vertex & Fragment shaders

Geometry Shaders. And how to use them

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

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

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

0.Features of the OpenGL Shading Language

Mobile Application Programing: Android. OpenGL Operation

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

0.Features of the OpenGL Shading Language

Introduction to GLSL. Announcements. Agenda. Course Contents. Patrick Cozzi University of Pennsylvania CIS Fall 2012

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

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

COMP371 COMPUTER GRAPHICS

2D graphics with WebGL

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

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

Shading Language Update

Shading Languages. Ari Silvennoinen Apri 12, 2004

OpenGL shaders and programming models that provide object persistence

Programmable Graphics Hardware

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

Shaders. Slide credit to Prof. Zwicker

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Mobile Application Programming: Android. OpenGL Operation

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

The Graphics Pipeline

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

CS 432 Interactive Computer Graphics

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

An Overview GLUT GLSL GLEW

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

The Graphics Pipeline

GLSL v1.20. Scott MacHaffie Schrödinger, Inc.

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

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

Programmable GPUs Outline

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

GPU Programming EE Final Examination

Shaders (some slides taken from David M. course)

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

Introduction to the OpenGL Shading Language (GLSL)

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

GLSL Programming. Nicolas Holzschuch

Introduction to Shaders.

We assume that you are familiar with the following:

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

Why modern versions of OpenGL should be used Some useful API commands and extensions

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

CS195V Week 3. GLSL Programming

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

Programmable shader. Hanyang University

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

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

Introduction to Computer Graphics. Hardware Acceleration Review

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

LSU EE Homework 3 Solution Due: 16 November 2012

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

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

GPU Programming EE Final Examination

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

1 of 5 9/10/ :11 PM

OpenGL Programmable Shaders

Mobile graphics API Overview

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

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

Lecture 11 Shaders and WebGL. October 8, 2015

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

Computergraphics Exercise 15/ Shading & Texturing

Pipeline Operations. CS 4620 Lecture 14

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

OPENGL RENDERING PIPELINE

CS 4620 Program 3: Pipeline

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

Appendix A Cg Language Specification

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

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

Transcription:

The OpenGL Shading Language

GLSL In the previous lecture we looked at the process of loading shaders into the programmable hardware (GPU) The actual language syntax of GLSL and the principles behind it will be covered in this lecture First we will look at the different processors in the GPU

Geometric Data Passed as generic vertex attributes Vertex Shading Tessellation Shading Geometry Shading Rasterizer Fragment Shading Pixel Data Textures or Texture buffer objects Tessellation Shading Tessellation control Shader new in OpenGL 4.3 Compute Shading Primitive Generator Tessellation Evaluation Shader

Vertex Processor The vertex processor is a programmable unit that operates on incoming vertices and their associated data. Compilation units written in the OpenGL Shading Language to run on this processor are called vertex shaders. The vertex processor operates on one vertex at a time. It does not replace graphics operations that require knowledge of several vertices at a time.

Vertex processor (Rost 2009)

Geometry Processor The geometry processor is a programmable unit that operates on data for incoming vertices for a primitive assembled after vertex processing and outputs a sequence of vertices forming output primitives. A single invocation of the geometry shader executable on the geometry processor will operate on a declared input primitive with a fixed number of vertices. This single invocation can emit a variable number of vertices that are assembled into primitives of a declared output primitive type and passed to subsequent pipeline stages.

Fragment Processor The fragment processor is a programmable unit that operates on fragment values and their associated data A fragment shader cannot change a fragment's (x, y) position. Access to neighbouring fragments is not allowed. The values computed by the fragment shader are ultimately used to update framebuffer memory or texture memory, depending on the current OpenGL state and the OpenGL command that caused the fragments to be generated.

The Fragment (2) Processor (Rost 2009)

Vertex Processor Input Vertex shader is executed once each time a vertex position is specified gldrawarrays or other vertex array calls Per-vertex input values are called attributes colour, normal, position, arbitrary values Change every vertex Passed through normal OpenGL mechanisms (per-vertex API or vertex arrays) More persistent input values are called uniforms lights, material properties, arbitrary values Constant across at least one primitive, typically constant for many primitives Passed through new OpenGL API calls (gluniform...)

Vertex Processor Output Vertex shader uses input values to compute output values Vertex shader must compute gl_position Mandatory, needed by the rasterizer Usually this will be transformed by our own MVP matrix Vertex shader may compute: gl_clipvertex (if user clipping is to be performed) gl_pointsize (if point parameters are to be used)

Vertex Processor Output Other output values are called varying variables E.g., colour, texture co-ordinates, arbitrary data Will be interpolated in a perspective-correct fashion across the primitives Defined by the vertex shader Can be of type float, vec2, vec3, vec4, mat2, mat3, mat4, or arrays of these Output of vertex processor feeds into OpenGL fixed functionality If a fragment shader is active, output of vertex shader must match input of fragment shader If no fragment shader is active, output of vertex shader must match the needs of fixed functionality fragment processing

Fragment Processor Input At it s simplest level the output of the Vertex Processor is the input to the fragment (if we ignore geo and tess shaders) Compatibility is checked when linking occurs Compatibility between the two is based on varying variables that are defined in both shaders and that match in type and name Fragment shader is executed for each fragment produced by rasterization For each fragment, fragment shader has access to the interpolated value for each varying variable Colour, normal, texture coordinates, arbitrary values

Fragment Processor Input Fragment shader may access: gl_frontfacing contains facingness of primitive that produced the fragment gl_fragcoord contains computed window relative coordinates x, y, z, 1/w Uniform variables are also available

Fragment Processor Output Output of the fragment processor goes on to the fixed function fragment operations and frame buffer operations using built-in variables gl_fragcolor computed R, G, B, A for the fragment gl_fragdepth computed depth value for the fragment Both values are destined for writing into the frame buffer if back end tests all pass Clamping or format conversion to the target buffer is done automatically outside of the fragment shader

Fragment Processor The fragment processor executes the fragment shader The fragment processor has knowledge of only the current fragment An implementation may have multiple fragment processors operating in parallel The fragment processor in glsl 150 and above use generic output attributes, by default the first attribute is the fragment colour, however we can bind different attributes using glbindfragdatalocation(program, 0, "fragcolor");

GLSL Language Syntax GLSL Language syntax is very similar to that of Renderman SL Most of the usual data types are present Other elements are available to fit into the GL pipeline

Shader Basic Structure A shader is a sequence of declarations and function bodies Curly braces are used to group sequences of statements A shader must have a main function Statements end with a semi-colon

pre-processor The GLSL compiler has a pre-processor just like a normal C compiler. It has the following keywords #define #undef #if #ifdef #ifndef #else #elif #endif #error #pragma #extension #version #line

#version #version is used to determine what version of GLSL is the minimum required to compile the shader. The language version a shader is written to is specified by #version number (optional) profile If the optional profile argument is provided, it must be the name of an OpenGL profile. Currently, there are three choices: core compatibility es

#pragma Used to enable compiler options / features Common ones used are #pragma optionnv(fastmath on) #pragma optionnv(fastprecision on) #pragma optionnv(ifcvt none) #pragma optionnv(inline all) #pragma optionnv(strict on) #pragma optionnv(unroll all)

Type Qualifiers There are many different type qualifiers in GLSL for a thorough examination of them see http:// www.opengl.org/wiki/type_qualifier_(glsl) Typical use for these qualifiers are to tell glsl what direction the data is coming from (in / out) How it changes as the shader is executed How and if interpolation should be used. Some versions of GLSL (ES for example) will need these specified by default.

Vertex shader attribute index Vertex shader inputs can specify the attribute index that the particular input uses. This is done with this syntax: layout(location = attribute index) in vec3 position; This means that we don t need to use glbindattriblocation If you try to combine the two and they conflict, the layout qualifier always wins.

Vertex shader attribute index Attributes that take up multiple attribute slots will be given a sequential block of that number of attributes in order starting with the given attribute. For example: layout(location = 2) in vec3 values[4]; This will allocate the attribute indices 2, 3, 4, and 5.

Scalars The basic non-tuple data types in GLSL are uint: an unsigned 32-bit integer float: a floating point number bool: conditional type, values may be either true or false int: a signed, two's complement, 32-bit integer double: a double-precision floating-point number

Vectors Each of the scalar types, including booleans, have 2, 3, and 4-component vector equivalents. The n digit below can be 2, 3, or 4: bvecn : a vector of booleans ivecn : a vector of signed integers uvecn : a vector of unsigned integers vecn : a vector of single-precision floating-point numbers dvecn : a vector of double-precision floating-point numbers

Swizzling Vector component access can be achieved in a number of ways You can use x, y, z, or w, referring to the first, second, third, and fourth components, respectively. vec4 somevec; somevec.x + somevec.y; vec2 somevec; vec4 othervec = somevec.xyxx; vec3 thirdvec = othervec.zyy; vec4 somevec; somevec.wzyx = vec4(1.0, 2.0, 3.0, 4.0); somevec.zx = vec2(3.0, 5.0);

Swizzling You cannot use the same swizzle component twice. So somevec.xx = vec2(4.0, 4.0); is not allowed. Additionally, there are 3 sets of swizzle masks. You can use xyzw, rgba (for colors), or stpq (for texture coordinates). These three sets have no actual difference they're just syntactic sugar. (but try and use the correct one in context) You cannot combine names from different sets in a single swizzle operation. So ".xrs" is not a valid swizzle mask.

Swizzling Vector components can be referred to using array syntax or a single letter: [0], [1], [2], [3] r, g, b, a x, y, z, w s, t, p, q This syntax can be used to extract, duplicate, or swizzle components 1 vec4 pos = vec4(1.0, 2.0, 3.0, 4.0); 2 vec4 swiz= pos.wzyx; // swiz = (4.0, 3.0, 2.0, 1.0) 3 vec4 dup = pos.xxyy; // dup = (1.0, 1.0, 2.0, 2.0) 4 5 pos.xw = vec2(5.0, 6.0); // pos = (5.0, 2.0, 3.0, 6.0) 6 pos.wx = vec2(7.0, 8.0); // pos = (8.0, 2.0, 3.0, 7.0) 7 pos.xx = vec2(3.0, 4.0); // illegal - 'x' used twice

Matrices All matrix types are floating-point, either single-precision or double-precision. Matrix types are as follows, where n and m can be the numbers 2, 3, or 4: matnxm : A matrix with n columns and m rows. OpenGL uses column-major matrices, which is standard for mathematics users. Example: mat3x4. matn : A matrix with n columns and n rows. Shorthand for matnxn

Matrices Swizzling does not work with matrices. Instead [] array access is used mat3 m1; m1[1] = vec3(1.0, 2.0, 0.0); m1[2][0] = 16.0; mat3 m1; m1[1].yzx = vec3(3.0, 1.0, 2.0);

Matrix Components Matrix components can be accessed using array subscripting syntax A single subscript selects a single column A second subscript selects a component within a column 1 mat4 m; 2 m[1] = vec4(2.0); // sets the second column to all 2.0 3 m[0][0] = 1.0; // sets the upper left element to 1.0 4 m[2][3] = 2.0; // sets the 4th element of the third 5 // column to 2.0

structs 1 struct surfacematerial 2 { 3 float ambient; 4 float diffuse; 5 float specular; 6 vec3 basecolor; 7 } surf; 8 9 surfmaterial surf1, surf2; User-defined types can be created using struct with previously defined types Creates a new type called surfacematerial Defines variables of this type called surf, surf1, and surf2 Structures can include arrays Fields are selected using the period (. )

Structure Constructors Constructor for a structure is available once structure is defined Example: 1 struct light 2 { 3 float intensity; 4 vec3 position; 5 }; 6 7 light newlight = light(3.0, vec3(1.0, 2.0, 3.0));

1 float ramp[10]; 2 vec4 colors[4]; 3 bool results[3]; Arrays structures can be aggregated into arrays Only 1D arrays are supported (GLSL 4.3 has multidimensional arrays but not all gpu s support this) Size of array can be expressed as an integral constant expression within square brackets ([ ]) Arrays can be declared without a size, and then redeclared later with the same type and a size Using an index that goes beyond an array s bounds results in undefined behaviour

Arrays The length of an array variable can be computed with the.length() function. uniform float myvalues[12];... myvalues.length(); //Returns 12

Type Qualifiers const variable is a constant and can only be written during its declaration in per-vertex data values provided to the vertex shader out per-vertex data values passed from vertex to fragment shader uniform (relatively) constant data provided by the application or by OpenGL for use in the shader in for function parameters copied into a function, but not copied out out for function parameters copied out of a function, but not copied in

Function Examples Declaration Definition 1 vec3 computecolor (in vec3 c1, in vec3 c2); 2 float radians (float degrees); 1 float myfunc (in float f1, // f1 is copied in 2 inout float f2) // f2 is copied in and out 3 { 4 float myresult; 5 6 // do computations 7 8 return myresult; 9 }

Vertex Shader Built-in Variables The following special variables are available in a vertex shader: 1 vec4 gl_position; // must be written to 2 float gl_pointsize; // may be written to 3 vec4 gl_clipvertex; // may be written to Every execution of a vertex shader must write the homogeneous vertex position into gl_position Vertex shaders may write the size of points to be rasterized (measured in pixels) into the built-in variable gl_pointsize Vertex shaders may write the transformed coordinate to be used in conjunction with user clipping planes into gl_clipvertex

Vertex Shader Built-in Attributes The following are available from a vertex shader for accessing standard OpenGL vertex attributes, however they are deprecated in the latest standard. 1 attribute vec4 gl_color; 2 attribute vec4 gl_secondarycolor; 3 attribute vec3 gl_normal; 4 attribute vec4 gl_vertex; 5 attribute vec4 gl_multitexcoord0; 6 attribute vec4 gl_multitexcoord1; 7 attribute vec4 gl_multitexcoord2; 8 attribute vec4 gl_multitexcoord3; 9 attribute vec4 gl_multitexcoord4; 10 attribute vec4 gl_multitexcoord5; 11 attribute vec4 gl_multitexcoord6; 12 attribute vec4 gl_multitexcoord7; 13 attribute float gl_fogcoord;

Built-in Functions Trigonometry/angle radians, degrees, sin, cos, tan, asin, acos, atan Exponential pow, exp2, log2, sqrt, inversesqrt Common abs, sign, floor, ceil, fract, mod, min, max, clamp, mix, step, smoothstep Geometric and matrix length, distance, dot, cross, normalize, ftransform, faceforward, reflect, matrixcompmult Vector relational lessthan, lessthanequal, greaterthan, greaterthanequal, equal, any, all Texture lookup texture1d/2d/3d, texture1d/2d/3dproj, texturecube, texture1d/2dshadow, texture1d/2dshadowproj Fragment shader only dfdx, dfdy, fwidth Noise noise1/2/3/4 (Never seen this implemented!)

Accessing Built In Pipeline GLSL has the ability to access the built in Light and Material Properties however these are now deprecated and we will discuss other ways of accessing this in a later lecture Two main structures for this are gl_frontmaterial gl_lightsource[gl_maxlights]

Geometry Shaders The geometry processor is a programmable unit that operates on data for incoming vertices for a primitive assembled after vertex processing and outputs a sequence of vertices forming output primitives. A single invocation of the geometry shader executable on the geometry processor will operate on a declared input primitive with a fixed number of vertices. This single invocation can emit a variable number of vertices that are assembled into primitives of a declared output primitive type and passed to subsequent pipeline stages.

Geometry shaders Unlike Vertex and Fragment shaders, Geo shaders have access to the whole primitive (triangle, line or point) at once The geo shader can change the amount of data in the OpenGL pipeline This is in contrast with the vertex shader which works in a one in / one out form or the fragment shader which can only discard a fragment if it doesn t want to process it.

Basic pass through #version 150 layout (triangles) in; layout (triangle_strip) out; layout (max_vertices=3) out; void main(void) { for(int i=0; i<gl_in.length(); ++i) { gl_position=gl_in[i].glposition; EmitVertex(); } EndPrimitive(); }

pass through This shader just sends the input to the output and doesn t generate any new geometry The first elements of the shader are know as layout qualifiers These inform the shader what the input will be, the output format of the geometry and the max number of vertices that may be generated This have the following type

in layout qualifiers Shader Input points lines triangles lines_adjacency triangles_adjacency Draw Modes GL_POINTS GL_LINES, GL_LINE_LOOP,GL_LINE_STRIP GL_TRIANGLES,GL_TRIANGLE_FAN,GL_TRIANGLE _STRIP GL_LINES_ADJACENCY GL_TRIANGLES_ADJACENCY

output qualifiers The geometry shader can only output the following primitive types points line_strip triangle_strip The max_vertices value is used to indicate the max size that may be output, this should be kept to a minimum as OpenGL may allocate buffer space for this and will reduce performance

Geometry Shaders Geo shaders uses a the following structures to receive and emit vertices in gl_pervertex { vec4 gl_position; float gl_pointsize; float gl_clipdistance[]; } gl_in[]; in int gl_primitiveidin; out gl_pervertex { vec4 gl_position; float gl_pointsize; float gl_clipdistance[]; };

Size of input array Input primitive Size of array points 1 lines 2 triangles 3 lines_adjacency 4 triangles_adjacency 6

EmitVertex() EmitVertex tells the geometry shader that all the information for the current vertex has been filled in Any other variables set at this stage will be passed to the fragment shader (for example colour) Other attributes active at this stage will also be passed onto the fragment shader EmitVertex() can be called as many times as you like as long as it doesn t reach the max_vertices value

EmitPrimitive() EmitPrimitive() indicates that we have finished emitting vertices. It is important that enough vertices have been created for the primitive type specified in the layout qualifier

Visualising Normals The following geometry shader is used to draw both face and vertex normals The shader is split into 3 parts and loaded using ngl::shaderlib

Vertex Shader #version 150 precision highp float; /// @brief the vertex passed in in vec3 invert; /// @brief the normal passed in in vec3 innormal; /// @brief the in uv in vec2 inuv; uniform mat4 MVP; uniform float normalsize; uniform vec4 vertnormalcolour; uniform vec4 facenormalcolour; out vec4 normal; uniform bool drawfacenormals; uniform bool drawvertexnormals; void main(void) { gl_position = MVP*vec4(inVert,1); normal=mvp*vec4(innormal,0); }

Fragment Shader #version 150 /// @brief our output fragment colour out vec4 fragcolour; in vec4 pernormalcolour; void main () { fragcolour = pernormalcolour; }

#version 150 layout(triangles) in; layout(line_strip, max_vertices = 8) out; in vec4 normal[]; uniform float normalsize; uniform vec4 vertnormalcolour; uniform vec4 facenormalcolour; uniform bool drawfacenormals; uniform bool drawvertexnormals; out vec4 pernormalcolour; void main() { if (drawvertexnormals == true) { pernormalcolour=vertnormalcolour; for(int i = 0; i<gl_in.length(); ++i) { gl_position = gl_in[i].gl_position; EmitVertex(); gl_position = gl_in[i].gl_position + normal[i] * abs(normalsize); EmitVertex(); EndPrimitive(); } } if (drawfacenormals == true) { pernormalcolour=facenormalcolour; vec4 cent = (gl_in[0].gl_position + gl_in[1].gl_position + gl_in[2].gl_position) / 3.0; vec3 face_normal = normalize(cross(gl_in[2].gl_position.xyz - gl_in[0].gl_position.xyz, gl_in[1].gl_position.xyz - gl_in[0].gl_position.xyz)); } } gl_position = cent; EmitVertex(); gl_position = (cent + vec4(face_normal * abs(normalsize), 0.0)); EmitVertex(); EndPrimitive();

loading shader Adding a geometry shader is the same as any other ngl::shader we just set the type to ngl::geometry as shown shader->attachshader("normalgeo",ngl::geometry); shader->loadshadersource("normalgeo","shaders/normalgeo.gs"); shader->compileshader("normalgeo"); shader->attachshadertoprogram("normalshader","normalgeo");

references http://www.opengl.org/registry/doc/glslangspec. 4.30.6.pdf http://www.opengl.org/wiki/ Tutorial:_OpenGL_3.1_The_First_Triangle_(C%2B %2B/Win) 3DLabs OpenGL Shading Language Master Class Lecture Notes 2004

References Rost, R, Licea-Kane B (2009). OpenGL Shading Language. 3rd. ed. New York: Addison Wesley. OpenGL Architecture Review Board, OpenGL Reference Manual, Fourth Edition: The Official Reference to OpenGL, Version 1.4, Editor: Dave Shreiner, Addison- Wesley, Reading, Massachusetts, 2004. Segal, Mark, and Kurt Akeley, The OpenGL Graphics System: A Specification (Version 3.1), Editor (v1.1): Chris Frazier, (v1.2 3.1): Jon Leech, (v2.0): Jon Leech and Pat Brown, March 2008. www.opengl.org/documentation/ spec.html