I dug this out of my prototypes folder, from the first days of Cg.
All it does is render the mesh in UV space with the most basic lighting and everything hardcoded (even the light is just a node, instead of a light ;D), but it could be a good place to start. Putting an actual point light in there would probably be a good beginner Cg training .
You could dig more advanced lighting code from shaders dumped by Panda’s shader generator, but it wouldn’t be much of use – simple lightmaps can’t hold enough pixels for normal mapping.
from direct.directbase import DirectStart
from pandac.PandaModules import *
winProps = WindowProperties()
winProps.setSize(512, 512)
base.win.requestProperties(winProps)
model = loader.loadModel("../cube.egg")
model.reparentTo(render)
model.setPos(0, 0, -1)
base.cam.setPos(Vec3(0, -10, 0))
base.cam.lookAt(model)
light = NodePath("pointLight")
light.setPos(Vec3(0, 5, -5))
myShader = Shader.load("uv_render.sha")
model.setShader(myShader)
model.setShaderInput("pointLight0", light)
def turnModel(direction):
model.setHpr(model, direction)
base.accept("w", turnModel, [Vec3(0, 10, 0)])
run()
//Cg
void vshader(
// Input
float4 vtx_position : POSITION,
float2 vtx_texcoord0 : TEXCOORD0,
float3 vtx_tangent0,
float3 vtx_binormal0,
float4 vtx_normal : NORMAL,
// Input Uniform
uniform float4x4 mat_modelproj,
uniform float4x4 trans_model_to_world,
// Output
out float2 l_texcoord0 : TEXCOORD0,
out float4 l_worldPosition : TEXCOORD6,
out float4 l_normal : TEXCOORD7,
//~ out float4 l_position : POSITION
//~ // Texture render output
out float2 l_positionUV : POSITION,
out float4 l_position
)
{
l_texcoord0 = vtx_texcoord0;
l_worldPosition = mul(trans_model_to_world, vtx_position);
//~ l_position = mul(mat_modelproj, vtx_position);
//~ // Texture render output
l_position = mul(mat_modelproj, vtx_position);
l_positionUV = vtx_texcoord0 * 2.0 - 1.0;
l_normal = mul(trans_model_to_world, vtx_normal);
l_normal = normalize(l_normal);
}
void fshader(
// Input
float4 l_position,
float2 l_texcoord0 : TEXCOORD0,
float4 l_worldPosition : TEXCOORD6,
float4 l_normal : TEXCOORD7,
// Input Uniform
uniform float4 wspos_pointLight0,
// Output
out float4 o_color: COLOR
)
{
// BEGIN Test lighting
float4 lightColor = float4(1, 1, 1, 1);
float4 direction = l_worldPosition - wspos_pointLight0;
float4 dirNormal = normalize(direction);
float distance = length(direction);
float4 att = float4(1.0, 0, 0, 0.001);
float distAtt = 1.0 / (att[0] + att[1] + (att[2] * distance) + (att[3] * (distance * distance)));
o_color = lightColor * abs(dot(dirNormal, l_normal)) * distAtt;
// END Test lighting
//~ o_color = (l_normal + 1) * 0.5;
//~ o_color = (l_worldPosition + 1) * 0.5;
}