Home This Article Is Taken From The Game Programming MegaSite, A Definitive Resource For Game Developers!

Bump Mapping
By: Tom Hammersley

Introduction

OK, I've been getting lotsa replies on the bump mapping scene, so I think its time to report on what you've been passing on. I'll try and give credit where its due (if I can find your addresses...). Great reponse, keep it up...

#1: Jean-Francois Dube

Ok, Jean (deks) was first off the mark. His scheme is quite complex, reminds me a lot of water in some places... The basic idea however is very similar to other schemes. I'll start of with a modified sample of code he sent me, I've made it into pseudo-code:


        for x1 to x2 do

                normal-x = interpolated-normal-x >> 20

                normal-y = interpolated-normal-y >> 20

                index = (v >> 16) << 8) + (u >> 16)

                texel = texture[index]

                bump-normal-x = bumpmap[index+1] - bumpmap[index - 1]

                bump-normal-y = bumpmap[index+256 - bumpmap[index - 256]

                p = 127 - (bump-normal-x - normal-x)

                q = 127 - (bump-normal-y - normal-y)



                if(p < 0) then p = 0

                if(q < 0) then q = 0

        

                colour = colourtable[texel][phongmap[p*256+q]]

                putpixel(x, y, colour)



                (and just interpolate normal + texture here...)

        end

Quite a nice method, but perhaps the inner loop is a little complex.

#2: Paul Adams

Next up is Paul (aka Frenzy). Well.. this guy lives in the same town as me, yet I still haven't been able to spot him... Paul, if you're reading, show yourself... anyway back to the code. His suggestion is to have a 'heightfield' map (could be same map as texture if working in monochrome, colour paletted images may freak out...). You then take:


nx = map[u-1] - map[u+1]
ny = map[v-1] - map[v+1]

Which sounds a little odd... I think he means...

nx = map[u-1] - map[u+1]
ny = map[v-256] - map[v+256]

Which would make a little more sense... Then just do the old ny*256+nx, to get the index into your phongmap. Not too bad..

#3: Jacco Bikker

This is the third and final entry. I just chose 3 to keep it simple, a lot of stuff was duplicated. His idea revolves around the idea of a relief map. This is an array of 8-bit bytes, broken up into 2 4-bit halfs. The upper 4 bits are filled with the X difference between the pixels neighbours, and the lower 4 are filled with the Y difference. With 4 bits, we have a range of (he said) -7 to +8 (I think its -8 to +7 though). You have to clamp to this range, to avoid problems later on...

Next you have to create a 16x16 array. In this array, we interpolate a normal vector, I'd imagine this would be from (-0.5, -0.5, 0) to (0.5, 0.5, 0). Now, don't store this as array[16][16], store it as array[256]. This is because we shall now index into that array. And what do we use to index with? The value calculated in the paragraph above. Then, mix these values together with lighting, and texture, and plot. Quite a complex system, I have to say.

Summary

I have to say #2 is by far the simplest to code. It may also be the quickest, if when you find the nx and ny values you just clip them to 8 bits (by storing them in a byte register) then we can speed it up nicely. It'll also fit quite nicely into common 256x256 mappers. Anyway, have a play with them, I'm sure you'll find this lot handy.

OK, now if you want to send me something, then make it cells+portals this time, I've already had one reply (Jacco Bikker) which was interesting, I'd like to hear other views on the subject. I have some ideas of my own on this (which are quite complex, but sounds reasonable, to me at least...). Again, any tidbits or code samples will be nice, lets see what we can learn about them.



The Game Programming MegaSite - ⌐1996- Matt Reiferson.