Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/hmiwww/thehazymind.com/smf/Sources/Load.php(225) : runtime-created function on line 3
Bumpmap Shader
XNA Game Development Forums
2017/04/24 06:17:16 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1] 2 3 ... 5
  Print  
Author Topic: Bumpmap Shader  (Read 565707 times)
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« on: 2007/03/09 13:02:31 »

I have been working on a bumpmap shader from a book I got (Programming Vertex & Pixel Shaders by Wolfgan Engel) and got it working lovely with my engine. BUT to night I came on to start the next shader tutorial in the book and now my bumpmap shader is doing some odd stuff (see screen shot below)...

Any one got any ideas what is going on here or has any one had a similar experience and managed to find out what went wrong?

This is the shader:
Code:
float4x4 wvp;
float4x4 world;
float3 LightPosition;
float3 EyePosition;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
float3 Light : TEXCOORD1;
float3 View : TEXCOORD2;
};

VS_OUTPUT VS(float4 Pos : POSITION,float2 Tex : TEXCOORD,float3 Normal : NORMAL,float3 Tangent : TANGENT)
{
VS_OUTPUT Out = (VS_OUTPUT)0;

Out.Pos = mul(Pos,wvp);

float3x3 worldToTangentSpace;
worldToTangentSpace[0] = mul(Tangent,world);
worldToTangentSpace[1] = mul(cross(Tangent,Normal),world);
worldToTangentSpace[2] = mul(Normal,world);

Out.Tex = Tex;

float4 PosWorld = mul(Pos,world);

Out.Light = mul(worldToTangentSpace,LightPosition);
Out.View = mul(worldToTangentSpace,-EyePosition - PosWorld);

return Out;
}

texture ColorMap;
sampler ColorMapSampler = sampler_state
{
Texture = <ColorMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};

texture BumpMap;
sampler BumpMapSampler = sampler_state
{
Texture = <BumpMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};

float4 PS(float2 Tex : TEXCOORD0,float3 Light : TEXCOORD1,float3 View : TEXCOORD2) : COLOR
{
float4 Color = tex2D(ColorMapSampler,Tex);
float3 Normal = (2 * (tex2D(BumpMapSampler,Tex))) - 1.0;

float3 LightDir = normalize(Light);
float3 ViewDir = normalize(View);

float Diffuse = saturate(dot(Normal,LightDir));
float Reflect = normalize(2 * Diffuse * Normal - LightDir);

float Specular = min(pow(saturate(dot(Reflect,ViewDir)),3),Color.w);

return 0.2 * Color + Color * Diffuse + Specular;
}

technique BumpMapShader
{
    pass P0
    {
        Sampler[0] = (ColorMapSampler);
        Sampler[1] = (BumpMapSampler);
   
        VertexShader = compile vs_2_0 VS();
        PixelShader  = compile ps_2_0 PS();
    }
}

[attachment deleted by admin]
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #1 on: 2007/03/09 14:26:58 »

Now how odd is that?!?!?!?!?!??!?!!?

It's working again now. I have not altered the shader at all, all I have done is play around with the post processor a bit used it then switched it off again......

 Huh WHAT IS GOING ON THERE THEN?!??!  Huh

I know my PC is poop, but this is some strange stuff...

Anyone got any ideas?

[attachment deleted by admin]
Logged
EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« Reply #2 on: 2007/03/09 14:50:47 »

lol

It's really wierd
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #3 on: 2007/03/09 15:00:24 »

Well it is starting to do my head in now! It has gone screwy again after I played with the old PP code, but I have put the code back as it was and now the thing is rendering it dead odd.....

The attached is the globe from the other side, the bumpmapping seems to be working but the lighting has now gone mental!

I have commented out the PP altogether and yet is it till playing up!! It's driving me mad  Cry

[attachment deleted by admin]
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #4 on: 2007/03/09 16:29:27 »

