Can i write glsl code in .cg/.cgfx file?

hi, all
I see the following code in Cg Refercence Manual -> cg Profile -> glslv :
glslv void main(float4 position : POSITION,
out float4 opos : POSITION)
{
opos = mul(gl_ModelViewMatrix, position);
}

My question is, does it mean that I can wirte glsl code in .cg/.cgfx file?

Then I write the following code in my .cgfx file:
glslv void main(
uniform float pointRadius,
uniform float pointScale,
uniform vec4 eyePos
)
{
vec4 wpos = vec4(gl_Vertex.xyz, 1.0);
gl_Position = gl_ModelViewProjectionMatrix * wpos;

// calculate window-space point size
vec4 eyeSpacePos = gl_ModelViewMatrix * wpos;
float dist = length(eyeSpacePos.xyz);
gl_PointSize = pointRadius * (pointScale / dist);

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = eyeSpacePos;

gl_FrontColor = gl_Color;

}

But there are serveal errors when i compile this code in FxComposer.
Error error C5059 stdlib “gl_” variables are not accessible
Error error C5059 stdlib “gl_” variables are not accessible

It seems that the variables which contain prefix “gl_” are not recognised by the compiler.

so, can I wirte glsl code in .cg/.cgfx file?

Thank you.

I don’t know about mixing GLSL and Cg/CgFX, though it certainly seems like it’s possible if the compiler isn’t throwing a huge fit.

However, the error messages you’re seeing may be unrelated. Most of the built-in gl_* variables (including the ones you’re using in the sample above) are now deprecated as of GLSL 1.30. FxComposer may be adhering to a more recent version of the GLSL standard, in which case the use of those variables is indeed an error. All GL state variables (e.g. gl_ModelViewMatrix) must now be explicitly declared passed in as normal uniform variables, and all vertex attributes (e.g. gl_Vertex, gl_MultiTexCoord0) must be explicitly mapped using glBindAttribLocation() and friends.

Alternatively, there may be some way to force FxComposer into compatibility mode…

Check out this post by Ilian Dinev. This speaks to using GLSL in Cg, and gives examples. Also potentially useful: this

But there are [several] errors when i compile this code in FxComposer. … It seems that the variables which contain prefix “gl_” are not [recognized] by the compiler.

They work for me, cross-compiling GLSL to asm for checking dead code elimination and such. Try a command-line like this:

cgc -oglsl -strict -glslWerror -profile gpu_vp vert.glsl
cgc -oglsl -strict -glslWerror -profile gpu_fp frag.glsl

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.