Ogre Shutdown while set ShaderConstant

Jo0oker

23-06-2010 01:24:58

Hello,

i have a problem with setting constants in a vertexprogram.

This is my material:

fragment_program Shader/Fader_FP cg
{
source Fader.cg
entry_point main_fp
profiles ps_1_1 arbfp1
}

material Sky/Sun
{
technique
{
pass
{
fragment_program_ref Shader/Fader_FP
{

}


lighting off
scene_blend add
depth_write off

texture_unit
{
texture Sun.png
}
}
}
}


This is my C#-Code:

/// <summary>
/// Einblenden
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void OnBlendInPhaseChangeEventHandler(object sender, EventArgs e)
{
Console.WriteLine("Blendin: " + (sender as ITSkyBlender).BlendPercent.ToString());

myCurrentBlending = (float)((sender as ITSkyBlender).BlendPercent / 100);

GpuProgramParametersSharedPtr t = myBillboardLight.GetMaterial().GetBestTechnique().GetPass(0).GetFragmentProgramParameters();

String progName = myBillboardLight.GetMaterial().GetBestTechnique().GetPass(0).FragmentProgramName;

t.SetNamedConstant("diffuse", new ColourValue(1, 0, 0, 1));
}


My programm always crashes at this line: t.SetNamedConstant("diffuse", new ColourValue(1, 0, 0, 1));

My cg-file content is:

float4 main_fp( float4 diffuse : TEXCOORD0 ) : COLOR
{
float4 colourOut;

colourOut.r = diffuse.r;
colourOut.g = diffuse.g;
colourOut.b = diffuse.b;
colourOut.a = diffuse.a;

return colourOut;
}



And finally the errormessage:

OGRE EXCEPTION(2:InvalidParametersException): Parameter called diffuse does not exist. in GpuProgramParameters::_findNamedConstantDefinition at ..\src\OgreGpuProgram.cpp (line 1097)


Greats,
Jo0oker

smiley80

23-06-2010 02:03:08

'float4 diffuse : TEXCOORD0' tells the GPU to assign the texture coordinates of the first texture unit to 'diffuse'.
If you want to be able to change its value from outside, change the declaration to 'uniform float4 diffuse'.

Jo0oker

23-06-2010 10:11:12

Thank, this solved the problem.

But now, i have another, litte problem.
The result isn´t what i want.

I only want to set the transperency, but now it look like this:



I hope some one can help me.

smiley80

23-06-2010 11:29:58

If you want to change the colour of a pixel based on the current colour, you basically have to do:
float4 main(float2 vIn : TEXCOORD0,
sampler2D tex0 : TEXUNIT0
) : COLOR
{
float4 color = tex2D(tex0, vIn);

float4 cOut = DO_SOMETHING_WITH_color;
return cOut;
}


For example, this fragment shader will colourise the texture with the 'diffuse' colour:
float4 main(float2 vIn : TEXCOORD0,
sampler2D tex0 : TEXUNIT0,
uniform float4 diffuse
) : COLOR
{
float4 weightsBW = float4(0.3, 0.59, 0.11, 0);
float4 color = tex2D(tex0, vIn);
float brightness = dot(color, weightsBW);
float4 cOut = brightness * diffuse;
cOut.a = color.a;
return cOut;
}


Edit: correction

Jo0oker

23-06-2010 15:40:45

Ok,

but when i take your second Shader, and set the constants like this:

GpuProgramParametersSharedPtr t = myBillboardLight.GetMaterial().GetBestTechnique().GetPass(0).GetFragmentProgramParameters();

String progName = myBillboardLight.GetMaterial().GetBestTechnique().GetPass(0).FragmentProgramName;

t.SetNamedConstant("tex0", 0);
t.SetNamedConstant("diffuse", new ColourValue(myBillboardColour.r, myBillboardColour.g, myBillboardColour.b, myCurrentBlending));


I get the following error again:

OGRE EXCEPTION(2:InvalidParametersException): Parameter called tex0 does not exist.


Thanks,
Jo0oker

smiley80

23-06-2010 16:08:45

You don't have to set 'tex0', it's automatically set to the first texture.
I've edited my post, the 'uniform' keyword was redundant.

Jo0oker

24-06-2010 07:24:37

Thank,

you helped me to understand shaders a little bit more :)

I will test it as soon as i am home.

Thank you a lot,
Jo0oker

Jo0oker

24-06-2010 21:36:43

Ok,
i don´t really get it.

But what i am doing wrong:

fragment_program Shader/Fader_FP cg
{
source Fader.cg
entry_point main_fp
profiles ps_1_1 arbfp1
}

material Sky/Sun
{
technique
{
pass
{
fragment_program_ref Shader/Fader_FP
{

}

lighting off
scene_blend alpha_blend
depth_write off
cull_hardware none

texture_unit
{
texture SunAlpha.png
tex_address_mode clamp
}
}
}
}


Shader:

float4 main_fp(float2 vIn : TEXCOORD0,
sampler2D tex0 : TEXUNIT0,
uniform float DiffuseAlpha
) : COLOR
{
float4 color = tex2D(tex0, vIn);
color.a = DiffuseAlpha;
float4 cOut = color;
return cOut;
}


Now i see nothing ):

The sund is a simple texture, with a Alphachannel.

Thanks,
Jo0oker

smiley80

24-06-2010 22:09:48

Yeah, you need Pixel Shader 2.0 for that:

change
profiles ps_1_1 arbfp1
to
profiles ps_2_0 arbfp1

Jo0oker

24-06-2010 22:20:19

Hm,

but than it looks again like this:



P.S.: I am also from germany, if you want, we can talk over ICQ
or some thing like that(Only if its no problem for you!) and i would
post the final solution here :)

Thanks,
Jo0oker