This shader is still not playing ball, I will have another look tomorrow. I got a parallax bump map shader working in the mean time though.  Grin small things pleas small minds I suppose  Wink

[attachment deleted by admin]
Logged
Tiago
Newbie
*
Offline Offline

Posts: 42


View Profile
« Reply #5 on: 2007/03/09 21:34:31 »

this would be a wild gess but are you shure your object has correct tangents and normals?
Logged
EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« Reply #6 on: 2007/03/10 01:28:34 »

This shader is still not playing ball, I will have another look tomorrow. I got a parallax bump map shader working in the mean time though.  Grin small things pleas small minds I suppose  Wink

Share it with us  Wink
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #7 on: 2007/03/10 04:21:04 »

Tiago,

I am using the same model that comes with the book that has the example shader in it so I am assuming it' tangents are all OK. If you can think of any other reason why the behavior of this shader should change when neither the shader the model or the assets have not altered I would appreciate it Smiley

I think I know why it is playing up now and that is down to the way i is currently calculating the Specular,  I think the next chapter holds the answer....

EclipsE,

The bumpmap shader is already here, but this is the parallax bumpmap.

Code:
float4x4 wvp;
float4x4 world;
float3 LightPosition;
float3 EyePosition;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
float3 Light : TEXCOORD1;
float3 View : TEXCOORD2;
};

VS_OUTPUT VS(float4 Pos : POSITION,float2 Tex : TEXCOORD,float3 Normal : NORMAL,float3 Tangent : TANGENT)
{
VS_OUTPUT Out = (VS_OUTPUT)0;

Out.Pos = mul(Pos,wvp);

float3x3 worldToTangentSpace;
worldToTangentSpace[0] = mul(Tangent,world);
worldToTangentSpace[1] = mul(cross(Tangent,Normal),world);
worldToTangentSpace[2] = mul(Normal,world);

Out.Tex = Tex;

float4 PosWorld = mul(Pos,world);

Out.Light = mul(worldToTangentSpace,LightPosition);
Out.View = mul(worldToTangentSpace,EyePosition - PosWorld);

return Out;
}

texture ColorMap;
sampler ColorMapSampler = sampler_state
{
Texture = <ColorMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};

texture BumpMap;
sampler BumpMapSampler = sampler_state
{
Texture = <BumpMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};

texture HeightMap;
sampler HeightMapSampler = sampler_state
{
Texture = <HeightMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};

float4 PS(float2 Tex : TEXCOORD0,float3 Light : TEXCOORD1,float3 View : TEXCOORD2) : COLOR
{
const float scale = {0.04f};
const float bias = {0.02};
float3 LightDir = normalize(-Light);
float3 ViewDir = normalize(View);

float Height = scale * tex2D(HeightMapSampler,Tex) - bias;

float2 TexCorrected = Height * ViewDir + Tex;
float4 Color;
float3 Normal;


Color = tex2D(ColorMapSampler,TexCorrected);
Normal = (2 * (tex2D(BumpMapSampler,TexCorrected))) - 1.0;


float Diff = saturate(dot(Normal,LightDir));

return 0.2 * Color + Color * Diff;
}

technique BumpMapShader
{
    pass P0
    {
        Sampler[0] = (ColorMapSampler);
        Sampler[1] = (BumpMapSampler);
   
        VertexShader = compile vs_2_0 VS();
        PixelShader  = compile ps_2_0 PS();
    }
}

In both shaders, please forgive my lack of Semantics and comments, I am writing them then testing them as I go and will tidy them up later. Just thought, I just said I am writing them, I'm not, I am doing the examples from the book and then getting them to fit my engine... How my ego keeps trying to build me up   :Smiley

Also, your model needs the UV image of the ColorMap  for some reason, I will alter the shader at a later date so this can be passed to the shader rather than it relying on the model having it loaded.

Mike/Chr0n1x,

I think I should have put this thread in the code section now, any chance you guys can move it?  Unless you think it is OK here...
« Last Edit: 2007/03/10 04:24:42 by Nemo Krad » Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #8 on: 2007/03/10 04:42:54 »

