Shader doesn't look the same in OGL and DX

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
User avatar
guyver6
Greenskin
Posts: 106
Joined: Mon Dec 23, 2002 10:16 pm
Location: Warsaw, Poland

Shader doesn't look the same in OGL and DX

Post by guyver6 »

Hi,

We're having a problem with shaders, which doesn't look the same under DX and OGL. It's basicly normal mapping shader.

OpenGL:
Image
Image

DX:
Image
Image

Now comes the shader code:

Code: Select all

void main(
		float2 TexCoord0 : TEXCOORD0,
		float3 L : TEXCOORD1,
		float3 H : TEXCOORD2,
		
		//my outputs
		out float4 oColor : COLOR,

		//textures
		uniform sampler2D diffuseMap,
		uniform sampler2D normalMap,
		uniform sampler2D specularMap,
		
		//uniform state
		uniform float3 Ambient,
		uniform float3 Diffuse,
		uniform float3 OceanSpecular,
		uniform float3 ContinentSpecular,
		uniform float OceanShininess,
		uniform float ContinentShininess)
{
   //fetch normal diffuse color
   float3 diffuseTexture = tex2D(diffuseMap, TexCoord0).xyz;
   float3 specularCoefficient = tex2D(specularMap, TexCoord0).xyz;
   float3 N = tex2D(normalMap, TexCoord0).xyz;
   
   //prepare parameters for lighting calculations
   N = (N - 0.5) * 2.0;
   N = normalize(N);

   L = normalize(L);   
   H = normalize(H);
   
   float D = max(dot(N, L), 0.0);
   
   //specular including selfshadowing
   float spec = max(dot(N, H), 0.0);
   spec = (D > 0.5)?spec:0.0;
   
   float3 oceanSpec = pow(spec, OceanShininess) * OceanSpecular;
   float3 continentSpec = pow(spec, ContinentShininess) * ContinentSpecular;
   float3 specularColor = lerp(continentSpec, oceanSpec, specularCoefficient); 
   
   float3 outputColor = min(Ambient + Diffuse * D, 1.0) * diffuseTexture;
   
   outputColor += specularColor;
   
   //oColor = float4(outputColor, 1.0);
   oColor = float4(L, 1.0);
}
I'll sent here developer who have written this shader if you want to know details. Anyway I want to ask why does it look different under different renderers.

EDIT:
Here comes vertex shader:

Code: Select all

void main(
		//my inputs
		float4 Position : POSITION,
		float3 normal : NORMAL,
		float2 TexCoord0 : TEXCOORD0,
		float3 tangent : TEXCOORD1,
		
		//my outputs
		out float4 oPosition : POSITION,
		out float4 oColor : COLOR,
		out float2 oTexCoord0 : TEXCOORD0,
		out float3 oTexCoord1 : TEXCOORD1,
		out float3 oTexCoord2 : TEXCOORD2,
		
		//uniform state
		uniform float4x4 ModelViewProj,
		uniform float4 LightPos,
		uniform float3 EyePos)
{
	oPosition = mul(ModelViewProj, Position);
	oTexCoord0 = TexCoord0;
	oColor = float4(1.0, 1.0, 1.0, 1.0);
	
	float3 lightDir = LightPos.xyz;
	
	normal = normalize(normal);
	tangent = normalize(tangent);
	float3 binormal = cross(tangent, normal);
	
	float3x3 rotation = float3x3(tangent, binormal, normal);
	
	// Transform the light vector according to this matrix
	float3 L = normalize(mul(rotation, lightDir));
	oTexCoord1 = L;
	//oTexCoord1 = normalize(lightDir);
	
	//calculate half-angle for specular
	float3 V = EyePos - Position.xyz;
	V = normalize(mul(rotation, V));
	oTexCoord2 = normalize(V + L);
}
More info on mesh is that the parts that doesn't look right in OGL screenshots are submeshes without shared geometry (boudary vertices are duplicated, becouse each submesh have it's own texture coords (we need that becouse all 10 submeshes has different textures and it can't be done with shared vertices... believe us, we've tried).

Guyver
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66

Post by sinbad »

Not sure why it looks different offhand, but you're doing some pretty expensive and unnecessary operations in the pixel shader there. You should be using a normalisation cubemap (see our normal mapping shader) instead of using normalize() calls in the pixel shader - that's 3 square roots per pixel you're doing there (ouch). You must be using ps_2_0 or above anyway otherwise you wouldn't have pow(), but still it's a waste of resources.

All I would advise is to try removing sections of the calculation until it does look the same so you can determine which single factor makes the difference between the two. Then, use the command-line cgc tool and look at the assembler to see if you can see what's logically different with that one thing. It might just be a precision thing.
rincewind
Kobold
Posts: 33
Joined: Tue Jan 04, 2005 5:57 pm

Post by rincewind »

This is related to the problem about different texcoords showing in the Help-Forum http://www.ogre3d.org/phpBB2/viewtopic.php?t=7283.
Same fix works for this problem, here.

Just so nobody has to crack their head about it anymore.

Rincewind