CG shader and camera_position

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
kubatp
Gnome
Posts: 368
Joined: Tue Jan 06, 2009 1:12 pm
x 43

CG shader and camera_position

Post by kubatp »

Hi,
I have been trying to figure this out already for four hours and I was not able to make it work.
I am trying to create an entity (like a fog), which when you get closer with the camera, it changes its opacity (alpha channel).
Basically, what I want is to pass global position to a pixel shader. This is what I have, but it doesn't work

Code: Select all

fragment_program fog_ps_cg cg
{
	source fog.cg
	entry_point main
	profiles ps_2_0 arbfp1		
}

vertex_program fog_vs_cg cg
{
   source fog.cg
   entry_point mainVS
   profiles vs_1_1 arbvp1

   default_params
   {
      param_named_auto worldViewProj_m worldviewproj_matrix
      param_named_auto cameraPos camera_position
   }
}

Code: Select all

struct vertexIn
{
   float4 position : POSITION;
   float2 texCoord0    : TEXCOORD0;
};

struct vertexOut
{
   float4 position : POSITION;
   float2 texCoord0    : TEXCOORD0;
   float4 cameraPosition    : TEXCOORD1;
};

vertexOut mainVS(vertexIn input,
   uniform float4x4  worldViewProj_m,
   uniform float4 cameraPos)
{
   vertexOut output = (vertexOut)0;
   output.texCoord0 = input.texCoord0;
   output.position = mul(worldViewProj_m, input.position);    
   output.cameraPosition = cameraPos;
   return output;
}

float4 main(
	vertexOut vertex_output,
    uniform sampler2D fog						: register(s0) 	
    ) : COLOR
{           
   float4 result = tex2D(fog, vertex_output.texCoord0);   
   result.a = vertex_output.cameraPos / 10; 
   return result;
}
There are no errors in log, but result.a is always 0 once I see the result. It is not a problem of texture, when I comment out this line

Code: Select all

result.a = vertex_output.cameraPos / 10; 
it works just fine.

Thank you for help
kubatp
Gnome
Posts: 368
Joined: Tue Jan 06, 2009 1:12 pm
x 43

Re: CG shader and camera_position

Post by kubatp »

Noone can help me here? I am sure it is something simple I overlooked, but I cannot find out what.
Basically I want to find two distances
  • Horizontal distance (distance on x and z axis). Higher distance means that result.a should be higher (fog should be heavier)
  • Vertical distance (distance on y axis). Higher distance means that result.a should be lower (fog should be lighter)
The reasons for this are simple
- once user zooms in, the fog gets heavy and further from camera is the fog heavier and heavier.
- when user zooms out, the fog gradually disappears and user gets nice and clear map overview.

I already have some experience with shaders, but none of the with camera position or distance camera from pixel.
kubatp
Gnome
Posts: 368
Joined: Tue Jan 06, 2009 1:12 pm
x 43

Re: CG shader and camera_position

Post by kubatp »

Ok, it was something really stupid:-/ I forgot to add vertex shader to the material definition.
Can admin delete this post please? Thank you
Post Reply