Author Topic: bumpmapping  (Read 29491 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
bumpmapping
« on: May 05, 2013 »
hey folks another boring sunday == some more code  ;D

ive coded some grayscale bumpmapping. now i remember seeing this done years ago but heres my take on it.. google was a poor source for info so i had too wing it.. im not 100% its correct so would be very greatfull if those with the knowledge could give some pointers.

cheers guys. 

very slow atm just want it too work properly first.

(-edit Removed exe too keep forum clean as they were rather large check further down )
« Last Edit: May 07, 2013 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #1 on: May 06, 2013 »
think ive got it  :)

i noticed while tinkering that rgb channels of the normalmap = x y z  direction vectors for light reflections i was thinking of them in color space not vector. so a dot product later and here we go. if you guys see anything wrong with it please let me know so i can mess around and learn. cheers..


(-Edit removed exe too keep forum clean as it was rather large check further down )
« Last Edit: May 07, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: bumpmapping
« Reply #2 on: May 06, 2013 »
Nice one, Nino.
The pure 2d bump usually just offsets the light-texture coordinate according to the normal (that's how it's done here).
The light-map was made big enough that you didn't have to care about clipping, so you don't have to recalculate it every frame.
Code: [Select]
for y= ...
  for x= ...
    col= colorMap[y][x]

    normal= normalMap[y][x]
    nx= normal shr 16 and 255
    ny= normal shr 8 and 255
           
    light= lightMap[y+ny+lightPosY][x+nx+lightPosX]

    dst[y][x]= blend(col, light)
If you really want to work in 3d, you can handle diffuse and reflection separately, though.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #3 on: May 06, 2013 »
what an elegant and cheep way of getting the effect. thanks very much hellfire..

im wanting to do specular and diffuse seprate though manly for learning. ive got the diffuse working properly it cycles through diffrent colors.. just wondering how specular works i would like it too look kind of wet like like so many modern games..

its manly the part where you calculate angle between eye and normal vector that i get lost.

ive got an idea but everytime i try and put it down it doesnt go well.

(-Edit removed exe too keep forum clean as it was rather large check further down )



« Last Edit: May 07, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: bumpmapping
« Reply #4 on: May 06, 2013 »
im wanting to do specular and diffuse seprate though manly for learning.
its manly the part where you calculate angle between eye and normal vector that i get lost.
Let's say your bitmap is on a plane in 3d space, eg. from (-320,-240,0) - (320,240,0).
The camera is in the center, somewhere above, eg. at (0,0,50).
And there's a position of the light-source somewhere between the plane and the camera.
For each pixel you've got a normal vector (stored in the normal map), a view direction (vector from the camera to the pixel) and a light direction (vector from the light source to the pixel).

You've already figured out the diffuse lighting which is
  diffuse= dot3(lightdir, normal)

For the specular term you reflect the view direction at the normal:
   reflectdir= viewDir - normal * 2 * dot3(normal, viewDir);
(viewDir and normal must be normalized, so it might be cheaper to use blinn's half angle apprroximation instead)

Check if it's facing the light source and use some glossiness function:
   specular= dot3(reflectdir, lightdir)
   if (specular > 0.0) specular= pow(specular, 40.0)

The final color is something like
   textureColor * diffuse * lightColor + specular * specularColor
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #5 on: May 06, 2013 »
wow thats awsome mate k++ i think ive cracked it and finally understand specular lighting!!

your explenation of how it all goes together into the final color helped hugely. does this look about right.


(-Edit removed exe too keep forum clean as it was rather large check further down )
« Last Edit: May 07, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: bumpmapping
« Reply #6 on: May 06, 2013 »
Nice!
Looks like the pixels which are facing away from the light-source are now getting most of the specular term, though.
So I think your reflection vector is probably negated.
It's also useful to have another map (or abuse the alpha-channel of the color- or normal-map) to store a "specular level".
This way you can give less specular to the dirty parts between the stones.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #7 on: May 07, 2013 »
yeah your right Mate it wasnt right.

 i slowed down a little and really went over all you wrote and im 72.594% sure i finaly have it  :).

i changed the bump map image too clearly show the specular term working. im intrested in what you wrote about packing a specular level into the alpha channel too stop incorrect lighting going on would it be possible for you too elaborate a little further?

thanks for all the help so far. there's no way i would have gotten this far without.

(-Edit removed exe too keep forum clean as it was rather large check further down )
« Last Edit: May 07, 2013 by ninogenio »
Challenge Trophies Won:

Offline Kirl

  • Senior Member
  • Pentium
  • ********
  • Posts: 1219
  • Karma: 231
    • View Profile
    • Homepage
Re: bumpmapping
« Reply #8 on: May 07, 2013 »
Looks great nino! Never coded bumps myself but use them extensively in my 3d work. Now I can't wait to give it a try as well, interesting thread. :)
www.kirl.nl
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #9 on: May 07, 2013 »
Thanks kirl!

yeah thats pretty much the same as me i use them a lot without even thinking how they work. my aim over this next while is too try and get behind lots of stuff i have seen and try and understand how they work.

glad you like the thread. maybe if you find anything on your bumpmapping quest that isnt covered you can share. that would be awsome.

in this version i have tried height mapping if im correct height mapping just makes a grey scale copy of the original image and each grey level represents a height vector. this height vector then gets normalised and mixed in with the z normals to extend each normal by heightmap factor. does that sound right i am getting a nice effect but maybe not to the extent i had hoped for.

