My first HLSL shader

IvanJ147

26-02-2014 18:55:43

Hi all!!
I'm trying yto write my first shader, but I have a problem.
I've created my material file in this way:


vertex_program VertexMain hlsl
{
source MyFirstShader.hlsl
target vs_2_0
entry_point vs_main
default_params
{
param_named_auto matViewProjection worldviewproj_matrix
}
}
fragment_program PixelMain hlsl
{
source MyFirstShader.hlsl
target ps_2_0
entry_point ps_main
}

material lambert4
{
technique
{
pass
{
vertex_program_ref VertexMain
{
}
fragment_program_ref PixelMain
{
}
}
}
}


And this is MyFirstShader file:

float4x4 matViewProjection;

struct VS_INPUT
{
float4 Position : POSITION0;
};

struct VS_OUTPUT
{
float4 Position : POSITION0;
};

VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Position = mul(matViewProjection,Input.Position);
return( Output );
}

float4 ps_main() : COLOR0
{
return( float4(1, 0, 0, 1 ) );
}


In my application I expect to see my model all red, but actually I see it totally white.
I've tested the shader with RenderMonkey, and it works like I expect, so I think the problem is in the material settings.
Can anybody explain me what's wrong?
Thanks for your help.

BrainScan

27-02-2014 11:00:58

I don't see anything obviously wrong. If there's a syntax error somewhere in either file, it should be listed in the Ogre.log file. There should also be an error in there if it can't find your hlsl file. So, looking there should be the easiest way to track down the problem.

IvanJ147

09-03-2014 20:47:30

Unfortunately in the Ogre.log file there weren't errors; anyway I think I've found the problem, I've changed this code:

material lambert4
{
technique
{
pass
{
vertex_program_ref VertexMain
{
}
fragment_program_ref PixelMain
{
}
}
}
}

in this way:

material lambert4
{
technique
{
pass
{
vertex_program_ref VertexMain
{
}
fragment_program_ref PixelMain
{
}
texture_unit
{
}
}
}
}

IvanJ147

09-03-2014 20:58:50

I have another question (I don't know if this is the most appropriate section, in this case I'll remove my question): which is the correct code to obtain a "neutral" shader? I mean, I want a shader that if use it I see the same 3D scene that I obtain when I don't use it. Is it possible?
I've tried this code, but I see all black in my scene:

Vertex shader

float4x4 matViewProjection;

struct VS_INPUT
{
float4 Position : POSITION0;
float4 Color: COLOR0;
};

struct VS_OUTPUT
{
float4 Position : POSITION0;
float4 Color: COLOR0;
};

VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;

Output.Position = mul( Input.Position, matViewProjection );
Output.Color=Input.Color;

return( Output );

}


Pixel shader:

struct PS_INPUT
{
float4 Color: COLOR0;
};

struct PS_OUTPUT
{
float4 Color: COLOR0;
};

PS_OUTPUT ps_main( PS_INPUT Input)
{ PS_OUTPUT Output;
Output=Input;
return (Output);

}


In your opinion, what's wong?

BrainScan

12-03-2014 00:38:38

Your HLSL code looks very similar to what I do in CG, so I'd say you've got the right idea at least. Unfortunately, I'm not good with the HLSL syntax so if there is a syntax error I might not be able to see it. When I have a syntax error in my CG programs it always appears in the ogre log file. So, if there's nothing there I would assume the syntax is fine. How about your program definition? This is what mine looks like for CG.

vertex_program Widget_vp cg
{
source Widgets.cg
profiles vs_1_1 arbvp1
entry_point Widgets_vp
default_params
{
param_named_auto cWorldViewProj worldviewproj_matrix
}
}

Note the default_params section. This is what assigns the correct matrix to my shader parameter. In this case my parameter name is cWorldViewProj. You'd have to modify that as appropriate.

If that's not it, I would try asking somewhere in the main Ogre forums since this isn't a Mogre specific question. There's probably someone over there that knows HLSL better.