OK, this goes from bad to worse...

First run of the code today, no changes made to the code other than adding another shader and model to show it's effect. and my bump mapping is back to square one....

What on earth is going on?

[attachment deleted by admin]
Logged
Tiago
Newbie
*
Offline Offline

Posts: 42


View Profile
« Reply #9 on: 2007/03/10 11:36:34 »

the only reason i can think of to explain why it behaves diferently each time without you making any changes is that the shader is being passed data from an uninitialized point in memory.

its something like this, lets say you create an array[4][4] with but you never initialize the array, ussually you would get an error in C#, but its happened to me a lot of times in C++, and what you end up with is an array with so much garbage in it that when you try to read it it makes you think you just got a virus.

anyway, uninitialized memory is my theory. now, on how to fix it? not a clue really but hopefully this will help someone else help you with that
Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #10 on: 2007/03/10 14:36:38 »

Sounds like you are not setting device states correctly. You will probably have to do that explicitly in each shader to be what they need to be.
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #11 on: 2007/03/10 15:00:42 »

Ahhh, thats possible, I would have thought the book might have mentioned that, maybe it did and I skim read past it wanting to see the blighter working. I will take a look Mike.

Tiago,

I would accept that if the issue occurred on other shaders but it does not. I know exactly what you are talking about though, you don't play around with void **, malloc etc.. without coming across that sort of thing  Wink
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #12 on: 2007/03/10 15:35:48 »

Mike,

I can't for the life of me see where it mentions required render states. In the C++ examples there are no states that I can see that get set and the shader example from the book is pretty much the same as the ones I have posted here.

I have tried having just the one model with the shader, disabled the pass to the light parameter (which left me with a model with a regular image on it but no bump mapping) I have tried all sorts and still to no avail (even a reboot lol). So, Mike, if you know of any states that should be set for a bump map shader then I would really appreciate it if you could tell me. It's driving me mad especially as I have had it working, I think that's what is most annoying the fact that it worked and I must have done something to break it, but for the life of me I don't know what....

Help  Cry
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #13 on: 2007/03/11 06:40:09 »

OK, solved it...

I had some third party software on my PC that seemed to be interfering with my GCard when it came to bump maps, don't know why, but it did. All I know is I un-installed it and it all worked fine... .go figure...

Thanks to you all that had a look at this...
Logged
EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« Reply #14 on: 2007/03/11 09:18:39 »

Wierd... btw, look at this Smiley

http://img231.imageshack.us/img231/7669/specialeffectsrf9.png
Logged
Pages: [1] 2 3 ... 5
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Shader compiler General Discussion endre 2 9361 Last post 2006/08/12 01:24:40
by Chr0n1x
Shader implemented fixed function Hazy Mind XNA Engine mikeschuld 2 8568 Last post 2006/11/07 02:07:22
by mikeschuld
Shader tutorials General Discussion endre 2 10749 Last post 2006/12/22 18:12:48
by mikeschuld
My Shader Hell... General Discussion « 1 2 » Nemo Krad 17 20332 Last post 2007/02/09 21:12:14
by Chr0n1x
What's wrong with my shader? :'( Hazy Mind XNA Engine EclipsE 6 9763 Last post 2007/03/06 15:43:17
by EclipsE
Point Light Shader General Discussion Nemo Krad 0 6664 Last post 2007/03/18 07:18:13
by Nemo Krad
Model & Shader Problems Hazy Mind XNA Engine Themodem 5 20008 Last post 2007/03/26 11:12:53
by Tiago
Terrain with Bumpmap Show Off Board Nemo Krad 8 16006 Last post 2007/06/29 01:22:38
by Nemo Krad
Playing with lighting : intermediate shader techniques? Hazy Mind XNA Engine 5parrowhawk 3 16969 Last post 2009/03/18 23:04:27
by Chr0n1x
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.168 seconds with 60 queries.