cse 381 – advanced game programming glsl. rendering revisited
TRANSCRIPT
CSE 381 – Advanced Game ProgrammingGLSL
Rendering Revisited
In a vertex shader
• You can write code for tasks such as: – Vertex position transformation using the modelview
and projection matrices – Normal transformation, and if required its
normalization – Texture coordinate generation and transformation – Lighting per vertex or computing values for lighting
per pixel – Color computation
GLSL Vertex Shader Responsibility
• The vertex shader is responsible for at least writing a variable: gl_Position– usually transforming the vertex with the modelview
and projection matrices.
• A vertex processor has access to OpenGL state– so it can perform operations that involve lighting for
instance, and use materials. – It can also access textures (only available in the newest
hardware). – There is no access to the frame buffer.
Shader Beware
• There is no requirement to perform all the necessary operations in your shader– your application may not use lighting for instance.
• Warning:– once you write a vertex shader you are replacing the
full functionality of the vertex processor• you can't perform normal transformation and expect the
fixed functionality to perform texture coordinate generation
– When a vertex shader is used it becomes responsible for replacing all the needed functionality of this stage of the pipeline
The fragment (pixel) shader
• Is responsible for operations like: – Computing colors, and texture coordinates per pixel – Texture application – Fog computation – Computing normals if you want lighting per pixel
Pixel Shader Responsibilities
• A fragment shader has two output options: – to discard the fragment, hence outputting nothing – to compute either gl_FragColor (the final color of the
fragment), or gl_FragData when rendering to multiple targets.
• Depth can also be written but it is not required since the previous stage already has computed it.
• Note: – the fragment shader has no access to the frame buffer.– operations such as blending occur only after the
fragment shader has run.
GLSL Use Overview
A note about programs
• A GLSL “Program” does not refer to your game
• What is it?– a GLSL “program”– basically a set of shaders
Creating a Shader
1. Create a shader container
2. Define the contents of your shader
3. Compile your shader
Creating a Shader Container
• First, create the shader container
• Use:GLhandleARB glCreateShaderObjectARB(GLenum shaderType);
• Ex:GLhandleARB shaderContainer;
shaderContainer = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
…
shaderContainer = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
How many shaders can you create?
• As many as you want to add to a program,
• NOTE: there can only be:– one main function for the set of vertex shaders
AND– one main function for the set of fragment shaders in
each single program.
Defining your shader• The source code for a shader is a string array, although you can use a pointer to a
single string.
• To set the source code for a shader use:
void glShaderSourceARB( GLhandleARB shader,
int numOfStrings,
const char **strings,
int *lenOfStrings);
• Parameters: – shader - the handle to the shader.– numOfStrings - the number of strings in the array.– strings - the array of strings.– lenOfStrings - an array with the length of each string, or NULL,
meaning that the strings are NULL terminated.
Compile the shader
• Use:
void glCompileShaderARB(GLhandleARB program);
• Parameter: – program - the handle to the program.
Creating a Program
1. Create a program container
2. Attach the shaders to the program
3. Link the program
4. Use the shader
Create a program container
• What’s a program container?
• Use:
GLhandleARB glCreateProgramObjectARB(void);
• Returns:– a handle for the container
How many programs?
• As many programs as you want
• Once rendering:– you can switch from program to program– go back to fixed functionality during a single frame
• Ex: – you may want to draw a teapot with refraction and
reflection shaders
WHILE– having a cube map displayed for background using
OpenGL's fixed functionality
Attach the shaders to the program
• The shaders:– do not need to be compiled at this time– they don't even have to have source code
• Why is that good?– platform independence
• To attach a shader to a program:void glAttachObjectARB( GLhandleARB program,
GLhandleARB shader);
• Parameters: – program - the handle to the program– shader - the handle to the shader you want to attach
Dealing with multiple shaders
• If you have a pair vertex/fragment shaders, you'll need to attach both to the program
• You can have many shaders of the same type (vertex or fragment) attached to the same program,– just like a C program can have many modules.
• For each type of shader there can only be one shader with a main function, also as in C.
• You can attach a shader to multiple programs, for instance if you plan to use the same vertex shader in several programs.
Link the program
• In order to carry out this step the shaders must be compiled as described previously
• Use:
void glLinkProgramARB(GLhandleARB program);
• Parameters: – program - the handler to the program.
• After the link operation, the shader's source can be modified, and the shaders recompiled without affecting the program.
Use a shader program
• To actually load and use a shader program, use:
void glUSeProgramObjectARB(GLhandleARB prog);
• Parameters: – prog – either:
• the handle to the program you want to use
• zero to return to fixed functionality
Notes on using a program
• Each program is assigned a handle
• You can have as many programs linked and ready to use as you want– and your hardware allows
• If a program is in use, and it is linked again, it will automatically be placed in use again– so in this case you don't need to call this function
again. – if the parameter is zero then the fixed functionality is
activated.
Let’s open up an example• http://www.lighthouse3d.com/opengl/glsl/examples/glutglsl.zip
• Next time:– we’ll examine how to write the shaders
References
• GLSL Tutorial– http://www.lighthouse3d.com/opengl/glsl/index.php?oglshader
• OpenGL Shading Language (the orange book)– http://3dshaders.com/home/index.php?option=com_weblinks&catid=14&Itemid=34