opengl shading language (advanced computer graphics) ernest tatum

20
OpenGL Shading Language OpenGL Shading Language (Advanced Computer Graphics) (Advanced Computer Graphics) Ernest Tatum Ernest Tatum

Upload: leslie-fleming

Post on 29-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

OpenGL Shading LanguageOpenGL Shading Language(Advanced Computer Graphics)(Advanced Computer Graphics)

Ernest TatumErnest Tatum

Page 2: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

OpenGLOpenGL

OpenGL (Open Graphics Library) is a OpenGL (Open Graphics Library) is a standard specification defining a cross-standard specification defining a cross-language cross-platform API for writing language cross-platform API for writing applications that produce 3D computer applications that produce 3D computer graphics and 2D computer graphics as graphics and 2D computer graphics as well. The interface consists of over 250 well. The interface consists of over 250 different function calls which can be used different function calls which can be used to draw complex three-dimensional scenes to draw complex three-dimensional scenes from simple primitives. Con’tfrom simple primitives. Con’t

Page 3: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

OpenGL was developed by OpenGL was developed by Silicon Graphics Inc. (SGI) in 1992 and is Silicon Graphics Inc. (SGI) in 1992 and is

popular in the video game industry where it popular in the video game industry where it competes with Direct3D on Microsoft Windows competes with Direct3D on Microsoft Windows platforms . OpenGL is also widely used in CAD, platforms . OpenGL is also widely used in CAD, virtual reality, scientific visualization, information virtual reality, scientific visualization, information visualization, flight simulation and video game visualization, flight simulation and video game development.development.

OpenGL con’t

Page 4: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Background of OpenGLBackground of OpenGL With the recent advancements in graphics cards, With the recent advancements in graphics cards,

new features have been added to allow for new features have been added to allow for increased flexibility in the rendering pipeline at increased flexibility in the rendering pipeline at the vertex and fragment level. Programmability the vertex and fragment level. Programmability at this level is achieved with the use of fragment at this level is achieved with the use of fragment and vertex shaders.and vertex shaders.

Page 5: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

DetailsDetails

Data TypesData Types The OpenGL Shading Language Specification The OpenGL Shading Language Specification

defines 22 basic data types. Some are the defines 22 basic data types. Some are the same as used in the C programming language, same as used in the C programming language, while others are specific to graphics processing.while others are specific to graphics processing.

Bool- conditional type, values may be either true Bool- conditional type, values may be either true or falseor false

Int- a signed integerInt- a signed integer Data types con’tData types con’t

Page 6: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Data types con’tData types con’t

Float- a floating point numberFloat- a floating point number vec2 – a 2 component floating point vector vec2 – a 2 component floating point vector vec3 – a 3 component floating point vector vec3 – a 3 component floating point vector vec4 – a 4 component floating point vector vec4 – a 4 component floating point vector bvec2 – a 2 component Boolean vector bvec2 – a 2 component Boolean vector bvec3 – a 3 component Boolean vector bvec3 – a 3 component Boolean vector bvec4 – a 4 component Boolean vector bvec4 – a 4 component Boolean vector ivec2 – a 2 component vector of integers ivec2 – a 2 component vector of integers

Page 7: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Data types con’tData types con’t

ivec3 – a 3 component vector of integers ivec3 – a 3 component vector of integers ivec4 – a 4 component vector of integers ivec4 – a 4 component vector of integers mat2 – a 2X2 matrix of floating point numbers mat2 – a 2X2 matrix of floating point numbers mat3 – a 3X3 matrix of floating point numbers mat3 – a 3X3 matrix of floating point numbers mat4 – a 4X4 matrix of floating point numbers mat4 – a 4X4 matrix of floating point numbers sampler1D – a handle for accessing a texture sampler1D – a handle for accessing a texture

with 1 dimension with 1 dimension sampler2D – a handle for accessing a texture sampler2D – a handle for accessing a texture

with 2 dimensions with 2 dimensions

Page 8: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Data types con’tData types con’tsampler3D – a handle for accessing a texture sampler3D – a handle for accessing a texture

with 3 dimensions with 3 dimensions

samplerCube – a handle for accessing cube samplerCube – a handle for accessing cube mapped textures mapped textures

sampler1Dshadow – a handle for accessing sampler1Dshadow – a handle for accessing a depth texture in one dimension a depth texture in one dimension

sampler2Dshadow – a handle for accessing sampler2Dshadow – a handle for accessing a depth texture in two dimensions a depth texture in two dimensions

Page 9: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

OperatorsOperators

The OpenGL Shading Language provides The OpenGL Shading Language provides many operators familiar to those with a many operators familiar to those with a background in using the background in using the C programming languageC programming language. This gives . This gives shader developers flexibility when writing shader developers flexibility when writing shaders. GLSL contains the shaders. GLSL contains the operators in C and C++operators in C and C++, with the exception , with the exception of bitwise operators and of bitwise operators and pointerspointers..

Page 10: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Functions and Control structuresFunctions and Control structures

Similar to the Similar to the C programming languageC programming language, , GLSL supports loops and branching, GLSL supports loops and branching, including if, else, if/else, for, do-while, including if, else, if/else, for, do-while, break, continue, etc.break, continue, etc.

Page 11: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Con’tCon’t

User defined functions are supported, and a User defined functions are supported, and a wide variety of commonly used functions are wide variety of commonly used functions are provided built-in as well. This allows the graphics provided built-in as well. This allows the graphics card manufacturer the ability to optimize these card manufacturer the ability to optimize these built in functions at the hardware level if they are built in functions at the hardware level if they are inclined to do so. Many of these functions are inclined to do so. Many of these functions are similar to those found in the C programming similar to those found in the C programming language such as exp() and abs() while others language such as exp() and abs() while others are specific to graphics programming such as are specific to graphics programming such as smoothstep() and texture2D().smoothstep() and texture2D().

