Finding the correct patch

Blitz3D Forums/Blitz3D Programming/Finding the correct patch

PaulJG(Posted 2003) [#1]
Stuck again :( sorry guys !

Bit hard to explain this one, so I'll try to give as much detail as possible.

Anyway.. here's the story - I've written this terrain system that uses patches.

Each patch contains 256 vertexes, 16 x 16

I have a total of 256 patches.. again 16 x 16

So think of it as a blanket, patch 1 being top left, patch 17 being on the second row under the first.

Now I'm mapping my heightmap onto the vertexes, I'm going through the entire bitmap and setting each y vertex as needed.

Now the problem.. I can get the correct patch using the following code - which works, although probably not the fastest or prettiest !.

x#=8			; x example coord of pixel	
y#=10			; y example coord of pixel

num1#=x/16		; divide patch size with x coord
num2#=y/16		; divide patch size with y coord

num3=Floor(num1#)+1		; round down to  a whole number
num4=Floor(num2#)			; Same !

num5=num3+(num4*16)	; Find the exact patch needed


Note, the X# and Y# coords are the placement vars when reading the bitmap.

As I've said, this works - it returns the correct patch.., but I also need the offsets, so that I can find out which vertex I need to edit on the patch.

I think it's something to do with the offset that results from the calc in NUM1 and NUM2 - but how do I convert it into a useable number ? (which I know will somewhere be in the scale of 1-256.

The vertexes in the patch are setup going from left to right, top to bottom. (same as the patches)

Help !!!.. pretty please ;)

btw..dont laugh - but the above code took me 4 hours to get right !. ;) .. oh go on then.. laugh !!!


Floyd(Posted 2003) [#2]
This will be a *lot* easier if you start the numbering from 0.
i.e. 0 to 15 rather than 1 to 16, 0 to 255 rather than 1 to 256 etc.

And the variables should be integers, not floating point.

Here's how to get the patch, and the vertex within the patch.
patch = 16*(y/16) + (x/16)
vert  = 16*(y Mod 16) + (x Mod 16)  

; Fast versions using bitwise operators...

patch = (y And -16) Or (x Shr 4)
vert  = ( (y And %1111) Shl 4 ) Or (x And %1111)

The ideas behind this should be clear if you experiment with 10 by 10, rather than 16 by 16.
You can then see when you want to use the final digit of x,y and when
you should use the digits before the last one.

The tricky looking code does the same thing, but with base sixteen digits rather than base ten.