200 likes | 508 Views
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum. OpenGL.
E N D
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum
OpenGL • OpenGL (Open Graphics Library) is a standard specification defining a cross-language cross-platform API for writing applications that produce 3D computer graphics and 2D computer graphics as well. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. Con’t
OpenGL con’t • OpenGL was developed by • Silicon Graphics Inc. (SGI) in 1992 and is popular in the video game industry where it competes with Direct3D on Microsoft Windows platforms . OpenGL is also widely used in CAD, virtual reality, scientific visualization, information visualization, flight simulation and video game development.
Background of OpenGL • With the recent advancements in graphics cards, new features have been added to allow for increased flexibility in the rendering pipeline at the vertex and fragment level. Programmability at this level is achieved with the use of fragment and vertex shaders.
Details • Data Types • The OpenGL Shading Language Specification defines 22 basic data types. Some are the same as used in the C programming language, while others are specific to graphics processing. • Bool- conditional type, values may be either true or false • Int- a signed integer • Data types con’t
Data types con’t • Float- a floating point number • vec2 – a 2 component floating point vector • vec3 – a 3 component floating point vector • vec4 – a 4 component floating point vector • bvec2 – a 2 component Boolean vector • bvec3 – a 3 component Boolean vector • bvec4 – a 4 component Boolean vector • ivec2 – a 2 component vector of integers
Data types con’t • ivec3 – a 3 component vector of integers • ivec4 – a 4 component vector of integers • mat2 – a 2X2 matrix of floating point numbers • mat3 – a 3X3 matrix of floating point numbers • mat4 – a 4X4 matrix of floating point numbers • sampler1D – a handle for accessing a texture with 1 dimension • sampler2D – a handle for accessing a texture with 2 dimensions
Data types con’t sampler3D – a handle for accessing a texture with 3 dimensions samplerCube – a handle for accessing cube mapped textures sampler1Dshadow – a handle for accessing a depth texture in one dimension sampler2Dshadow – a handle for accessing a depth texture in two dimensions
Operators • The OpenGL Shading Language provides many operators familiar to those with a background in using the C programming language. This gives shader developers flexibility when writing shaders. GLSL contains the operators in C and C++, with the exception of bitwise operators and pointers.
Functions and Control structures • Similar to the C programming language, GLSL supports loops and branching, including if, else, if/else, for, do-while, break, continue, etc.
Con’t • User defined functions are supported, and a wide variety of commonly used functions are provided built-in as well. This allows the graphics card manufacturer the ability to optimize these built in functions at the hardware level if they are inclined to do so. Many of these functions are similar to those found in the C programming language such as exp() and abs() while others are specific to graphics programming such as smoothstep() and texture2D().
Compilation and Execution • GLSL shaders are not stand-alone applications; they require an application that utilizes the OpenGL API. C, C++, C#, Delphi and Java all support the OpenGL API and have support for the OpenGL Shading Language.
Con’t • GLSL shaders themselves are simply a set of strings that are passed to the hardware vendor’s driver for compilation from within an application using the OpenGL API’s entry points. Shaders can be created on the fly from within an application or read in as text files, but must be sent to the driver in the form of a string
Con’t • The set of APIs used to compile, link, and pass parameters to GLSL programs are specified in three OpenGL extensions, and became part of core OpenGL as of OpenGL Version 2.0. These OpenGL APIs are found in the extensions: • ARB vertex shader • ARB fragment shader • ARB shader objects
Example • A sample trivial GLSL Vertex Shader • void main(void) { gl_Position = ftransform(); } • A sample trivial GLSL Fragment Shader • void main(void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } void main(void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }
Example • This example will draw a green square on the screen. OpenGL has several ways to accomplish this task, but this is the easiest to understand. • glClear( GL_COLOR_BUFFER_BIT ); • This statement clears the color buffer, so that the screen will start blank.
Con’t • glMatrixMode( GL_PROJECTION ); /* Subsequent matrix commands will affect the projection matrix */ glLoadIdentity(); /* Initialise the projection matrix to identity */ glFrustum( -1, 1, -1, 1, 1, 1000 ); /* Apply a perspective-projection matrix */ • These statements initialize the projection matrix, setting a 3d frustum matrix that represents the viewable area. This matrix transforms objects from camera-relative space to OpenGL's projection space.
Con’t • glMatrixMode( GL_MODELVIEW ); /* Subsequent matrix commands will affect the modelview matrix */ glLoadIdentity(); /* Initialise the modelview to identity */ glTranslatef( 0, 0, -3 ); /* Translate the modelview 3 units along the Z axis */ • These statements initialize the modelview matrix. This matrix defines a transform from model-relative coordinates to camera space. The combination of the modelview matrix and the projection matrix transforms objects from model-relative space to projection screen space
Con’t • glBegin( GL_POLYGON ); /* Begin issuing a polygon */ glColor3f( 0, 1, 0 ); /* Set the current color to green */ glVertex3f( -1, -1, 0 ); /* Issue a vertex */ glVertex3f( -1, 1, 0 ); /* Issue a vertex */
Con’t • glVertex3f( 1, 1, 0 ); /* Issue a vertex */ glVertex3f( 1, -1, 0 ); /* Issue a vertex */ glEnd(); /* Finish issuing the polygon */ • These commands draw a green square in the XY plane.