SLIDE 10 1/1/2011 10
All of the State (except the near and far
plane) have been deprecated.
These were a nice convenience, but…
GLSL Built-in State
uniform mat4 uniform mat4 gl_ModelV gl_ModelViewMatrix iewMatrix; uniform mat4 uniform mat4 gl_Projec gl_ProjectionMatri tionMatrix; x; uniform mat4 uniform mat4 gl_ModelV gl_ModelViewProjec iewProjectionMatri tionMatrix; x; uniform mat4 uniform mat4 gl_Textur gl_TextureMatrix[g eMatrix[gl_MaxText l_MaxTextureCoords ureCoords]; ]; // Derived st // Derived state ate uniform mat3 uniform mat3 gl_Normal gl_NormalMatrix; Matrix; // transpo / transpose of the se of the i inverse of the nverse of the // upp // upper leftmo er leftmost 3x3 of st 3x3 of g gl_ModelViewMa l_ModelViewMatrix trix uniform mat4 uniform mat4 gl_ModelV gl_ModelViewMatrix iewMatrixInverse; Inverse; uniform mat4 uniform mat4 gl_Projec gl_ProjectionMatri tionMatrixInverse; xInverse; uniform mat4 uniform mat4 gl_ModelV gl_ModelViewProjec iewProjectionMatri tionMatrixInverse; xInverse; uniform mat4 uniform mat4 gl_Textur gl_TextureMatrixIn eMatrixInverse[gl_ verse[gl_MaxTextur MaxTextureC eCoords];
uniform mat4 uniform mat4 gl_ModelV gl_ModelViewMatrix iewMatrixTranspose Transpose; uniform mat4 uniform mat4 gl_Projec gl_ProjectionMatri tionMatrixTranspos xTranspose; e; uniform mat4 uniform mat4 gl_ModelV gl_ModelViewProjec iewProjectionMatri tionMatrixTranspos xTranspose; e; … OK, I must admit, that the spec has these in a different section
from the specials. Not clear why these are different than gl_ClipDistance for instance, except that those values would be used by the fixed-function clipping.
Vertex varying variables Fragment varying variables
GLSL Vertex to Fragment Vars
gl_FrontColor rontColor;
gl_BackColor; ackColor;
gl_FrontSecon rontSecondaryColor daryColor;
gl_BackSecond ackSecondaryColor; aryColor;
gl_TexCoord[] exCoord[]; ; // Depr // Deprecated ecated
gl_FogFragCo FogFragCoord;
// Deprecated eprecated in vec4 in vec4 gl_Co gl_Color; lor; in vec4 in vec4 gl_Se gl_SecondaryCo condaryColor; lor; in vec2 in vec2 gl_Po gl_PointCoord; intCoord; in float in float gl_F gl_FogFragCoo
rd; // De // Deprecated precated in vec4 in vec4 gl_Te gl_TexCoord[]; xCoord[]; // Depre // Deprecated cated
Vertex Shader
Compute projected position Compute vertex color Compute vertex texture coordinates
Example (old 3.0)
void void main() main() { // transf // transform verte
x to clip space co space coordinates
gl_Positi gl_Position = gl_M
- n = gl_ModelViewP
- delViewProjection
rojectionMatrix * Matrix * gl gl_Vertex; _Vertex; // Copy o // Copy over the v ver the vertex col ertex color.
gl_FrontC gl_FrontColor = gl
_Color; // transf // transform textu
re coordinates nates gl_TexCoo gl_TexCoord[ rd[0] = g ] = gl_Texture l_TextureMatrix[ Matrix[0] * gl_Mul * gl_Multi tiTexCoord0; TexCoord0; }
Fragment Shader
Look-up (sample) the texture color Multiply it with the base color and set the
final fragment color.
Note: The depth is not changed
gl_FragDepth = gl_FragCoord.z
Example
uniform sampl uniform sampler2D er2D text texture ure; ; void void main() main() { vec4 vec4 color color = textur = texture2D( text e2D( texture, gl_T ure, gl_TexCoord[0 exCoord[0]. ].st ); st ); gl_FragCol gl_FragColor = gl_C
- r = gl_Color * co
- lor * color;
lor; }
Some things to note:
1.
There is a main() for both the vertex and the fragment shader
2.
There is no concept of the stream
3.
Data can not be shared between instances
4.
There is little to no difference between the built-in state and user defined uniform variables.
These are really kernels. There are applied to the stream (similar to the Select clause in LINQ or SQL).
Example
class MyShader{ public Color BrickColor { get; set; } public Color MortarColor { get; set; } public IEnumerable<VertexColor> ProcessStream(IEnumerable<VertexNormal> vertexStream, Func<VertexNormal,VertexColor> kernel); }
OK, we can define these kernels, but how do we tell the
system (OpenGL) to use them?
Two new objects in OpenGL
Shader Routine – a compilation unit Shader Program – a linked unit
Setting up your shader program then takes a few steps:
1.
Create object handlers for each routine.
2.
Load the source into each routine.
3.
Compile each routine.
4.
Create object handler for the shader program
5.
Attach each routine to the program
6.
Link the program
7.
Use the program
OpenGL and GLSL