Cg/HLSL Saturate

I’ve been converting my Cg shaders I wrote for my AGT work in university into GLSL, however I came by an annoying problem, Cg uses a function that doesn’t exist in GLSL, but does exist in HLSL. I eventually found documentation on what it does and so looked up the GLSL equivilant. The Cg version shown below…

float3 outValue = saturate( inValue );

Becomes the following when converted to GLSL…

vec3 outValue = clamp( inValue, 0.0, 1.0 );

 

Thought this might be useful for others who might be having the same problem.

Texture Splat Shader

Texture Splat BaseA shader for using an RGBA Map to texture map a primitive with four textures.

The first texture is the base texture splat map. The image on this blog entry is a texture splat map that I’ve used, anywhere there is a green component is grass, anywhere there is blue is sand/dirt, and anywhere there is red is rock/road. The colour component acts as a blend value.

The other three textures represent the area I just mentioned, there is room for texture to be applied to the same texture splat if the alpha channel is used, but I prefer to save that for use as a height map in the vertex stage.

// --- [ structs ] --------------------------------------------
//
struct VS_OUTPUT
{
	float4 position : POSITION;
	float2 uv : TEXCOORD0;
};
 
struct VS_INPUT
{
	float4 position : POSITION;
	float2 uv : TEXCOORD0;
};
 
// --- [ vertex program ] -------------------------------------
//
VS_OUTPUT main_vp( VS_INPUT In, uniform float4x4 worldViewProj )
{
	VS_OUTPUT Out;
 
	Out.position = mul(worldViewProj, In.position );
	Out.uv = In.uv;
 
	return Out;
}
 
// --- [ fragment program ] -----------------------------------
//
float4 main_fp( float2 uv : TEXCOORD0, uniform sampler2D texBase, uniform sampler2D tex0, uniform sampler2D tex1, uniform sampler2D tex2 ) : COLOR
{
	float3 colour = tex2D(texBase, uv).rgb;
	return tex2D(tex0, uv) * colour.r + tex2D(tex1, uv) * colour.g + tex2D(tex2, uv) * colour.b;
}

 

Basic Water Shader

A very simple vertex shader for creating basic wave motion. I designed this shader for use with the Tales of Phantasia Remake for the river in Totus Village. And despite its simplicity it turned out quite well.

// --- [ structs ] --------------------------------------------
//
struct VS_OUTPUT
{
	float4 position : POSITION;
	float2 uv : TEXCOORD0;
};
 
struct VS_INPUT
{
	float4 position : POSITION;
	float2 uv : TEXCOORD0;
};
 
// --- [ vertex program ] -------------------------------------
//
VS_OUTPUT main_vp( VS_INPUT In, uniform float4x4 worldViewProj, uniform float time )
{
	VS_OUTPUT Out;
 
	Out.position = In.position;
	Out.position.y = In.position.y + 0.10 * ( sin( ( In.position.z + ( time * 30 ) ) + ( In.position.x + ( time * 30 ) ) ) ) - 0.10;
	Out.position = mul(worldViewProj, Out.position);
	Out.uv.x = ( In.uv.x - time * 5 ) * 0.5;
	Out.uv.y = ( In.uv.y + time * 10 ) * 0.5;
 
	return Out;
}
 
// --- [ fragment program ] -----------------------------------
//
float4 main_fp( float2 uv : TEXCOORD0, uniform sampler2D tex0 ) : COLOR
{
	return tex2D(tex0, uv);
}