Nice work Valle,
I took what you did and used something I found by IanM in another thread for manipulating memblocks and came up with the following. My idea stems from the fact that grass is not always green. So if you had a brown, yellow or even pink texture for the terrain you could apply a mask to it and basically extrude the colors out of that texture to make grass that would actually look like it belonged on that particular terrain.
I _barely_ modified the code that you and Ian had already presented so I can't claim credit for anything other than seeing a neat way to fit them together. Anyway, let me know what you think and I'd be glad to hear suggestions about ways of improving this. Also, if anyone decides to use this technique in a game, I'd love to see it!
set display mode 1024, 768, 32
sync on : sync rate 60
height = screen height()-1
width = screen width()-1
dim color(15)
color(0) = rgb(255, 0, 0)
color(1) = rgb(0, 255, 0)
color(2) = rgb(0, 0, 255)
color(3) = rgb(255, 255, 0)
color(4) = rgb(255, 0, 255)
color(5) = rgb(0, 255, 255)
color(6) = rgb(128, 0, 0)
color(7) = rgb(0, 128, 0)
color(8) = rgb(0, 0, 128)
color(9) = rgb(255, 128, 0)
color(10) = rgb(255, 0, 128)
color(11) = rgb(128, 255, 0)
color(12) = rgb(0, 255, 128)
color(13) = rgb(128, 0, 255)
color(14) = rgb(0, 128, 255)
color(15) = rgb(255, 255, 255)
size = 128
` get a blank image for our final "grass" image
get image 3, 0, 0, size-1, size-1
` make the mask
ink rgb(255, 255, 255), 0
for i = 0 to (size * 20)
x = rnd(size-1)
y = rnd(size-1)
dot x, y
next i
get image 2, 0, 0, size-1, size-1
` make the "ground" tile using my pre-made color palette
index = 0
for y = 0 to 3
for x = 0 to 3
ink color(index), 0
box x*32, y*32, ((x+1)*32)-1, ((y+1)*32)-1
inc index
next x
next y
get image 1, 0, 0, size-1, size-1
applyMask(1, 2, 3)
make object plain 1, size, size
texture object 1, 1
xrotate object 1, 90
for i = 2 to 20
make object plain i, size, size
texture object i, 3
xrotate object i, 90
position object i, 0, i, i*0.5
set object transparency i, 3
next i
do
position camera cos(a)*size*1.5,size*2,sin(a)*size*1.5 : inc a
point camera 0,0,0
sync
loop
`------------------------------------------------------------------------------
` This function takes our terrain texture and applies a black & white mask
` to get our new grass texture. All the textures must have the same
` dimensions for this to work.
`------------------------------------------------------------------------------
function applyMask(background, mask, final)
make memblock from image 1, background
make memblock from image 2, mask
make memblock from image 3, final
ptr1 = get memblock ptr(1)
ptr2 = get memblock ptr(2)
ptr3 = get memblock ptr(3)
tsize = get memblock size(1) - 4
` skip the first 12 bytes (3 dwords)
` -- image memblocks use these for height, width and depth
for pos = 12 to tsize step 4
val1 = *ptr1
val2 = *ptr2
val3 = (val1 && val2)
*ptr3 = val3
inc ptr1, 4
inc ptr2, 4
inc ptr3, 4
next pos
make image from memblock final, 3
endfunction