Intro to Shaders
Shaders
are like mystical black magic in a text file, the visual effects they can
achieve is incredible. Shaders are responsible for all most all of the
graphical advancements in games. A shader manipulates data in the graphics
pipeline at certain programmable steps. I cannot stress this enough: what is
passed to a shader is just DATA and you can do whatever you want to DATA! There
are currently 5 programmable stages in the pipeline the vertex shader,
Tessellation Control & Evaluation Shaders, Geometry Shaders, Fragment
Shaders and Compute Shaders. Below is a diagram of the full openGL graphics
pipeline.
From http://img.hexus.net/v2/lowe/News/Khronos/OGLCS.jpg |
Most games can
get away with only using the vertex and fragment shaders to create Interesting
visual effects. This blog will be focused on the vertex and fragment shader as
they are the easiest to understand. Below is a simplified version of the
Graphics pipeline that makes it easier to understand what happens.
From http://goanna.cs.rmit.edu.au/`gl/teaching/Interactive3D/2012/images/pipline.png |
Shaders
are written in a couple of programming languages such as HLSL, GLSL and CG.
These languages have different syntax but they all do the same stuff. The
program language I use is GLSL because I found it to be congruent with openGL, the
graphics APU I use. A Vertex has a specific job to process individual vertices
and send out its new position to the pipeline. Here is a simple Vertex shader
in GLSL.
#version 150
uniform mat4 uni_ModelViewProjection;
in vec3 in_vertex;
void main(void)
{
gl_Position
= uni_ModelViewProjection * vec4(in_vertex, 1.0);
}
All this Shader does
is takes a vertex -in_vertex- and transforms it by the modelVeiwProjection
matrix which both are variables passed in from the c++ program. It then outputs
the new vertex in the system variable gl_Position. A vertex Shader must set
gl_Position! If it doesn’t set it the shader will not compile.
The Fragment shader
is next which manipulates the pixel colour of an object per fragment. The only
thing the following example does, is set every output color to red. A fragment shader must send out a colour vec4
or vec3.
#version 150
out vec4 out_color;
void main(void)
{
out_color = vec4 (1.0f, 0.0f, 0.0f, 0.0f);
}
With
vertex and fragment shaders you can make amazing visual effects from the extremely
easy one I showed to very nice and complex ones like these:
From http://www.userinter.net/wp-content/uploads/2011/04/Shader-Effects.jpg |
From http://devmaster.net/assets/post/3021/featured_image/sized_for_post_17ced3ab-a06c-4edd-9b0d-a1ef4be7c4b7.jpg |
No comments:
Post a Comment