0
votes

I recently started developing an application using Monogame and entered the world of shaders and shading languages. The target platform is GL. Which means that I have to write my shader in HLSL, which subsequently gets converted into GLSL by MojoShader.

I apperently miss something very obvious and I was wondering if anyone could help me out. Here is the code that is supposed to paint every object in the scene in a uniform dark-grey color.

float4x4 World;
float4x4 View;
float4x4 Projection;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;

struct VertexShaderInput
{
    float4 Position : POSITION0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    return AmbientColor * AmbientIntensity;
}

technique Ambient
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

Unfortunately, it simply gives me a blank screen. But here is the thing, when I change that one line of code in the pixel shader return AmbientColor * AmbientIntensity; with return float4(1,1,1,1) * float(1); it gives me the correct result and I can actually see the object on the screen. It's as if when I am using the global variables they are set to zero.

Can it be that there might be issues when converting from HLSL to GLSL and I have to keep in mind certain specificities when writing my HLSL code?

I would greatly appreciate if you could help me answering this questions or point me in the right direction. Thank you.

1

1 Answers

0
votes

The FX system in DirectX allows you to initialise the uniform variables AmbientColor and AmbientIntensity with values, but GLSL does not allow you to do this. For that matter, neither does HLSL - it's only the FX system built on top of HLSL that allows it.

So those uniforms will be defaulting to zero (or possibly some other uninitialized value).

You need to set the uniform constants in your program code before using the GLSL shader. In OpenGL this is usually done by calling glUniform1f and glUniform4f.