(-Edit removed exe too keep forum clean as it was rather large check further down )
« Last Edit: May 07, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: bumpmapping
« Reply #10 on: May 07, 2013 »
im 72.594% sure i finaly have it  :)
Looks pretty cool but this piece is certainly wrong because you're just using the normal vector to calculate the reflection:
Code: [Select]
SpecDot = ( Nx * -Nx + -Ny * Ny + Nz * -Nz )
Rx = (-Nx)-Nx*2.0*SpecDot
Ry = (-Ny)-Ny*2.0*SpecDot
Rz = (-Nz)-Nz*2.0*SpecDot
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #11 on: May 07, 2013 »
woops..

it should be
specDot( pixellocation(-320 to 320)*Nx.... etc )
Rx = PixelLocation( -320 to 320 )-Nx*2.0*SpecDot
etc..

i was looking at some half angle stuff and got my wires extremely mixed up i hope ive got it close now. it looks much better at least.

(- Edit changed a few bits again added grey scale height mapp etc.modded exe attached )
« Last Edit: May 09, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: bumpmapping
« Reply #12 on: May 07, 2013 »
im intrested in what you wrote about packing a specular level into the alpha channel too stop incorrect lighting going on would it be possible for you too elaborate a little further?
In Reply #4 I suggested a exponential function for the specular term:
Code: [Select]
if (specular > 0.0) specular= pow(specular, 40.0)The exponent is supposed to simulate the reflection of a light source.
A higher exponent creates a smaller highlight and makes the material appear more shiny (for example see here).
Now your surface might not be equally shiny everywhere, eg. imagine some rusty spots on a metal plane (like this).

One way to do this is to have a separate (grey-scale) map to define the brightness of the specular term, like this:
Code: [Select]
if (specular > 0.0) specular= pow(specular, 40.0) * specularLevel[pixelPosition]
Another way is to store the specular exponent in the map:
Code: [Select]
if (specular > 0.0) specular= pow(specular, specularLevel[pixelPosition])
Since the exponential function is somewhat expensive, you'd probably use the first version in a software renderer and pick the pow-function with a constant exponent from a lookup table.

For the color- and normal-maps you're typically working with 32bit rgba-colors and the alpha-channel is often unused.
And as the specular-map just contains a single scalar value for each pixel, it can be stored in the alpha-component of one of the other maps to avoid another texture fetch.
Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: bumpmapping
« Reply #13 on: May 07, 2013 »
Great stuff ninogenio :) And a ton of really insightful stuff from Hellfire too! Double win :)
K++
raizor

Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #14 on: May 08, 2013 »
cheers mate!  :)


@hellfire thanks for picking that up freebasic didnt have a pow func and i wasnt quite sure how that part should go ive replaced it with this SpecDot = (Exp(10*log(SpecDot)))*Height
ive added grey scale height mapping and also got rid of the lightmap as with the specular term there was no need so its running much faster ive stripped out a lot of the fudgy stuff as it was quite hard too read which resulted in me finding a few bugs.
« Last Edit: May 08, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: bumpmapping
« Reply #15 on: May 08, 2013 »
i've replaced it with this Exp(10*log(SpecDot))
Since floating point numbers are represented as x*2^exp, pow2 and log2 can be approximated by fiddling with the exponent-bits within the float.
And because
Code: [Select]
pow(x, exp) = pow2(exp * log2(x))you can do a *very* rough approximation with:
Code: [Select]
   const float inv23= 1.0f / (1 << 23);
   const float bias= 126.94269504f;

   // approximate log2(x)
   int *ip= (int*)&x; // cast x to integer
   float y = ip[0];
   y= y * inv23 - bias;
   e*=y;

   // approximate 2.0^e
   int i= (1 << 23) * (e + bias);
   float *fp= (float*)&i; // cast i to float
   float result= fp[0];
Be aware that you can be off by a factor of 2 but for a shading function that usually doesn't really matter.
And if you're limiting this to positive integer exponents, it gets quite another bit tighter.
« Last Edit: May 08, 2013 by hellfire »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #16 on: May 08, 2013 »
thanks very much hellfire k++, some of this stuff you post is pure gold. using your pow function the specular light now bends round certain surfaces and does a bit of scattering. in my mind it behaves in a quite realistic manner now.
« Last Edit: May 09, 2013 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #17 on: May 08, 2013 »
hey again.

is this sort of specular term only possible when viewing from different angles and distances.

http://www.keithlantz.net/2011/10/tangent-space-normal-mapping-with-glsl/

the specular i have now is just a bit too overpowering but everything i try to get it more speckled and scattered just fades the whole specular term out. im even yousing the same texture and normals as the link posted.
« Last Edit: May 12, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: bumpmapping
« Reply #18 on: May 08, 2013 »
If you're looking from (almost) the same angle as the light source hits the surface, diffuse and specular are on the same spot and hard to distinguish.
A typically specular-only setting is the sun going down over the ocean (example) as the diffuse term gets almost zero but the light reflects towards the viewer.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: bumpmapping
« Reply #19 on: May 08, 2013 »
just had a big break through with this it just all come together! looks like glass now  :).

thanks hellfire mate!! might have a go at mapping this onto a cube and see what it looks like in 3d space with a dynamic camera proper light etc.
« Last Edit: May 14, 2013 by ninogenio »
Challenge Trophies Won: