G3D::Shader Class Reference#include <Shader.h>
Inherits G3D::ReferenceCountedObject.
List of all members.
Detailed Description
Abstraction of the programmable hardware pipeline. Use with G3D::RenderDevice::setShader. G3D::Shader allows you to specify C++ code (by overriding the methods) that executes for each group of primitives and OpenGL Shading Language (GLSL) code that executes for each vertex and each pixel.
Uses G3D::VertexAndPixelShader internally. What we call pixel shaders are really "fragment shaders" in OpenGL terminology.
Unless DO_NOT_DEFINE_G3D_UNIFORMS is specified to the static constructor, the following additional variables will be available inside the shaders:
uniform mat4 g3d_WorldToObjectMatrix;
uniform mat4 g3d_ObjectToWorldMatrix;
uniform mat4 g3d_WorldToCameraMatrix;
uniform mat4 g3d_CameraToWorldMatrix;
uniform int g3d_NumLights; // 1 + highest index of the enabled lights
uniform int g3d_NumTextures; // 1 + highest index of the enabled textures
uniform vec4 g3d_ObjectLight0; // g3d_WorldToObject * gl_LightState[0].position
vec2 g3d_sampler2DSize(sampler2D t); // Returns the x and y dimensions of t
vec2 g3d_sampler2DInvSize(sampler2D t); // Returns vec2(1.0, 1.0) / g3d_size(t) at no additional cost
g3d_sampler2DSize and g3d_sampler2DInvSize require that there be no additional space between the function name and parens and no space between the parens and sampler name. There is no cost for definining and then not using any of these; unused variables do not increase the runtime cost of the shader.
If your shader begins with include or define the line numbers will be off by one in error messages because the G3D uniforms are inserted on the first line.
ATI's RenderMonkey is an excellent tool for writing GLSL shaders under an IDE. http://www.ati.com/developer/rendermonkey/index.html You can then load those shaders into G3D::Shader and use them with your application.
Example
The following example computes lambertian + ambient shading in camera space, on the vertex processor. Although the shader could easily have been loaded from a file, the example loads it from a string (which is conveniently created with the STR macro)
Vertex shaders are widely supported, so this will run on any graphics card produced since 2001 (e.g. GeForce3 and up). Pixel shaders are available on "newer" cards (e.g. GeForceFX 5200 and up).
IFSModelRef model;
ShaderRef lambertian;
...
Initialization
model = IFSModel::create(app->dataDir + "ifs/teapot.ifs");
lambertian = Shader::fromStrings(STR(
uniform vec3 k_A;
void main(void) {
gl_Position = ftransform();
gl_FrontColor.rgb = max(dot(gl_Normal, g3d_ObjectLight0.xyz), 0.0) * gl_LightSource[0].diffuse + k_A;
}), "");
...
Rendering loop
app->renderDevice->setLight(0, GLight::directional(Vector3(1,1,1), Color3::white() - Color3(.2,.2,.3)));
app->renderDevice->setShader(lambertian);
lambertian->args.set("k_A", Color3(.2,.2,.3));
model->pose()->render(app->renderDevice);
This example explicitly sets the ambient light color to demonstrate how arguments are used. That could instead have been read from gl_LightModel.ambient. Note the use of g3d_ObjectLight0. Had we not used that variable, Shader would not have computed or set it.
BETA API This API is subject to change.
Constructor & Destructor Documentation
| G3D::Shader::Shader |
( |
|
) |
[inline, protected] |
|
Member Function Documentation
| virtual void G3D::Shader::afterPrimitive |
( |
class RenderDevice * |
renderDevice |
) |
[virtual] |
|
|
|
Default implementation pops state. |
| virtual void G3D::Shader::beforePrimitive |
( |
class RenderDevice * |
renderDevice |
) |
[virtual] |
|
|
|
Invoked by RenderDevice immediately before a primitive group. Override to set state on the RenderDevice (including the underlying vertex and pixel shader).
If overriding, do not call RenderDevice::setShader from this routine.
Default implementation pushes state, sets the g3d_ uniforms, and loads the vertex and pixel shader. |
| static ShaderRef G3D::Shader::fromFiles |
( |
const std::string & |
vertexFile, |
|
|
const std::string & |
pixelFile, |
|
|
UseG3DUniforms |
u = DEFINE_G3D_UNIFORMS |
|
) |
[inline, static] |
|
| static ShaderRef G3D::Shader::fromStrings |
( |
const std::string & |
vertexName, |
|
|
const std::string & |
vertexCode, |
|
|
const std::string & |
pixelName, |
|
|
const std::string & |
pixelCode, |
|
|
UseG3DUniforms |
u = DEFINE_G3D_UNIFORMS |
|
) |
[inline, static] |
|
|
|
Names are purely for debugging purposes |
| static ShaderRef G3D::Shader::fromStrings |
( |
const std::string & |
vertexCode, |
|
|
const std::string & |
pixelCode, |
|
|
UseG3DUniforms |
u = DEFINE_G3D_UNIFORMS |
|
) |
[inline, static] |
|
| bool G3D::Shader::hasArgument |
( |
const std::string & |
argname |
) |
const |
|
|
|
Returns true if this shader is declared to accept the specified argument. |
| virtual const std::string& G3D::Shader::messages |
( |
|
) |
const [virtual] |
|
| virtual bool G3D::Shader::ok |
( |
|
) |
const [virtual] |
|
| virtual bool G3D::Shader::preserveState |
( |
|
) |
const [inline, virtual] |
|
| void G3D::ReferenceCountedObject::ReferenceCountedObject_zeroWeakPointers |
( |
|
) |
[inline, inherited] |
|
|
|
Automatically called immediately before the object is deleted. This is not called from the destructor because it needs to be invoked before the subclass destructor. |
| virtual void G3D::Shader::setPreserveState |
( |
bool |
s |
) |
[inline, virtual] |
|
|
|
When true, any RenderDevice state that the shader configured before a primitive it restores at the end of the primitive. When false, the shader is allowed to corrupt state. Corruption is fast and is useful when you know that the next primitive will also be rendered with a shader, since shaders tend to set all of the state that they need.
Defaults to true |
| static bool G3D::Shader::supportsPixelShaders |
( |
|
) |
[static] |
|
|
|
Returns true if this card supports pixel shaders. If vertex but not pixel shaders are supported you can pass an empty string as the pixel shader. |
| static bool G3D::Shader::supportsVertexShaders |
( |
|
) |
[static] |
|
|
|
Returns true if this card supports vertex shaders |
Member Data Documentation
|
|
Arguments to the vertex and pixel shader. You may change these either before or after the shader is set on G3D::RenderDevice-- either way they will take effect immediately. |
|
|
The long name is to keep this from accidentally conflicting with a subclass's variable name. Do not use or explicitly manipulate this value--its type may change in the future and is not part of the supported API. |
|
|
Linked list of all weak pointers that reference this (some may be on the stack!). Do not use or explicitly manipulate this value. |
The documentation for this class was generated from the following file:
Generated on Mon Jul 17 11:50:48 2006 for G3D by
1.4.5
Hosted by
|