Page 12: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Compilation and ExecutionCompilation and Execution

GLSL shaders are not stand-alone GLSL shaders are not stand-alone applications; they require an application applications; they require an application that utilizes the that utilizes the OpenGLOpenGL API. C, C++, C#, API. C, C++, C#, Delphi and Java all support the Delphi and Java all support the OpenGLOpenGL API and have support for the OpenGL API and have support for the OpenGL Shading Language. Shading Language.

Page 13: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Con’tCon’t

GLSL shaders themselves are simply a GLSL shaders themselves are simply a set of strings that are passed to the set of strings that are passed to the hardware vendor’s driver for compilation hardware vendor’s driver for compilation from within an application using the from within an application using the OpenGL API’s entry points. Shaders can OpenGL API’s entry points. Shaders can be created on the fly from within an be created on the fly from within an application or read in as text files, but must application or read in as text files, but must be sent to the driver in the form of a string be sent to the driver in the form of a string

Page 14: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Con’tCon’t

The set of APIs used to compile, link, and The set of APIs used to compile, link, and pass parameters to GLSL programs are pass parameters to GLSL programs are specified in three OpenGL extensions, and specified in three OpenGL extensions, and became part of core OpenGL as of became part of core OpenGL as of OpenGL Version 2.0. These OpenGL APIs OpenGL Version 2.0. These OpenGL APIs are found in the extensions:are found in the extensions:

ARB vertex shader ARB vertex shader ARB fragment shader ARB fragment shader ARB shader objects ARB shader objects

Page 15: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

ExampleExample A sample trivial GLSL Vertex ShaderA sample trivial GLSL Vertex Shader void main(void) { gl_Position = void main(void) { gl_Position =

ftransform(); } ftransform(); }

A sample trivial GLSL Fragment Shader A sample trivial GLSL Fragment Shader void main(void) { gl_FragColor = vec4(1.0, void main(void) { gl_FragColor = vec4(1.0,

0.0, 0.0, 1.0); } 0.0, 0.0, 1.0); }

void main(void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }

Page 16: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

ExampleExample

This example will draw a green square on This example will draw a green square on the screen. OpenGL has several ways to the screen. OpenGL has several ways to accomplish this task, but this is the easiest accomplish this task, but this is the easiest to understand.to understand.

glClear( GL_COLOR_BUFFER_BIT ); glClear( GL_COLOR_BUFFER_BIT ); This statement clears the color buffer, so that This statement clears the color buffer, so that

the screen will start blank. the screen will start blank.

Page 17: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Con’tCon’t

glMatrixMode( GL_PROJECTION ); /* glMatrixMode( GL_PROJECTION ); /* Subsequent matrix commands will affect the Subsequent matrix commands will affect the projection matrix */ glLoadIdentity(); /* Initialise projection matrix */ glLoadIdentity(); /* Initialise the projection matrix to identity */ glFrustum( -1, the projection matrix to identity */ glFrustum( -1, 1, -1, 1, 1, 1000 ); /* Apply a perspective-1, -1, 1, 1, 1000 ); /* Apply a perspective-projection matrix */ projection matrix */ These statements initialize the projection matrix, These statements initialize the projection matrix,

setting a 3d frustum matrix that represents the setting a 3d frustum matrix that represents the viewable area. This matrix transforms objects from viewable area. This matrix transforms objects from camera-relative space to OpenGL's projection space.camera-relative space to OpenGL's projection space.

Page 18: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Con’tCon’t

glMatrixMode( GL_MODELVIEW ); /* glMatrixMode( GL_MODELVIEW ); /* Subsequent matrix commands will affect the Subsequent matrix commands will affect the modelview matrix */ glLoadIdentity(); /* Initialise modelview matrix */ glLoadIdentity(); /* Initialise the modelview to identity */ glTranslatef( 0, 0, -the modelview to identity */ glTranslatef( 0, 0, -3 ); /* Translate the modelview 3 units along the 3 ); /* Translate the modelview 3 units along the Z axis */ Z axis */ These statements initialize the modelview matrix. This These statements initialize the modelview matrix. This

matrix defines a transform from model-relative matrix defines a transform from model-relative coordinates to camera space. The combination of the coordinates to camera space. The combination of the modelview matrix and the projection matrix transforms modelview matrix and the projection matrix transforms objects from model-relative space to projection screen objects from model-relative space to projection screen spacespace

Page 19: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Con’tCon’t

glBegin( GL_POLYGON ); /* Begin issuing glBegin( GL_POLYGON ); /* Begin issuing a polygon */ glColor3f( 0, 1, 0 ); /* Set the a polygon */ glColor3f( 0, 1, 0 ); /* Set the current color to green */ glVertex3f( -1, -1, current color to green */ glVertex3f( -1, -1, 0 ); /* Issue a vertex */ glVertex3f( -1, 1, 0 ); /* Issue a vertex */ glVertex3f( -1, 1, 0 ); /* Issue a vertex */ 0 ); /* Issue a vertex */

Page 20: OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum

Con’tCon’t

glVertex3f( 1, 1, 0 ); /* Issue a vertex */ glVertex3f( 1, 1, 0 ); /* Issue a vertex */ glVertex3f( 1, -1, 0 ); /* Issue a vertex */ glVertex3f( 1, -1, 0 ); /* Issue a vertex */ glEnd(); /* Finish issuing the polygon */ glEnd(); /* Finish issuing the polygon */ These commands draw a green square in the These commands draw a green square in the

XY plane. XY plane.