2 registered members (3s05bmmc, flink),
650
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Cubemap normalization
#83742
07/29/06 13:16
07/29/06 13:16
|
Joined: Jan 2004
Posts: 2,013 The Netherlands
Excessus
OP
Expert
|
OP
Expert
Joined: Jan 2004
Posts: 2,013
The Netherlands
|
I'm trying to get my normalmapping shader with blinn-phong specular to work on ps 1.1. I have to normalize the halfangle vector in the pixel shader but because I can't afford another normalize() (max 10 instructions..), I think I'll have to use cubemap normalization. What I have is this: Code:
c-script: bmap NormalizeCubeMap = "normalize.dds"; .. material abc { skin1 = NormalizeCubeMap; .. } .. bmap_to_cubemap(abc.skin1);
hlsl: texture mtlSkin1; samplerCUBE NormalizationCubeMapSampler = sampler_state { Texture = <mtlSkin1>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = Clamp; AddressV = Clamp; };
pixel shader: InHalf = 2 * texCUBE(NormalizationCubeMapSampler,2 * InHalf - 1) - 1;
Explanation: I first define a bitmap that contains the normalized vectors. The sides are in the following order: x, -x, y, -y, z, -z (I found this image on the web). Then I assign that bitmap as the first material skin. Then I use bmap_to_cubemap to convert the bitmap to a cubemap, I'm not sure this is needed so I also tried without that instruction and it doesn't work either. I then set up the texture and sampler in hlsl. In the pixelshader I try to normalize the vector InHalf. InHalf has been shifted to the range [0..1] so I have to shift it back to [-1..1] by 2 * InHalf - 1. Then I use that as the 3d coordinate to lookup in the cubemap. I then shift the resulting vector from [0..1] to [-1..1]. The results are wrong. It's not just inacurate it just gives incorrect results. I'm pretty sure the normalize.dds is correct (atleast it contains normalized vectors, I checked that) but I'm not too sure about the order of the sides.. Can anyone spot the mistake here? btw, my shader works fine when I use normalize() and ps_2_0 as compile target.
|
|
|
Re: Cubemap normalization
[Re: Matt_Aufderheide]
#83744
08/03/06 19:33
08/03/06 19:33
|
Joined: Jan 2004
Posts: 2,013 The Netherlands
Excessus
OP
Expert
|
OP
Expert
Joined: Jan 2004
Posts: 2,013
The Netherlands
|
Hi Matt, I use the blinn-phong algorithm that uses a halfway vector between the view and light directions. Here is the code: Code:
float4 BumpSpec_PS( in float2 InTexN : TEXCOORD0, in float3 InHalfVec : TEXCOORD1, in float InShadow : TEXCOORD2) : COLOR { float3 BumpNormal = 2 * tex2D(NormalMapSampler, InTexN.xy) - 1.0; // Normalize halfway vector with cubemap: // InHalfVec = 2 * texCUBE(NormalizationCubeMapSampler,2 * InHalfVec.xyz - 1) - 1; // doesn't work..
// Ordinary normalize: InHalfVec = normalize(2 * InHalfVec - 1); // Works only in ps_2_0 // Calculate specular: float Specular = pow(saturate(dot(BumpNormal, InHalfVec)), SpecPower); // For debugging you can output the would-be normalized half vector: // return float4(normalize(2 * InHalfVec - 1).xyz, 1); // How it should look // return 2 * texCUBE(NormalizationCubeMapSampler, 2 * InHalfVec - 1) - 1; // How it looks with cubemap normalization. return Specular.xxxx * InShadow * SpecularCoef; }
Thanks for lookin into it. I could also post the whole shader, or even a demo implementation, if you can't find the error this way and you have the time to look at it.. I want my game to support ps_1_1 hardware and I'd like to enable normalmapping for the first LOD on those cards, so thats why I need ps_1_1 support.
|
|
|
Re: Cubemap normalization
[Re: Matt_Aufderheide]
#83746
08/04/06 10:00
08/04/06 10:00
|
Joined: Jan 2004
Posts: 2,013 The Netherlands
Excessus
OP
Expert
|
OP
Expert
Joined: Jan 2004
Posts: 2,013
The Netherlands
|
Infact the halfvector is being calculated in the vertex shader, but since texcoords are automatically clamped between 0 and 1 in ps_1_1, I had to compress the halfvector to [0..1] and expand it again in the pixelshader. It doesn't seem to be a problem though, the compiler doesn't complain. This is what it looks like in ps_2_0 with normalize: Image This is what it looks like when I use cubemap normalization: ImageAnd the cubemap: ImageI'm not mipmapping the cubemap (I don't use bmap_to_mipmap and it's not a .dds image).
|
|
|
Re: Cubemap normalization
[Re: Matt_Aufderheide]
#83750
08/04/06 14:47
08/04/06 14:47
|
Joined: Jan 2004
Posts: 2,013 The Netherlands
Excessus
OP
Expert
|
OP
Expert
Joined: Jan 2004
Posts: 2,013
The Netherlands
|
Ok, I calculated a 3d texcoord in the vs: OutCoord = PosWorld - float3(53, 103, 30); Output it in the ps: return texCUBEproj(NormalizationCubeMapSampler, float4(InCoord,1)); This is what I get when I use bmap_to_cubemap on the cubemap: ImageI assume that is needed, but just incase, here is what I get without bmap_to_cubemap: Image
|
|
|
|