Hello,
We have just committed into the CVS a new feature for the panda3D shader system that extends the set of available inputs that can be passed to the Cg shader.
In details:
[size=150]1. Panda3D data types:[/size]
This is complete list of numeric parameters that can now be passed to a Cg shader.
from pandac.PandaModules import Vec4
from pandac.PandaModules import Vec3
from pandac.PandaModules import Vec2
from pandac.PandaModules import Point4
from pandac.PandaModules import Point3
from pandac.PandaModules import Point2
from pandac.PandaModules import Mat4
from pandac.PandaModules import Mat3
# PTA means Pointer to Array
from pandac.PandaModules import PTAMat4
from pandac.PandaModules import PTAMat3
# Point and Vector derive from VecBase
from pandac.PandaModules import PTAVecBase4
from pandac.PandaModules import PTAVecBase3
from pandac.PandaModules import PTAVecBase2
# Pointer to 1D arrays
from pandac.PandaModules import PTAFloat
from pandac.PandaModules import PTADouble
[size=150]2. Passing the inputs:[/size]
All the parameters are passed through the well known function:
setShaderInput(name,arg)
[size=150]3. Relationship with Cg data types:[/size]
A complete list of possible Cg data types can be found here: pastebin.com/WRcT9D2S
Our work makes available all the numeric types and arrays of them. By numeric I mean all the parameters belonging to the following classes:
- CG_PARAMETERCLASS_SCALAR
- CG_PARAMETERCLASS_VECTOR
- CG_PARAMETERCLASS_MATRIXPanda3d types and Cg types are connected only by their size. The size in this case represent the number of elements (scalars) contained by the data type. For example:
x1 = Mat4()
x2 = PTAVecBase4.empty_array(4)
x3 = PTAFloat.empty_array(16)
sizeof(x1) = sizeof(x2) = sizeof(x3) = 16
In practice this means that setShaderInput(pname, element_of_size_16) can be used to set any of the Cg parameters having the same size. For example it is legal to do:
//Panda3D
setShaderInput(pname, x1)
//Cg
uniform float4x4 pname;
uniform float2x4 pname[2];
uniform float4x2 pname[2];
uniform float4 pname[4];
uniform float2 pname[8];
uniform float pname[16];
//Note: float can be changed to: double, bool, fixed, half
This gives a lot more freedom to the user. Now the user not only has access to the data types represented directly in panda3d (i.e. Mat4 -> float4x4), but also has access to the more exotic types made available by Cg (i.e. bool4x2 )
[size=150]4. Putting it all together:[/size]
//Panda3d
import os
import sys
#panda imports
import direct.directbase.DirectStart
from pandac.PandaModules import *
def main(argv):
base.setBackgroundColor(0.1, 0.1, 0.1)
base.camLens.setNearFar(1.0, 50.0)
base.camLens.setFov(45.0)
base.disableMouse()
base.accept("escape", sys.exit)
camera.setPos(0.0, -15.0, 10.0)
camera.lookAt(0.0, 0.0, 0.0)
root = render.attachNewNode("Root")
teapot = loader.loadModel(os.path.join( os.getcwd(),"teapot.egg"))
teapot.reparentTo(render);
# Shader Inputs
x1 = Mat4()
x2 = PTAVecBase4.emptyArray(4)
x3 = PTAFloat.emptyArray(16)
teapot.setShaderInput("IN.pname11",x1);
teapot.setShaderInput("IN.pname12",x1);
teapot.setShaderInput("IN.pname21",x2);
teapot.setShaderInput("IN.pname22",x2);
teapot.setShaderInput("IN.pname31",x3);
teapot.setShaderInput("IN.pname32",x3);
teapot.setShader(loader.loadShader("shader.sha"))
run()
if __name__ == '__main__':
main(sys.argv)
//Cg
struct VertexDataIN{
float4 vtx_position :POSITION;
float4 vtx_normal :NORMAL;
float4 vtx_color :COLOR;
float2 vtx_texcoord0 :TEXCOORD0;
};
struct VertexDataOUT{
float4 l_position :POSITION;
float4 l_normal :TEXCOORD1;
float4 l_color :COLOR;
float2 l_texcoord0 :TEXCOORD0;
};
void vshader(VertexDataIN IN,
out VertexDataOUT OUT,
uniform float4x4 mat_modelproj)
{
OUT.l_position = mul(mat_modelproj, IN.vtx_position);
OUT.l_color = IN.vtx_color;
OUT.l_normal = IN.vtx_normal;
OUT.l_texcoord0 = IN.vtx_texcoord0;
}
struct ShaderInputs{
uniform float4x4 pname11;
uniform float2x4 pname12[2];
uniform float4x2 pname21[2];
uniform float4 pname22[4];
uniform float2 pname31[8];
uniform float pname32[16];
};
void fshader(VertexDataOUT vIN,
ShaderInputs IN,
out float4 o_color : COLOR)
{
o_color = IN.pname11[0][1];
o_color = IN.pname12[1][0];
o_color = transpose(IN.pname21[1])[1];
o_color = IN.pname22[1];
o_color = float4(IN.pname31[1],0,1);
o_color = float4(IN.pname32[1],0,1,0);
}
[size=150]4. Conclusion:[/size]
This feature will be soon documented in the Panda3D manual, in the meanwhile, if you have any doubts or suggestions on how to make the documentation clearer then this post, please let me know.
[size=150]4. Notes:[/size]1. We got rid of the k_ prefix. This update is backward compatible, but the k_ prefix shouldn’t be used any more.
Thanks,
Federico