For lighting in your custom shader, being it vertex color shader or texture colors shader you´ll need to add:
At your game code:
plight = self.render.attachNewNode("point light")
plight.setPos(30,-40,0)
#ambient light
alight = 1
self.render.setShaderInput("alight", Vec4(alight,alight,alight,0))
self.render.setShaderInput("plight", plight)
The set shader input send the variables above which the shader will need(if you want ambient color and/or point light); then at vertex shader start:
in float4 vtx_position : POSITION,
in float3 vtx_normal : NORMAL,
out float3 l_myposition : TEXCOORD2,
out float3 l_mynormal : TEXCOORD3,
uniform float4x4 mat_modelproj
Between vertex shader and fragment shader you´ll need:
l_position = mul( mat_modelproj, vtx_position );
l_myposition = vtx_position.xyz;
l_mynormal = normalize( vtx_normal );
“mul” means you will multiply mat_modelproj with every vertex position, to have no distortions in your graphic; any time you can use a CG function like mul, max and etc instead of doing the calculations the usual way, you should do so. At fragment shader start:
in float3 l_myposition : TEXCOORD2,
in float3 l_mynormal : TEXCOORD3,
uniform float4 mspos_plight,
uniform float3 k_alight
The special point above is mspos_plight, which is the variable we passed earlier from python, were saying it is the light position. k_alight is the other variable sent earlier, we will make it act as a ambient light. Then:
float4 result;
float3 lightpos = mspos_plight.xyz;
float3 modelpos = l_myposition;
float3 normal = normalize( l_mynormal );
float3 direction = normalize( lightpos - modelpos );
float light = max( dot( normal, direction ), 0 ) ;
//here is where the light gets "Cartonized"
if ( light < 0.5 ) light = 0.7;
else light = 1;
result = texcolor;
result.rgb *= k_alight.rgb;
result *= light;
o_color = result;
If in cg you don´t code something, it will not exist in your program graphic, here we build our lighting. As far as I understand normalize(l_mynormal) turn whateaver value given into -1,0,1 range. normalize(lightpos - modelpos) gives you a vector, a vector is a point with a direction; then at “max” you get the greatest value from the ones passed to “max”; dot is scalar dot product, which means if normal is (1,0,-1) and direction(0,1,1) then the result will be calculated as result = (10)+(01)+(-1*1). Dot product is used to discover the value between two vectors. Then you get that and simplify, making light have only 2 values with an if. Then you just multiply the ambient light with the result(which already contains texcolor), and do the same for the point light.
At least thats how I understood it, finding info about cg is a bit hard without asking someone who knows about it to teach you. I myself I´m still learning.