[Tutorial]Using HLSLshaders for bumpmapping + lighting (WIP)

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Granyte »

Tanks for the turorial it finaly alowed me to use texture atlasses because my previous normal maping technique was nnot working when i was atlassing normal map and this one works

the only bad side is that i don't yet understand the principle behind normal maping
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Mel »

The normal mapping is like the regular lighting, but instead of calculating the normal, you read it from a texture, then you transform the normalized light vector to the tangent space for the light equation and perform the NdL product as always.

That is why the tangent space vectors are needed for.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Granyte »

yup the only thing is that in this shader the normal is put to tangent space not the light vector.....
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Abraxas) »

There are many ways of doing lighting. Normal texture and simple light attenuation is quickest way of making little highlights, and specular lighting is the best way of making things a little "shinier". This shader is kinda crappy because it doesn't do things in the "best, most correct way" but it's a good starting point. Also, per pixel lighting, in every case I've found so far, looks better than irrlicht's lights.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Mel »

Granyte wrote:yup the only thing is that in this shader the normal is put to tangent space not the light vector.....
Not exactly, What the shader is doing is to convert the normals to worldSpace, so, light direction and normals are expressed in this case in world space, and the calculation can be done the same, although that is slower because it moves the normal calculation to the pixel shader instead of transforming the light vector in the vertex shader, but in exchange it may perform per pixel look ups on 3D textures for environmental calculations using the normals in world space.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Granyte »

i"m trying to use this shader with the following code to generate a normal froma height map but some how it won't work.

Code: Select all

float3 norm =normalize(float3(0,0,1));//= normal;
    float me = tex2D(tex0,TexCoord).r;
    float n = tex2D(tex0,saturate(float2(TexCoord.x,TexCoord.y+1.0/texsize))).r/2.0f;
    float s = tex2D(tex0,saturate(float2(TexCoord.x,TexCoord.y-1.0/texsize))).r/2.0f;
    float e = tex2D(tex0,saturate(float2(TexCoord.x+1.0/texsize,TexCoord.y))).r/2.0f;
    float w = tex2D(tex0,saturate(float2(TexCoord.x-1.0/texsize,TexCoord.y))).r/2.0f;
    
    float ne = tex2D(tex0,saturate(float2(TexCoord.x+1.0/texsize,TexCoord.y+1.0/texsize))).r/6.0f;
    float sw = tex2D(tex0,saturate(float2(TexCoord.x-1.0/texsize,TexCoord.y-1.0/texsize))).r/6.0f;
    float se = tex2D(tex0,saturate(float2(TexCoord.x+1.0/texsize,TexCoord.y-1.0/texsize))).r/6.0f;
    float nw = tex2D(tex0,saturate(float2(TexCoord.x-1.0/texsize,TexCoord.y+1.0/texsize))).r/6.0f;
 
    n += tex2D(tex0,saturate(float2(TexCoord.x,TexCoord.y+2.0/texsize))).r/6.0f;
    s += tex2D(tex0,saturate(float2(TexCoord.x,TexCoord.y-2.0/texsize))).r/6.0f;
    e += tex2D(tex0,saturate(float2(TexCoord.x+2.0/texsize,TexCoord.y))).r/6.0f;
    w += tex2D(tex0,saturate(float2(TexCoord.x-2.0/texsize,TexCoord.y))).r/6.0f;
    
    n += ne;
    n += nw;
    s += se;
    s += sw;
    e += ne;
    e += se;
    w += nw;
    w += sw;
                            //find perpendicular vector to norm:        
                    float3 temp = norm; //a temporary vector that is not parallel to norm
                    if(norm.x==1)        
                        temp.y+=0.5;
                    else       
                        temp.x+=0.5;
                            //form a basis with norm being one of the axes:
                            float3 perp1 = normalize(cross(norm,temp));
                            float3 perp2 = normalize(cross(norm,perp1));
                            //use the basis to move the normal in its own space by the offset        
                            float3 normalOffset = -0.1*(((n-me)-(s-me))*perp1 + ((e-me)-(w-me))*perp2);
                            norm +=normalOffset;
                            norm = normalize(norm);
                            
i have been so far totaly unable to get it to work any sugestion?
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Abraxas) »

Just use the height2normal program or any other heightmap to normal converter. You might as well begin your program with everything precalculated no?
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Granyte »

that is impossible in my case since everything is generated on the fly using procedural algorythm

and just because i see it comming no i cannot use the built in make normal map function because my height map can be 16 or 32 bit precision if the user want it (standar 8bit color return crappy result over a complete planet)
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by hendu »

I pointed out the paper to do that in another topic; search for "tangentless normal mapping".
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Granyte »

i had a tengent less normal maping implemented befor only at planetary scale it was giving horrible arifacts when people were getting to close to the groundyuiiii
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by The_Glitch »

I would like to use this but Im not sure how,do all the code above go in the d3d9.hlsl or is it all suppose to be setup in your main programs code?

I would like to make some shaders,I'm able to make them in fx composer easily,I just dont understand what irrlicht needs it in.fx composer has alot of extra of extra code,but none of it is like irrlichts example.
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Abraxas) »

Only the shader part goes in the hlsl file. You need to start the engine in directx9 mode, load the shader and set the call back, and then set the material type like in the example.
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by The_Glitch »

Well Abraxas I wasnt able to get the code snippet on the first page to work,Theres some missing code that is from his project.
Think you can post it working or any suggestions.
Im working on a 3d screensaver,and Id like to use normal mapping on animated objects,is the code snippet above only for static objects?
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: [Tutorial]Using HLSLshaders for bumpmapping + lighting (

Post by Abraxas) »

hmmm.. if you begin with the shader example and use this code, combined with a bumpmap, you shouldn't be missing anything.
Post Reply