PDA

View Full Version : Indecent request: help converting cg to glsl



Alessandro_dup1
04-13-2006, 05:14 PM
Hello everybody, as the subject says, i'd like to convert the following cg shaders into glsl language. Can someone help me ? Thanks



void watervert ( float4 position : POSITION,
float2 texCoord : TEXCOORD0,
float3 normalcg : NORMAL,

out float4 oPosition : POSITION,
out float3 lightDirection : TEXCOORD1,
out float2 oTexCoord : TEXCOORD2,
out float4 Position : TEXCOORD3,
out float4 oPosition2 : TEXCOORD4,

out float oExtinction : COLOR0,

uniform float3 lightPosition,
uniform float3 eyePos,
uniform float4x4 ModelViewProj
)
{
oTexCoord = texCoord; // normal map texture coord
oPosition = mul(ModelViewProj, position);
Position = position;
oPosition2 = oPosition;
lightDirection = lightPosition;

float z = length(position.xyz-eyePos);
oExtinction = exp(-0.0000001*z*0.65);
}

void waterfrag(
float Extinction : COLOR0,
float3 lightDir : TEXCOORD1,
float2 normalMapTexCoord : TEXCOORD2,
float4 Position : TEXCOORD3,
float4 Pos : TEXCOORD4,
uniform sampler2D reflMap,
out float4 ocolor : COLOR,

uniform float4 SunColorIntensity,
uniform float3 eyePosition,
uniform sampler2D normalMap,
uniform sampler2D environmentMap)
{

//const float4 waterColor = float4(0.55,0.6,0.65, 1.0);
const float4 waterColor = float4(0.55,0.65,0.79, 1.0);
const float4 refractedColor = float4(0.04, 0.20, 0.50, 1.0);
const float4 specularColor = float4(1.0, 0.88, 0.65, 1.0);

float3 eyeDirNew=normalize(eyePosition-Position.xyz);

lightDir.x=0.1;
lightDir.y=1;
lightDir.z=-0.8;

float3 N = 2*(2*tex2D(normalMap,normalMapTexCoord)-1.0); // normal
N += 2*(2*tex2D(normalMap,Position.xz*0.001)-1.0); // normal
N = normalize(N);
/////////////////////////////
float foam = 0.0;

float3 R = reflect(-eyeDirNew,N); // reflection vector
float diffuse = max(dot(N,lightDir), 0);
float3 halfVec = normalize(lightDir + eyeDirNew);
float specular = max(dot(N, halfVec),0);
float4 lighting = lit(diffuse, specular, 48);
float reflectionFactor = 0.02 + 0.98*pow(1.0 - dot(eyeDirNew,N), 4);

//////////////////////////
float d = 0.8f; /* amount of distortion */
float4 projCoord = Pos/Pos.z;
projCoord = (projCoord + 1.0) * 0.5f;
projCoord.xz += d * N.xz; /* add normal for distortion */
projCoord = clamp(projCoord, 0.001, 0.999); /* coords must be kept inside texture */
//projCoord.z=0.25*projCoord.z;
float4 reflectedColor = tex2D(environmentMap, projCoord.xy);

ocolor = waterColor*reflectedColor;
ocolor = lerp(refractedColor,ocolor,reflectionFactor) + lighting.z*specularColor + foam;
ocolor.rgb = lerp(SunColorIntensity.rgb, ocolor.rgb, Extinction);
//ocolor.rgb = reflectedColor.rgb;
}

ScottManDeath
04-13-2006, 09:07 PM
You can use the new CG 1.5. it has glsl profiles...

Alessandro_dup1
04-14-2006, 01:19 AM
ok, but with glsl profiles i can use glsl shaders code with cg, while i need the exact contrary.

jra101
04-14-2006, 10:25 AM
1.5 allows you to take a Cg file and compile it into GLSL vertex and fragment shaders.

For example:

cgc -profile glslf -entry waterfrag water_frag.cg -o water_frag.glsl

Alessandro_dup1
04-14-2006, 10:47 AM
ok, thank you very much for the explanation ! I'm going to try it ...