G3D::Shader Class ReferenceAbstraction of the programmable hardware pipeline.
More...
#include <Shader.h>
Inherits G3D::ReferenceCountedObject.
List of all members.
|
Public Types |
| enum | ShaderType {
SHADER_NONE,
VERTEX_STRING,
VERTEX_FILE,
GEOMETRY_STRING,
GEOMETRY_FILE,
PIXEL_STRING,
PIXEL_FILE
} |
Public Member Functions |
| virtual void | afterPrimitive (class RenderDevice *renderDevice) |
| virtual void | beforePrimitive (class RenderDevice *renderDevice) |
| bool | hasArgument (const std::string &argname) const |
| virtual const std::string & | messages () const |
| virtual bool | ok () const |
| virtual bool | preserveState () const |
| void | ReferenceCountedObject_zeroWeakPointers () |
| virtual void | setPreserveState (bool s) |
Static Public Member Functions |
| static ShaderRef | create (ShaderType type0, const std::string &value0, ShaderType type1=SHADER_NONE, const std::string &value1="", ShaderType type2=SHADER_NONE, const std::string &value2="") |
| static ShaderRef | fromFiles (const std::string &vertexFile, const std::string &pixelFile, UseG3DUniforms u=DEFINE_G3D_UNIFORMS) |
| static ShaderRef | fromStrings (const std::string &vertexName, const std::string &vertexCode, const std::string &pixelName, const std::string &pixelCode, UseG3DUniforms u=DEFINE_G3D_UNIFORMS) |
| static ShaderRef | fromStrings (const std::string &vertexCode, const std::string &pixelCode, UseG3DUniforms u=DEFINE_G3D_UNIFORMS) |
| static bool | supportsGeometryShaders () |
| static bool | supportsPixelShaders () |
| static bool | supportsVertexShaders () |
Public Attributes |
| VertexAndPixelShader::ArgList | args |
| AtomicInt32 | ReferenceCountedObject_refCount |
| _WeakPtrLinkedList * | ReferenceCountedObject_weakPointer |
Protected Member Functions |
| | Shader () |
| | Shader (VertexAndPixelShaderRef v, UseG3DUniforms u) |
Protected Attributes |
| bool | _preserveState |
| UseG3DUniforms | _useUniforms |
| VertexAndPixelShaderRef | _vertexAndPixelShader |
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 mat3 g3d_ObjectToWorldNormalMatrix; // Upper 3x3 matrix (assumes that the transformation is RT so that the inverse transpose of the upper 3x3 is just R)
uniform mat3 g3d_WorldToObjectNormalMatrix; // Upper 3x3 matrix (assumes that the transformation is RT so that the inverse transpose of the upper 3x3 is just R)
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 * g3d_CameraToWorld * gl_LightState[0].position
uniform vec4 g3d_WorldLight0; // g3d_CameraToWorld * 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
The macros G3D_OSX, G3D_WIN32, G3D_FREEBSD, G3D_LINUX, G3D_ATI, G3D_NVIDIA, G3D_MESA are defined on the relevant platforms.
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 GLSL 1.1 shader begins with include or define the line numbers will be off by 1 to 3 in error messages because the G3D uniforms are inserted on the first line. GLSL 1.2 shaders do not have this problem.
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::creates(Shader::VERTEX_STRING, 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.
See also: http://developer.download.nvidia.com/opengl/specs/GL_EXT_geometry_shader4.txt
BETA API This API is subject to change.
Member Enumeration Documentation
Describe the arguments to Shader::create.
- Enumerator:
-
| SHADER_NONE |
|
| VERTEX_STRING |
|
| VERTEX_FILE |
|
| GEOMETRY_STRING |
|
| GEOMETRY_FILE |
|
| PIXEL_STRING |
|
| PIXEL_FILE |
|
Constructor & Destructor Documentation
| G3D::Shader::Shader |
( |
|
) |
[inline, protected] |
For subclasses to invoke.
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::create |
( |
ShaderType |
type0, |
|
|
const std::string & |
value0, |
|
|
ShaderType |
type1 = SHADER_NONE, |
|
|
const std::string & |
value1 = "", |
|
|
ShaderType |
type2 = SHADER_NONE, |
|
|
const std::string & |
value2 = "" | |
|
) |
| | [static] |
Create a new GLSL shader.
Arguments are in pairs. The first argument in each pair is an ArgumentType specifying the type of shader (vertex, geometry, or pixel) and whether it comes from a string or a file. The second argument in each pair is the filename or shader source itself.
Example:
std::string vs = STR(
void main() {
gl_Position = ftransform();
});
ShaderRef myEffect = Shader::create(
Shader::VERTEX_STRING, vs,
Shader::GEOMETRY_FILE, "effect.geom.glsl",
Shader::PIXEL_FILE, "effect.pixel.glsl");
| 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. Setting to false can lead to faster operation 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::supportsGeometryShaders |
( |
|
) |
[static] |
Returns true if this card supports geometry shaders.
| static bool G3D::Shader::supportsPixelShaders |
( |
|
) |
[static] |
Returns true if this card supports pixel shaders.
| 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 Thu Aug 2 11:40:47 2007 for G3D by
1.5.2
Hosted by
|