Panda3D Manual: Shader BasicsOverview of ShadersAs of version 1.7.0, Panda3D supports two shading languages: Cg and GLSL. Section assumes that you have a working knowledge of a shader language. If not, it would be wise to read about Cg or GLSL before trying to understand how they fit into Panda3D. A shader program can consist of three shaders: a vertex shader, a geometry shader and a fragment shader (often referred to a pixel shader). The vertex shader operates on every individual vertex. The processed vertices are then passed to the geometry shader (if it exists), which converts it into visible geometry (e.g. triangles, lines, points). The fragment shader is then executed on every pixel of the geometry and outputs the color of the pixel. As geometry shaders are a relatively new feature to graphic cards, they are often omitted and therefore you will often only find a vertex and fragment shader.
Cg ShadersOverview of Cg ShadersA Cg shader must contain procedures named Single-File Cg ShadersTo write a Cg shader in a single file, you must create a shader program that looks much like the one shown below. This example preserves position but switches the red and green channels of everything it is applied to: void vshader(float4 vtx_position : POSITION, Multi-File Cg ShadersCg shaders can be divided into several files as well; one for the vertex shader, another for the fragment shader, and a third for the geometry shader. The procedure names are still required to be
GLSL ShadersOverview of GLSL ShadersTo write a GLSL shader, you must write your vertex, pixel and geometry shaders separately. GLSL ExampleThis example preserves position but switches the red and green channels of everything it is applied to. This is the vertex shader: void main() { Since this does the default operation, it can be omitted. This is the fragment shader: void main() {
Using Shaders in Panda3DShaders in Panda3D use the Including the Shader Class DeclarationBefore a shader can be applied, the declaration of the Panda3D Shader class must be included.
#include "shader.h"
Loading a Cg ShaderLoading a single-file Cg shader is done with the
PT(Shader) myShader = Shader::load("myshader.sha", Shader.SLCg);
Loading a multi-file Cg shader requires a different set of parameters for
PT(Shader) myShader = Shader::load(Shader.SLCg, "myvertexshader.sha", "myfragmentshader.sha", "mygeometryshader.sha")
One last thing to note about //Cg
PT(Shader) myShader = Shader::load("myshader.sha");
Loading a GLSL ShaderIn the following code sample, a GLSL shader is loaded:
PT(Shader) myShader = Shader::load(Shader.SLGLSL, "myvertexshader.glsl", "myfragmentshader.glsl", "mygeometryshader.glsl");
Applying a ShaderShaders can be applied to any
myModel.set_shader(myShader);
The call to The Shader can Fetch Data from the Panda3D RuntimeEach shader program contains a parameter list. Panda3D scans the parameter list and interprets each parameter name as a request to extract data from the panda runtime. For example, if the shader contains a parameter declaration Panda3D will generate an error if the parameter qualifiers do not match what Panda3D is expecting. For example, if you declare the parameter Again, all parameter names must be recognized. There is a list of possible Cg shader inputs that shows all the valid parameter names and the data that Panda3D will supply. Supplying data to the Shader ManuallyMost of the data that the shader could want can be fetched from Panda3D at runtime by using the appropriate parameter names. However, it is sometimes necessary to supply some user-provided data to the shader. For this, you need myModel.set_shader_input("tint", LVector4f(1.0, 0.5, 0.5, 1.0)); The method The data that you store using To fetch data that was supplied using Shader Inputs propagate down the scene graph, and accumulate as they go. For example, if you store Shader Render AttributesThe functions In rare occasions, it is necessary to manipulate Be careful: attribs are immutable objects. So when you apply a function like Deferred Shader CompilationWhen you create an object of class shader, it compiles the shader, checking for syntax errors. But it does not check whether or not your video card is powerful enough to handle the shader. Panda3D only does that later on, when you try to render something with the shader. In the unusual event that your computer contains multiple video cards, the shader may be compiled more than once. It is possible that the compilation could succeed for one video card, and fail for the other. © Carnegie Mellon University 2010 |