CG Depth Shader problem

Problems building or running the engine, queries about how to use features etc.
Murphy
Greenskin
Posts: 102
Joined: Tue May 10, 2005 11:42 pm
Location: SF, California

CG Depth Shader problem

Post by Murphy »

I am trying to color an object based on it's distance from the camera using a CG shader. I got the shader code from the NVidia shader library site.

Here is the shader:

Code: Select all

float4 NearColor = {1.0f, 1.0f, 1.0f, 1.0f};

float4 FarColor = {0.0f, 0.0f, 0.0f, 1.0f};

float Hither = 0.1;

float Yon = 1000.0;

float Gamma = 1.0;

void depthVS(
    in float4 Position : POSITION,
    out float4 HPosition : POSITION,
    out float4 Color : COLOR0,
    uniform float4x4 worldViewProj)
{
    float4 Po = float4(Position.xyz, 1.0);
    float4 hpos = mul(worldViewProj, Po);
    float dl = (hpos.z - Hither) / (Yon - Hither);
    dl = min(dl, 1.0);
    dl = max(dl, 0.0);
    dl = pow(dl, Gamma);
    Color = lerp(NearColor, FarColor, dl);
    HPosition = hpos;
}


float4 depthFS(in float4 color : COLOR) :  COLOR0
{
	return(color);
}
I have used this shader code before in another, non-Ogre project and it does work.

Here is the .program file:

Code: Select all

vertex_program Depth_VS cg
{
	source depth.cg
	entry_point depthVS
	profiles vs_1_1 arbvp1

	//This block saves us from manually setting parameters in code
      default_params
	{
     		//Ogre will put the worldviewproj into our 'worldViewProj' parameter for us.
		param_named_auto worldViewProj worldviewproj_matrix
            //Note that 'worldViewProj' is a parameter in the cg code.
	}
}

fragment_program Depth_FS cg
{
	source depth.cg
	entry_point depthFS
	profiles ps_1_1
}
And finally, here is the .material:

Code: Select all

material tile
{
	technique
	{
		pass
		{
			vertex_program_ref Depth_VS
			{
			}

			fragment_program_ref Depth_FS
			{
			}

			//lighting off

			//ambient 0 0 0 1
			//diffuse 1 1 1 1
			//specular 0 0 0 1
			//emissive 0 0 0

			//texture_unit
			//{
			//	texture tile.tga
			//	tex_coord_set 0
			//	colour_op modulate
			//	scale 1 1
			//	scroll 0 0
			//	rotate 0
			//}
		}
	}
}
I commented out those lines to try to simplify the process but I get the same result with or without the comments.

The result is a grey model no matter the distance from the camera. There are no errors reported in Ogre.log either. I have been trying to solve this all day with no luck. I hope I am just missing some small syntax error or something.
Murphy
Greenskin
Posts: 102
Joined: Tue May 10, 2005 11:42 pm
Location: SF, California

Post by Murphy »

I just remembered something. This was in the cgfx file that the shader was in. I didn't think that ogre could use these settings but maybe I can somehow set ogre to use them in the material script???

Code: Select all

technique Main <
    string Script = "Pass=p0;"; > {
    pass p0  < string Script = "Draw=geometry;"; > {		
	VertexShader = compile vp40 depthVS();
	    DepthTestEnable = true;
		DepthMask = true;
		CullFaceEnable = false;
		BlendEnable = false;
		DepthFunc = LEqual;
	// no pixel shader - rasterization will be accurate
    }
}
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66

Post by sinbad »

Well, you need to use the correct profile for one. Your material file says 'vs_1_1 arbvp1' whilst you appear to be trying to use 'vp40' in the fx file. Change the compiler profile in the material file.
Murphy
Greenskin
Posts: 102
Joined: Tue May 10, 2005 11:42 pm
Location: SF, California

Post by Murphy »

I tried changing to vp40 and got this warning in ogre.log:

"12:07:28: WARNING: material tile has no supportable Techniques and will be blank. Explanation:
Pass 0: Vertex program shader/Depth_VS cannot be used - not supported."

I don't think that this shader code should require vp40, but I am not the best with shaders, so maybe it does. What is strange is that this shader has worked on this machine before. I guess I am going to try it as a hlsl shader (although cg would be preferable).
Murphy
Greenskin
Posts: 102
Joined: Tue May 10, 2005 11:42 pm
Location: SF, California

Post by Murphy »

I changed to HLSL and I think I almost have this. Here is my problem and I think it is a syntax problem.

When I do this:

Code: Select all

//This is global
float4 TestColor = {0.0f, 1.0f, 0.0f, 0.0f};
...
//This is in the vertex shader
Out.color = TestColor;
Out.pos = hpos;

return Out;
I only see black.

When I do this:

Code: Select all

...
TestColor.r = 0.0;
TestColor.g = 1.0;
TestColor.b = 0.0;
TestColor.a = 0.0;
...
I see green. I think this is related to me only seeing black when using the default depth shader.
User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 11

Post by nullsquared »

Murphy wrote:I changed to HLSL and I think I almost have this. Here is my problem and I think it is a syntax problem.

When I do this:

Code: Select all

//This is global
float4 TestColor = {0.0f, 1.0f, 0.0f, 0.0f};
...
//This is in the vertex shader
Out.color = TestColor;
Out.pos = hpos;

return Out;
I only see black.

When I do this:

Code: Select all

...
TestColor.r = 0.0;
TestColor.g = 1.0;
TestColor.b = 0.0;
TestColor.a = 0.0;
...
I see green. I think this is related to me only seeing black when using the default depth shader.
float4 TestColor = float4(0, 1, 0, 1);? I have no idea how the whole {} thing works, I've always used the constructor-type-of-thing.