Skip to content

Instantly share code, notes, and snippets.

@CFSworks
Created December 30, 2018 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CFSworks/d7e7ce79f46260d958d22c788e7ec3b8 to your computer and use it in GitHub Desktop.
Save CFSworks/d7e7ce79f46260d958d22c788e7ec3b8 to your computer and use it in GitHub Desktop.
Panda3D issue #496
// g++ -o cg cg.cxx -I /opt/nvidia-cg-toolkit/include/ /opt/nvidia-cg-toolkit/lib64/libCg.so
#include <Cg/cg.h>
#include <iostream>
#include <fstream>
int main()
{
const char *args = nullptr;
std::ifstream file("shader.txt");
std::string text((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();
CGcontext ctx = cgCreateContext();
std::cout << ctx << std::endl;
CGprofile profile = cgGetProfile("generic");
CGprogram prog = cgCreateProgram(ctx, CG_SOURCE, text.c_str(),
profile, "fshader", &args);
std::cout << prog << std::endl;
std::cout << cgGetErrorString(cgGetError()) << std::endl;
std::cout << cgGetLastListing(ctx) << std::endl;
return 0;
}
//Cg
/* Generated shader for render state:
CullFaceAttrib:cull_clockwise
LightAttrib:on
render/pointLightHelper/sphere.egg/redPointLight
render/pointLightHelper/sphere.egg/greenPointLight
render/pointLightHelper/sphere.egg/bluePointLight
render/ambientLight
render/directionalLight
render/camera/spotlight
RescaleNormalAttrib:none
ShaderAttrib:auto
*/
void vshader(
in float4 vtx_color : ATTR3,
out float4 l_color : COLOR0,
uniform float4x4 trans_model_to_view,
out float4 l_eye_position : TEXCOORD0,
uniform float4x4 tpose_view_to_model,
out float3 l_eye_normal : TEXCOORD1,
in float3 vtx_normal : ATTR2,
in float4 vtx_position : ATTR0,
out float4 l_position : POSITION,
uniform float4x4 mat_modelproj
) {
l_position = mul(mat_modelproj, vtx_position);
l_eye_position = mul(trans_model_to_view, vtx_position);
l_eye_normal = normalize(mul((float3x3)tpose_view_to_model, vtx_normal));
l_color = vtx_color;
}
void fshader(
in float4 l_eye_position : TEXCOORD0,
in float3 l_eye_normal : TEXCOORD1,
uniform float4x4 attr_light0,
uniform float4 attr_lspec0,
uniform float4x4 attr_light1,
uniform float4x4 attr_light2,
uniform float4x4 attr_light3,
uniform float4x4 attr_light4,
uniform float4 attr_lspec4,
out float4 o_color : COLOR0,
in float4 l_color : COLOR0,
uniform float4 attr_ambient,
uniform float4 attr_colorscale
) {
float4 result;
// Fetch all textures.
// Correct the surface normal for interpolation effects
l_eye_normal = normalize(l_eye_normal);
// Begin view-space light calculations
float ldist,lattenv,langle,lshad;
float4 lcolor,lspec,lpoint,latten,ldir,leye;
float3 lvec,lhalf;
float4 tot_diffuse = float4(0,0,0,0);
tot_diffuse += attr_ambient;
// Spot Light 0
lcolor = attr_light0[0];
lspec = attr_lspec0;
latten = attr_light0[1];
ldir = attr_light0[2];
lpoint = attr_light0[3];
lvec = lpoint.xyz - l_eye_position.xyz;
ldist = length(lvec);
lvec /= ldist;
langle = saturate(dot(ldir.xyz, lvec));
lattenv = 1/(latten.x + latten.y*ldist + latten.z*ldist*ldist);
lattenv *= pow(langle, latten.w);
if (langle < ldir.w) lattenv = 0;
lcolor *= lattenv * saturate(dot(l_eye_normal, lvec));
tot_diffuse += lcolor;
// Directional Light 1
lcolor = attr_light1[0];
lspec = lcolor;
lvec = attr_light1[3].xyz;
lcolor *= saturate(dot(l_eye_normal, lvec.xyz));
tot_diffuse += lcolor;
// Point Light 2
lcolor = attr_light2[0];
lspec = lcolor;
latten = attr_light2[1];
lpoint = attr_light2[3];
lvec = lpoint.xyz - l_eye_position.xyz;
ldist = length(lvec);
lvec /= ldist;
lattenv = 1/(latten.x + latten.y*ldist + latten.z*ldist*ldist);
lcolor *= lattenv * saturate(dot(l_eye_normal, lvec));
tot_diffuse += lcolor;
// Point Light 3
lcolor = attr_light3[0];
lspec = lcolor;
latten = attr_light3[1];
lpoint = attr_light3[3];
lvec = lpoint.xyz - l_eye_position.xyz;
ldist = length(lvec);
lvec /= ldist;
lattenv = 1/(latten.x + latten.y*ldist + latten.z*ldist*ldist);
lcolor *= lattenv * saturate(dot(l_eye_normal, lvec));
tot_diffuse += lcolor;
// Point Light 4
lcolor = attr_light4[0];
lspec = attr_lspec4;
latten = attr_light4[1];
lpoint = attr_light4[3];
lvec = lpoint.xyz - l_eye_position.xyz;
ldist = length(lvec);
lvec /= ldist;
lattenv = 1/(latten.x + latten.y*ldist + latten.z*ldist*ldist);
lcolor *= lattenv * saturate(dot(l_eye_normal, lvec));
tot_diffuse += lcolor;
// Begin view-space light summation
result = float4(0,0,0,0);
result += tot_diffuse * l_color;
result = saturate(result);
// End view-space light calculations
result *= attr_colorscale;
o_color = result * 1.000001;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment